Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON reporter #2706

Merged
merged 41 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
2acb192
Add initial class for JsonWriter
uyha Jun 18, 2023
85c8fdc
Add full implementation for JsonWriter and tests for it
uyha Jun 25, 2023
3bccecf
Make JsonWriter accept more arithmetic types
uyha Jun 25, 2023
aabaced
Make JsonValueWriter accept even more types
uyha Jun 25, 2023
e499b9c
Make constructor with indent level public, remove useless JsonWriter,…
uyha Jun 25, 2023
af8d480
Handle moved from Json{Object,Array}Writer
uyha Jun 25, 2023
8450e1f
Fix quoting bug
uyha Jun 25, 2023
50818bb
Add test for Custom class for JsonValueWriter
uyha Jun 25, 2023
7645901
Handle quote and add test for it
uyha Jun 25, 2023
51daec3
Add barebone JsonReporter which only reports test cases
uyha Jun 25, 2023
3466ae2
Remove unnecessary functions
uyha Jun 25, 2023
5de01e6
Change function names to {start,end}{Object,Array}, add section infor…
uyha Jun 26, 2023
bca353b
Escape quotes for JSON report passing regular expression
uyha Jun 26, 2023
875a266
Fix PR according to comments
uyha Jul 29, 2023
8311231
Add missing endline in test case
uyha Jul 29, 2023
aef7fb0
Add "version" and "metadata" field
uyha Jul 29, 2023
6552d85
Implement listing
uyha Jul 29, 2023
0231352
Remove unnecessary startObject
uyha Jul 29, 2023
17d5ac1
Change how writeSourceInfo and writeCounts are implemented
uyha Jul 30, 2023
1a77b58
Fix "totals" attribute
uyha Jul 30, 2023
27d765e
Change "test-cases" to "test-resulst", use `writeSourceInfo` instead …
uyha Jul 30, 2023
cf58e93
Unify source-location, show properties as a list
uyha Jul 30, 2023
755800a
Adjust section statistics to comments
uyha Jul 30, 2023
23cfd79
Have `startArray` and `startObject` return the writer
uyha Aug 12, 2023
c83f25f
Make the code compilable for C++ 14
uyha Oct 7, 2023
f836897
put sections in `sections` property, still need to make them nested
uyha Oct 7, 2023
0737f88
switch to using `CumulativeReporterBase` instead of `StreamingReporte…
uyha Oct 7, 2023
2e422fe
JSON reporter: clean up the listings a bit
horenmar Oct 10, 2023
d3747f5
JSON reporter asks for stdout redirect
horenmar Oct 10, 2023
88ef5d6
Cleanout superfluous variable assignments
horenmar Oct 10, 2023
151f344
Stub of streaming json reporter
horenmar Nov 13, 2023
121ffa6
Fix size_t -> uint64_t confusion
horenmar Nov 13, 2023
d4f6649
Fix test
horenmar Nov 13, 2023
87b31a0
Actually commit updated approval tests
horenmar Nov 14, 2023
1f5542b
Meson build fix
horenmar Nov 14, 2023
1c41509
Fix license header
horenmar Nov 14, 2023
ff586de
Try fix for old GCC versions
horenmar Nov 14, 2023
d2dcbe7
Fix raw string literal compilation for older compiler
horenmar Nov 14, 2023
9b0c7b3
.
horenmar Nov 14, 2023
934c4ef
meson
horenmar Nov 14, 2023
f372cec
Fix missing include
horenmar Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions src/catch2/reporters/catch_reporter_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ namespace Catch {
CumulativeReporterBase{ CATCH_MOVE( config ) } {
m_objectWriters.emplace( m_stream );
m_writers.emplace( Writer::Object );
auto& writer = m_objectWriters.top();

writer.write( "version" ).write( 1 );

{
auto metadata_writer = writer.write( "metadata" ).writeObject();
metadata_writer.write( "name" ).write( m_config->name() );
metadata_writer.write( "rng-seed" ).write( m_config->rngSeed() );
metadata_writer.write( "catch2-version" ).write( libraryVersion() );
if ( m_config->testSpec().hasFilters() ) {
metadata_writer.write( "filters" )
.write( m_config->testSpec() );
}
}
}

JsonReporter::~JsonReporter() {
Expand All @@ -87,6 +101,7 @@ namespace Catch {
break;
}
}
m_stream << std::endl;
}

JsonArrayWriter& JsonReporter::startArray() {
Expand Down Expand Up @@ -128,29 +143,18 @@ namespace Catch {
return !m_writers.empty() && m_writers.top() == writer;
}

void JsonReporter::startListing() {
m_startedListing = true;
startObject( "listings" );
}

std::string JsonReporter::getDescription() {
return "Reports test results as a JSON document";
}

void JsonReporter::testRunStarting( TestRunInfo const& testInfo ) {
CumulativeReporterBase::testRunStarting( testInfo );

void JsonReporter::testRunStarting( TestRunInfo const& /*testInfo*/ ) {
if ( !isInside( Writer::Object ) ) { return; }

auto& writer = m_objectWriters.top();

writer.write( "version" ).write( 1 );

{
auto metadata_writer = writer.write( "metadata" ).writeObject();
metadata_writer.write( "name" ).write( m_config->name() );
metadata_writer.write( "rng-seed" ).write( m_config->rngSeed() );
metadata_writer.write( "catch2-version" ).write( libraryVersion() );
if ( m_config->testSpec().hasFilters() ) {
metadata_writer.write( "filters" )
.write( m_config->testSpec() );
}
}
if ( m_startedListing ) { endObject(); }

startArray( "test-cases" );
}
Expand All @@ -176,6 +180,11 @@ namespace Catch {

void JsonReporter::listReporters(
std::vector<ReporterDescription> const& descriptions ) {
if ( !m_startedListing ) {
startListing();
m_startedListing = true;
}

if ( !isInside( Writer::Object ) ) { return; }

auto writer = m_objectWriters.top().write( "reporters" ).writeArray();
Expand All @@ -187,6 +196,10 @@ namespace Catch {
}
void JsonReporter::listListeners(
std::vector<ListenerDescription> const& descriptions ) {
if ( !m_startedListing) {
startListing();
m_startedListing = true;
}
if ( !isInside( Writer::Object ) ) { return; }

auto writer = m_objectWriters.top().write( "listeners" ).writeArray();
Expand All @@ -198,6 +211,12 @@ namespace Catch {
}
}
void JsonReporter::listTests( std::vector<TestCaseHandle> const& tests ) {
if ( !m_startedListing ) {
startListing();
m_startedListing = true;
}

// TODO: Why? This should just assert. If the object is not there, there is a bug.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what to do when writing this, since I saw something similar to this in the XML reporter, I followed it

if ( !isInside( Writer::Object ) ) { return; }

auto writer = m_objectWriters.top().write( "tests" ).writeArray();
Expand All @@ -218,6 +237,10 @@ namespace Catch {
}
}
void JsonReporter::listTags( std::vector<TagInfo> const& tags ) {
if ( !m_startedListing ) {
startListing();
m_startedListing = true;
}
if ( !isInside( Writer::Object ) ) { return; }

auto writer = m_objectWriters.top().write( "tags" ).writeArray();
Expand Down
4 changes: 4 additions & 0 deletions src/catch2/reporters/catch_reporter_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace Catch {

bool isInside( Writer writer );

void startListing();

// Invariant:
// When m_writers is not empty and its top element is
// - Writer::Object, then m_objectWriters is not be empty
Expand All @@ -67,6 +69,8 @@ namespace Catch {
std::stack<JsonArrayWriter> m_arrayWriters{};
std::stack<Writer> m_writers{};

bool m_startedListing = false;

// std::size_t m_sectionDepth = 0;
// std::size_t m_sectionStarted = 0;
};
Expand Down