Skip to content

Commit

Permalink
Fix special character escaping in JsonWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Nov 14, 2023
1 parent 7bf136b commit 733b901
Show file tree
Hide file tree
Showing 19 changed files with 557 additions and 45 deletions.
20 changes: 18 additions & 2 deletions src/catch2/internal/catch_jsonwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/SelfTest/Baselines/automake.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/SelfTest/Baselines/automake.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,15 @@ Json.tests.cpp:<line number>: passed: stream.str() == "[\n]" for: "[
"[
]"
Json.tests.cpp:<line number>: passed: stream.str() == "\"custom\"" for: ""custom"" == ""custom""
Json.tests.cpp:<line number>: passed: stream.str() == "\"\\\"\"" for: ""\""" == ""\"""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\"\"" for: ""\""" == ""\"""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"/\"" for: ""/"" == ""/""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\b\"" for: ""\b"" == ""\b""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\f\"" for: ""\f"" == ""\f""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\n\"" for: ""\n"" == ""\n""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\r\"" for: ""\r"" == ""\r""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\t\"" for: ""\t"" == ""\t""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n""
Compilation.tests.cpp:<line number>: passed: []() { return true; }() for: true
Approx.tests.cpp:<line number>: passed: d <= Approx( 1.24 ) for: 1.23 <= Approx( 1.24 )
Approx.tests.cpp:<line number>: passed: d <= Approx( 1.23 ) for: 1.23 <= Approx( 1.23 )
Expand Down Expand Up @@ -2675,7 +2683,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: 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


14 changes: 11 additions & 3 deletions tests/SelfTest/Baselines/compact.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,15 @@ Json.tests.cpp:<line number>: passed: stream.str() == "[\n]" for: "[
"[
]"
Json.tests.cpp:<line number>: passed: stream.str() == "\"custom\"" for: ""custom"" == ""custom""
Json.tests.cpp:<line number>: passed: stream.str() == "\"\\\"\"" for: ""\""" == ""\"""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\"\"" for: ""\""" == ""\"""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\\\"" for: ""\\"" == ""\\""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"/\"" for: ""/"" == ""/""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\b\"" for: ""\b"" == ""\b""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\f\"" for: ""\f"" == ""\f""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\n\"" for: ""\n"" == ""\n""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\r\"" for: ""\r"" == ""\r""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\t\"" for: ""\t"" == ""\t""
Json.tests.cpp:<line number>: passed: sstream.str() == "\"\\\\/\\t\\r\\n\"" for: ""\\/\t\r\n"" == ""\\/\t\r\n""
Compilation.tests.cpp:<line number>: passed: []() { return true; }() for: true
Approx.tests.cpp:<line number>: passed: d <= Approx( 1.24 ) for: 1.23 <= Approx( 1.24 )
Approx.tests.cpp:<line number>: passed: d <= Approx( 1.23 ) for: 1.23 <= Approx( 1.23 )
Expand Down Expand Up @@ -2664,7 +2672,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: 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


4 changes: 2 additions & 2 deletions tests/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

106 changes: 101 additions & 5 deletions tests/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( stream.str() == "\"\\\"\"" )
REQUIRE( sstream.str() == "\"\\\"\"" )
with expansion:
""\""" == ""\"""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Backslash in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\\\\"" )
with expansion:
""\\"" == ""\\""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Forward slash in a string is **not** escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"/\"" )
with expansion:
""/"" == ""/""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Backspace in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\b\"" )
with expansion:
""\b"" == ""\b""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Formfeed in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\f\"" )
with expansion:
""\f"" == ""\f""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
linefeed in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: 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:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\r\"" )
with expansion:
""\r"" == ""\r""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
tab in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\t\"" )
with expansion:
""\t"" == ""\t""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
combination of characters is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" )
with expansion:
""\\/\t\r\n"" == ""\\/\t\r\n""

-------------------------------------------------------------------------------
Lambdas in assertions
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -18595,6 +18691,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: 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

106 changes: 101 additions & 5 deletions tests/SelfTest/Baselines/console.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( stream.str() == "\"\\\"\"" )
REQUIRE( sstream.str() == "\"\\\"\"" )
with expansion:
""\""" == ""\"""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Backslash in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\\\\"" )
with expansion:
""\\"" == ""\\""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Forward slash in a string is **not** escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"/\"" )
with expansion:
""/"" == ""/""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Backspace in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\b\"" )
with expansion:
""\b"" == ""\b""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
Formfeed in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\f\"" )
with expansion:
""\f"" == ""\f""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
linefeed in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: 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:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\r\"" )
with expansion:
""\r"" == ""\r""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
tab in a string is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\t\"" )
with expansion:
""\t"" == ""\t""

-------------------------------------------------------------------------------
JsonWriter escapes charaters in strings properly
combination of characters is escaped
-------------------------------------------------------------------------------
Json.tests.cpp:<line number>
...............................................................................

Json.tests.cpp:<line number>: PASSED:
REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" )
with expansion:
""\\/\t\r\n"" == ""\\/\t\r\n""

-------------------------------------------------------------------------------
Lambdas in assertions
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -18584,6 +18680,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: 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

12 changes: 10 additions & 2 deletions tests/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2258" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="129" skipped="12" tests="2266" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="random-seed" value="1"/>
<property name="filters" value="&quot;*&quot; ~[!nonportable] ~[!benchmark] ~[approvals]"/>
Expand Down Expand Up @@ -862,7 +862,15 @@ at Condition.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="JsonWriter/Moved from JsonObjectWriter shall not insert superfluous brace" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter/Moved from JsonArrayWriter shall not insert superfluous bracket" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter/Custom class shall be quoted" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter/String with a quote shall be espaced" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/Quote in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/Backslash in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/Forward slash in a string is **not** escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/Backspace in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/Formfeed in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/linefeed in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/carriage return in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/tab in a string is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="JsonWriter escapes charaters in strings properly/combination of characters is escaped" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Lambdas in assertions" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Less-than inequalities with different epsilons" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="ManuallyRegistered" time="{duration}" status="run"/>
Expand Down
Loading

0 comments on commit 733b901

Please sign in to comment.