2014-05-01 08:27:57 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin Moene on 2013-12-05.
|
|
|
|
* Copyright 2012 Martin Moene. 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_COMPACT_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_COMPACT_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_reporter_bases.hpp"
|
|
|
|
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_console_colour.hpp"
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
struct CompactReporter : StreamingReporterBase {
|
|
|
|
|
|
|
|
CompactReporter( ReporterConfig const& _config )
|
|
|
|
: StreamingReporterBase( _config )
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~CompactReporter();
|
|
|
|
|
|
|
|
static std::string getDescription() {
|
|
|
|
return "Reports test results on a single line, suitable for IDEs";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ReporterPreferences getPreferences() const {
|
|
|
|
ReporterPreferences prefs;
|
|
|
|
prefs.shouldRedirectStdOut = false;
|
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void noMatchingTestCases( std::string const& spec ) {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << "No test cases matched '" << spec << '\'' << std::endl;
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 16:16:17 +01:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) {}
|
2014-05-01 08:27:57 +02:00
|
|
|
|
|
|
|
virtual bool assertionEnded( AssertionStats const& _assertionStats ) {
|
|
|
|
AssertionResult const& result = _assertionStats.assertionResult;
|
|
|
|
|
|
|
|
bool printInfoMessages = true;
|
|
|
|
|
|
|
|
// Drop out if result was successful and we're not printing those
|
|
|
|
if( !m_config->includeSuccessfulResults() && result.isOk() ) {
|
|
|
|
if( result.getResultType() != ResultWas::Warning )
|
|
|
|
return false;
|
|
|
|
printInfoMessages = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertionPrinter printer( stream, _assertionStats, printInfoMessages );
|
|
|
|
printer.print();
|
|
|
|
|
|
|
|
stream << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void sectionEnded(SectionStats const& _sectionStats) override {
|
2017-03-02 16:16:17 +01:00
|
|
|
if (m_config->showDurations() == ShowDurations::Always) {
|
|
|
|
stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 08:27:57 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& _testRunStats ) {
|
|
|
|
printTotals( _testRunStats.totals );
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << '\n' << std::endl;
|
2014-05-01 08:27:57 +02:00
|
|
|
StreamingReporterBase::testRunEnded( _testRunStats );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class AssertionPrinter {
|
|
|
|
void operator= ( AssertionPrinter const& );
|
|
|
|
public:
|
|
|
|
AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages )
|
|
|
|
: stream( _stream )
|
|
|
|
, stats( _stats )
|
|
|
|
, result( _stats.assertionResult )
|
|
|
|
, messages( _stats.infoMessages )
|
|
|
|
, itMessage( _stats.infoMessages.begin() )
|
|
|
|
, printInfoMessages( _printInfoMessages )
|
|
|
|
{}
|
|
|
|
|
|
|
|
void print() {
|
|
|
|
printSourceInfo();
|
|
|
|
|
|
|
|
itMessage = messages.begin();
|
|
|
|
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::Ok:
|
|
|
|
printResultType( Colour::ResultSuccess, passedString() );
|
|
|
|
printOriginalExpression();
|
|
|
|
printReconstructedExpression();
|
|
|
|
if ( ! result.hasExpression() )
|
|
|
|
printRemainingMessages( Colour::None );
|
|
|
|
else
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ExpressionFailed:
|
|
|
|
if( result.isOk() )
|
|
|
|
printResultType( Colour::ResultSuccess, failedString() + std::string( " - but was ok" ) );
|
|
|
|
else
|
|
|
|
printResultType( Colour::Error, failedString() );
|
|
|
|
printOriginalExpression();
|
|
|
|
printReconstructedExpression();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
printResultType( Colour::Error, failedString() );
|
|
|
|
printIssue( "unexpected exception with message:" );
|
|
|
|
printMessage();
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
2014-08-22 09:07:39 +02:00
|
|
|
case ResultWas::FatalErrorCondition:
|
|
|
|
printResultType( Colour::Error, failedString() );
|
|
|
|
printIssue( "fatal error condition with message:" );
|
|
|
|
printMessage();
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
2014-05-01 08:27:57 +02:00
|
|
|
case ResultWas::DidntThrowException:
|
|
|
|
printResultType( Colour::Error, failedString() );
|
|
|
|
printIssue( "expected exception, got none" );
|
|
|
|
printExpressionWas();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
|
|
|
printResultType( Colour::None, "info" );
|
|
|
|
printMessage();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
|
|
|
printResultType( Colour::None, "warning" );
|
|
|
|
printMessage();
|
|
|
|
printRemainingMessages();
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
printResultType( Colour::Error, failedString() );
|
|
|
|
printIssue( "explicitly" );
|
|
|
|
printRemainingMessages( Colour::None );
|
|
|
|
break;
|
|
|
|
// These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
printResultType( Colour::Error, "** internal error **" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Colour::LightGrey
|
|
|
|
|
2014-05-19 19:57:14 +02:00
|
|
|
static Colour::Code dimColour() { return Colour::FileName; }
|
2014-05-01 08:27:57 +02:00
|
|
|
|
|
|
|
#ifdef CATCH_PLATFORM_MAC
|
|
|
|
static const char* failedString() { return "FAILED"; }
|
|
|
|
static const char* passedString() { return "PASSED"; }
|
|
|
|
#else
|
|
|
|
static const char* failedString() { return "failed"; }
|
|
|
|
static const char* passedString() { return "passed"; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void printSourceInfo() const {
|
|
|
|
Colour colourGuard( Colour::FileName );
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << result.getSourceInfo() << ':';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:16:43 +01:00
|
|
|
void printResultType( Colour::Code colour, std::string const& passOrFail ) const {
|
2014-05-01 08:27:57 +02:00
|
|
|
if( !passOrFail.empty() ) {
|
|
|
|
{
|
|
|
|
Colour colourGuard( colour );
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << ' ' << passOrFail;
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << ':';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:16:43 +01:00
|
|
|
void printIssue( std::string const& issue ) const {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << ' ' << issue;
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void printExpressionWas() {
|
|
|
|
if( result.hasExpression() ) {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << ';';
|
2014-05-01 08:27:57 +02:00
|
|
|
{
|
|
|
|
Colour colour( dimColour() );
|
|
|
|
stream << " expression was:";
|
|
|
|
}
|
|
|
|
printOriginalExpression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printOriginalExpression() const {
|
|
|
|
if( result.hasExpression() ) {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << ' ' << result.getExpression();
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printReconstructedExpression() const {
|
|
|
|
if( result.hasExpandedExpression() ) {
|
|
|
|
{
|
|
|
|
Colour colour( dimColour() );
|
|
|
|
stream << " for: ";
|
|
|
|
}
|
|
|
|
stream << result.getExpandedExpression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printMessage() {
|
|
|
|
if ( itMessage != messages.end() ) {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << " '" << itMessage->message << '\'';
|
2014-05-01 08:27:57 +02:00
|
|
|
++itMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 19:57:14 +02:00
|
|
|
void printRemainingMessages( Colour::Code colour = dimColour() ) {
|
2014-05-01 08:27:57 +02:00
|
|
|
if ( itMessage == messages.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// using messages.end() directly yields compilation error:
|
|
|
|
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 );
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << " with " << pluralise( N, "message" ) << ':';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(; itMessage != itEnd; ) {
|
|
|
|
// If this assertion is a warning ignore any INFO messages
|
|
|
|
if( printInfoMessages || itMessage->type != ResultWas::Info ) {
|
2017-01-29 23:07:15 +01:00
|
|
|
stream << " '" << itMessage->message << '\'';
|
2014-05-01 08:27:57 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Colour, message variants:
|
|
|
|
// - white: No tests ran.
|
|
|
|
// - red: Failed [both/all] N test cases, failed [both/all] M assertions.
|
|
|
|
// - white: Passed [both/all] N test cases (no assertions).
|
|
|
|
// - red: Failed N tests cases, failed M assertions.
|
|
|
|
// - green: Passed [both/all] N tests cases with M assertions.
|
|
|
|
|
2014-05-01 20:03:56 +02:00
|
|
|
std::string bothOrAll( std::size_t count ) const {
|
2017-01-29 23:07:15 +01:00
|
|
|
return count == 1 ? std::string() : count == 2 ? "both " : "all " ;
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void printTotals( const Totals& totals ) const {
|
|
|
|
if( totals.testCases.total() == 0 ) {
|
|
|
|
stream << "No tests ran.";
|
|
|
|
}
|
|
|
|
else if( totals.testCases.failed == totals.testCases.total() ) {
|
|
|
|
Colour colour( Colour::ResultError );
|
|
|
|
const std::string qualify_assertions_failed =
|
|
|
|
totals.assertions.failed == totals.assertions.total() ?
|
2017-01-29 23:07:15 +01:00
|
|
|
bothOrAll( totals.assertions.failed ) : std::string();
|
2014-05-01 08:27:57 +02:00
|
|
|
stream <<
|
2014-05-01 20:03:56 +02:00
|
|
|
"Failed " << bothOrAll( totals.testCases.failed )
|
2014-05-01 08:27:57 +02:00
|
|
|
<< pluralise( totals.testCases.failed, "test case" ) << ", "
|
|
|
|
"failed " << qualify_assertions_failed <<
|
2017-01-29 23:07:15 +01:00
|
|
|
pluralise( totals.assertions.failed, "assertion" ) << '.';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
else if( totals.assertions.total() == 0 ) {
|
|
|
|
stream <<
|
2014-05-01 20:03:56 +02:00
|
|
|
"Passed " << bothOrAll( totals.testCases.total() )
|
2014-05-01 08:27:57 +02:00
|
|
|
<< pluralise( totals.testCases.total(), "test case" )
|
|
|
|
<< " (no assertions).";
|
|
|
|
}
|
|
|
|
else if( totals.assertions.failed ) {
|
|
|
|
Colour colour( Colour::ResultError );
|
|
|
|
stream <<
|
|
|
|
"Failed " << pluralise( totals.testCases.failed, "test case" ) << ", "
|
2017-01-29 23:07:15 +01:00
|
|
|
"failed " << pluralise( totals.assertions.failed, "assertion" ) << '.';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Colour colour( Colour::ResultSuccess );
|
|
|
|
stream <<
|
2014-05-01 20:03:56 +02:00
|
|
|
"Passed " << bothOrAll( totals.testCases.passed )
|
2014-05-01 08:27:57 +02:00
|
|
|
<< pluralise( totals.testCases.passed, "test case" ) <<
|
2017-01-29 23:07:15 +01:00
|
|
|
" with " << pluralise( totals.assertions.passed, "assertion" ) << '.';
|
2014-05-01 08:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "compact", CompactReporter )
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_COMPACT_HPP_INCLUDED
|