Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/SelfTest/IntrospectiveTests
diff options
context:
space:
mode:
authorBeartama2023-11-14 23:52:15 +0200
committerGitHub2023-11-14 22:52:15 +0100
commit7bf136b501fef3e189b45091e8a4757a5eaef31f (patch)
treefe788efc997f7f4ed26fbe5d564d8d40df83c938 /tests/SelfTest/IntrospectiveTests
parent2c68a0d05f4fa8fb79cd43824f69568eb3954d4c (diff)
downloadcatch3-7bf136b501fef3e189b45091e8a4757a5eaef31f.tar.gz
Add JSON reporter (#2706)
Co-authored-by: Martin Hořeňovský <martin.horenovsky@gmail.com>
Diffstat (limited to 'tests/SelfTest/IntrospectiveTests')
-rw-r--r--tests/SelfTest/IntrospectiveTests/Json.tests.cpp116
-rw-r--r--tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp4
2 files changed, 119 insertions, 1 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/Json.tests.cpp b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp
new file mode 100644
index 00000000..9ade94ee
--- /dev/null
+++ b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp
@@ -0,0 +1,116 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/internal/catch_jsonwriter.hpp>
+#include <catch2/matchers/catch_matchers_string.hpp>
+
+#include <sstream>
+
+namespace {
+ struct Custom {};
+ std::ostream& operator<<( std::ostream& os, Custom const& ) {
+ return os << "custom";
+ }
+} // namespace
+
+TEST_CASE( "JsonWriter", "[JSON][JsonWriter]" ) {
+
+ std::stringstream stream{};
+ SECTION( "Newly constructed JsonWriter does nothing" ) {
+ Catch::JsonValueWriter writer{ stream };
+ REQUIRE( stream.str() == "" );
+ }
+
+ SECTION( "Calling writeObject will create an empty pair of braces" ) {
+ { auto writer = Catch::JsonValueWriter{ stream }.writeObject(); }
+ REQUIRE( stream.str() == "{\n}" );
+ }
+
+ SECTION( "Calling writeObject with key will create an object to write the "
+ "value" ) {
+ using Catch::Matchers::ContainsSubstring;
+ {
+ auto writer = Catch::JsonValueWriter{ stream }.writeObject();
+ writer.write( "int" ).write( 1 );
+ writer.write( "double" ).write( 1.5 );
+ writer.write( "true" ).write( true );
+ writer.write( "false" ).write( false );
+ writer.write( "string" ).write( "this is a string" );
+ writer.write( "array" ).writeArray().write( 1 ).write( 2 );
+ }
+ REQUIRE_THAT(
+ stream.str(),
+ ContainsSubstring( "\"int\": 1," ) &&
+ ContainsSubstring( "\"double\": 1.5," ) &&
+ ContainsSubstring( "\"true\": true," ) &&
+ ContainsSubstring( "\"false\": false," ) &&
+ ContainsSubstring( "\"string\": \"this is a string\"," ) &&
+ ContainsSubstring( "\"array\": [\n 1,\n 2\n ]\n}" ) );
+ }
+
+ SECTION( "nesting objects" ) {
+ using Catch::Matchers::ContainsSubstring;
+ {
+ auto writer = Catch::JsonValueWriter{ stream }.writeObject();
+ writer.write( "empty_object" ).writeObject();
+ writer.write( "fully_object" )
+ .writeObject()
+ .write( "key" )
+ .write( 1 );
+ }
+ REQUIRE_THAT( stream.str(),
+ ContainsSubstring( "\"empty_object\": {\n }," ) &&
+ ContainsSubstring(
+ "\"fully_object\": {\n \"key\": 1\n }" ) );
+ }
+
+ SECTION( "Calling writeArray will create an empty pair of braces" ) {
+ { auto writer = Catch::JsonValueWriter{ stream }.writeArray(); }
+ REQUIRE( stream.str() == "[\n]" );
+ }
+
+ SECTION( "Calling writeArray creates array to write the values to" ) {
+ {
+ auto writer = Catch::JsonValueWriter{ stream }.writeArray();
+ writer.write( 1 );
+ writer.write( 1.5 );
+ writer.write( true );
+ writer.write( false );
+ writer.write( "this is a string" );
+ writer.writeObject().write( "object" ).write( 42 );
+ writer.writeArray().write( "array" ).write( 42.5 );
+ }
+ 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]" );
+ }
+
+ SECTION(
+ "Moved from JsonObjectWriter shall not insert superfluous brace" ) {
+ {
+ auto writer = Catch::JsonObjectWriter{ stream };
+ auto another_writer = std::move( writer );
+ }
+ REQUIRE( stream.str() == "{\n}" );
+ }
+ SECTION(
+ "Moved from JsonArrayWriter shall not insert superfluous bracket" ) {
+ {
+ auto writer = Catch::JsonArrayWriter{ stream };
+ auto another_writer = std::move( writer );
+ }
+ REQUIRE( stream.str() == "[\n]" );
+ }
+ SECTION( "Custom class shall be quoted" ) {
+ Catch::JsonValueWriter{ stream }.write( Custom{} );
+ REQUIRE( stream.str() == "\"custom\"" );
+ }
+ SECTION( "String with a quote shall be espaced" ) {
+ Catch::JsonValueWriter{ stream }.write( "\"" );
+ REQUIRE( stream.str() == "\"\\\"\"" );
+ }
+}
diff --git a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp
index 1568c951..e5a65bda 100644
--- a/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp
+++ b/tests/SelfTest/IntrospectiveTests/Reporters.tests.cpp
@@ -109,7 +109,9 @@ TEST_CASE( "Reporter's write listings to provided stream", "[reporters]" ) {
auto sstream = Catch::Detail::make_unique<StringIStream>();
auto& sstreamRef = *sstream.get();
- Catch::Config config( Catch::ConfigData{} );
+ Catch::ConfigData cfg_data;
+ cfg_data.rngSeed = 1234;
+ Catch::Config config( cfg_data );
auto reporter = factory.second->create( Catch::ReporterConfig{
&config, CATCH_MOVE( sstream ), Catch::ColourMode::None, {} } );