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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "catch_reporter_bases.hpp"
|
|
|
|
|
2011-06-02 09:49:47 +02:00
|
|
|
#include "../internal/catch_capture.hpp"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_xmlwriter.hpp"
|
2014-10-12 01:57:45 +02:00
|
|
|
#include "../internal/catch_timer.h"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
namespace Catch {
|
2014-10-11 23:20:55 +02:00
|
|
|
class XmlReporter : public StreamingReporterBase {
|
2010-11-10 00:24:00 +01:00
|
|
|
public:
|
2014-10-11 23:20:55 +02:00
|
|
|
XmlReporter( ReporterConfig const& _config )
|
|
|
|
: StreamingReporterBase( _config ),
|
2016-11-26 12:11:23 +01:00
|
|
|
m_xml(_config.stream()),
|
2014-10-11 23:20:55 +02:00
|
|
|
m_sectionDepth( 0 )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual ~XmlReporter() CATCH_OVERRIDE;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
static std::string getDescription() {
|
2010-11-10 00:24:00 +01:00
|
|
|
return "Reports test results as an XML document";
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:09:43 +01:00
|
|
|
virtual std::string getStylesheetRef() const {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:26:17 +01:00
|
|
|
void writeSourceInfo( SourceLineInfo const& sourceInfo ) {
|
|
|
|
m_xml
|
|
|
|
.writeAttribute( "filename", sourceInfo.file )
|
|
|
|
.writeAttribute( "line", sourceInfo.line );
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
public: // StreamingReporterBase
|
2012-02-17 10:28:21 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void noMatchingTestCases( std::string const& s ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::noMatchingTestCases( s );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& testInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testRunStarting( testInfo );
|
2017-02-08 00:09:43 +01:00
|
|
|
std::string stylesheetRef = getStylesheetRef();
|
|
|
|
if( !stylesheetRef.empty() )
|
|
|
|
m_xml.writeStylesheetRef( stylesheetRef );
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.startElement( "Catch" );
|
|
|
|
if( !m_config->name().empty() )
|
|
|
|
m_xml.writeAttribute( "name", m_config->name() );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupStarting( GroupInfo const& groupInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.startElement( "Group" )
|
2014-10-11 23:20:55 +02:00
|
|
|
.writeAttribute( "name", groupInfo.name );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testCaseStarting(testInfo);
|
2017-02-07 23:52:32 +01:00
|
|
|
m_xml.startElement( "TestCase" )
|
|
|
|
.writeAttribute( "name", trim( testInfo.name ) )
|
|
|
|
.writeAttribute( "description", testInfo.description )
|
|
|
|
.writeAttribute( "tags", testInfo.tagsAsString );
|
2014-10-12 01:57:45 +02:00
|
|
|
|
2017-02-17 11:26:17 +01:00
|
|
|
writeSourceInfo( testInfo.lineInfo );
|
|
|
|
|
2014-10-12 01:57:45 +02:00
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
m_testCaseTimer.start();
|
2017-02-13 16:56:25 +01:00
|
|
|
m_xml.ensureTagClosed();
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
2013-07-24 20:13:08 +02:00
|
|
|
if( m_sectionDepth++ > 0 ) {
|
|
|
|
m_xml.startElement( "Section" )
|
2014-10-11 23:20:55 +02:00
|
|
|
.writeAttribute( "name", trim( sectionInfo.name ) )
|
|
|
|
.writeAttribute( "description", sectionInfo.description );
|
2017-02-17 11:26:17 +01:00
|
|
|
writeSourceInfo( sectionInfo.lineInfo );
|
2017-02-13 16:56:25 +01:00
|
|
|
m_xml.ensureTagClosed();
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { }
|
2014-10-11 23:20:55 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE {
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-03-03 15:12:47 +01:00
|
|
|
AssertionResult const& result = assertionStats.assertionResult;
|
|
|
|
|
|
|
|
bool includeResults = m_config->includeSuccessfulResults() || result.isOk();
|
|
|
|
|
|
|
|
if( includeResults ) {
|
|
|
|
// Print any info messages in <Info> tags.
|
2014-10-11 23:20:55 +02:00
|
|
|
for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end();
|
2017-03-03 15:12:47 +01:00
|
|
|
it != itEnd;
|
|
|
|
++it ) {
|
2014-10-11 23:20:55 +02:00
|
|
|
if( it->type == ResultWas::Info ) {
|
|
|
|
m_xml.scopedElement( "Info" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeText( it->message );
|
2014-10-11 23:20:55 +02:00
|
|
|
} else if ( it->type == ResultWas::Warning ) {
|
|
|
|
m_xml.scopedElement( "Warning" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeText( it->message );
|
2014-10-11 23:20:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Drop out if result was successful but we're not printing them.
|
2017-03-03 15:12:47 +01:00
|
|
|
if( !includeResults && result.getResultType() != ResultWas::Warning )
|
2014-10-11 23:20:55 +02:00
|
|
|
return true;
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2017-03-03 15:12:47 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Print the expression if there is one.
|
2017-03-03 15:12:47 +01:00
|
|
|
if( result.hasExpression() ) {
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.startElement( "Expression" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeAttribute( "success", result.succeeded() )
|
|
|
|
.writeAttribute( "type", result.getTestMacroName() );
|
2017-02-17 11:26:17 +01:00
|
|
|
|
2017-03-03 15:12:47 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Original" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeText( result.getExpression() );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Expanded" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeText( result.getExpandedExpression() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// And... Print a result applicable to each result type.
|
2017-03-03 15:12:47 +01:00
|
|
|
switch( result.getResultType() ) {
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::ThrewException:
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.startElement( "Exception" );
|
2017-03-03 15:12:47 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.endElement();
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
2014-08-22 09:07:39 +02:00
|
|
|
case ResultWas::FatalErrorCondition:
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.startElement( "FatalErrorCondition" );
|
2017-03-03 15:12:47 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.endElement();
|
2014-08-22 09:07:39 +02:00
|
|
|
break;
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::Info:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Info" )
|
2017-03-03 15:12:47 +01:00
|
|
|
.writeText( result.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2014-12-30 19:24:31 +01:00
|
|
|
// Warning will already have been written
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.startElement( "Failure" );
|
2017-03-03 15:12:47 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
2017-02-17 11:26:17 +01:00
|
|
|
m_xml.endElement();
|
2010-12-28 15:42:46 +01:00
|
|
|
break;
|
2014-12-30 19:24:31 +01:00
|
|
|
default:
|
|
|
|
break;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-03-03 15:12:47 +01:00
|
|
|
if( result.hasExpression() )
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::sectionEnded( sectionStats );
|
|
|
|
if( --m_sectionDepth > 0 ) {
|
2014-10-12 01:57:45 +02:00
|
|
|
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 );
|
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds );
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-06-01 20:40:27 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
2014-10-12 01:57:45 +02:00
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
|
2014-12-30 19:24:31 +01:00
|
|
|
e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() );
|
2014-10-12 01:57:45 +02:00
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() );
|
|
|
|
|
2017-02-06 17:14:06 +01:00
|
|
|
if( !testCaseStats.stdOut.empty() )
|
|
|
|
m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false );
|
|
|
|
if( !testCaseStats.stdErr.empty() )
|
|
|
|
m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false );
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.endElement();
|
2012-06-01 20:40:27 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
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();
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
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 );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
private:
|
2014-10-12 01:57:45 +02:00
|
|
|
Timer m_testCaseTimer;
|
2010-12-10 21:01:40 +01:00
|
|
|
XmlWriter m_xml;
|
2013-07-24 20:13:08 +02:00
|
|
|
int m_sectionDepth;
|
2010-11-10 00:24:00 +01:00
|
|
|
};
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 15:42:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|