From 9ff341cb0bdea32881fc95b6fadceba30b7e5ade Mon Sep 17 00:00:00 2001 From: Raphael Schaller Date: Sat, 12 Mar 2022 17:49:12 +0100 Subject: [PATCH 1/2] StringMaker for std::vector reference --- src/catch2/catch_tostring.hpp | 26 +++++++++++++++++++ .../UsageTests/ToStringVector.tests.cpp | 14 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index 9182a119fe..331bbca06d 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -272,6 +272,32 @@ namespace Catch { } }; + template<> + struct StringMaker::reference> { + static std::string convert(std::vector::reference b) { + return StringMaker::convert(b); + } + }; + + // For libstdc++ and MSVC's STL, std::vector::const_reference is just + // an alias for bool. For libc++, however, std::vector::const_reference + // is a distinct type. The following code will conditionally specialize + // StringMaker for std::vector::const_reference if and only if it's a + // distinct type. + struct Dummy { + operator bool() const { return false; } + }; + using VectorBoolConstRef = std::conditional_t< + std::is_same::const_reference, bool>::value, + Dummy, + std::vector::const_reference>; + template <> + struct StringMaker { + static std::string convert( VectorBoolConstRef b ) { + return StringMaker::convert(b); + } + }; + template<> struct StringMaker { static std::string convert(char c); diff --git a/tests/SelfTest/UsageTests/ToStringVector.tests.cpp b/tests/SelfTest/UsageTests/ToStringVector.tests.cpp index a5ccbfa70a..0627d32dbb 100644 --- a/tests/SelfTest/UsageTests/ToStringVector.tests.cpp +++ b/tests/SelfTest/UsageTests/ToStringVector.tests.cpp @@ -92,3 +92,17 @@ TEST_CASE( "array -> toString", "[toString][containers][array]" ) { std::array twoValues = {{ 42, 250 }}; REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" ); } + +TEST_CASE( "vector::reference -> toString", + "[toString][containers][vector]" ) { + std::vector bools{ false, true }; + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ); + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ); +} + +TEST_CASE( "vector::const_reference -> toString", + "[toString][containers][vector]" ) { + const std::vector bools{ false, true }; + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ); + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ); +} From 4fad3d212ba538d50bae85dc3bd7fc22478491be Mon Sep 17 00:00:00 2001 From: Raphael Schaller Date: Sun, 13 Mar 2022 12:59:46 +0100 Subject: [PATCH 2/2] update approval tests --- .../Baselines/automake.sw.approved.txt | 2 + .../Baselines/automake.sw.multi.approved.txt | 2 + .../Baselines/compact.sw.approved.txt | 4 ++ .../Baselines/compact.sw.multi.approved.txt | 4 ++ .../Baselines/console.std.approved.txt | 4 +- .../Baselines/console.sw.approved.txt | 36 +++++++++++++++- .../Baselines/console.sw.multi.approved.txt | 36 +++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 4 +- .../Baselines/junit.sw.multi.approved.txt | 4 +- .../Baselines/sonarqube.sw.approved.txt | 2 + .../Baselines/sonarqube.sw.multi.approved.txt | 2 + tests/SelfTest/Baselines/tap.sw.approved.txt | 10 ++++- .../Baselines/tap.sw.multi.approved.txt | 10 ++++- .../Baselines/teamcity.sw.approved.txt | 4 ++ .../Baselines/teamcity.sw.multi.approved.txt | 4 ++ tests/SelfTest/Baselines/xml.sw.approved.txt | 42 ++++++++++++++++++- .../Baselines/xml.sw.multi.approved.txt | 42 ++++++++++++++++++- 17 files changed, 198 insertions(+), 14 deletions(-) diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index f9ab0b7746..fe1bd6ac26 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -384,6 +384,8 @@ loose text artifact :test-result: PASS unique_ptr reimplementation: basic functionality :test-result: PASS vec> -> toString :test-result: PASS vector -> toString +:test-result: PASS vector::const_reference -> toString +:test-result: PASS vector::reference -> toString :test-result: PASS vector -> toString :test-result: PASS vector -> toString :test-result: PASS vector -> toString diff --git a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt index f9a6431fbb..b52fefddb0 100644 --- a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt @@ -376,6 +376,8 @@ :test-result: PASS unique_ptr reimplementation: basic functionality :test-result: PASS vec> -> toString :test-result: PASS vector -> toString +:test-result: PASS vector::const_reference -> toString +:test-result: PASS vector::reference -> toString :test-result: PASS vector -> toString :test-result: PASS vector -> toString :test-result: PASS vector -> toString diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 9a32e6ca7d..46e7ecad5e 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -2471,6 +2471,10 @@ ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(v) == ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ }" for: "{ }" == "{ }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ true }" for: "{ true }" == "{ true }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ true, false }" for: "{ true, false }" == "{ true, false }" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ 42 }" for: "{ 42 }" == "{ 42 }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ 42, 250 }" for: "{ 42, 250 }" == "{ 42, 250 }" diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index 4b54102bc3..3052de5554 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -2463,6 +2463,10 @@ ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(v) == ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ }" for: "{ }" == "{ }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ true }" for: "{ true }" == "{ true }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(bools) == "{ true, false }" for: "{ true, false }" == "{ true, false }" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ 42 }" for: "{ 42 }" == "{ 42 }" ToStringVector.tests.cpp:: passed: ::Catch::Detail::stringify(vv) == "{ 42, 250 }" for: "{ 42, 250 }" == "{ 42, 250 }" diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index e37fd27c32..5bfebe7e5c 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1395,6 +1395,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 385 | 309 passed | 69 failed | 7 failed as expected -assertions: 2216 | 2061 passed | 128 failed | 27 failed as expected +test cases: 387 | 311 passed | 69 failed | 7 failed as expected +assertions: 2220 | 2065 passed | 128 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index c17023ac3c..572e812bb9 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -17543,6 +17543,38 @@ ToStringVector.tests.cpp:: PASSED: with expansion: "{ true, false }" == "{ true, false }" +------------------------------------------------------------------------------- +vector::const_reference -> toString +------------------------------------------------------------------------------- +ToStringVector.tests.cpp: +............................................................................... + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ) +with expansion: + "false" == "false" + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ) +with expansion: + "true" == "true" + +------------------------------------------------------------------------------- +vector::reference -> toString +------------------------------------------------------------------------------- +ToStringVector.tests.cpp: +............................................................................... + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ) +with expansion: + "false" == "false" + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ) +with expansion: + "true" == "true" + ------------------------------------------------------------------------------- vector -> toString ------------------------------------------------------------------------------- @@ -17811,6 +17843,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 385 | 295 passed | 83 failed | 7 failed as expected -assertions: 2231 | 2061 passed | 143 failed | 27 failed as expected +test cases: 387 | 297 passed | 83 failed | 7 failed as expected +assertions: 2235 | 2065 passed | 143 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index d034ed2715..86dfe698b2 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -17535,6 +17535,38 @@ ToStringVector.tests.cpp:: PASSED: with expansion: "{ true, false }" == "{ true, false }" +------------------------------------------------------------------------------- +vector::const_reference -> toString +------------------------------------------------------------------------------- +ToStringVector.tests.cpp: +............................................................................... + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ) +with expansion: + "false" == "false" + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ) +with expansion: + "true" == "true" + +------------------------------------------------------------------------------- +vector::reference -> toString +------------------------------------------------------------------------------- +ToStringVector.tests.cpp: +............................................................................... + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[0] ) == "false" ) +with expansion: + "false" == "false" + +ToStringVector.tests.cpp:: PASSED: + REQUIRE( ::Catch::Detail::stringify( bools[1] ) == "true" ) +with expansion: + "true" == "true" + ------------------------------------------------------------------------------- vector -> toString ------------------------------------------------------------------------------- @@ -17803,6 +17835,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 385 | 295 passed | 83 failed | 7 failed as expected -assertions: 2231 | 2061 passed | 143 failed | 27 failed as expected +test cases: 387 | 297 passed | 83 failed | 7 failed as expected +assertions: 2235 | 2065 passed | 143 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index 84c3625e41..c16ae6e825 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -1882,6 +1882,8 @@ Exception.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt index d1a41e13fc..3712c3cf79 100644 --- a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt @@ -1,6 +1,6 @@ - + @@ -1881,6 +1881,8 @@ Exception.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index 9d6e859f62..b2051bfb29 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -1859,6 +1859,8 @@ Misc.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt index a412dbc234..ef5a0c8f8c 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt @@ -1858,6 +1858,8 @@ Misc.tests.cpp: + + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 97f7df9b60..195feda863 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -4398,6 +4398,14 @@ ok {test-number} - ::Catch::Detail::stringify(bools) == "{ }" for: "{ }" == "{ ok {test-number} - ::Catch::Detail::stringify(bools) == "{ true }" for: "{ true }" == "{ true }" # vector -> toString ok {test-number} - ::Catch::Detail::stringify(bools) == "{ true, false }" for: "{ true, false }" == "{ true, false }" +# vector::const_reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +# vector::const_reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" +# vector::reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +# vector::reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" # vector -> toString ok {test-number} - ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }" # vector -> toString @@ -4464,5 +4472,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2231 +1..2235 diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index ca6dfc81b3..5b91c3b027 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -4390,6 +4390,14 @@ ok {test-number} - ::Catch::Detail::stringify(bools) == "{ }" for: "{ }" == "{ ok {test-number} - ::Catch::Detail::stringify(bools) == "{ true }" for: "{ true }" == "{ true }" # vector -> toString ok {test-number} - ::Catch::Detail::stringify(bools) == "{ true, false }" for: "{ true, false }" == "{ true, false }" +# vector::const_reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +# vector::const_reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" +# vector::reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[0] ) == "false" for: "false" == "false" +# vector::reference -> toString +ok {test-number} - ::Catch::Detail::stringify( bools[1] ) == "true" for: "true" == "true" # vector -> toString ok {test-number} - ::Catch::Detail::stringify(vv) == "{ }" for: "{ }" == "{ }" # vector -> toString @@ -4456,5 +4464,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2231 +1..2235 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index 4963db33a8..7fbc92580a 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -916,6 +916,10 @@ Exception.tests.cpp:|nunexpected exception with message:|n "Why wo ##teamcity[testFinished name='vec> -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] ##teamcity[testFinished name='vector -> toString' duration="{duration}"] +##teamcity[testStarted name='vector::const_reference -> toString'] +##teamcity[testFinished name='vector::const_reference -> toString' duration="{duration}"] +##teamcity[testStarted name='vector::reference -> toString'] +##teamcity[testFinished name='vector::reference -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] ##teamcity[testFinished name='vector -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] diff --git a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt index b91e8c7572..6a3dae391b 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt @@ -915,6 +915,10 @@ Exception.tests.cpp:|nunexpected exception with message:|n "Why wo ##teamcity[testFinished name='vec> -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] ##teamcity[testFinished name='vector -> toString' duration="{duration}"] +##teamcity[testStarted name='vector::const_reference -> toString'] +##teamcity[testFinished name='vector::const_reference -> toString' duration="{duration}"] +##teamcity[testStarted name='vector::reference -> toString'] +##teamcity[testFinished name='vector::reference -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] ##teamcity[testFinished name='vector -> toString' duration="{duration}"] ##teamcity[testStarted name='vector -> toString'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 0de045c728..5f463c187c 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -20639,6 +20639,44 @@ loose text artifact + + + + ::Catch::Detail::stringify( bools[0] ) == "false" + + + "false" == "false" + + + + + ::Catch::Detail::stringify( bools[1] ) == "true" + + + "true" == "true" + + + + + + + + ::Catch::Detail::stringify( bools[0] ) == "false" + + + "false" == "false" + + + + + ::Catch::Detail::stringify( bools[1] ) == "true" + + + "true" == "true" + + + + @@ -20931,6 +20969,6 @@ loose text artifact - - + + diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index b685fcc538..ccd899eb9a 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -20638,6 +20638,44 @@ There is no extra whitespace here + + + + ::Catch::Detail::stringify( bools[0] ) == "false" + + + "false" == "false" + + + + + ::Catch::Detail::stringify( bools[1] ) == "true" + + + "true" == "true" + + + + + + + + ::Catch::Detail::stringify( bools[0] ) == "false" + + + "false" == "false" + + + + + ::Catch::Detail::stringify( bools[1] ) == "true" + + + "true" == "true" + + + + @@ -20930,6 +20968,6 @@ There is no extra whitespace here - - + +