I have an enum class that I would like to use in my unit tests:
enum class MyEnumClass
{
MyEntryA,
MyEntryB
};
I would like to use it as follows:
MyEnumClass myEnumValue = MyEnumClass::MyEntryA;
BOOST_CHECK_EQUAL(myEnumValue, MyEnumClass::MyEntryB);
But I get this error, clearly because boost test is trying to output the value:
include/boost/test/test_tools.hpp:326:14: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
ostr << t; // by default print the value
^
Adding ugly static_cast
"solves" the problem:
BOOST_CHECK_EQUAL(static_cast<int>(myEnumValue), static_cast<int>(MyEnumClass::MyEntryB));
But I would like to avoid doing that for every enum class. I would also like to avoid defining <<
stream operators for every enum class.
Is there a simpler way to use enum classes with boost test?
Or do other unit test frameworks have a better way to deal with enum classes?
You can disable printing of the type in question with
BOOST_TEST_DONT_PRINT_LOG_VALUE()
. From the Boost docs:In this case should you get a mismatch, the test error message will tell you, but it but won't print the actual differing values.