2015-10-15 08:54:37 +02:00
|
|
|
/*
|
2017-02-22 13:28:13 +01:00
|
|
|
* Created by Colton Wolkins on 2015-08-15.
|
|
|
|
* Copyright 2015 Martin Moene. All rights reserved.
|
2015-10-15 08:54:37 +02:00
|
|
|
*
|
|
|
|
* 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_TAP_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|
|
|
|
|
|
|
|
|
2017-02-22 13:28:13 +01:00
|
|
|
// Don't #include any Catch headers here - we can assume they are already
|
|
|
|
// included before this header.
|
|
|
|
// This is not good practice in general but is necessary in this case so this
|
|
|
|
// file can be distributed as a single header that works with the main
|
|
|
|
// Catch single header.
|
|
|
|
|
|
|
|
#include <algorithm>
|
2015-10-15 08:54:37 +02:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
2017-07-09 11:46:53 +02:00
|
|
|
struct TAPReporter : StreamingReporterBase<TAPReporter> {
|
2015-10-15 08:54:37 +02:00
|
|
|
|
2017-04-25 19:56:53 +02:00
|
|
|
using StreamingReporterBase::StreamingReporterBase;
|
2015-10-15 08:54:37 +02:00
|
|
|
|
|
|
|
virtual ~TAPReporter();
|
|
|
|
|
|
|
|
static std::string getDescription() {
|
|
|
|
return "Reports test results in TAP format, suitable for test harneses";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ReporterPreferences getPreferences() const {
|
|
|
|
ReporterPreferences prefs;
|
|
|
|
prefs.shouldRedirectStdOut = false;
|
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void noMatchingTestCases( std::string const& spec ) {
|
|
|
|
stream << "# No test cases matched '" << spec << "'" << std::endl;
|
|
|
|
}
|
|
|
|
|
2017-02-22 13:28:13 +01:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) {}
|
2015-10-15 08:54:37 +02:00
|
|
|
|
|
|
|
virtual bool assertionEnded( AssertionStats const& _assertionStats ) {
|
2017-03-01 10:24:58 +01:00
|
|
|
++counter;
|
2015-10-15 08:54:37 +02:00
|
|
|
|
2017-03-02 15:54:08 +01:00
|
|
|
AssertionPrinter printer( stream, _assertionStats, counter );
|
2015-10-15 08:54:37 +02:00
|
|
|
printer.print();
|
2017-02-22 13:28:13 +01:00
|
|
|
stream << " # " << currentTestCaseInfo->name ;
|
2015-10-15 08:54:37 +02:00
|
|
|
|
|
|
|
stream << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void testRunEnded( TestRunStats const& _testRunStats ) {
|
|
|
|
printTotals( _testRunStats.totals );
|
|
|
|
stream << "\n" << std::endl;
|
|
|
|
StreamingReporterBase::testRunEnded( _testRunStats );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-25 19:56:53 +02:00
|
|
|
size_t counter = 0;
|
2015-10-15 08:54:37 +02:00
|
|
|
class AssertionPrinter {
|
|
|
|
public:
|
2017-07-20 00:27:28 +02:00
|
|
|
AssertionPrinter& operator= ( AssertionPrinter const& ) = delete;
|
|
|
|
AssertionPrinter( AssertionPrinter const& ) = delete;
|
2017-03-02 15:54:08 +01:00
|
|
|
AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, size_t counter )
|
2015-10-15 08:54:37 +02:00
|
|
|
: stream( _stream )
|
|
|
|
, stats( _stats )
|
|
|
|
, result( _stats.assertionResult )
|
|
|
|
, messages( _stats.infoMessages )
|
|
|
|
, itMessage( _stats.infoMessages.begin() )
|
2017-03-02 15:54:08 +01:00
|
|
|
, printInfoMessages( true )
|
2017-02-22 13:28:13 +01:00
|
|
|
, counter(counter)
|
2015-10-15 08:54:37 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
void print() {
|
|
|
|
itMessage = messages.begin();
|
|
|
|
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::Ok:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( passedString() );
|
2015-10-15 08:54:37 +02:00
|
|
|
printOriginalExpression();
|
2017-02-22 13:28:13 +01:00
|
|
|
printReconstructedExpression();
|
2015-10-15 08:54:37 +02:00
|
|
|
if ( ! result.hasExpression() )
|
|
|
|
printRemainingMessages( Colour::None );
|
|
|
|
else
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ExpressionFailed:
|
2017-02-22 13:28:13 +01:00
|
|
|
if (result.isOk()) {
|
|
|
|
printResultType(passedString());
|
|
|
|
} else {
|
|
|
|
printResultType(failedString());
|
|
|
|
}
|
2015-10-15 08:54:37 +02:00
|
|
|
printOriginalExpression();
|
2017-02-22 13:28:13 +01:00
|
|
|
printReconstructedExpression();
|
|
|
|
if (result.isOk()) {
|
|
|
|
printIssue(" # TODO");
|
|
|
|
}
|
2015-10-15 08:54:37 +02:00
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ThrewException:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( failedString() );
|
2015-10-15 08:54:37 +02:00
|
|
|
printIssue( "unexpected exception with message:" );
|
|
|
|
printMessage();
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::FatalErrorCondition:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( failedString() );
|
2015-10-15 08:54:37 +02:00
|
|
|
printIssue( "fatal error condition with message:" );
|
|
|
|
printMessage();
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::DidntThrowException:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( failedString() );
|
2015-10-15 08:54:37 +02:00
|
|
|
printIssue( "expected exception, got none" );
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( "info" );
|
2015-10-15 08:54:37 +02:00
|
|
|
printMessage();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( "warning" );
|
2015-10-15 08:54:37 +02:00
|
|
|
printMessage();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( failedString() );
|
2015-10-15 08:54:37 +02:00
|
|
|
printIssue( "explicitly" );
|
|
|
|
printRemainingMessages( Colour::None );
|
|
|
|
break;
|
|
|
|
// These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
2017-02-22 13:28:13 +01:00
|
|
|
printResultType( "** internal error **" );
|
2015-10-15 08:54:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static Colour::Code dimColour() { return Colour::FileName; }
|
|
|
|
|
|
|
|
static const char* failedString() { return "not ok"; }
|
|
|
|
static const char* passedString() { return "ok"; }
|
|
|
|
|
|
|
|
void printSourceInfo() const {
|
2017-03-02 15:54:08 +01:00
|
|
|
Colour colourGuard( dimColour() );
|
2015-10-15 08:54:37 +02:00
|
|
|
stream << result.getSourceInfo() << ":";
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:16:43 +01:00
|
|
|
void printResultType( std::string const& passOrFail ) const {
|
2015-10-15 08:54:37 +02:00
|
|
|
if( !passOrFail.empty() ) {
|
2017-03-02 15:54:08 +01:00
|
|
|
stream << passOrFail << ' ' << counter << " -";
|
2015-10-15 08:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:16:43 +01:00
|
|
|
void printIssue( std::string const& issue ) const {
|
2015-10-15 08:54:37 +02:00
|
|
|
stream << " " << issue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printExpressionWas() {
|
|
|
|
if( result.hasExpression() ) {
|
|
|
|
stream << ";";
|
|
|
|
{
|
|
|
|
Colour colour( dimColour() );
|
|
|
|
stream << " expression was:";
|
|
|
|
}
|
|
|
|
printOriginalExpression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printOriginalExpression() const {
|
|
|
|
if( result.hasExpression() ) {
|
|
|
|
stream << " " << result.getExpression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printReconstructedExpression() const {
|
|
|
|
if( result.hasExpandedExpression() ) {
|
|
|
|
{
|
|
|
|
Colour colour( dimColour() );
|
|
|
|
stream << " for: ";
|
|
|
|
}
|
2017-02-22 13:28:13 +01:00
|
|
|
std::string expr = result.getExpandedExpression();
|
|
|
|
std::replace( expr.begin(), expr.end(), '\n', ' ');
|
2015-10-15 08:54:37 +02:00
|
|
|
stream << expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printMessage() {
|
|
|
|
if ( itMessage != messages.end() ) {
|
|
|
|
stream << " '" << itMessage->message << "'";
|
|
|
|
++itMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printRemainingMessages( Colour::Code colour = dimColour() ) {
|
2017-03-02 15:54:08 +01:00
|
|
|
if (itMessage == messages.end()) {
|
2015-10-15 08:54:37 +02:00
|
|
|
return;
|
2017-03-02 15:54:08 +01:00
|
|
|
}
|
2015-10-15 08:54:37 +02:00
|
|
|
|
2017-07-18 09:08:17 +02:00
|
|
|
// using messages.end() directly (or auto) yields compilation error:
|
2015-10-15 08:54:37 +02:00
|
|
|
std::vector<MessageInfo>::const_iterator itEnd = messages.end();
|
|
|
|
const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) );
|
|
|
|
|
|
|
|
{
|
|
|
|
Colour colourGuard( colour );
|
|
|
|
stream << " with " << pluralise( N, "message" ) << ":";
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; itMessage != itEnd; ) {
|
|
|
|
// If this assertion is a warning ignore any INFO messages
|
|
|
|
if( printInfoMessages || itMessage->type != ResultWas::Info ) {
|
|
|
|
stream << " '" << itMessage->message << "'";
|
|
|
|
if ( ++itMessage != itEnd ) {
|
|
|
|
Colour colourGuard( dimColour() );
|
|
|
|
stream << " and";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostream& stream;
|
|
|
|
AssertionStats const& stats;
|
|
|
|
AssertionResult const& result;
|
|
|
|
std::vector<MessageInfo> messages;
|
|
|
|
std::vector<MessageInfo>::const_iterator itMessage;
|
|
|
|
bool printInfoMessages;
|
2017-02-22 13:28:13 +01:00
|
|
|
size_t counter;
|
2015-10-15 08:54:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void printTotals( const Totals& totals ) const {
|
|
|
|
if( totals.testCases.total() == 0 ) {
|
|
|
|
stream << "1..0 # Skipped: No tests ran.";
|
2017-02-22 13:28:13 +01:00
|
|
|
} else {
|
2015-10-15 08:54:37 +02:00
|
|
|
stream << "1.." << counter;
|
2017-02-22 13:28:13 +01:00
|
|
|
}
|
2015-10-15 08:54:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-22 13:28:13 +01:00
|
|
|
#ifdef CATCH_IMPL
|
|
|
|
TAPReporter::~TAPReporter() {}
|
|
|
|
#endif
|
|
|
|
|
2015-10-15 08:54:37 +02:00
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "tap", TAPReporter )
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_TAP_HPP_INCLUDED
|