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 {
|
|
|
|
|
2013-01-03 10:04:46 +01:00
|
|
|
struct ConsoleReporter : StreamingReporterBase {
|
2012-12-05 09:40:53 +01:00
|
|
|
ConsoleReporter( ReporterConfig const& _config )
|
2013-01-03 10:04:46 +01:00
|
|
|
: StreamingReporterBase( _config ),
|
2012-12-13 13:46:47 +01:00
|
|
|
m_atLeastOneTestCasePrinted( false )
|
2012-12-05 09:40:53 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
2013-01-13 22:51:44 +01:00
|
|
|
|
2012-12-09 12:20:46 +01:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) {
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
|
|
|
|
2013-01-13 22:51:44 +01:00
|
|
|
virtual void assertionEnded( AssertionStats const& _assertionStats ) {
|
2013-01-03 10:04:46 +01:00
|
|
|
AssertionResult const& result = _assertionStats.assertionResult;
|
2012-12-09 12:20:46 +01:00
|
|
|
|
|
|
|
// Drop out if result was successful and we're not printing those
|
|
|
|
if( !m_config.includeSuccessfulResults() && result.isOk() )
|
|
|
|
return;
|
2013-01-13 22:51:44 +01:00
|
|
|
|
2012-12-09 12:20:46 +01:00
|
|
|
lazyPrint();
|
|
|
|
|
2013-01-13 22:51:44 +01:00
|
|
|
{
|
|
|
|
TextColour colour( TextColour::FileName );
|
|
|
|
stream << result.getSourceInfo() << ":\n";
|
2012-12-09 12:20:46 +01:00
|
|
|
}
|
2013-01-13 22:51:44 +01:00
|
|
|
|
2013-01-14 20:28:28 +01:00
|
|
|
ResultComponents components( result );
|
2013-01-14 09:34:50 +01:00
|
|
|
bool endsWithNewLine = false;
|
2013-01-13 22:51:44 +01:00
|
|
|
if( _assertionStats.totals.assertions.total() > 0 ) {
|
2013-01-14 20:28:28 +01:00
|
|
|
printOriginalExpression( result );
|
|
|
|
printResultType( components );
|
|
|
|
endsWithNewLine = printReconstructedExpression( result );
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-01-14 20:28:28 +01:00
|
|
|
endsWithNewLine |= printMessage( components );
|
2013-01-14 09:34:50 +01:00
|
|
|
if( !endsWithNewLine )
|
|
|
|
stream << "\n";
|
2013-01-13 22:51:44 +01:00
|
|
|
stream << std::endl;
|
|
|
|
}
|
|
|
|
|
2013-01-14 20:28:28 +01:00
|
|
|
struct ResultComponents {
|
|
|
|
ResultComponents( AssertionResult const& _result )
|
2013-01-15 09:09:20 +01:00
|
|
|
: colour( TextColour::None ),
|
|
|
|
message( _result.getMessage() )
|
2013-01-14 20:28:28 +01:00
|
|
|
{
|
|
|
|
switch( _result.getResultType() ) {
|
|
|
|
case ResultWas::Ok:
|
|
|
|
colour = TextColour::Success;
|
|
|
|
passOrFail = "passed";
|
2013-01-15 09:09:20 +01:00
|
|
|
if( _result.hasMessage() )
|
2013-01-14 20:28:28 +01:00
|
|
|
messageLabel = "with message";
|
|
|
|
break;
|
|
|
|
case ResultWas::ExpressionFailed:
|
|
|
|
if( _result.isOk() ) {
|
|
|
|
colour = TextColour::Success;
|
|
|
|
passOrFail = "failed - but was ok";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
colour = TextColour::Error;
|
|
|
|
passOrFail = "failed";
|
|
|
|
}
|
|
|
|
if( _result.hasMessage() ){
|
|
|
|
messageLabel = "with message";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
colour = TextColour::Error;
|
|
|
|
passOrFail = "failed";
|
|
|
|
messageLabel = "due to unexpected exception with message";
|
|
|
|
break;
|
|
|
|
case ResultWas::DidntThrowException:
|
|
|
|
colour = TextColour::Error;
|
|
|
|
passOrFail = "failed";
|
|
|
|
messageLabel = "because no exception was thrown where one was expected";
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
|
|
|
messageLabel = "info";
|
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
|
|
|
messageLabel = "warning";
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
passOrFail = "failed";
|
|
|
|
colour = TextColour::Error;
|
|
|
|
messageLabel = "explicitly with message";
|
|
|
|
break;
|
|
|
|
case ResultWas::Exception:
|
|
|
|
passOrFail = "failed";
|
|
|
|
colour = TextColour::Error;
|
2013-01-15 09:09:20 +01:00
|
|
|
if( _result.hasMessage() )
|
2013-01-14 20:28:28 +01:00
|
|
|
messageLabel = "with message";
|
|
|
|
break;
|
2013-01-15 09:09:20 +01:00
|
|
|
|
|
|
|
// These cases are here to prevent compiler warnings
|
|
|
|
case ResultWas::Unknown:
|
2013-01-14 20:28:28 +01:00
|
|
|
case ResultWas::FailureBit:
|
|
|
|
passOrFail = "** internal error **";
|
|
|
|
colour = TextColour::Error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TextColour::Colours colour;
|
|
|
|
std::string passOrFail;
|
|
|
|
std::string messageLabel;
|
|
|
|
std::string message;
|
|
|
|
};
|
|
|
|
|
|
|
|
void printResultType( ResultComponents const& _components ) {
|
|
|
|
if( !_components.passOrFail.empty() ) {
|
|
|
|
TextColour colour( _components.colour );
|
|
|
|
stream << _components.passOrFail << " ";
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 09:34:50 +01:00
|
|
|
bool printOriginalExpression( AssertionResult const& _result ) {
|
|
|
|
if( _result.hasExpression() ) {
|
|
|
|
TextColour colour( TextColour::OriginalExpression );
|
|
|
|
stream << " ";
|
|
|
|
if( !_result.getTestMacroName().empty() )
|
|
|
|
stream << _result.getTestMacroName() << "( ";
|
|
|
|
stream << _result.getExpression();
|
|
|
|
if( !_result.getTestMacroName().empty() )
|
|
|
|
stream << " )";
|
|
|
|
stream << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool printReconstructedExpression( AssertionResult const& _result ) {
|
|
|
|
if( _result.hasExpandedExpression() ) {
|
2013-01-14 19:36:25 +01:00
|
|
|
stream << "with expansion:\n";
|
2013-01-14 09:34:50 +01:00
|
|
|
TextColour colour( TextColour::ReconstructedExpression );
|
|
|
|
stream << wrapLongStrings( _result.getExpandedExpression() ) << "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-14 20:28:28 +01:00
|
|
|
bool printMessage( ResultComponents const& _components ) {
|
2013-01-14 09:34:50 +01:00
|
|
|
bool endsWithNewLine = false;
|
2013-01-14 20:28:28 +01:00
|
|
|
if( !_components.messageLabel.empty() ) {
|
|
|
|
stream << _components.messageLabel << ":" << "\n";
|
2013-01-14 09:34:50 +01:00
|
|
|
endsWithNewLine = true;
|
|
|
|
}
|
2013-01-14 20:28:28 +01:00
|
|
|
if( !_components.message.empty() ) {
|
|
|
|
stream << wrapLongStrings( _components.message ) << "\n";
|
2013-01-14 09:34:50 +01:00
|
|
|
endsWithNewLine = true;
|
|
|
|
}
|
|
|
|
return endsWithNewLine;
|
2013-01-13 22:51:44 +01:00
|
|
|
}
|
2013-01-14 19:51:49 +01:00
|
|
|
|
2013-01-03 10:04:46 +01:00
|
|
|
virtual void sectionEnded( SectionStats const& _sectionStats ) {
|
|
|
|
if( _sectionStats.missingAssertions ) {
|
2012-12-09 12:20:46 +01:00
|
|
|
lazyPrint();
|
|
|
|
TextColour colour( TextColour::ResultError );
|
2013-01-03 10:04:46 +01:00
|
|
|
stream << "\nNo assertions in section, '" << _sectionStats.sectionInfo.name << "'\n" << std::endl;
|
2012-12-09 12:20:46 +01:00
|
|
|
}
|
2013-01-03 10:04:46 +01:00
|
|
|
StreamingReporterBase::sectionEnded( _sectionStats );
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
2013-01-13 22:51:44 +01:00
|
|
|
|
2013-01-03 10:04:46 +01:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& _testCaseStats ) {
|
2013-01-13 22:51:44 +01:00
|
|
|
|
2013-01-03 10:04:46 +01:00
|
|
|
if( _testCaseStats.missingAssertions ) {
|
2012-12-09 12:20:46 +01:00
|
|
|
lazyPrint();
|
|
|
|
TextColour colour( TextColour::ResultError );
|
2013-01-03 10:04:46 +01:00
|
|
|
stream << "\nNo assertions in test case, '" << _testCaseStats.testInfo.name << "'\n" << std::endl;
|
2012-12-09 12:20:46 +01:00
|
|
|
}
|
2013-01-03 10:04:46 +01:00
|
|
|
StreamingReporterBase::testCaseEnded( _testCaseStats );
|
2012-12-05 09:40:53 +01:00
|
|
|
}
|
2013-01-03 10:04:46 +01:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& _testGroupStats ) {
|
2012-12-09 12:20:46 +01:00
|
|
|
if( !unusedGroupInfo ) {
|
2013-01-13 22:51:44 +01:00
|
|
|
printSummaryDivider();
|
2013-01-03 10:04:46 +01:00
|
|
|
stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n";
|
|
|
|
printTotals( _testGroupStats.totals );
|
2012-12-13 13:46:47 +01:00
|
|
|
stream << "\n" << std::endl;
|
2012-12-09 12:20:46 +01:00
|
|
|
}
|
2013-01-03 10:04:46 +01:00
|
|
|
StreamingReporterBase::testGroupEnded( _testGroupStats );
|
2012-12-06 09:44:51 +01:00
|
|
|
}
|
2013-01-03 10:04:46 +01:00
|
|
|
virtual void testRunEnded( TestRunStats const& _testRunStats ) {
|
2012-12-13 13:46:47 +01:00
|
|
|
if( m_atLeastOneTestCasePrinted )
|
|
|
|
printTotalsDivider();
|
2013-01-03 10:04:46 +01:00
|
|
|
printTotals( _testRunStats.totals );
|
2012-12-13 13:46:47 +01:00
|
|
|
stream << "\n" << std::endl;
|
2013-01-03 10:04:46 +01:00
|
|
|
StreamingReporterBase::testRunEnded( _testRunStats );
|
2012-12-06 09:44:51 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 13:46:47 +01:00
|
|
|
private:
|
2013-01-14 19:51:49 +01:00
|
|
|
std::string wrapLongStrings( std::string const& _string ) {
|
|
|
|
return Catch::wrapLongStrings( _string, 70, 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void lazyPrint() {
|
|
|
|
|
|
|
|
if( testRunInfo )
|
|
|
|
lazyPrintRunInfo();
|
|
|
|
if( unusedGroupInfo )
|
|
|
|
lazyPrintGroupInfo();
|
|
|
|
if( unusedTestCaseInfo )
|
|
|
|
lazyPrintTestCaseInfo();
|
2013-01-16 10:30:41 +01:00
|
|
|
if( unusedSectionInfo)
|
2013-01-14 19:51:49 +01:00
|
|
|
lazyPrintSectionInfo();
|
|
|
|
|
|
|
|
m_atLeastOneTestCasePrinted = true;
|
|
|
|
}
|
|
|
|
void lazyPrintRunInfo() {
|
|
|
|
stream << "\n" << testRunInfo->name
|
|
|
|
<< " is a CATCH v" << libraryVersion.majorVersion << "."
|
|
|
|
<< libraryVersion.minorVersion << " b"
|
|
|
|
<< libraryVersion.buildNumber;
|
|
|
|
if( libraryVersion.branchName != "master" )
|
|
|
|
stream << " (" << libraryVersion.branchName << ")";
|
|
|
|
stream << " host application.\n"
|
|
|
|
<< "Run with -? for options\n\n";
|
|
|
|
|
|
|
|
testRunInfo.reset();
|
|
|
|
}
|
|
|
|
void lazyPrintGroupInfo() {
|
|
|
|
if( !unusedGroupInfo->name.empty() && unusedGroupInfo->groupsCounts > 1 ) {
|
|
|
|
printHeader( "Group", unusedGroupInfo->name );
|
|
|
|
unusedGroupInfo.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void lazyPrintTestCaseInfo() {
|
2013-01-15 09:43:27 +01:00
|
|
|
if( !currentSectionInfo ) {
|
|
|
|
stream << getDashes() << "\n"
|
|
|
|
<< "Test case" << ": '" << unusedTestCaseInfo->name << "'\n";
|
2013-01-16 10:30:41 +01:00
|
|
|
stream << getDashes() << "\n" << std::endl;
|
2013-01-15 09:43:27 +01:00
|
|
|
unusedTestCaseInfo.reset();
|
|
|
|
}
|
2013-01-14 19:51:49 +01:00
|
|
|
}
|
|
|
|
void lazyPrintSectionInfo() {
|
2013-01-15 09:43:27 +01:00
|
|
|
|
2013-01-14 19:51:49 +01:00
|
|
|
std::vector<ThreadedSectionInfo*> sections;
|
|
|
|
for( ThreadedSectionInfo* section = unusedSectionInfo.get();
|
2013-01-15 09:43:27 +01:00
|
|
|
section;
|
2013-01-14 19:51:49 +01:00
|
|
|
section = section->parent.get() )
|
|
|
|
sections.push_back( section );
|
|
|
|
|
2013-01-15 09:43:27 +01:00
|
|
|
// Sections
|
|
|
|
if( !sections.empty() ) {
|
|
|
|
stream << getDashes() << "\n"
|
|
|
|
<< "Test case" << ": '" << unusedTestCaseInfo->name << "'\n"
|
|
|
|
<< getDashes() << "\n";
|
|
|
|
|
|
|
|
std::string firstInset;
|
|
|
|
std::string inset;
|
|
|
|
if( sections.size() > 1 ) {
|
|
|
|
firstInset = "Sections: ";
|
|
|
|
inset = " ";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
firstInset = "Section: ";
|
|
|
|
inset = " ";
|
|
|
|
}
|
|
|
|
typedef std::vector<ThreadedSectionInfo*>::const_reverse_iterator It;
|
|
|
|
for( It it = sections.rbegin(), itEnd = sections.rend(); it != itEnd; ++it ) {
|
|
|
|
if( it == sections.rbegin() )
|
|
|
|
stream << firstInset;
|
|
|
|
else
|
|
|
|
stream << inset;
|
|
|
|
stream << (*it)->name << "\n";
|
|
|
|
}
|
2013-01-16 10:30:41 +01:00
|
|
|
stream << getDashes() << "\n" << std::endl;
|
2013-01-15 09:43:27 +01:00
|
|
|
unusedSectionInfo.reset();
|
2013-01-14 19:51:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printHeader( std::string const& _type, std::string const& _name ) {
|
|
|
|
stream << getDashes() << "\n"
|
|
|
|
<< _type << ": '" << _name << "'\n"
|
|
|
|
<< getDashes() << std::endl;
|
|
|
|
}
|
|
|
|
void printTotals( const Totals& totals ) {
|
|
|
|
if( totals.assertions.total() == 0 ) {
|
|
|
|
stream << "No tests ran";
|
|
|
|
}
|
|
|
|
else if( totals.assertions.failed ) {
|
|
|
|
TextColour colour( TextColour::ResultError );
|
|
|
|
printCounts( "test case", totals.testCases );
|
|
|
|
if( totals.testCases.failed > 0 ) {
|
|
|
|
stream << " (";
|
|
|
|
printCounts( "assertion", totals.assertions );
|
|
|
|
stream << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TextColour colour( TextColour::ResultSuccess );
|
|
|
|
stream << "All tests passed ("
|
|
|
|
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
|
|
|
|
<< pluralise( totals.testCases.passed, "test case" ) << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void printCounts( 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-13 13:46:47 +01:00
|
|
|
void printTotalsDivider() {
|
2013-01-13 22:51:44 +01:00
|
|
|
stream << getDoubleDashes() << "\n";
|
2012-12-13 13:46:47 +01:00
|
|
|
}
|
2013-01-13 22:51:44 +01:00
|
|
|
void printSummaryDivider() {
|
|
|
|
stream << getDashes() << "\n";
|
2012-12-11 10:02:31 +01:00
|
|
|
}
|
2013-01-14 19:51:49 +01:00
|
|
|
static std::string const& getDashes() {
|
|
|
|
static const std::string dashes
|
|
|
|
= "----------------------------------------------------------------";
|
|
|
|
return dashes;
|
|
|
|
}
|
|
|
|
static std::string const& getDoubleDashes() {
|
|
|
|
static const std::string doubleDashes
|
|
|
|
= "================================================================";
|
|
|
|
return doubleDashes;
|
|
|
|
}
|
2013-01-13 22:51:44 +01:00
|
|
|
|
|
|
|
private:
|
2012-12-13 13:46:47 +01:00
|
|
|
bool m_atLeastOneTestCasePrinted;
|
2012-12-11 10:02:31 +01:00
|
|
|
|
2012-12-05 09:40:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "console", ConsoleReporter )
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_CONSOLE_HPP_INCLUDED
|