Prefer operator<< to range-based stringification

Fixes #1172
This commit is contained in:
Martin Hořeňovský 2018-02-01 14:07:23 +01:00
parent 5e8df1c384
commit 126850e76b
7 changed files with 163 additions and 8 deletions

View File

@ -387,7 +387,7 @@ namespace Catch {
}
template<typename R>
struct StringMaker<R, typename std::enable_if<is_range<R>::value && !std::is_array<R>::value>::type> {
struct StringMaker<R, typename std::enable_if<is_range<R>::value && !::Catch::Detail::IsStreamInsertable<R>::value>::type> {
static std::string convert( R const& range ) {
return rangeToString( range );
}

View File

@ -987,6 +987,14 @@ Misc.tests.cpp:<line number>: passed: result == "/"wide load/"" for: ""wide load
Misc.tests.cpp:<line number>: passed: result == "/"wide load/"" for: ""wide load"" == ""wide load""
Misc.tests.cpp:<line number>: passed: result == "/"wide load/"" for: ""wide load"" == ""wide load""
Misc.tests.cpp:<line number>: passed: result == "/"wide load/"" for: ""wide load"" == ""wide load""
ToStringWhich.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(streamable_range{}) == "op<<(streamable_range)" for: "op<<(streamable_range)"
==
"op<<(streamable_range)"
ToStringWhich.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(stringmaker_range{}) == "stringmaker(streamable_range)" for: "stringmaker(streamable_range)"
==
"stringmaker(streamable_range)"
ToStringWhich.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(just_range{}) == "{ 1, 2, 3, 4 }" for: "{ 1, 2, 3, 4 }" == "{ 1, 2, 3, 4 }"
ToStringWhich.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(disabled_range{}) == "{?}" for: "{?}" == "{?}"
ToStringWhich.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker> }" for: "{ StringMaker<has_maker> }"
==
"{ StringMaker<has_maker> }"

View File

@ -1064,6 +1064,6 @@ with expansion:
"first" == "second"
===============================================================================
test cases: 197 | 146 passed | 47 failed | 4 failed as expected
assertions: 992 | 866 passed | 105 failed | 21 failed as expected
test cases: 198 | 147 passed | 47 failed | 4 failed as expected
assertions: 996 | 870 passed | 105 failed | 21 failed as expected

View File

@ -7894,6 +7894,40 @@ PASSED:
with expansion:
""wide load"" == ""wide load""
-------------------------------------------------------------------------------
toString streamable range
-------------------------------------------------------------------------------
ToStringWhich.tests.cpp:<line number>
...............................................................................
ToStringWhich.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(streamable_range{}) == "op<<(streamable_range)" )
with expansion:
"op<<(streamable_range)"
==
"op<<(streamable_range)"
ToStringWhich.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(stringmaker_range{}) == "stringmaker(streamable_range)" )
with expansion:
"stringmaker(streamable_range)"
==
"stringmaker(streamable_range)"
ToStringWhich.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(just_range{}) == "{ 1, 2, 3, 4 }" )
with expansion:
"{ 1, 2, 3, 4 }" == "{ 1, 2, 3, 4 }"
ToStringWhich.tests.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify(disabled_range{}) == "{?}" )
with expansion:
"{?}" == "{?}"
-------------------------------------------------------------------------------
toString( vectors<has_maker> )
-------------------------------------------------------------------------------
@ -8407,6 +8441,6 @@ Misc.tests.cpp:<line number>:
PASSED:
===============================================================================
test cases: 197 | 132 passed | 61 failed | 4 failed as expected
assertions: 1003 | 862 passed | 120 failed | 21 failed as expected
test cases: 198 | 133 passed | 61 failed | 4 failed as expected
assertions: 1007 | 866 passed | 120 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="15" failures="106" tests="1004" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="15" failures="106" tests="1008" 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="#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}"/>
@ -825,6 +825,7 @@ Tricky.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="toString on const wchar_t pointer returns the string contents" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString on wchar_t const pointer returns the string contents" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString on wchar_t returns the string contents" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString streamable range" time="{duration}"/>
<testcase classname="<exe-name>.global" name="toString( vectors&lt;has_maker> )" 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}"/>

View File

@ -8750,6 +8750,45 @@ loose text artifact
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString streamable range" tags="[toString]" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Original>
::Catch::Detail::stringify(streamable_range{}) == "op&lt;&lt;(streamable_range)"
</Original>
<Expanded>
"op&lt;&lt;(streamable_range)"
==
"op&lt;&lt;(streamable_range)"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Original>
::Catch::Detail::stringify(stringmaker_range{}) == "stringmaker(streamable_range)"
</Original>
<Expanded>
"stringmaker(streamable_range)"
==
"stringmaker(streamable_range)"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Original>
::Catch::Detail::stringify(just_range{}) == "{ 1, 2, 3, 4 }"
</Original>
<Expanded>
"{ 1, 2, 3, 4 }" == "{ 1, 2, 3, 4 }"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Original>
::Catch::Detail::stringify(disabled_range{}) == "{?}"
</Original>
<Expanded>
"{?}" == "{?}"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString( vectors&lt;has_maker> )" tags="[toString]" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringWhich.tests.cpp" >
<Original>
@ -9257,7 +9296,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="862" failures="121" expectedFailures="21"/>
<OverallResults successes="866" failures="121" expectedFailures="21"/>
</Group>
<OverallResults successes="862" failures="120" expectedFailures="21"/>
<OverallResults successes="866" failures="120" expectedFailures="21"/>
</Catch>

View File

@ -71,3 +71,76 @@ TEST_CASE( "toString( vectors<has_maker_and_operator> )", "[toString]" ) {
std::vector<has_maker_and_operator> v(1);
REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker_and_operator> }" );
}
// Conversion should go
// StringMaker specialization, operator<<, range/enum detection, unprintable
struct int_iterator {
using iterator_category = std::input_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = int;
using reference = int&;
using pointer = int*;
int_iterator() = default;
int_iterator(int i) :val(i) {}
value_type operator*() const { return val; }
bool operator==(int_iterator rhs) const { return val == rhs.val; }
bool operator!=(int_iterator rhs) const { return val != rhs.val; }
int_iterator operator++() { ++val; return *this; }
int_iterator operator++(int) {
auto temp(*this);
++val;
return temp;
}
private:
int val = 5;
};
struct streamable_range {
int_iterator begin() const { return int_iterator{ 1 }; }
int_iterator end() const { return {}; }
};
std::ostream& operator<<(std::ostream& os, const streamable_range&) {
os << "op<<(streamable_range)";
return os;
}
struct stringmaker_range {
int_iterator begin() const { return int_iterator{ 1 }; }
int_iterator end() const { return {}; }
};
namespace Catch {
template <>
struct StringMaker<stringmaker_range> {
static std::string convert(stringmaker_range const&) {
return "stringmaker(streamable_range)";
}
};
}
struct just_range {
int_iterator begin() const { return int_iterator{ 1 }; }
int_iterator end() const { return {}; }
};
struct disabled_range {
int_iterator begin() const { return int_iterator{ 1 }; }
int_iterator end() const { return {}; }
};
namespace Catch {
template <>
struct is_range<disabled_range> {
static const bool value = false;
};
}
TEST_CASE("toString streamable range", "[toString]") {
REQUIRE(::Catch::Detail::stringify(streamable_range{}) == "op<<(streamable_range)");
REQUIRE(::Catch::Detail::stringify(stringmaker_range{}) == "stringmaker(streamable_range)");
REQUIRE(::Catch::Detail::stringify(just_range{}) == "{ 1, 2, 3, 4 }");
REQUIRE(::Catch::Detail::stringify(disabled_range{}) == "{?}");
}