From 0b4d7a5a1696dbdfb23fccad8c50db9dc9a417ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 29 Jul 2026 10:05:39 +0200 Subject: [PATCH] JSONReporter's listTests only lists class-name if it is non-empty --- benchmarks/discover_tests/listing_template.json | 11 +---------- extras/CatchAddTests.cmake | 2 +- src/catch2/reporters/catch_reporter_json.cpp | 8 ++++++-- tests/SelfTest/Baselines/compact.sw.approved.txt | 7 +++---- .../SelfTest/Baselines/compact.sw.multi.approved.txt | 7 +++---- tests/SelfTest/Baselines/console.sw.approved.txt | 7 +++---- .../SelfTest/Baselines/console.sw.multi.approved.txt | 7 +++---- tests/SelfTest/Baselines/tap.sw.approved.txt | 6 +++--- tests/SelfTest/Baselines/tap.sw.multi.approved.txt | 6 +++--- tests/SelfTest/Baselines/xml.sw.approved.txt | 7 +++---- tests/SelfTest/Baselines/xml.sw.multi.approved.txt | 7 +++---- tests/TestScripts/DiscoverTests/VerifyRegistration.py | 2 +- 12 files changed, 33 insertions(+), 44 deletions(-) diff --git a/benchmarks/discover_tests/listing_template.json b/benchmarks/discover_tests/listing_template.json index 43861ffd..7e976162 100644 --- a/benchmarks/discover_tests/listing_template.json +++ b/benchmarks/discover_tests/listing_template.json @@ -1,5 +1,5 @@ { - "version": 1, + "version": 2, "metadata": { "name": "benchmark-template", "catch2-version": "3.15.2" @@ -8,7 +8,6 @@ "tests": [ { "name": "Comparing function pointers", - "class-name": "", "tags": [ "function pointer", "Tricky" @@ -20,7 +19,6 @@ }, { "name": "Testing checked-if 4", - "class-name": "", "tags": [ "!shouldfail", "checked-if" @@ -32,7 +30,6 @@ }, { "name": "count_equidistant_floats - double", - "class-name": "", "tags": [ "approvals", "distance", @@ -45,7 +42,6 @@ }, { "name": "Usage of AllTrue range matcher", - "class-name": "", "tags": [ "matchers", "quantifiers", @@ -58,7 +54,6 @@ }, { "name": "Exception matchers that succeed", - "class-name": "", "tags": [ "!throws", "exceptions", @@ -83,7 +78,6 @@ }, { "name": "Approximate PI", - "class-name": "", "tags": [ "Approx", "PI" @@ -95,7 +89,6 @@ }, { "name": "TextFlow::Column respects width setting", - "class-name": "", "tags": [ "approvals", "column", @@ -108,7 +101,6 @@ }, { "name": "Generators internals", - "class-name": "", "tags": [ "generators", "internals" @@ -120,7 +112,6 @@ }, { "name": "Mayfail test case with nested sections", - "class-name": "", "tags": [ "!mayfail" ], diff --git a/extras/CatchAddTests.cmake b/extras/CatchAddTests.cmake index 59a39c70..b0d8c02b 100644 --- a/extras/CatchAddTests.cmake +++ b/extras/CatchAddTests.cmake @@ -326,7 +326,7 @@ function(catch_discover_tests_impl) # Parse JSON output for list of tests/class names/tags string(JSON version GET "${listing_output}" "version") - if(NOT version STREQUAL "1") + if(NOT version STREQUAL "2") message(FATAL_ERROR "Unsupported catch output version: '${version}'") endif() diff --git a/src/catch2/reporters/catch_reporter_json.cpp b/src/catch2/reporters/catch_reporter_json.cpp index 4328d560..6c093b40 100644 --- a/src/catch2/reporters/catch_reporter_json.cpp +++ b/src/catch2/reporters/catch_reporter_json.cpp @@ -16,6 +16,8 @@ namespace Catch { namespace { + static size_t kJsonOutputVersion = 2; + void writeSourceInfo( JsonObjectWriter& writer, SourceLineInfo const& sourceInfo ) { auto source_location_writer = @@ -58,7 +60,7 @@ namespace Catch { m_writers.emplace( Writer::Object ); auto& writer = m_objectWriters.top(); - writer.write( "version"_sr ).write( 1 ); + writer.write( "version"_sr ).write( kJsonOutputVersion ); { auto metadata_writer = writer.write( "metadata"_sr ).writeObject(); @@ -344,7 +346,9 @@ namespace Catch { auto const& info = test.getTestCaseInfo(); desc_writer.write( "name"_sr ).write( info.name ); - desc_writer.write( "class-name"_sr ).write( info.className ); + if (!info.className.empty()) { + desc_writer.write( "class-name"_sr ).write( info.className ); + } { auto tag_writer = desc_writer.write( "tags"_sr ).writeArray(); for ( auto const& tag : info.tags ) { diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 95809e24..94898c37 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1696,7 +1696,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fa " ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: console' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fakeTag"s) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1713,7 +1713,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fak ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fake reporter"s) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1728,7 +1728,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fak ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1738,7 +1738,6 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fa "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index 889ad483..2d268b2f 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -1694,7 +1694,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fa " ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: console' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fakeTag"s) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1711,7 +1711,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fak ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fake reporter"s) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1726,7 +1726,7 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring("fak ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' Reporters.tests.cpp:: passed: !(factories.empty()) for: !false Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -1736,7 +1736,6 @@ Reporters.tests.cpp:: passed: listingString, ContainsSubstring( "fa "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 1ae34284..9d8fa88f 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -11233,7 +11233,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring("fakeTag"s) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11273,7 +11273,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring("fake reporter"s) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11311,7 +11311,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11321,7 +11321,6 @@ with expansion: "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index 897f79ef..fcdf5f9f 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -11231,7 +11231,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring("fakeTag"s) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11271,7 +11271,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring("fake reporter"s) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11309,7 +11309,7 @@ Reporters.tests.cpp:: PASSED: REQUIRE_THAT( listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) ) with expansion: "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -11319,7 +11319,6 @@ with expansion: "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 9a4826ed..657b86ff 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2767,15 +2767,15 @@ ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && Cont # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring("fakeTag"s) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tags": [ { "aliases": [ "fakeTag" ], "count": 1 } ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring("fakeTag"s) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tags": [ { "aliases": [ "fakeTag" ], "count": 1 } ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring("fake reporter"s) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "reporters": [ { "name": "fake reporter", "description": "fake description" } ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring("fake reporter"s) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "reporters": [ { "name": "fake reporter", "description": "fake description" } ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tests": [ { "name": "fake test name", "class-name": "", "tags": [ "fakeTestTag" ], "source-location": { "filename": "fake-file.cpp", "line": 123456789 } } ]" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tests": [ { "name": "fake test name", "tags": [ "fakeTestTag" ], "source-location": { "filename": "fake-file.cpp", "line": 123456789 } } ]" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index cad770a2..5626093a 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -2765,15 +2765,15 @@ ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && Cont # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring("fakeTag"s) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tags": [ { "aliases": [ "fakeTag" ], "count": 1 } ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring("fakeTag"s) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tags": [ { "aliases": [ "fakeTag" ], "count": 1 } ]" contains: "fakeTag" with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring("fake reporter"s) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "reporters": [ { "name": "fake reporter", "description": "fake description" } ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring("fake reporter"s) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "reporters": [ { "name": "fake reporter", "description": "fake description" } ]" contains: "fake reporter" with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream -ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ "version": 1, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tests": [ { "name": "fake test name", "class-name": "", "tags": [ "fakeTestTag" ], "source-location": { "filename": "fake-file.cpp", "line": 123456789 } } ]" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: JSON' +ok {test-number} - listingString, ContainsSubstring( "fake test name"s ) && ContainsSubstring( "fakeTestTag"s ) for: "{ "version": 2, "metadata": { "name": "", "rng-seed": 1234, "catch2-version": "" }, "listings": { "tests": [ { "name": "fake test name", "tags": [ "fakeTestTag" ], "source-location": { "filename": "fake-file.cpp", "line": 123456789 } } ]" ( contains: "fake test name" and contains: "fakeTestTag" ) with 1 message: 'Tested reporter: JSON' # Reporter's write listings to provided stream ok {test-number} - !(factories.empty()) for: !false # Reporter's write listings to provided stream diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index cf9dfdb2..42b41eca 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -13419,7 +13419,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13456,7 +13456,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13491,7 +13491,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13501,7 +13501,6 @@ Approx( 0.98999999999999999 ) "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index 0ff255a2..3d265d40 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -13419,7 +13419,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13456,7 +13456,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13491,7 +13491,7 @@ Approx( 0.98999999999999999 ) "{ - "version": 1, + "version": 2, "metadata": { "name": "", "rng-seed": 1234, @@ -13501,7 +13501,6 @@ Approx( 0.98999999999999999 ) "tests": [ { "name": "fake test name", - "class-name": "", "tags": [ "fakeTestTag" ], diff --git a/tests/TestScripts/DiscoverTests/VerifyRegistration.py b/tests/TestScripts/DiscoverTests/VerifyRegistration.py index c281a403..2b85e969 100644 --- a/tests/TestScripts/DiscoverTests/VerifyRegistration.py +++ b/tests/TestScripts/DiscoverTests/VerifyRegistration.py @@ -87,7 +87,7 @@ def get_test_names(build_path: str) -> List[TestInfo]: with open(fname, mode='r', encoding='utf-8') as file: test_listing = json.load(file) - assert test_listing['version'] == 1 + assert test_listing['version'] == 2 tests = [] for test in test_listing['listings']['tests']: