From 733b901dd26110039466c04a74633a5709a1229e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 14 Nov 2023 23:35:22 +0100 Subject: [PATCH] Fix special character escaping in JsonWriter --- src/catch2/internal/catch_jsonwriter.hpp | 20 +++- .../Baselines/automake.sw.approved.txt | 1 + .../Baselines/automake.sw.multi.approved.txt | 1 + .../Baselines/compact.sw.approved.txt | 14 ++- .../Baselines/compact.sw.multi.approved.txt | 14 ++- .../Baselines/console.std.approved.txt | 4 +- .../Baselines/console.sw.approved.txt | 106 +++++++++++++++++- .../Baselines/console.sw.multi.approved.txt | 106 +++++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 12 +- .../Baselines/junit.sw.multi.approved.txt | 12 +- .../Baselines/sonarqube.sw.approved.txt | 10 +- .../Baselines/sonarqube.sw.multi.approved.txt | 10 +- tests/SelfTest/Baselines/tap.sw.approved.txt | 22 +++- .../Baselines/tap.sw.multi.approved.txt | 22 +++- .../Baselines/teamcity.sw.approved.txt | 2 + .../Baselines/teamcity.sw.multi.approved.txt | 2 + tests/SelfTest/Baselines/xml.sw.approved.txt | 99 +++++++++++++++- .../Baselines/xml.sw.multi.approved.txt | 99 +++++++++++++++- .../IntrospectiveTests/Json.tests.cpp | 46 +++++++- 19 files changed, 557 insertions(+), 45 deletions(-) diff --git a/src/catch2/internal/catch_jsonwriter.hpp b/src/catch2/internal/catch_jsonwriter.hpp index 9cac3de6cb..1b6ac96701 100644 --- a/src/catch2/internal/catch_jsonwriter.hpp +++ b/src/catch2/internal/catch_jsonwriter.hpp @@ -47,10 +47,26 @@ namespace Catch { m_sstream << value; while ( true ) { char c = m_sstream.get(); - if ( m_sstream.eof() ) { break; } + + // see https://www.json.org/json-en.html, string definition for the escape list if ( c == '"' ) { - m_os << '\\' << '"'; + m_os << "\\\""; + } else if ( c == '\\' ) { + m_os << "\\\\"; + // Note that while forward slash _can_ be escaped, it + // does not have to be, if JSON is not further embedded + // somewhere where forward slash is meaningful. + } else if ( c == '\b' ) { + m_os << "\\b"; + } else if ( c == '\f' ) { + m_os << "\\f"; + } else if ( c == '\n' ) { + m_os << "\\n"; + } else if ( c == '\r' ) { + m_os << "\\r"; + } else if ( c == '\t') { + m_os << "\\t"; } else { m_os << c; } diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index 0f029e6d28..e492fb9f98 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -171,6 +171,7 @@ Nor would this :test-result: XFAIL Inequality checks that should fail :test-result: PASS Inequality checks that should succeed :test-result: PASS JsonWriter +:test-result: PASS JsonWriter escapes charaters in strings properly :test-result: PASS Lambdas in assertions :test-result: PASS Less-than inequalities with different epsilons :test-result: PASS ManuallyRegistered diff --git a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt index a3af0658ff..afb7d9c3c7 100644 --- a/tests/SelfTest/Baselines/automake.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.multi.approved.txt @@ -169,6 +169,7 @@ :test-result: XFAIL Inequality checks that should fail :test-result: PASS Inequality checks that should succeed :test-result: PASS JsonWriter +:test-result: PASS JsonWriter escapes charaters in strings properly :test-result: PASS Lambdas in assertions :test-result: PASS Less-than inequalities with different epsilons :test-result: PASS ManuallyRegistered diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index e78c989f51..455b4945b2 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1055,7 +1055,15 @@ Json.tests.cpp:: passed: stream.str() == "[\n]" for: "[ "[ ]" Json.tests.cpp:: passed: stream.str() == "\"custom\"" for: ""custom"" == ""custom"" -Json.tests.cpp:: passed: stream.str() == "\"\\\"\"" for: ""\""" == ""\""" +Json.tests.cpp:: passed: sstream.str() == "\"\\\"\"" for: ""\""" == ""\""" +Json.tests.cpp:: passed: sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\"" +Json.tests.cpp:: passed: sstream.str() == "\"/\"" for: ""/"" == ""/"" +Json.tests.cpp:: passed: sstream.str() == "\"\\b\"" for: ""\b"" == ""\b"" +Json.tests.cpp:: passed: sstream.str() == "\"\\f\"" for: ""\f"" == ""\f"" +Json.tests.cpp:: passed: sstream.str() == "\"\\n\"" for: ""\n"" == ""\n"" +Json.tests.cpp:: passed: sstream.str() == "\"\\r\"" for: ""\r"" == ""\r"" +Json.tests.cpp:: passed: sstream.str() == "\"\\t\"" for: ""\t"" == ""\t"" +Json.tests.cpp:: passed: sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n"" Compilation.tests.cpp:: passed: []() { return true; }() for: true Approx.tests.cpp:: passed: d <= Approx( 1.24 ) for: 1.23 <= Approx( 1.24 ) Approx.tests.cpp:: passed: d <= Approx( 1.23 ) for: 1.23 <= Approx( 1.23 ) @@ -2675,7 +2683,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: 414 | 309 passed | 85 failed | 6 skipped | 14 failed as expected -assertions: 2246 | 2065 passed | 146 failed | 35 failed as expected +test cases: 415 | 310 passed | 85 failed | 6 skipped | 14 failed as expected +assertions: 2254 | 2073 passed | 146 failed | 35 failed as expected diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index 8f177e4627..42eb635c61 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -1053,7 +1053,15 @@ Json.tests.cpp:: passed: stream.str() == "[\n]" for: "[ "[ ]" Json.tests.cpp:: passed: stream.str() == "\"custom\"" for: ""custom"" == ""custom"" -Json.tests.cpp:: passed: stream.str() == "\"\\\"\"" for: ""\""" == ""\""" +Json.tests.cpp:: passed: sstream.str() == "\"\\\"\"" for: ""\""" == ""\""" +Json.tests.cpp:: passed: sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\"" +Json.tests.cpp:: passed: sstream.str() == "\"/\"" for: ""/"" == ""/"" +Json.tests.cpp:: passed: sstream.str() == "\"\\b\"" for: ""\b"" == ""\b"" +Json.tests.cpp:: passed: sstream.str() == "\"\\f\"" for: ""\f"" == ""\f"" +Json.tests.cpp:: passed: sstream.str() == "\"\\n\"" for: ""\n"" == ""\n"" +Json.tests.cpp:: passed: sstream.str() == "\"\\r\"" for: ""\r"" == ""\r"" +Json.tests.cpp:: passed: sstream.str() == "\"\\t\"" for: ""\t"" == ""\t"" +Json.tests.cpp:: passed: sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n"" Compilation.tests.cpp:: passed: []() { return true; }() for: true Approx.tests.cpp:: passed: d <= Approx( 1.24 ) for: 1.23 <= Approx( 1.24 ) Approx.tests.cpp:: passed: d <= Approx( 1.23 ) for: 1.23 <= Approx( 1.23 ) @@ -2664,7 +2672,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: 414 | 309 passed | 85 failed | 6 skipped | 14 failed as expected -assertions: 2246 | 2065 passed | 146 failed | 35 failed as expected +test cases: 415 | 310 passed | 85 failed | 6 skipped | 14 failed as expected +assertions: 2254 | 2073 passed | 146 failed | 35 failed as expected diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index 9aa0d0385c..7cfdff4812 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1588,6 +1588,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 414 | 323 passed | 70 failed | 7 skipped | 14 failed as expected -assertions: 2229 | 2065 passed | 129 failed | 35 failed as expected +test cases: 415 | 324 passed | 70 failed | 7 skipped | 14 failed as expected +assertions: 2237 | 2073 passed | 129 failed | 35 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 71cbbd2746..036cd36848 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -7436,17 +7436,113 @@ with expansion: ""custom"" == ""custom"" ------------------------------------------------------------------------------- -JsonWriter - String with a quote shall be espaced +JsonWriter escapes charaters in strings properly + Quote in a string is escaped ------------------------------------------------------------------------------- Json.tests.cpp: ............................................................................... Json.tests.cpp:: PASSED: - REQUIRE( stream.str() == "\"\\\"\"" ) + REQUIRE( sstream.str() == "\"\\\"\"" ) with expansion: ""\""" == ""\""" +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Backslash in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\\\\"" ) +with expansion: + ""\\"" == ""\\"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Forward slash in a string is **not** escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"/\"" ) +with expansion: + ""/"" == ""/"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Backspace in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\b\"" ) +with expansion: + ""\b"" == ""\b"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Formfeed in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\f\"" ) +with expansion: + ""\f"" == ""\f"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + linefeed in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\n\"" ) +with expansion: + ""\n"" == ""\n"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + carriage return in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\r\"" ) +with expansion: + ""\r"" == ""\r"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + tab in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\t\"" ) +with expansion: + ""\t"" == ""\t"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + combination of characters is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" ) +with expansion: + ""\\/\t\r\n"" == ""\\/\t\r\n"" + ------------------------------------------------------------------------------- Lambdas in assertions ------------------------------------------------------------------------------- @@ -18595,6 +18691,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 414 | 309 passed | 85 failed | 6 skipped | 14 failed as expected -assertions: 2246 | 2065 passed | 146 failed | 35 failed as expected +test cases: 415 | 310 passed | 85 failed | 6 skipped | 14 failed as expected +assertions: 2254 | 2073 passed | 146 failed | 35 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index ccb27333f0..33ee3da5a7 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -7434,17 +7434,113 @@ with expansion: ""custom"" == ""custom"" ------------------------------------------------------------------------------- -JsonWriter - String with a quote shall be espaced +JsonWriter escapes charaters in strings properly + Quote in a string is escaped ------------------------------------------------------------------------------- Json.tests.cpp: ............................................................................... Json.tests.cpp:: PASSED: - REQUIRE( stream.str() == "\"\\\"\"" ) + REQUIRE( sstream.str() == "\"\\\"\"" ) with expansion: ""\""" == ""\""" +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Backslash in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\\\\"" ) +with expansion: + ""\\"" == ""\\"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Forward slash in a string is **not** escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"/\"" ) +with expansion: + ""/"" == ""/"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Backspace in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\b\"" ) +with expansion: + ""\b"" == ""\b"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + Formfeed in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\f\"" ) +with expansion: + ""\f"" == ""\f"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + linefeed in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\n\"" ) +with expansion: + ""\n"" == ""\n"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + carriage return in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\r\"" ) +with expansion: + ""\r"" == ""\r"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + tab in a string is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\t\"" ) +with expansion: + ""\t"" == ""\t"" + +------------------------------------------------------------------------------- +JsonWriter escapes charaters in strings properly + combination of characters is escaped +------------------------------------------------------------------------------- +Json.tests.cpp: +............................................................................... + +Json.tests.cpp:: PASSED: + REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" ) +with expansion: + ""\\/\t\r\n"" == ""\\/\t\r\n"" + ------------------------------------------------------------------------------- Lambdas in assertions ------------------------------------------------------------------------------- @@ -18584,6 +18680,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 414 | 309 passed | 85 failed | 6 skipped | 14 failed as expected -assertions: 2246 | 2065 passed | 146 failed | 35 failed as expected +test cases: 415 | 310 passed | 85 failed | 6 skipped | 14 failed as expected +assertions: 2254 | 2073 passed | 146 failed | 35 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index beb235b578..2a4e2f7ef0 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -862,7 +862,15 @@ at Condition.tests.cpp: - + + + + + + + + + diff --git a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt index a5dd30636c..c6d7586378 100644 --- a/tests/SelfTest/Baselines/junit.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.multi.approved.txt @@ -1,6 +1,6 @@ - + @@ -861,7 +861,15 @@ at Condition.tests.cpp: - + + + + + + + + + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index eaef6357e6..4b35842625 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -140,7 +140,15 @@ 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 bb23a9955d..49389acdd8 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.multi.approved.txt @@ -139,7 +139,15 @@ at AssertionHandler.tests.cpp: - + + + + + + + + + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index e32e658937..aeefa5ae39 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -1884,8 +1884,24 @@ ok {test-number} - stream.str() == "{\n}" for: "{ }" == "{ }" ok {test-number} - stream.str() == "[\n]" for: "[ ]" == "[ ]" # JsonWriter ok {test-number} - stream.str() == "\"custom\"" for: ""custom"" == ""custom"" -# JsonWriter -ok {test-number} - stream.str() == "\"\\\"\"" for: ""\""" == ""\""" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\"\"" for: ""\""" == ""\""" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"/\"" for: ""/"" == ""/"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\b\"" for: ""\b"" == ""\b"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\f\"" for: ""\f"" == ""\f"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\n\"" for: ""\n"" == ""\n"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\r\"" for: ""\r"" == ""\r"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\t\"" for: ""\t"" == ""\t"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n"" # Lambdas in assertions ok {test-number} - []() { return true; }() for: true # Less-than inequalities with different epsilons @@ -4521,5 +4537,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2258 +1..2266 diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index 5d3f056042..28763e3138 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -1882,8 +1882,24 @@ ok {test-number} - stream.str() == "{\n}" for: "{ }" == "{ }" ok {test-number} - stream.str() == "[\n]" for: "[ ]" == "[ ]" # JsonWriter ok {test-number} - stream.str() == "\"custom\"" for: ""custom"" == ""custom"" -# JsonWriter -ok {test-number} - stream.str() == "\"\\\"\"" for: ""\""" == ""\""" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\"\"" for: ""\""" == ""\""" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"/\"" for: ""/"" == ""/"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\b\"" for: ""\b"" == ""\b"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\f\"" for: ""\f"" == ""\f"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\n\"" for: ""\n"" == ""\n"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\r\"" for: ""\r"" == ""\r"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\t\"" for: ""\t"" == ""\t"" +# JsonWriter escapes charaters in strings properly +ok {test-number} - sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n"" # Lambdas in assertions ok {test-number} - []() { return true; }() for: true # Less-than inequalities with different epsilons @@ -4510,5 +4526,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2258 +1..2266 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index 35ff74ac48..d178c96364 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -422,6 +422,8 @@ ##teamcity[testFinished name='Inequality checks that should succeed' duration="{duration}"] ##teamcity[testStarted name='JsonWriter'] ##teamcity[testFinished name='JsonWriter' duration="{duration}"] +##teamcity[testStarted name='JsonWriter escapes charaters in strings properly'] +##teamcity[testFinished name='JsonWriter escapes charaters in strings properly' duration="{duration}"] ##teamcity[testStarted name='Lambdas in assertions'] ##teamcity[testFinished name='Lambdas in assertions' duration="{duration}"] ##teamcity[testStarted name='Less-than inequalities with different epsilons'] diff --git a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt index d559c1fe7f..be1c615b13 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.multi.approved.txt @@ -422,6 +422,8 @@ ##teamcity[testFinished name='Inequality checks that should succeed' duration="{duration}"] ##teamcity[testStarted name='JsonWriter'] ##teamcity[testFinished name='JsonWriter' duration="{duration}"] +##teamcity[testStarted name='JsonWriter escapes charaters in strings properly'] +##teamcity[testFinished name='JsonWriter escapes charaters in strings properly' duration="{duration}"] ##teamcity[testStarted name='Lambdas in assertions'] ##teamcity[testFinished name='Lambdas in assertions' duration="{duration}"] ##teamcity[testStarted name='Less-than inequalities with different epsilons'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index c6059ea89e..4d70789dcd 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -8946,10 +8946,13 @@ C -
+ + + +
- stream.str() == "\"\\\"\"" + sstream.str() == "\"\\\"\"" ""\""" == ""\""" @@ -8957,6 +8960,94 @@ C
+
+ + + sstream.str() == "\"\\\\\"" + + + ""\\"" == ""\\"" + + + +
+
+ + + sstream.str() == "\"/\"" + + + ""/"" == ""/"" + + + +
+
+ + + sstream.str() == "\"\\b\"" + + + ""\b"" == ""\b"" + + + +
+
+ + + sstream.str() == "\"\\f\"" + + + ""\f"" == ""\f"" + + + +
+
+ + + sstream.str() == "\"\\n\"" + + + ""\n"" == ""\n"" + + + +
+
+ + + sstream.str() == "\"\\r\"" + + + ""\r"" == ""\r"" + + + +
+
+ + + sstream.str() == "\"\\t\"" + + + ""\t"" == ""\t"" + + + +
+
+ + + sstream.str() == "\"\\\\/\\t\\r\\n\"" + + + ""\\/\t\r\n"" == ""\\/\t\r\n"" + + + +
@@ -21562,6 +21653,6 @@ b1!
- - + + diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index 23df72f326..0e39016503 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -8946,10 +8946,13 @@ C -
+ + + +
- stream.str() == "\"\\\"\"" + sstream.str() == "\"\\\"\"" ""\""" == ""\""" @@ -8957,6 +8960,94 @@ C
+
+ + + sstream.str() == "\"\\\\\"" + + + ""\\"" == ""\\"" + + + +
+
+ + + sstream.str() == "\"/\"" + + + ""/"" == ""/"" + + + +
+
+ + + sstream.str() == "\"\\b\"" + + + ""\b"" == ""\b"" + + + +
+
+ + + sstream.str() == "\"\\f\"" + + + ""\f"" == ""\f"" + + + +
+
+ + + sstream.str() == "\"\\n\"" + + + ""\n"" == ""\n"" + + + +
+
+ + + sstream.str() == "\"\\r\"" + + + ""\r"" == ""\r"" + + + +
+
+ + + sstream.str() == "\"\\t\"" + + + ""\t"" == ""\t"" + + + +
+
+ + + sstream.str() == "\"\\\\/\\t\\r\\n\"" + + + ""\\/\t\r\n"" == ""\\/\t\r\n"" + + + +
@@ -21561,6 +21652,6 @@ b1!
- - + + diff --git a/tests/SelfTest/IntrospectiveTests/Json.tests.cpp b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp index 9ade94eec9..8204e3c4b1 100644 --- a/tests/SelfTest/IntrospectiveTests/Json.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp @@ -14,14 +14,14 @@ namespace { struct Custom {}; - std::ostream& operator<<( std::ostream& os, Custom const& ) { + static std::ostream& operator<<( std::ostream& os, Custom const& ) { return os << "custom"; } } // namespace TEST_CASE( "JsonWriter", "[JSON][JsonWriter]" ) { - std::stringstream stream{}; + std::stringstream stream; SECTION( "Newly constructed JsonWriter does nothing" ) { Catch::JsonValueWriter writer{ stream }; REQUIRE( stream.str() == "" ); @@ -109,8 +109,44 @@ TEST_CASE( "JsonWriter", "[JSON][JsonWriter]" ) { Catch::JsonValueWriter{ stream }.write( Custom{} ); REQUIRE( stream.str() == "\"custom\"" ); } - SECTION( "String with a quote shall be espaced" ) { - Catch::JsonValueWriter{ stream }.write( "\"" ); - REQUIRE( stream.str() == "\"\\\"\"" ); +} + +TEST_CASE( "JsonWriter escapes charaters in strings properly", "[JsonWriter]" ) { + std::stringstream sstream; + SECTION( "Quote in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\"" ); + REQUIRE( sstream.str() == "\"\\\"\"" ); + } + SECTION("Backslash in a string is escaped") { + Catch::JsonValueWriter{ sstream }.write( "\\" ); + REQUIRE( sstream.str() == "\"\\\\\"" ); + } + SECTION( "Forward slash in a string is **not** escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "/" ); + REQUIRE( sstream.str() == "\"/\"" ); + } + SECTION( "Backspace in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\b" ); + REQUIRE( sstream.str() == "\"\\b\"" ); + } + SECTION( "Formfeed in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\f" ); + REQUIRE( sstream.str() == "\"\\f\"" ); + } + SECTION( "linefeed in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\n" ); + REQUIRE( sstream.str() == "\"\\n\"" ); + } + SECTION( "carriage return in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\r" ); + REQUIRE( sstream.str() == "\"\\r\"" ); + } + SECTION( "tab in a string is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\t" ); + REQUIRE( sstream.str() == "\"\\t\"" ); + } + SECTION( "combination of characters is escaped" ) { + Catch::JsonValueWriter{ sstream }.write( "\\/\t\r\n" ); + REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" ); } }