Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Prefer operator<< to range-based stringification

Fixes #1172
Commit
126850e76b017ca5b6d5597a3e7545619cb3379a
Author
Martin Hořeňovský ·
Parents
5e8df1c38489

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.h                  |  2 +-
 .../SelfTest/Baselines/compact.sw.approved.txt     |  8 +++
 .../SelfTest/Baselines/console.std.approved.txt    |  4 +-
 .../SelfTest/Baselines/console.sw.approved.txt     | 38 ++++++++++-
 projects/SelfTest/Baselines/junit.sw.approved.txt  |  3 +-
 projects/SelfTest/Baselines/xml.sw.approved.txt    | 43 ++++++++++++-
 .../SelfTest/UsageTests/ToStringWhich.tests.cpp    | 73 ++++++++++++++++++++++
 7 files changed, 163 insertions(+), 8 deletions(-)

diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index a61e45be..9ca5d6e8 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -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 );
         }
diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt
index 25e183bb..947ef425 100644
--- a/projects/SelfTest/Baselines/compact.sw.approved.txt
+++ b/projects/SelfTest/Baselines/compact.sw.approved.txt
@@ -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> }"
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index 310a3d2e..a94cc4fa 100644
--- a/projects/SelfTest/Baselines/console.std.approved.txt
+++ b/projects/SelfTest/Baselines/console.std.approved.txt
@@ -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
 
diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt
index eaf2cb41..2b7159e6 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -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
 
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 4bd6458c..b99f0b8c 100644
--- a/projects/SelfTest/Baselines/junit.sw.approved.txt
+++ b/projects/SelfTest/Baselines/junit.sw.approved.txt
@@ -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}"/>
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index a95f74f1..ceac33bf 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -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>
diff --git a/projects/SelfTest/UsageTests/ToStringWhich.tests.cpp b/projects/SelfTest/UsageTests/ToStringWhich.tests.cpp
index 2bf467cb..fe961865 100644
--- a/projects/SelfTest/UsageTests/ToStringWhich.tests.cpp
+++ b/projects/SelfTest/UsageTests/ToStringWhich.tests.cpp
@@ -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{}) == "{?}");
+}