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

Provide useful and unambigous stringification for static arrays

Commit
8d854c689b7f4388ced1a9fd0c71042b2075f26f
Author
Martin Hořeňovský ·
Parents
f0909dfe02df

Files · Download .tar.gz

Changes

 include/internal/catch_tostring.h                  | 22 +++++------
 .../SelfTest/Baselines/compact.sw.approved.txt     |  5 +++
 .../SelfTest/Baselines/console.std.approved.txt    |  4 +-
 .../SelfTest/Baselines/console.sw.approved.txt     | 45 +++++++++++++++++++++-
 projects/SelfTest/Baselines/junit.sw.approved.txt  |  5 ++-
 projects/SelfTest/Baselines/xml.sw.approved.txt    | 42 +++++++++++++++++++-
 .../SelfTest/UsageTests/ToStringGeneral.tests.cpp  | 15 ++++++++
 7 files changed, 118 insertions(+), 20 deletions(-)

diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 0bcf6bbb..a61e45be 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -136,18 +136,6 @@ namespace Catch {
         static std::string convert(wchar_t * str);
     };
 
-    template<typename T>
-    struct is_string_array : std::false_type {};
-
-    template<std::size_t N>
-    struct is_string_array<char[N]> : std::true_type {};
-
-    template<std::size_t N>
-    struct is_string_array<signed char[N]> : std::true_type {};
-
-        template<std::size_t N>
-    struct is_string_array<unsigned char[N]> : std::true_type {};
-
     template<int SZ>
     struct StringMaker<char[SZ]> {
         static std::string convert(const char* str) {
@@ -399,12 +387,20 @@ namespace Catch {
     }
 
     template<typename R>
-    struct StringMaker<R, typename std::enable_if<is_range<R>::value && !is_string_array<R>::value>::type> {
+    struct StringMaker<R, typename std::enable_if<is_range<R>::value && !std::is_array<R>::value>::type> {
         static std::string convert( R const& range ) {
             return rangeToString( range );
         }
     };
 
+    template <typename T, int SZ>
+    struct StringMaker<T[SZ]> {
+        static std::string convert(T const(&arr)[SZ]) {
+            return rangeToString(arr);
+        }
+    };
+
+
 } // namespace Catch
 
 // Separate std::chrono::duration specialization
diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt
index f8c2ba21..68c9e5cf 100644
--- a/projects/SelfTest/Baselines/compact.sw.approved.txt
+++ b/projects/SelfTest/Baselines/compact.sw.approved.txt
@@ -598,6 +598,11 @@ Message from section one
 Message from section two
 Matchers.tests.cpp:<line number>: failed: testStringForMatching(), StartsWith("This String") for: "this string contains 'abc' as a substring" starts with: "This String"
 Matchers.tests.cpp:<line number>: failed: testStringForMatching(), StartsWith("string", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" starts with: "string" (case insensitive)
+ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify(singular) == "{ 1 }" for: "{ 1 }" == "{ 1 }"
+ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify(arr) == "{ 3, 2, 1 }" for: "{ 3, 2, 1 }" == "{ 3, 2, 1 }"
+ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })" for: "{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
+==
+"{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
 Matchers.tests.cpp:<line number>: passed: testStringForMatching(), Contains("string") for: "this string contains 'abc' as a substring" contains: "string"
 Matchers.tests.cpp:<line number>: passed: testStringForMatching(), Contains("string", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" contains: "string" (case insensitive)
 Matchers.tests.cpp:<line number>: passed: testStringForMatching(), Contains("abc") for: "this string contains 'abc' as a substring" contains: "abc"
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index 79387418..310a3d2e 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: 196 | 145 passed |  47 failed |  4 failed as expected
-assertions: 989 | 863 passed | 105 failed | 21 failed as expected
+test cases: 197 | 146 passed |  47 failed |  4 failed as expected
+assertions: 992 | 866 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 29be3496..11b89a33 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -4749,6 +4749,47 @@ with expansion:
   "this string contains 'abc' as a substring" starts with: "string" (case
   insensitive)
 
+-------------------------------------------------------------------------------
+Static arrays are convertible to string
+  Single item
+-------------------------------------------------------------------------------
+ToStringGeneral.tests.cpp:<line number>
+...............................................................................
+
+ToStringGeneral.tests.cpp:<line number>:
+PASSED:
+  REQUIRE( Catch::Detail::stringify(singular) == "{ 1 }" )
+with expansion:
+  "{ 1 }" == "{ 1 }"
+
+-------------------------------------------------------------------------------
+Static arrays are convertible to string
+  Multiple
+-------------------------------------------------------------------------------
+ToStringGeneral.tests.cpp:<line number>
+...............................................................................
+
+ToStringGeneral.tests.cpp:<line number>:
+PASSED:
+  REQUIRE( Catch::Detail::stringify(arr) == "{ 3, 2, 1 }" )
+with expansion:
+  "{ 3, 2, 1 }" == "{ 3, 2, 1 }"
+
+-------------------------------------------------------------------------------
+Static arrays are convertible to string
+  Non-trivial inner items
+-------------------------------------------------------------------------------
+ToStringGeneral.tests.cpp:<line number>
+...............................................................................
+
+ToStringGeneral.tests.cpp:<line number>:
+PASSED:
+  REQUIRE( Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })" )
+with expansion:
+  "{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
+  ==
+  "{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
+
 -------------------------------------------------------------------------------
 String matchers
 -------------------------------------------------------------------------------
@@ -8288,6 +8329,6 @@ Misc.tests.cpp:<line number>:
 PASSED:
 
 ===============================================================================
-test cases: 196 | 143 passed |  49 failed |  4 failed as expected
-assertions: 988 | 859 passed | 108 failed | 21 failed as expected
+test cases: 197 | 144 passed |  49 failed |  4 failed as expected
+assertions: 991 | 862 passed | 108 failed | 21 failed as expected
 
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 136f8d38..79d8e6d9 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="94" tests="989" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
+  <testsuite name="<exe-name>" errors="15" failures="94" tests="992" 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}"/>
@@ -538,6 +538,9 @@ Matchers.tests.cpp:<line number>
 Matchers.tests.cpp:<line number>
       </failure>
     </testcase>
+    <testcase classname="<exe-name>.global" name="Static arrays are convertible to string/Single item" time="{duration}"/>
+    <testcase classname="<exe-name>.global" name="Static arrays are convertible to string/Multiple" time="{duration}"/>
+    <testcase classname="<exe-name>.global" name="Static arrays are convertible to string/Non-trivial inner items" time="{duration}"/>
     <testcase classname="<exe-name>.global" name="String matchers" time="{duration}"/>
     <testcase classname="<exe-name>.global" name="StringRef/Empty string" time="{duration}"/>
     <testcase classname="<exe-name>.global" name="StringRef/From string literal" time="{duration}"/>
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index e5759ee8..86557362 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -5477,6 +5477,44 @@ Message from section two
       </Expression>
       <OverallResult success="false"/>
     </TestCase>
+    <TestCase name="Static arrays are convertible to string" tags="[toString]" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+      <Section name="Single item" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+          <Original>
+            Catch::Detail::stringify(singular) == "{ 1 }"
+          </Original>
+          <Expanded>
+            "{ 1 }" == "{ 1 }"
+          </Expanded>
+        </Expression>
+        <OverallResults successes="1" failures="0" expectedFailures="0"/>
+      </Section>
+      <Section name="Multiple" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+          <Original>
+            Catch::Detail::stringify(arr) == "{ 3, 2, 1 }"
+          </Original>
+          <Expanded>
+            "{ 3, 2, 1 }" == "{ 3, 2, 1 }"
+          </Expanded>
+        </Expression>
+        <OverallResults successes="1" failures="0" expectedFailures="0"/>
+      </Section>
+      <Section name="Non-trivial inner items" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
+          <Original>
+            Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })"
+          </Original>
+          <Expanded>
+            "{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
+==
+"{ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } }"
+          </Expanded>
+        </Expression>
+        <OverallResults successes="1" failures="0" expectedFailures="0"/>
+      </Section>
+      <OverallResult success="true"/>
+    </TestCase>
     <TestCase name="String matchers" tags="[matchers]" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
       <Expression success="true" type="REQUIRE_THAT" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
         <Original>
@@ -9219,7 +9257,7 @@ loose text artifact
       </Section>
       <OverallResult success="true"/>
     </TestCase>
-    <OverallResults successes="859" failures="109" expectedFailures="21"/>
+    <OverallResults successes="862" failures="109" expectedFailures="21"/>
   </Group>
-  <OverallResults successes="859" failures="108" expectedFailures="21"/>
+  <OverallResults successes="862" failures="108" expectedFailures="21"/>
 </Catch>
diff --git a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
index 3c5a3d94..8ab33249 100644
--- a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
+++ b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
@@ -100,3 +100,18 @@ TEST_CASE( "std::set is convertible string", "[toString]" ) {
         REQUIRE( Catch::Detail::stringify( set ) == "{ \"abc\", \"def\", \"ghi\" }" );
     }
 }
+
+TEST_CASE("Static arrays are convertible to string", "[toString]") {
+    SECTION("Single item") {
+        int singular[1] = { 1 };
+        REQUIRE(Catch::Detail::stringify(singular) == "{ 1 }");
+    }
+    SECTION("Multiple") {
+        int arr[3] = { 3, 2, 1 };
+        REQUIRE(Catch::Detail::stringify(arr) == "{ 3, 2, 1 }");
+    }
+    SECTION("Non-trivial inner items") {
+        std::vector<std::string> arr[2] = { {"1:1", "1:2", "1:3"}, {"2:1", "2:2"} };
+        REQUIRE(Catch::Detail::stringify(arr) == R"({ { "1:1", "1:2", "1:3" }, { "2:1", "2:2" } })");
+    }
+}