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"
|
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 ),
|
|
|
|
m_sectionDepth( 0 )
|
|
|
|
{}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual ~XmlReporter();
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
public: // StreamingReporterBase
|
|
|
|
virtual ReporterPreferences getPreferences() const {
|
|
|
|
ReporterPreferences prefs;
|
|
|
|
prefs.shouldRedirectStdOut = true;
|
|
|
|
return prefs;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-02-17 10:28:21 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void noMatchingTestCases( std::string const& s ) {
|
|
|
|
StreamingReporterBase::noMatchingTestCases( s );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testRunStarting( testInfo );
|
|
|
|
m_xml.setStream( stream );
|
|
|
|
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
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void testGroupStarting( GroupInfo const& groupInfo ) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testCaseStarting(testInfo);
|
|
|
|
m_xml.startElement( "TestCase" ).writeAttribute( "name", trim( testInfo.name ) );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) {
|
|
|
|
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 );
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) { }
|
|
|
|
|
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) {
|
|
|
|
const AssertionResult& assertionResult = assertionStats.assertionResult;
|
|
|
|
|
|
|
|
// Print any info messages in <Info> tags.
|
|
|
|
if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) {
|
|
|
|
for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end();
|
|
|
|
it != itEnd;
|
|
|
|
++it ) {
|
|
|
|
if( it->type == ResultWas::Info ) {
|
|
|
|
m_xml.scopedElement( "Info" )
|
|
|
|
.writeText( it->message );
|
|
|
|
} else if ( it->type == ResultWas::Warning ) {
|
|
|
|
m_xml.scopedElement( "Warning" )
|
|
|
|
.writeText( it->message );
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
if( !m_config->includeSuccessfulResults() && isOk(assertionResult.getResultType()) )
|
|
|
|
return true;
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Print the expression if there is one.
|
2012-10-16 09:31:05 +02:00
|
|
|
if( assertionResult.hasExpression() ) {
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.startElement( "Expression" )
|
2012-11-13 10:44:52 +01:00
|
|
|
.writeAttribute( "success", assertionResult.succeeded() )
|
2012-10-24 22:59:47 +02:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Original" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getExpression() );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Expanded" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.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.
|
2012-10-16 09:31:05 +02:00
|
|
|
switch( assertionResult.getResultType() ) {
|
2014-10-11 23:20:55 +02:00
|
|
|
default:
|
|
|
|
break;
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::ThrewException:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Exception" )
|
2012-10-24 22:59:47 +02:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
2014-08-22 09:07:39 +02:00
|
|
|
case ResultWas::FatalErrorCondition:
|
|
|
|
m_xml.scopedElement( "Fatal Error Condition" )
|
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line )
|
|
|
|
.writeText( assertionResult.getMessage() );
|
|
|
|
break;
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::Info:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Info" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Warning" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Failure" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-12-28 15:42:46 +01:00
|
|
|
break;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2014-10-11 23:20:55 +02:00
|
|
|
|
2012-10-16 09:31:05 +02:00
|
|
|
if( assertionResult.hasExpression() )
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2014-10-11 23:20:55 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void sectionEnded( SectionStats const& sectionStats ) {
|
|
|
|
StreamingReporterBase::sectionEnded( sectionStats );
|
|
|
|
if( --m_sectionDepth > 0 ) {
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
|
|
|
.writeAttribute( "successes", sectionStats.assertions.passed )
|
|
|
|
.writeAttribute( "failures", sectionStats.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk );
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-06-01 20:40:27 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) {
|
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
|
|
|
m_xml.scopedElement( "OverallResult" )
|
|
|
|
.writeAttribute( "success", testCaseStats.totals.assertions.allPassed() );
|
|
|
|
m_xml.endElement();
|
2012-06-01 20:40:27 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
virtual void 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void 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 );
|
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:
|
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
|