From 435dee2d69a47c8d46aa1aab3d2906bfc8eab74e Mon Sep 17 00:00:00 2001 From: Markus Mohrhard Date: Tue, 13 Dec 2016 10:54:01 +0100 Subject: add support for enum class to the asserter The asserter now has special handling to convert the enum class to a std::string. This does not work without some template magic as enum class has no implicit conversion to int. --- include/cppunit/TestAssert.h | 45 +++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'include/cppunit') diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index fa0bfa7..89f9209 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -5,8 +5,10 @@ #include #include #include +#include #include #include // For struct assertion_traits +#include // Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T // is an enum type: @@ -17,6 +19,35 @@ CPPUNIT_NS_BEGIN +namespace impl { + +// work around to handle C++11 enum class correctly. We need an own conversion to std::string +// as there is no implicit coversion to int for enum class. +template +struct toString +{ + static std::string toStringImpl(const T& x) + { + OStringStream ost; + ost << x; + + return ost.str(); + } +}; + +template +struct toString::value >::type> +{ + static std::string toStringImpl(const T& x) + { + OStringStream ost; + ost << static_cast::type>(x); + + return ost.str(); + } +}; + +} /*! \brief Traits used by CPPUNIT_ASSERT* macros. * @@ -71,22 +102,10 @@ struct assertion_traits static std::string toString( const T& x ) { - OStringStream ost; -// Work around "passing 'T' chooses 'int' over 'unsigned int'" warnings when T -// is an enum type: -#if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wsign-promo" -#endif - ost << x; -#if defined __GNUC__ && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4) -#pragma GCC diagnostic pop -#endif - return ost.str(); + return impl::toString::toStringImpl(x); } }; - /*! \brief Traits used by CPPUNIT_ASSERT_DOUBLES_EQUAL(). * * This specialisation from @c struct @c assertion_traits<> ensures that -- cgit v1.2.1