diff options
| author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-12-13 10:54:01 +0100 |
|---|---|---|
| committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-12-13 10:58:43 +0100 |
| commit | 435dee2d69a47c8d46aa1aab3d2906bfc8eab74e (patch) | |
| tree | 093f947dd8a9c48bee612ed290ec920701e1adcc /include/cppunit | |
| parent | 98f0a738d2925c41b268388ce6defe86114f611a (diff) | |
| download | cppunit-435dee2d69a47c8d46aa1aab3d2906bfc8eab74e.tar.gz | |
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.
Diffstat (limited to 'include/cppunit')
| -rw-r--r-- | include/cppunit/TestAssert.h | 45 |
1 files changed, 32 insertions, 13 deletions
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 <cppunit/Exception.h> #include <cppunit/Asserter.h> #include <cppunit/portability/Stream.h> +#include <type_traits> #include <stdio.h> #include <float.h> // For struct assertion_traits<double> +#include <type_traits> // 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<typename T, typename Enable = void> +struct toString +{ + static std::string toStringImpl(const T& x) + { + OStringStream ost; + ost << x; + + return ost.str(); + } +}; + +template<typename T> +struct toString<T, typename std::enable_if<std::is_enum<T>::value >::type> +{ + static std::string toStringImpl(const T& x) + { + OStringStream ost; + ost << static_cast<typename std::underlying_type<T>::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<T>::toStringImpl(x); } }; - /*! \brief Traits used by CPPUNIT_ASSERT_DOUBLES_EQUAL(). * * This specialisation from @c struct @c assertion_traits<> ensures that |
