Debian Codegraph @

src/external/catch2/tests/SelfTest/IntrospectiveTests/Json.tests.cpp

raw
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/benchmark/catch_benchmark.hpp>
11 #include <catch2/generators/catch_generators.hpp>
12 #include <catch2/internal/catch_jsonwriter.hpp>
13 #include <catch2/matchers/catch_matchers_string.hpp>
14
15 #include <sstream>
16
17 namespace {
18 struct Custom {};
19 static std::ostream& operator<<( std::ostream& os, Custom const& ) {
20 return os << "custom";
21 }
22
23 TEST_CASE( "JsonWriter", "[JSON][JsonWriter]" ) {
24
25 std::stringstream stream;
26 SECTION( "Newly constructed JsonWriter does nothing" ) {
27 Catch::JsonValueWriter writer{ stream };
28 REQUIRE( stream.str() == "" );
29 }
30
31 SECTION( "Calling writeObject will create an empty pair of braces" ) {
32 { auto writer = Catch::JsonValueWriter{ stream }.writeObject(); }
33 REQUIRE( stream.str() == "{\n}" );
34 }
35
36 SECTION( "Calling writeObject with key will create an object to write the "
37 "value" ) {
38 using Catch::Matchers::ContainsSubstring;
39 {
40 auto writer = Catch::JsonValueWriter{ stream }.writeObject();
41 writer.write( "int" ).write( 1 );
42 writer.write( "double" ).write( 1.5 );
43 writer.write( "true" ).write( true );
44 writer.write( "false" ).write( false );
45 writer.write( "string" ).write( "this is a string" );
46 writer.write( "array" ).writeArray().write( 1 ).write( 2 );
47 }
48 REQUIRE_THAT(
49 stream.str(),
50 ContainsSubstring( "\"int\": 1," ) &&
51 ContainsSubstring( "\"double\": 1.5," ) &&
52 ContainsSubstring( "\"true\": true," ) &&
53 ContainsSubstring( "\"false\": false," ) &&
54 ContainsSubstring( "\"string\": \"this is a string\"," ) &&
55 ContainsSubstring( "\"array\": [\n 1,\n 2\n ]\n}" ) );
56 }
57
58 SECTION( "nesting objects" ) {
59 using Catch::Matchers::ContainsSubstring;
60 {
61 auto writer = Catch::JsonValueWriter{ stream }.writeObject();
62 writer.write( "empty_object" ).writeObject();
63 writer.write( "fully_object" )
64 .writeObject()
65 .write( "key" )
66 .write( 1 );
67 }
68 REQUIRE_THAT( stream.str(),
69 ContainsSubstring( "\"empty_object\": {\n }," ) &&
70 ContainsSubstring(
71 "\"fully_object\": {\n \"key\": 1\n }" ) );
72 }
73
74 SECTION( "Calling writeArray will create an empty pair of braces" ) {
75 { auto writer = Catch::JsonValueWriter{ stream }.writeArray(); }
76 REQUIRE( stream.str() == "[\n]" );
77 }
78
79 SECTION( "Calling writeArray creates array to write the values to" ) {
80 {
81 auto writer = Catch::JsonValueWriter{ stream }.writeArray();
82 writer.write( 1 );
83 writer.write( 1.5 );
84 writer.write( true );
85 writer.write( false );
86 writer.write( "this is a string" );
87 writer.writeObject().write( "object" ).write( 42 );
88 writer.writeArray().write( "array" ).write( 42.5 );
89 }
90 REQUIRE( stream.str() == "[\n 1,\n 1.5,\n true,\n false,\n \"this is a string\",\n {\n \"object\": 42\n },\n [\n \"array\",\n 42.5\n ]\n]" );
91 }
92
93 SECTION(
94 "Moved from JsonObjectWriter shall not insert superfluous brace" ) {
95 {
96 auto writer = Catch::JsonObjectWriter{ stream };
97 auto another_writer = std::move( writer );
98 }
99 REQUIRE( stream.str() == "{\n}" );
100 }
101 SECTION(
102 "Moved from JsonArrayWriter shall not insert superfluous bracket" ) {
103 {
104 auto writer = Catch::JsonArrayWriter{ stream };
105 auto another_writer = std::move( writer );
106 }
107 REQUIRE( stream.str() == "[\n]" );
108 }
109 SECTION( "Custom class shall be quoted" ) {
110 Catch::JsonValueWriter{ stream }.write( Custom{} );
111 REQUIRE( stream.str() == "\"custom\"" );
112 }
113 }
114
115 TEST_CASE( "JsonWriter escapes characters in strings properly", "[JsonWriter]" ) {
116 std::stringstream sstream;
117 SECTION( "Quote in a string is escaped" ) {
118 Catch::JsonValueWriter{ sstream }.write( "\"" );
119 REQUIRE( sstream.str() == "\"\\\"\"" );
120 }
121 SECTION("Backslash in a string is escaped") {
122 Catch::JsonValueWriter{ sstream }.write( "\\" );
123 REQUIRE( sstream.str() == "\"\\\\\"" );
124 }
125 SECTION( "Forward slash in a string is **not** escaped" ) {
126 Catch::JsonValueWriter{ sstream }.write( "/" );
127 REQUIRE( sstream.str() == "\"/\"" );
128 }
129 SECTION( "Backspace in a string is escaped" ) {
130 Catch::JsonValueWriter{ sstream }.write( "\b" );
131 REQUIRE( sstream.str() == "\"\\b\"" );
132 }
133 SECTION( "Formfeed in a string is escaped" ) {
134 Catch::JsonValueWriter{ sstream }.write( "\f" );
135 REQUIRE( sstream.str() == "\"\\f\"" );
136 }
137 SECTION( "linefeed in a string is escaped" ) {
138 Catch::JsonValueWriter{ sstream }.write( "\n" );
139 REQUIRE( sstream.str() == "\"\\n\"" );
140 }
141 SECTION( "carriage return in a string is escaped" ) {
142 Catch::JsonValueWriter{ sstream }.write( "\r" );
143 REQUIRE( sstream.str() == "\"\\r\"" );
144 }
145 SECTION( "tab in a string is escaped" ) {
146 Catch::JsonValueWriter{ sstream }.write( "\t" );
147 REQUIRE( sstream.str() == "\"\\t\"" );
148 }
149 SECTION( "combination of characters is escaped" ) {
150 Catch::JsonValueWriter{ sstream }.write( "\\/\t\r\n" );
151 REQUIRE( sstream.str() == "\"\\\\/\\t\\r\\n\"" );
152 }
153 }
154
155 TEST_CASE( "JsonWriter benchmarks", "[JsonWriter][!benchmark]" ) {
156 const auto input_length = GENERATE( as<size_t>{}, 10, 100, 10'000 );
157 std::string test_input( input_length, 'a' );
158 BENCHMARK_ADVANCED( "write string, no-escaping, len=" +
159 std::to_string( input_length ) )(
160 Catch::Benchmark::Chronometer meter ) {
161 std::stringstream sstream;
162 meter.measure( [&]( int ) {
163 Catch::JsonValueWriter( sstream ).write( test_input );
164 } );
165 };
166
167 std::string escape_input( input_length, '\b' );
168 BENCHMARK_ADVANCED( "write string, all-escaped, len=" +
169 std::to_string( input_length ) )(
170 Catch::Benchmark::Chronometer meter ) {
171 std::stringstream sstream;
172 meter.measure( [&]( int ) {
173 Catch::JsonValueWriter( sstream ).write( escape_input );
174 } );
175 };
176 }
177
178 } // namespace