2012-12-05 09:40:53 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 5/12/2012.
|
|
|
|
* Copyright 2012 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_CONSOLE_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_CONSOLE_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "../internal/catch_interfaces_reporter.h"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_console_colour.hpp"
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
struct ConsoleReporter : AccumulatingReporter {
|
|
|
|
ConsoleReporter( ReporterConfig const& _config )
|
|
|
|
: AccumulatingReporter( _config )
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~ConsoleReporter();
|
|
|
|
static std::string getDescription() {
|
|
|
|
return "Reports test results as plain lines of text";
|
|
|
|
}
|
|
|
|
virtual ReporterPreferences getPreferences() const {
|
|
|
|
ReporterPreferences prefs;
|
|
|
|
prefs.shouldRedirectStdOut = false;
|
|
|
|
return prefs;
|
|
|
|
|
|
|
|
}
|
|
|
|
void lazyPrintRunInfo() {
|
|
|
|
stream << "[Started testing: " << testRunInfo->name << "]" << std::endl;
|
|
|
|
testRunInfo.reset();
|
|
|
|
}
|
|
|
|
void lazyPrintGroupInfo() {
|
|
|
|
if( !unusedGroupInfo->name.empty() )
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Group: '" << unusedGroupInfo->name << "']" << std::endl;
|
2012-12-05 09:40:53 +01:00
|
|
|
unusedGroupInfo.reset();
|
|
|
|
}
|
|
|
|
void lazyPrintTestCaseInfo() {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Test case: '" << unusedTestCaseInfo->name << "']" << std::endl;
|
2012-12-05 09:40:53 +01:00
|
|
|
unusedTestCaseInfo.reset();
|
|
|
|
}
|
2012-12-09 12:20:46 +01:00
|
|
|
|
2012-12-10 09:54:57 +01:00
|
|
|
void lazyPrintSectionInfo() {
|
2012-12-09 12:20:46 +01:00
|
|
|
std::vector<ThreadedSectionInfo*> sections;
|
|
|
|
for( ThreadedSectionInfo* section = unusedSectionInfo.get();
|
|
|
|
section && !section->printed;
|
|
|
|
section = section->parent.get() )
|
|
|
|
sections.push_back( section );
|
|
|
|
|
|
|
|
typedef std::vector<ThreadedSectionInfo*>::const_reverse_iterator It;
|
|
|
|
for( It it = sections.rbegin(), itEnd = sections.rend(); it != itEnd; ++it ) {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Section: " << "'" + (*it)->name + "'" << "]" << std::endl;
|
2012-12-09 12:20:46 +01:00
|
|
|
(*it)->printed = true;
|
|
|
|
}
|
|
|
|
unusedSectionInfo.reset();
|
|
|
|
}
|
2012-12-05 09:40:53 +01:00
|
|
|
void lazyPrint() {
|
|
|
|
if( testRunInfo )
|
|
|
|
lazyPrintRunInfo();
|
|
|
|
if( unusedGroupInfo )
|
|
|
|
lazyPrintGroupInfo();
|
|
|
|
if( unusedTestCaseInfo )
|
|
|
|
lazyPrintTestCaseInfo();
|
2012-12-09 12:20:46 +01:00
|
|
|
if( currentSectionInfo && !currentSectionInfo->printed )
|
2012-12-10 09:54:57 +01:00
|
|
|
lazyPrintSectionInfo();
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 12:20:46 +01:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) {
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
|
|
|
virtual void assertionEnded( Ptr<AssertionStats const> const& _assertionStats ) {
|
|
|
|
|
2012-12-09 12:20:46 +01:00
|
|
|
AssertionResult const& result = _assertionStats->assertionResult;
|
|
|
|
|
|
|
|
// Drop out if result was successful and we're not printing those
|
|
|
|
if( !m_config.includeSuccessfulResults() && result.isOk() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
lazyPrint();
|
|
|
|
|
2012-12-11 10:02:31 +01:00
|
|
|
printLineInfo( result.getSourceInfo() );
|
2012-12-09 12:20:46 +01:00
|
|
|
|
|
|
|
if( result.hasExpression() ) {
|
|
|
|
TextColour colour( TextColour::OriginalExpression );
|
|
|
|
stream << result.getExpression();
|
|
|
|
if( result.succeeded() ) {
|
|
|
|
TextColour successColour( TextColour::Success );
|
|
|
|
stream << " succeeded";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TextColour errorColour( TextColour::Error );
|
|
|
|
stream << " failed";
|
|
|
|
if( result.isOk() ) {
|
|
|
|
TextColour okAnywayColour( TextColour::Success );
|
|
|
|
stream << " - but was ok";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
{
|
|
|
|
TextColour colour( TextColour::Error );
|
|
|
|
if( result.hasExpression() )
|
|
|
|
stream << " with unexpected";
|
|
|
|
else
|
|
|
|
stream << "Unexpected";
|
|
|
|
stream << " exception with message: '" << result.getMessage() << "'";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::DidntThrowException:
|
|
|
|
{
|
|
|
|
TextColour colour( TextColour::Error );
|
|
|
|
if( result.hasExpression() )
|
|
|
|
stream << " because no exception was thrown where one was expected";
|
|
|
|
else
|
|
|
|
stream << "No exception thrown where one was expected";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
|
|
|
{
|
|
|
|
TextColour colour( TextColour::ReconstructedExpression );
|
|
|
|
streamVariableLengthText( "info", result.getMessage() );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
|
|
|
{
|
|
|
|
TextColour colour( TextColour::ReconstructedExpression );
|
|
|
|
streamVariableLengthText( "warning", result.getMessage() );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
{
|
|
|
|
TextColour colour( TextColour::Error );
|
|
|
|
stream << "failed with message: '" << result.getMessage() << "'";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::Unknown: // These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Ok:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::ExpressionFailed:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
if( !result.hasExpression() ) {
|
|
|
|
if( result.succeeded() ) {
|
|
|
|
TextColour colour( TextColour::Success );
|
|
|
|
stream << " succeeded";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TextColour colour( TextColour::Error );
|
|
|
|
stream << " failed";
|
|
|
|
if( result.isOk() ) {
|
|
|
|
TextColour okAnywayColour( TextColour::Success );
|
|
|
|
stream << " - but was ok";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( result.hasMessage() ) {
|
|
|
|
stream << "\n";
|
|
|
|
TextColour colour( TextColour::ReconstructedExpression );
|
|
|
|
streamVariableLengthText( "with message", result.getMessage() );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( result.hasExpandedExpression() ) {
|
|
|
|
stream << " for: ";
|
|
|
|
if( result.getExpandedExpression().size() > 40 ) {
|
|
|
|
stream << "\n";
|
|
|
|
if( result.getExpandedExpression().size() < 70 )
|
|
|
|
stream << "\t";
|
|
|
|
}
|
|
|
|
TextColour colour( TextColour::ReconstructedExpression );
|
|
|
|
stream << result.getExpandedExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
stream << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamVariableLengthText( std::string const& prefix, std::string const& text ) {
|
|
|
|
std::string trimmed = trim( text );
|
|
|
|
if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) {
|
|
|
|
stream << "[" << prefix << ": " << trimmed << "]";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
|
|
|
|
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-11 09:27:21 +01:00
|
|
|
void printAssertionCounts( std::string const& label, Counts const& counts ) {
|
|
|
|
if( counts.total() == 1 ) {
|
|
|
|
stream << "1 " << label << " - ";
|
|
|
|
if( counts.failed )
|
|
|
|
stream << "failed";
|
|
|
|
else
|
|
|
|
stream << "passed";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream << counts.total() << " " << label << "s ";
|
|
|
|
if( counts.passed ) {
|
|
|
|
if( counts.failed )
|
|
|
|
stream << "- " << counts.failed << " failed";
|
|
|
|
else if( counts.passed == 2 )
|
|
|
|
stream << "- both passed";
|
|
|
|
else
|
|
|
|
stream << "- all passed";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( counts.failed == 2 )
|
|
|
|
stream << "- both failed";
|
|
|
|
else
|
|
|
|
stream << "- all failed";
|
|
|
|
}
|
|
|
|
}
|
2012-12-06 09:44:51 +01:00
|
|
|
}
|
|
|
|
|
2012-12-11 09:27:21 +01:00
|
|
|
void printTotals( const Totals& totals ) {
|
2012-12-06 09:44:51 +01:00
|
|
|
if( totals.assertions.total() == 0 ) {
|
|
|
|
stream << "No tests ran";
|
|
|
|
}
|
|
|
|
else if( totals.assertions.failed ) {
|
|
|
|
TextColour colour( TextColour::ResultError );
|
2012-12-11 09:27:21 +01:00
|
|
|
printAssertionCounts( "test case", totals.testCases );
|
2012-12-06 09:44:51 +01:00
|
|
|
if( totals.testCases.failed > 0 ) {
|
|
|
|
stream << " (";
|
2012-12-11 09:27:21 +01:00
|
|
|
printAssertionCounts( "assertion", totals.assertions );
|
2012-12-06 09:44:51 +01:00
|
|
|
stream << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TextColour colour( TextColour::ResultSuccess );
|
2012-12-11 09:27:21 +01:00
|
|
|
stream << "All tests passed ("
|
2012-12-06 09:44:51 +01:00
|
|
|
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
|
|
|
|
<< pluralise( totals.testCases.passed, "test case" ) << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 09:40:53 +01:00
|
|
|
virtual void sectionEnded( Ptr<SectionStats const> const& _sectionStats ) {
|
2012-12-11 10:02:31 +01:00
|
|
|
resetLastPrintedLine();
|
2012-12-09 12:20:46 +01:00
|
|
|
if( _sectionStats->missingAssertions ) {
|
|
|
|
lazyPrint();
|
|
|
|
TextColour colour( TextColour::ResultError );
|
|
|
|
stream << "\nNo assertions in section, '" << _sectionStats->sectionInfo.name << "'\n" << std::endl;
|
|
|
|
}
|
|
|
|
if( currentSectionInfo && currentSectionInfo->printed ) {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Summary for section '" << _sectionStats->sectionInfo.name << "': ";
|
2012-12-09 12:20:46 +01:00
|
|
|
Counts const& assertions = _sectionStats->assertions;
|
|
|
|
if( assertions.failed ) {
|
|
|
|
TextColour colour( TextColour::ResultError );
|
|
|
|
printAssertionCounts( "assertion", assertions );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TextColour colour( TextColour::ResultSuccess );
|
|
|
|
stream << ( assertions.passed > 1 ? "All " : "" )
|
|
|
|
<< pluralise( assertions.passed, "assertion" ) << " passed" ;
|
|
|
|
}
|
2012-12-06 09:44:51 +01:00
|
|
|
stream << "]\n" << std::endl;
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
|
|
|
AccumulatingReporter::sectionEnded( _sectionStats );
|
|
|
|
}
|
|
|
|
virtual void testCaseEnded( Ptr<TestCaseStats const> const& _testCaseStats ) {
|
2012-12-11 10:02:31 +01:00
|
|
|
resetLastPrintedLine();
|
2012-12-09 12:20:46 +01:00
|
|
|
if( _testCaseStats->missingAssertions ) {
|
|
|
|
lazyPrint();
|
|
|
|
TextColour colour( TextColour::ResultError );
|
|
|
|
stream << "\nNo assertions in test case, '" << _testCaseStats->testInfo.name << "'\n" << std::endl;
|
|
|
|
}
|
2012-12-05 09:40:53 +01:00
|
|
|
if( !unusedTestCaseInfo ) {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Summary for test case '" << _testCaseStats->testInfo.name << "': ";
|
2012-12-06 09:44:51 +01:00
|
|
|
printTotals( _testCaseStats->totals );
|
|
|
|
stream << "]\n" << std::endl;
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
|
|
|
AccumulatingReporter::testCaseEnded( _testCaseStats );
|
|
|
|
}
|
2012-12-06 09:44:51 +01:00
|
|
|
virtual void testGroupEnded( Ptr<TestGroupStats const> const& _testGroupStats ) {
|
2012-12-09 12:20:46 +01:00
|
|
|
if( !unusedGroupInfo ) {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Summary for group '" << _testGroupStats->groupInfo.name << "': ";
|
2012-12-09 12:20:46 +01:00
|
|
|
printTotals( _testGroupStats->totals );
|
|
|
|
stream << "]\n" << std::endl;
|
|
|
|
}
|
2012-12-06 09:44:51 +01:00
|
|
|
AccumulatingReporter::testGroupEnded( _testGroupStats );
|
|
|
|
}
|
|
|
|
virtual void testRunEnded( Ptr<TestRunStats const> const& _testRunStats ) {
|
2012-12-09 12:20:46 +01:00
|
|
|
if( !unusedTestCaseInfo ) {
|
2012-12-10 09:54:57 +01:00
|
|
|
stream << "[Summary for '" << _testRunStats->runInfo.name << "': ";
|
2012-12-09 12:20:46 +01:00
|
|
|
printTotals( _testRunStats->totals );
|
|
|
|
stream << "]\n" << std::endl;
|
|
|
|
}
|
2012-12-06 09:44:51 +01:00
|
|
|
AccumulatingReporter::testRunEnded( _testRunStats );
|
|
|
|
}
|
|
|
|
|
2012-12-11 10:02:31 +01:00
|
|
|
void printLineInfo( SourceLineInfo const& lineInfo ) {
|
|
|
|
if( !lineInfo.empty() ) {
|
|
|
|
if( m_lastPrintedLine.empty() ||
|
|
|
|
m_lastPrintedLine.file != lineInfo.file ||
|
|
|
|
abs( static_cast<int>( m_lastPrintedLine.line ) - static_cast<int>( lineInfo.line ) ) > 20 ) {
|
|
|
|
TextColour colour( TextColour::FileName );
|
|
|
|
stream << lineInfo << "\n";
|
|
|
|
m_lastPrintedLine = lineInfo;
|
|
|
|
}
|
|
|
|
else if( lineInfo.line != m_lastPrintedLine.line ) {
|
|
|
|
TextColour colour( TextColour::FileName );
|
|
|
|
stream << "line " << lineInfo.line << ":\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void resetLastPrintedLine() {
|
|
|
|
m_lastPrintedLine = SourceLineInfo();
|
|
|
|
}
|
|
|
|
SourceLineInfo m_lastPrintedLine;
|
|
|
|
|
2012-12-05 09:40:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "console", ConsoleReporter )
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_CONSOLE_HPP_INCLUDED
|