2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 28/10/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
#include "catch_reporter_xml.h"
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2011-06-02 09:49:47 +02:00
|
|
|
#include "../internal/catch_capture.hpp"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
2019-06-22 15:31:11 +02:00
|
|
|
#include "../internal/catch_list.h"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2017-10-30 12:14:20 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch
|
|
|
|
// Note that 4062 (not all labels are handled
|
|
|
|
// and default is missing) is enabled
|
|
|
|
#endif
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
namespace Catch {
|
2017-11-14 17:56:27 +01:00
|
|
|
XmlReporter::XmlReporter( ReporterConfig const& _config )
|
|
|
|
: StreamingReporterBase( _config ),
|
|
|
|
m_xml(_config.stream())
|
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
2018-07-14 20:51:02 +02:00
|
|
|
m_reporterPrefs.shouldReportAllAssertions = true;
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 01:02:19 +01:00
|
|
|
XmlReporter::~XmlReporter() = default;
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
std::string XmlReporter::getDescription() {
|
|
|
|
return "Reports test results as an XML document";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string XmlReporter::getStylesheetRef() const {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) {
|
|
|
|
m_xml
|
|
|
|
.writeAttribute( "filename", sourceInfo.file )
|
|
|
|
.writeAttribute( "line", sourceInfo.line );
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::noMatchingTestCases( std::string const& s ) {
|
|
|
|
StreamingReporterBase::noMatchingTestCases( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testRunStarting( testInfo );
|
|
|
|
std::string stylesheetRef = getStylesheetRef();
|
|
|
|
if( !stylesheetRef.empty() )
|
|
|
|
m_xml.writeStylesheetRef( stylesheetRef );
|
|
|
|
m_xml.startElement( "Catch" );
|
|
|
|
if( !m_config->name().empty() )
|
|
|
|
m_xml.writeAttribute( "name", m_config->name() );
|
2019-04-01 21:33:57 +02:00
|
|
|
if (m_config->testSpec().hasFilters())
|
|
|
|
m_xml.writeAttribute( "filters", serializeFilters( m_config->getTestsOrTags() ) );
|
2018-10-09 10:43:24 +02:00
|
|
|
if( m_config->rngSeed() != 0 )
|
|
|
|
m_xml.scopedElement( "Randomness" )
|
|
|
|
.writeAttribute( "seed", m_config->rngSeed() );
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) {
|
|
|
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
|
|
|
m_xml.startElement( "Group" )
|
|
|
|
.writeAttribute( "name", groupInfo.name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testCaseStarting(testInfo);
|
|
|
|
m_xml.startElement( "TestCase" )
|
|
|
|
.writeAttribute( "name", trim( testInfo.name ) )
|
|
|
|
.writeAttribute( "tags", testInfo.tagsAsString() );
|
|
|
|
|
|
|
|
writeSourceInfo( testInfo.lineInfo );
|
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
m_testCaseTimer.start();
|
|
|
|
m_xml.ensureTagClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) {
|
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
|
|
|
if( m_sectionDepth++ > 0 ) {
|
|
|
|
m_xml.startElement( "Section" )
|
2018-06-25 20:04:29 +02:00
|
|
|
.writeAttribute( "name", trim( sectionInfo.name ) );
|
2017-11-14 17:56:27 +01:00
|
|
|
writeSourceInfo( sectionInfo.lineInfo );
|
2017-02-13 16:56:25 +01:00
|
|
|
m_xml.ensureTagClosed();
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
void XmlReporter::assertionStarting( AssertionInfo const& ) { }
|
2014-10-11 23:20:55 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) {
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
AssertionResult const& result = assertionStats.assertionResult;
|
2017-03-03 15:12:47 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
bool includeResults = m_config->includeSuccessfulResults() || !result.isOk();
|
2017-03-03 15:12:47 +01:00
|
|
|
|
2017-12-06 15:30:17 +01:00
|
|
|
if( includeResults || result.getResultType() == ResultWas::Warning ) {
|
2017-11-14 17:56:27 +01:00
|
|
|
// Print any info messages in <Info> tags.
|
|
|
|
for( auto const& msg : assertionStats.infoMessages ) {
|
2017-12-06 15:30:17 +01:00
|
|
|
if( msg.type == ResultWas::Info && includeResults ) {
|
2017-11-14 17:56:27 +01:00
|
|
|
m_xml.scopedElement( "Info" )
|
|
|
|
.writeText( msg.message );
|
|
|
|
} else if ( msg.type == ResultWas::Warning ) {
|
|
|
|
m_xml.scopedElement( "Warning" )
|
|
|
|
.writeText( msg.message );
|
2014-10-11 23:20:55 +02:00
|
|
|
}
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// Drop out if result was successful but we're not printing them.
|
|
|
|
if( !includeResults && result.getResultType() != ResultWas::Warning )
|
|
|
|
return true;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// Print the expression if there is one.
|
|
|
|
if( result.hasExpression() ) {
|
|
|
|
m_xml.startElement( "Expression" )
|
|
|
|
.writeAttribute( "success", result.succeeded() )
|
|
|
|
.writeAttribute( "type", result.getTestMacroName() );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
m_xml.scopedElement( "Original" )
|
|
|
|
.writeText( result.getExpression() );
|
|
|
|
m_xml.scopedElement( "Expanded" )
|
|
|
|
.writeText( result.getExpandedExpression() );
|
2014-10-11 23:20:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// And... Print a result applicable to each result type.
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
m_xml.startElement( "Exception" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.endElement();
|
2017-11-14 17:56:27 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::FatalErrorCondition:
|
|
|
|
m_xml.startElement( "FatalErrorCondition" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
|
|
|
m_xml.endElement();
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
|
|
|
m_xml.scopedElement( "Info" )
|
|
|
|
.writeText( result.getMessage() );
|
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
|
|
|
// Warning will already have been written
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
m_xml.startElement( "Failure" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
|
|
|
m_xml.endElement();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-06-01 20:40:27 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
if( result.hasExpression() )
|
|
|
|
m_xml.endElement();
|
2014-10-12 01:57:45 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-06 17:14:06 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
void XmlReporter::sectionEnded( SectionStats const& sectionStats ) {
|
|
|
|
StreamingReporterBase::sectionEnded( sectionStats );
|
|
|
|
if( --m_sectionDepth > 0 ) {
|
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" );
|
|
|
|
e.writeAttribute( "successes", sectionStats.assertions.passed );
|
|
|
|
e.writeAttribute( "failures", sectionStats.assertions.failed );
|
|
|
|
e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
|
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
|
|
|
|
e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() );
|
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() );
|
|
|
|
|
|
|
|
if( !testCaseStats.stdOut.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), XmlFormatting::Newline );
|
2017-11-14 17:56:27 +01:00
|
|
|
if( !testCaseStats.stdErr.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), XmlFormatting::Newline );
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) {
|
|
|
|
StreamingReporterBase::testGroupEnded( testGroupStats );
|
|
|
|
// TODO: Check testGroupStats.aborting and act accordingly.
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
|
|
|
.writeAttribute( "successes", testGroupStats.totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", testGroupStats.totals.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk );
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) {
|
|
|
|
StreamingReporterBase::testRunEnded( testRunStats );
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
|
|
|
.writeAttribute( "successes", testRunStats.totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", testRunStats.totals.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk );
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2019-05-19 20:54:44 +02:00
|
|
|
#if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
|
2019-07-29 12:42:49 +02:00
|
|
|
void XmlReporter::benchmarkPreparing(std::string const& name) {
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.startElement("BenchmarkResults")
|
2019-07-29 12:42:49 +02:00
|
|
|
.writeAttribute("name", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::benchmarkStarting(BenchmarkInfo const &info) {
|
|
|
|
m_xml.writeAttribute("samples", info.samples)
|
2019-04-23 23:41:13 +02:00
|
|
|
.writeAttribute("resamples", info.resamples)
|
|
|
|
.writeAttribute("iterations", info.iterations)
|
|
|
|
.writeAttribute("clockResolution", static_cast<uint64_t>(info.clockResolution))
|
|
|
|
.writeAttribute("estimatedDuration", static_cast<uint64_t>(info.estimatedDuration))
|
|
|
|
.writeComment("All values in nano seconds");
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::benchmarkEnded(BenchmarkStats<> const& benchmarkStats) {
|
|
|
|
m_xml.startElement("mean")
|
|
|
|
.writeAttribute("value", static_cast<uint64_t>(benchmarkStats.mean.point.count()))
|
|
|
|
.writeAttribute("lowerBound", static_cast<uint64_t>(benchmarkStats.mean.lower_bound.count()))
|
|
|
|
.writeAttribute("upperBound", static_cast<uint64_t>(benchmarkStats.mean.upper_bound.count()))
|
|
|
|
.writeAttribute("ci", benchmarkStats.mean.confidence_interval);
|
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.startElement("standardDeviation")
|
|
|
|
.writeAttribute("value", benchmarkStats.standardDeviation.point.count())
|
|
|
|
.writeAttribute("lowerBound", benchmarkStats.standardDeviation.lower_bound.count())
|
|
|
|
.writeAttribute("upperBound", benchmarkStats.standardDeviation.upper_bound.count())
|
|
|
|
.writeAttribute("ci", benchmarkStats.standardDeviation.confidence_interval);
|
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.startElement("outliers")
|
|
|
|
.writeAttribute("variance", benchmarkStats.outlierVariance)
|
|
|
|
.writeAttribute("lowMild", benchmarkStats.outliers.low_mild)
|
|
|
|
.writeAttribute("lowSevere", benchmarkStats.outliers.low_severe)
|
|
|
|
.writeAttribute("highMild", benchmarkStats.outliers.high_mild)
|
|
|
|
.writeAttribute("highSevere", benchmarkStats.outliers.high_severe);
|
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::benchmarkFailed(std::string const &error) {
|
|
|
|
m_xml.scopedElement("failed").
|
|
|
|
writeAttribute("message", error);
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2019-05-19 20:54:44 +02:00
|
|
|
#endif // CATCH_CONFIG_ENABLE_BENCHMARKING
|
2019-04-23 23:41:13 +02:00
|
|
|
|
2019-06-22 15:31:11 +02:00
|
|
|
void XmlReporter::listReporters(std::vector<ReporterDescription> const& descriptions, Config const&) {
|
|
|
|
auto outerTag = m_xml.scopedElement("AvailableReporters");
|
|
|
|
for (auto const& reporter : descriptions) {
|
|
|
|
auto inner = m_xml.scopedElement("Reporter");
|
|
|
|
m_xml.startElement("Name", XmlFormatting::Indent)
|
|
|
|
.writeText(reporter.name, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Description", XmlFormatting::Indent)
|
|
|
|
.writeText(reporter.description, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 21:35:57 +01:00
|
|
|
void XmlReporter::listTests(std::vector<TestCaseHandle> const& tests, Config const&) {
|
2019-06-22 15:31:11 +02:00
|
|
|
auto outerTag = m_xml.scopedElement("MatchingTests");
|
|
|
|
for (auto const& test : tests) {
|
|
|
|
auto innerTag = m_xml.scopedElement("TestCase");
|
|
|
|
auto const& testInfo = test.getTestCaseInfo();
|
|
|
|
m_xml.startElement("Name", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.name, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("ClassName", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.className, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Tags", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.tagsAsString(), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
|
|
|
|
auto sourceTag = m_xml.scopedElement("SourceInfo");
|
|
|
|
m_xml.startElement("File", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.lineInfo.file, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Line", XmlFormatting::Indent)
|
|
|
|
.writeText(std::to_string(testInfo.lineInfo.line), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::listTags(std::vector<TagInfo> const& tags, Config const&) {
|
|
|
|
auto outerTag = m_xml.scopedElement("TagsFromMatchingTests");
|
|
|
|
for (auto const& tag : tags) {
|
|
|
|
auto innerTag = m_xml.scopedElement("Tag");
|
|
|
|
m_xml.startElement("Count", XmlFormatting::Indent)
|
|
|
|
.writeText(std::to_string(tag.count), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
auto aliasTag = m_xml.scopedElement("Aliases");
|
|
|
|
for (auto const& alias : tag.spellings) {
|
|
|
|
m_xml.startElement("Alias", XmlFormatting::Indent)
|
2019-11-07 12:39:07 +01:00
|
|
|
.writeText(static_cast<std::string>(alias), XmlFormatting::None)
|
2019-06-22 15:31:11 +02:00
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-17 18:07:24 +02:00
|
|
|
CATCH_REGISTER_REPORTER( "xml", XmlReporter )
|
2014-10-11 23:20:55 +02:00
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
2017-10-30 12:14:20 +01:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|