diff --git a/src/catch2/catch_tostring.cpp b/src/catch2/catch_tostring.cpp index f96c3fc1..940e61b5 100644 --- a/src/catch2/catch_tostring.cpp +++ b/src/catch2/catch_tostring.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -113,6 +114,15 @@ namespace Detail { rss << std::setw(2) << static_cast(bytes[i]); return rss.str(); } + + std::string makeExceptionHappenedString() { + return "{ stringification failed with an exception: \"" + + translateActiveException() + "\" }"; + + } + + + } // end Detail namespace diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index ab91c618..41f89413 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -139,11 +139,17 @@ namespace Catch { namespace Detail { + std::string makeExceptionHappenedString(); + // This function dispatches all stringification requests inside of Catch. // Should be preferably called fully qualified, like ::Catch::Detail::stringify template - std::string stringify(const T& e) { - return ::Catch::StringMaker>>::convert(e); + std::string stringify( const T& e ) { + CATCH_TRY { + return ::Catch::StringMaker< + std::remove_cv_t>>::convert( e ); + } + CATCH_CATCH_ALL { return makeExceptionHappenedString(); } } template diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index 084a3d26..aec224fa 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -248,6 +248,7 @@ Message from section two :test-result: PASS String matchers :test-result: PASS StringRef :test-result: PASS StringRef at compilation time +:test-result: PASS Stringifying bla bla bla :test-result: PASS Stringifying char arrays with statically known sizes - char :test-result: PASS Stringifying char arrays with statically known sizes - signed char :test-result: PASS Stringifying char arrays with statically known sizes - unsigned char diff --git a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt index 1c6b7d99..74eb7756 100644 --- a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt @@ -241,6 +241,7 @@ :test-result: PASS String matchers :test-result: PASS StringRef :test-result: PASS StringRef at compilation time +:test-result: PASS Stringifying bla bla bla :test-result: PASS Stringifying char arrays with statically known sizes - char :test-result: PASS Stringifying char arrays with statically known sizes - signed char :test-result: PASS Stringifying char arrays with statically known sizes - unsigned char diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index fe5ee317..445f059b 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1848,6 +1848,9 @@ String.tests.cpp:: passed: with 1 message: '!(sr1.empty())' String.tests.cpp:: passed: with 1 message: 'sr1.size() == 3' String.tests.cpp:: passed: with 1 message: 'sr2.empty()' String.tests.cpp:: passed: with 1 message: 'sr2.size() == 0' +ToString.tests.cpp:: passed: tos == tos for: { stringification failed with an exception: "Invalid" } +== +{ stringification failed with an exception: "Invalid" } ToString.tests.cpp:: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" @@ -2874,7 +2877,7 @@ InternalBenchmark.tests.cpp:: passed: med == 18. for: 18.0 == 18.0 InternalBenchmark.tests.cpp:: passed: q3 == 23. for: 23.0 == 23.0 Misc.tests.cpp:: passed: Misc.tests.cpp:: passed: -test cases: 433 | 315 passed | 95 failed | 6 skipped | 17 failed as expected -assertions: 2291 | 2093 passed | 157 failed | 41 failed as expected +test cases: 434 | 316 passed | 95 failed | 6 skipped | 17 failed as expected +assertions: 2292 | 2094 passed | 157 failed | 41 failed as expected diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index 4c8d923b..d85c8f12 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -1841,6 +1841,9 @@ String.tests.cpp:: passed: with 1 message: '!(sr1.empty())' String.tests.cpp:: passed: with 1 message: 'sr1.size() == 3' String.tests.cpp:: passed: with 1 message: 'sr2.empty()' String.tests.cpp:: passed: with 1 message: 'sr2.size() == 0' +ToString.tests.cpp:: passed: tos == tos for: { stringification failed with an exception: "Invalid" } +== +{ stringification failed with an exception: "Invalid" } ToString.tests.cpp:: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" @@ -2863,7 +2866,7 @@ InternalBenchmark.tests.cpp:: passed: med == 18. for: 18.0 == 18.0 InternalBenchmark.tests.cpp:: passed: q3 == 23. for: 23.0 == 23.0 Misc.tests.cpp:: passed: Misc.tests.cpp:: passed: -test cases: 433 | 315 passed | 95 failed | 6 skipped | 17 failed as expected -assertions: 2291 | 2093 passed | 157 failed | 41 failed as expected +test cases: 434 | 316 passed | 95 failed | 6 skipped | 17 failed as expected +assertions: 2292 | 2094 passed | 157 failed | 41 failed as expected diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index fc820cc5..3b6c86a0 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1719,6 +1719,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 433 | 333 passed | 76 failed | 7 skipped | 17 failed as expected -assertions: 2270 | 2093 passed | 136 failed | 41 failed as expected +test cases: 434 | 334 passed | 76 failed | 7 skipped | 17 failed as expected +assertions: 2271 | 2094 passed | 136 failed | 41 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 8e129486..ff7fdf04 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -11789,6 +11789,19 @@ String.tests.cpp:: PASSED: with message: sr2.size() == 0 +------------------------------------------------------------------------------- +Stringifying bla bla bla +------------------------------------------------------------------------------- +ToString.tests.cpp: +............................................................................... + +ToString.tests.cpp:: PASSED: + CHECK( tos == tos ) +with expansion: + { stringification failed with an exception: "Invalid" } + == + { stringification failed with an exception: "Invalid" } + ------------------------------------------------------------------------------- Stringifying char arrays with statically known sizes - char ------------------------------------------------------------------------------- @@ -19213,6 +19226,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 433 | 315 passed | 95 failed | 6 skipped | 17 failed as expected -assertions: 2291 | 2093 passed | 157 failed | 41 failed as expected +test cases: 434 | 316 passed | 95 failed | 6 skipped | 17 failed as expected +assertions: 2292 | 2094 passed | 157 failed | 41 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index 908b7a66..9ab35540 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -11782,6 +11782,19 @@ String.tests.cpp:: PASSED: with message: sr2.size() == 0 +------------------------------------------------------------------------------- +Stringifying bla bla bla +------------------------------------------------------------------------------- +ToString.tests.cpp: +............................................................................... + +ToString.tests.cpp:: PASSED: + CHECK( tos == tos ) +with expansion: + { stringification failed with an exception: "Invalid" } + == + { stringification failed with an exception: "Invalid" } + ------------------------------------------------------------------------------- Stringifying char arrays with statically known sizes - char ------------------------------------------------------------------------------- @@ -19202,6 +19215,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 433 | 315 passed | 95 failed | 6 skipped | 17 failed as expected -assertions: 2291 | 2093 passed | 157 failed | 41 failed as expected +test cases: 434 | 316 passed | 95 failed | 6 skipped | 17 failed as expected +assertions: 2292 | 2094 passed | 157 failed | 41 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index f9dba650..225b1459 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -1511,6 +1511,7 @@ at Matchers.tests.cpp: + diff --git a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt index e064e606..10396194 100644 --- a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt @@ -1,6 +1,6 @@ - + @@ -1510,6 +1510,7 @@ at Matchers.tests.cpp: + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index 066ec161..7663796e 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -388,6 +388,7 @@ at AssertionHandler.tests.cpp: + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt index 3358f32b..a899aafc 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt @@ -387,6 +387,7 @@ at AssertionHandler.tests.cpp: + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 51d649a0..5e8e508a 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2803,6 +2803,8 @@ ok {test-number} - with 1 message: 'sr1.size() == 3' ok {test-number} - with 1 message: 'sr2.empty()' # StringRef at compilation time ok {test-number} - with 1 message: 'sr2.size() == 0' +# Stringifying bla bla bla +ok {test-number} - tos == tos for: { stringification failed with an exception: "Invalid" } == { stringification failed with an exception: "Invalid" } # Stringifying char arrays with statically known sizes - char ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" # Stringifying char arrays with statically known sizes - char @@ -4603,5 +4605,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2303 +1..2304 diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index cd08187b..a748999c 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -2796,6 +2796,8 @@ ok {test-number} - with 1 message: 'sr1.size() == 3' ok {test-number} - with 1 message: 'sr2.empty()' # StringRef at compilation time ok {test-number} - with 1 message: 'sr2.size() == 0' +# Stringifying bla bla bla +ok {test-number} - tos == tos for: { stringification failed with an exception: "Invalid" } == { stringification failed with an exception: "Invalid" } # Stringifying char arrays with statically known sizes - char ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" # Stringifying char arrays with statically known sizes - char @@ -4592,5 +4594,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2303 +1..2304 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index e65e0719..9c6e190a 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -617,6 +617,8 @@ ##teamcity[testFinished name='StringRef' duration="{duration}"] ##teamcity[testStarted name='StringRef at compilation time'] ##teamcity[testFinished name='StringRef at compilation time' duration="{duration}"] +##teamcity[testStarted name='Stringifying bla bla bla'] +##teamcity[testFinished name='Stringifying bla bla bla' duration="{duration}"] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - char'] ##teamcity[testFinished name='Stringifying char arrays with statically known sizes - char' duration="{duration}"] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - signed char'] diff --git a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt index 41aab3c2..9cf4cba1 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt @@ -617,6 +617,8 @@ ##teamcity[testFinished name='StringRef' duration="{duration}"] ##teamcity[testStarted name='StringRef at compilation time'] ##teamcity[testFinished name='StringRef at compilation time' duration="{duration}"] +##teamcity[testStarted name='Stringifying bla bla bla'] +##teamcity[testFinished name='Stringifying bla bla bla' duration="{duration}"] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - char'] ##teamcity[testFinished name='Stringifying char arrays with statically known sizes - char' duration="{duration}"] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - signed char'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index d4a0ce02..9cf98a1a 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -13725,6 +13725,19 @@ Message from section two + + + + tos == tos + + + { stringification failed with an exception: "Invalid" } +== +{ stringification failed with an exception: "Invalid" } + + + + @@ -22214,6 +22227,6 @@ Approx( -1.95996398454005449 ) - - + + diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index 72c8ff9f..ef19a45e 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -13725,6 +13725,19 @@ Message from section two + + + + tos == tos + + + { stringification failed with an exception: "Invalid" } +== +{ stringification failed with an exception: "Invalid" } + + + + @@ -22213,6 +22226,6 @@ Approx( -1.95996398454005449 ) - - + + diff --git a/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp b/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp index 60fafc80..c3c70142 100644 --- a/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/ToString.tests.cpp @@ -118,3 +118,24 @@ TEST_CASE( "#2944 - Stringifying dates before 1970 should not crash", "[.approva Equals( "gmtime from provided timepoint has failed. This " "happens e.g. with pre-1970 dates using Microsoft libc" ) ); } + +namespace { + struct ThrowsOnStringification { + friend bool operator==( ThrowsOnStringification, + ThrowsOnStringification ) { + return true; + } + }; +} + +template <> +struct Catch::StringMaker { + static std::string convert(ThrowsOnStringification) { + throw std::runtime_error( "Invalid" ); + } +}; + +TEST_CASE( "Stringifying bla bla bla" ) { + ThrowsOnStringification tos; + CHECK( tos == tos ); +}