2010-11-09 18:24:00 -05: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
|
|
|
|
|
2011-06-02 03:49:47 -04:00
|
|
|
#include "../internal/catch_capture.hpp"
|
|
|
|
#include "../internal/catch_interfaces_reporter.h"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_xmlwriter.hpp"
|
2010-11-09 18:24:00 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
namespace Catch {
|
|
|
|
class XmlReporter : public SharedImpl<IReporter> {
|
2010-11-09 18:24:00 -05:00
|
|
|
public:
|
2012-07-20 14:07:42 -04:00
|
|
|
XmlReporter( const ReporterConfig& config ) : m_config( config ) {}
|
2010-11-09 18:24:00 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
static std::string getDescription() {
|
2010-11-09 18:24:00 -05:00
|
|
|
return "Reports test results as an XML document";
|
|
|
|
}
|
2012-08-13 02:46:10 -04:00
|
|
|
virtual ~XmlReporter();
|
2010-11-09 18:24:00 -05:00
|
|
|
|
2010-12-31 17:07:47 -05:00
|
|
|
private: // IReporter
|
2010-11-09 18:24:00 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual bool shouldRedirectStdout() const {
|
2012-02-17 04:28:21 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void StartTesting() {
|
2012-11-26 18:28:00 -05:00
|
|
|
m_xml = XmlWriter( m_config.stream() );
|
2011-04-12 02:39:45 -04:00
|
|
|
m_xml.startElement( "Catch" );
|
2012-11-26 18:28:00 -05:00
|
|
|
if( !m_config.name().empty() )
|
|
|
|
m_xml.writeAttribute( "name", m_config.name() );
|
2010-12-10 15:01:40 -05:00
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void EndTesting( const Totals& totals ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "OverallResults" )
|
2012-02-24 03:59:35 -05:00
|
|
|
.writeAttribute( "successes", totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", totals.assertions.failed );
|
2011-04-12 02:38:23 -04:00
|
|
|
m_xml.endElement();
|
2010-12-10 15:01:40 -05:00
|
|
|
}
|
2010-11-29 14:40:44 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void StartGroup( const std::string& groupName ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.startElement( "Group" )
|
|
|
|
.writeAttribute( "name", groupName );
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void EndGroup( const std::string&, const Totals& totals ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "OverallResults" )
|
2012-02-24 03:59:35 -05:00
|
|
|
.writeAttribute( "successes", totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", totals.assertions.failed );
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2010-11-29 14:40:44 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void StartSection( const std::string& sectionName, const std::string& description ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.startElement( "Section" )
|
|
|
|
.writeAttribute( "name", sectionName )
|
|
|
|
.writeAttribute( "description", description );
|
|
|
|
}
|
2012-08-31 03:10:36 -04:00
|
|
|
virtual void NoAssertionsInSection( const std::string& ) {}
|
|
|
|
virtual void NoAssertionsInTestCase( const std::string& ) {}
|
2010-12-10 15:01:40 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void EndSection( const std::string& /*sectionName*/, const Counts& assertions ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "OverallResults" )
|
2012-02-24 03:59:35 -05:00
|
|
|
.writeAttribute( "successes", assertions.passed )
|
|
|
|
.writeAttribute( "failures", assertions.failed );
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2010-11-29 14:40:44 -05:00
|
|
|
|
2012-11-25 06:19:55 -05:00
|
|
|
virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) {
|
|
|
|
m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.name );
|
2010-11-09 18:24:00 -05:00
|
|
|
m_currentTestSuccess = true;
|
|
|
|
}
|
|
|
|
|
2012-10-16 03:31:05 -04:00
|
|
|
virtual void Result( const Catch::AssertionResult& assertionResult ) {
|
2012-11-26 18:28:00 -05:00
|
|
|
if( !m_config.includeSuccessfulResults() && assertionResult.getResultType() == ResultWas::Ok )
|
2010-11-09 18:24:00 -05:00
|
|
|
return;
|
|
|
|
|
2012-10-16 03:31:05 -04:00
|
|
|
if( assertionResult.hasExpression() ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.startElement( "Expression" )
|
2012-11-13 04:44:52 -05:00
|
|
|
.writeAttribute( "success", assertionResult.succeeded() )
|
2012-10-24 16:59:47 -04:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line );
|
2010-12-10 15:01:40 -05:00
|
|
|
|
|
|
|
m_xml.scopedElement( "Original" )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getExpression() );
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "Expanded" )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getExpandedExpression() );
|
2012-11-13 04:44:52 -05:00
|
|
|
m_currentTestSuccess &= assertionResult.succeeded();
|
2010-11-09 18:24:00 -05:00
|
|
|
}
|
2010-12-10 15:01:40 -05:00
|
|
|
|
2012-10-16 03:31:05 -04:00
|
|
|
switch( assertionResult.getResultType() ) {
|
2010-11-09 18:24:00 -05:00
|
|
|
case ResultWas::ThrewException:
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "Exception" )
|
2012-10-24 16:59:47 -04:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-09 18:24:00 -05:00
|
|
|
m_currentTestSuccess = false;
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "Info" )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-09 18:24:00 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "Warning" )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-09 18:24:00 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "Failure" )
|
2012-10-16 03:31:05 -04:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-09 18:24:00 -05:00
|
|
|
m_currentTestSuccess = false;
|
|
|
|
break;
|
2011-09-29 03:58:40 -04:00
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::Ok:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::ExpressionFailed:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
case ResultWas::DidntThrowException:
|
2010-12-28 09:42:46 -05:00
|
|
|
break;
|
2010-11-09 18:24:00 -05:00
|
|
|
}
|
2012-10-16 03:31:05 -04:00
|
|
|
if( assertionResult.hasExpression() )
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.endElement();
|
2010-11-09 18:24:00 -05:00
|
|
|
}
|
2012-06-01 14:40:27 -04:00
|
|
|
|
|
|
|
virtual void Aborted() {
|
|
|
|
// !TBD
|
|
|
|
}
|
2010-11-09 18:24:00 -05:00
|
|
|
|
2012-11-25 06:19:55 -05:00
|
|
|
virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string&, const std::string& ) {
|
2010-12-10 15:01:40 -05:00
|
|
|
m_xml.scopedElement( "OverallResult" ).writeAttribute( "success", m_currentTestSuccess );
|
|
|
|
m_xml.endElement();
|
2010-11-09 18:24:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-07-20 14:07:42 -04:00
|
|
|
ReporterConfig m_config;
|
2010-11-09 18:24:00 -05:00
|
|
|
bool m_currentTestSuccess;
|
2010-12-10 15:01:40 -05:00
|
|
|
XmlWriter m_xml;
|
2010-11-09 18:24:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 09:42:46 -05:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|