Fix stringifying of unknown enums

This commit is contained in:
Phil Nash 2018-01-04 10:21:52 +00:00
parent 702cfdaf6e
commit 32eb90b9bd
7 changed files with 101 additions and 59 deletions

View File

@ -57,25 +57,38 @@ namespace Catch {
static const bool value = decltype(test<std::ostream, const T&>(0))::value; static const bool value = decltype(test<std::ostream, const T&>(0))::value;
}; };
template<typename E>
std::string convertUnknownEnumToString( E e );
template<typename T>
typename std::enable_if<!std::is_enum<T>::value, std::string>::type convertUnstreamable( T const& ) {
return Detail::unprintableString;
};
template<typename T>
typename std::enable_if<std::is_enum<T>::value, std::string>::type convertUnstreamable( T const& value ) {
return convertUnknownEnumToString( value );
};
} // namespace Detail } // namespace Detail
// If we decide for C++14, change these to enable_if_ts // If we decide for C++14, change these to enable_if_ts
template <typename T, typename = void> template <typename T, typename = void>
struct StringMaker { struct StringMaker {
template <typename Fake = T> template <typename Fake = T>
static static
typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
convert(const Fake& t) { convert(const Fake& value) {
ReusableStringStream rss; ReusableStringStream rss;
rss << t; rss << value;
return rss.str(); return rss.str();
} }
template <typename Fake = T> template <typename Fake = T>
static static
typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
convert(const Fake&) { convert( const Fake& value ) {
return Detail::unprintableString; return Detail::convertUnstreamable( value );
} }
}; };
@ -88,8 +101,12 @@ namespace Catch {
return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e); return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::convert(e);
} }
} // namespace Detail template<typename E>
std::string convertUnknownEnumToString( E e ) {
return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<E>::type>(e));
}
} // namespace Detail
// Some predefined specializations // Some predefined specializations
@ -233,13 +250,6 @@ namespace Catch {
} }
} }
template<typename T>
struct EnumStringMaker {
static std::string convert(const T& t) {
return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<T>::type>(t));
}
};
#ifdef __OBJC__ #ifdef __OBJC__
template<> template<>
struct StringMaker<NSString*> { struct StringMaker<NSString*> {

View File

@ -987,8 +987,8 @@ EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e1) ==
EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e3) == "Unknown enum value 10" for: "Unknown enum value 10" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e3) == "Unknown enum value 10" for: "Unknown enum value 10"
== ==
"Unknown enum value 10" "Unknown enum value 10"
EnumToString.tests.cpp:<line number>: failed: ::Catch::Detail::stringify(e0) == "0" for: "{?}" == "0" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e0) == "0" for: "0" == "0"
EnumToString.tests.cpp:<line number>: failed: ::Catch::Detail::stringify(e1) == "1" for: "{?}" == "1" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e1) == "1" for: "1" == "1"
EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e0) == "E2{0}" for: "E2{0}" == "E2{0}" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e0) == "E2{0}" for: "E2{0}" == "E2{0}"
EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e1) == "E2{1}" for: "E2{1}" == "E2{1}" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e1) == "E2{1}" for: "E2{1}" == "E2{1}"
EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e0) == "0" for: "0" == "0" EnumToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(e0) == "0" for: "0" == "0"
@ -1011,6 +1011,9 @@ ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(v) ==
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(v) == "{ { /"hello/" }, { /"world/" } }" for: "{ { "hello" }, { "world" } }" ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(v) == "{ { /"hello/" }, { /"world/" } }" for: "{ { "hello" }, { "world" } }"
== ==
"{ { "hello" }, { "world" } }" "{ { "hello" }, { "world" } }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(bools) == "{ }" for: "{ }" == "{ }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(bools) == "{ true }" for: "{ true }" == "{ true }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(bools) == "{ true, false }" for: "{ true, false }" == "{ true, false }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }" ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ 42 }" for: "{ 42 }" == "{ 42 }" ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ 42 }" for: "{ 42 }" == "{ 42 }"
ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ 42, 250 }" for: "{ 42, 250 }" == "{ 42, 250 }" ToStringVector.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(vv) == "{ 42, 250 }" for: "{ 42, 250 }" == "{ 42, 250 }"
@ -1041,5 +1044,5 @@ Misc.tests.cpp:<line number>: passed: v.size() == 5 for: 5 == 5
Misc.tests.cpp:<line number>: passed: v.capacity() >= 5 for: 5 >= 5 Misc.tests.cpp:<line number>: passed: v.capacity() >= 5 for: 5 >= 5
Misc.tests.cpp:<line number>: passed: Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: passed: Misc.tests.cpp:<line number>: passed:
Failed 50 test cases, failed 110 assertions. Failed 49 test cases, failed 108 assertions.

View File

@ -1063,23 +1063,7 @@ Tricky.tests.cpp:<line number>: FAILED:
with expansion: with expansion:
"first" == "second" "first" == "second"
-------------------------------------------------------------------------------
toString(enum class)
-------------------------------------------------------------------------------
EnumToString.tests.cpp:<line number>
...............................................................................
EnumToString.tests.cpp:<line number>: FAILED:
CHECK( ::Catch::Detail::stringify(e0) == "0" )
with expansion:
"{?}" == "0"
EnumToString.tests.cpp:<line number>: FAILED:
CHECK( ::Catch::Detail::stringify(e1) == "1" )
with expansion:
"{?}" == "1"
=============================================================================== ===============================================================================
test cases: 193 | 141 passed | 48 failed | 4 failed as expected test cases: 194 | 143 passed | 47 failed | 4 failed as expected
assertions: 977 | 849 passed | 107 failed | 21 failed as expected assertions: 980 | 854 passed | 105 failed | 21 failed as expected

View File

@ -7783,15 +7783,17 @@ toString(enum class)
EnumToString.tests.cpp:<line number> EnumToString.tests.cpp:<line number>
............................................................................... ...............................................................................
EnumToString.tests.cpp:<line number>: FAILED: EnumToString.tests.cpp:<line number>:
PASSED:
CHECK( ::Catch::Detail::stringify(e0) == "0" ) CHECK( ::Catch::Detail::stringify(e0) == "0" )
with expansion: with expansion:
"{?}" == "0" "0" == "0"
EnumToString.tests.cpp:<line number>: FAILED: EnumToString.tests.cpp:<line number>:
PASSED:
CHECK( ::Catch::Detail::stringify(e1) == "1" ) CHECK( ::Catch::Detail::stringify(e1) == "1" )
with expansion: with expansion:
"{?}" == "1" "1" == "1"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
toString(enum w/operator<<) toString(enum w/operator<<)
@ -7939,6 +7941,30 @@ with expansion:
== ==
"{ { "hello" }, { "world" } }" "{ { "hello" }, { "world" } }"
-------------------------------------------------------------------------------
vector<bool> -> toString
-------------------------------------------------------------------------------
ToStringVector.tests.cpp:<line number>
...............................................................................
ToStringVector.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(bools) == "{ }" )
with expansion:
"{ }" == "{ }"
ToStringVector.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }" )
with expansion:
"{ true }" == "{ true }"
ToStringVector.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }" )
with expansion:
"{ true, false }" == "{ true, false }"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
vector<int,allocator> -> toString vector<int,allocator> -> toString
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -8196,6 +8222,6 @@ Misc.tests.cpp:<line number>:
PASSED: PASSED:
=============================================================================== ===============================================================================
test cases: 193 | 139 passed | 50 failed | 4 failed as expected test cases: 194 | 141 passed | 49 failed | 4 failed as expected
assertions: 976 | 845 passed | 110 failed | 21 failed as expected assertions: 979 | 850 passed | 108 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="15" failures="96" tests="977" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="15" failures="94" tests="980" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/> <testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1027" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1027" time="{duration}"/>
@ -824,14 +824,7 @@ Tricky.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="toString( vectors&lt;has_maker_and_operator> )" time="{duration}"/> <testcase classname="<exe-name>.global" name="toString( vectors&lt;has_maker_and_operator> )" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString( vectors&lt;has_operator> )" time="{duration}"/> <testcase classname="<exe-name>.global" name="toString( vectors&lt;has_operator> )" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString(enum class w/operator&lt;&lt;)" time="{duration}"/> <testcase classname="<exe-name>.global" name="toString(enum class w/operator&lt;&lt;)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString(enum class)" time="{duration}"> <testcase classname="<exe-name>.global" name="toString(enum class)" time="{duration}"/>
<failure message="&quot;{?}&quot; == &quot;0&quot;" type="CHECK">
EnumToString.tests.cpp:<line number>
</failure>
<failure message="&quot;{?}&quot; == &quot;1&quot;" type="CHECK">
EnumToString.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/> <testcase classname="<exe-name>.global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString(enum)" time="{duration}"/> <testcase classname="<exe-name>.global" name="toString(enum)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="tuple&lt;>" time="{duration}"/> <testcase classname="<exe-name>.global" name="tuple&lt;>" time="{duration}"/>
@ -841,6 +834,7 @@ EnumToString.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="tuple&lt;string,string>" time="{duration}"/> <testcase classname="<exe-name>.global" name="tuple&lt;string,string>" time="{duration}"/>
<testcase classname="<exe-name>.global" name="tuple&lt;tuple&lt;int>,tuple&lt;>,float>" time="{duration}"/> <testcase classname="<exe-name>.global" name="tuple&lt;tuple&lt;int>,tuple&lt;>,float>" time="{duration}"/>
<testcase classname="<exe-name>.global" name="vec&lt;vec&lt;string,alloc>> -> toString" time="{duration}"/> <testcase classname="<exe-name>.global" name="vec&lt;vec&lt;string,alloc>> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="vector&lt;bool> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="vector&lt;int,allocator> -> toString" time="{duration}"/> <testcase classname="<exe-name>.global" name="vector&lt;int,allocator> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="vector&lt;int> -> toString" time="{duration}"/> <testcase classname="<exe-name>.global" name="vector&lt;int> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="vector&lt;string> -> toString" time="{duration}"/> <testcase classname="<exe-name>.global" name="vector&lt;string> -> toString" time="{duration}"/>

View File

@ -8702,24 +8702,24 @@ loose text artifact
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="toString(enum class)" tags="[.][enum][enumClass][toString]" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" > <TestCase name="toString(enum class)" tags="[enum][enumClass][toString]" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" >
<Expression success="false" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" >
<Original> <Original>
::Catch::Detail::stringify(e0) == "0" ::Catch::Detail::stringify(e0) == "0"
</Original> </Original>
<Expanded> <Expanded>
"{?}" == "0" "0" == "0"
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="false" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" >
<Original> <Original>
::Catch::Detail::stringify(e1) == "1" ::Catch::Detail::stringify(e1) == "1"
</Original> </Original>
<Expanded> <Expanded>
"{?}" == "1" "1" == "1"
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResult success="false"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="toString(enum w/operator&lt;&lt;)" tags="[enum][toString]" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" > <TestCase name="toString(enum w/operator&lt;&lt;)" tags="[enum][toString]" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/UsageTests/EnumToString.tests.cpp" >
@ -8868,6 +8868,33 @@ loose text artifact
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="vector&lt;bool> -> toString" tags="[containers][toString][vector]" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Original>
::Catch::Detail::stringify(bools) == "{ }"
</Original>
<Expanded>
"{ }" == "{ }"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Original>
::Catch::Detail::stringify(bools) == "{ true }"
</Original>
<Expanded>
"{ true }" == "{ true }"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Original>
::Catch::Detail::stringify(bools) == "{ true, false }"
</Original>
<Expanded>
"{ true, false }" == "{ true, false }"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="vector&lt;int,allocator> -> toString" tags="[toString][vector,allocator]" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" > <TestCase name="vector&lt;int,allocator> -> toString" tags="[toString][vector,allocator]" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringVector.tests.cpp" >
<Original> <Original>
@ -9114,7 +9141,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="845" failures="111" expectedFailures="21"/> <OverallResults successes="850" failures="109" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="845" failures="110" expectedFailures="21"/> <OverallResults successes="850" failures="108" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -28,9 +28,7 @@ TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) {
// Enum class without user-provided stream operator // Enum class without user-provided stream operator
enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 }; enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 };
// This fails, but has been hidden for a while - not sure if it's a regression or if it never worked TEST_CASE( "toString(enum class)", "[toString][enum][enumClass]" ) {
// - need to investigate
TEST_CASE( "toString(enum class)", "[toString][enum][enumClass][.]" ) {
EnumClass1 e0 = EnumClass1::EnumClass1Value0; EnumClass1 e0 = EnumClass1::EnumClass1Value0;
CHECK( ::Catch::Detail::stringify(e0) == "0" ); CHECK( ::Catch::Detail::stringify(e0) == "0" );
EnumClass1 e1 = EnumClass1::EnumClass1Value1; EnumClass1 e1 = EnumClass1::EnumClass1Value1;