Revert "Fixed issue #101, missing space in end of section message"

This reverts commit 1ad971ea2b.
This commit is contained in:
Martin Moene 2012-07-22 09:14:19 +02:00
parent 14f1c094f4
commit a0c496fefc
1 changed files with 333 additions and 333 deletions

View File

@ -1,333 +1,333 @@
/* /*
* Created by Phil on 28/10/2010. * Created by Phil on 28/10/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/ */
#ifndef TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED #define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
#include "../internal/catch_capture.hpp" #include "../internal/catch_capture.hpp"
#include "../internal/catch_interfaces_reporter.h" #include "../internal/catch_interfaces_reporter.h"
#include "../internal/catch_reporter_registrars.hpp" #include "../internal/catch_reporter_registrars.hpp"
#include "../internal/catch_console_colour.hpp" #include "../internal/catch_console_colour.hpp"
namespace Catch { namespace Catch {
struct pluralise { struct pluralise {
pluralise( std::size_t count, const std::string& label ) pluralise( std::size_t count, const std::string& label )
: m_count( count ), : m_count( count ),
m_label( label ) m_label( label )
{} {}
friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) { friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) {
os << pluraliser.m_count << " " << pluraliser.m_label; os << pluraliser.m_count << " " << pluraliser.m_label;
if( pluraliser.m_count != 1 ) if( pluraliser.m_count != 1 )
os << "s"; os << "s";
return os; return os;
} }
std::size_t m_count; std::size_t m_count;
std::string m_label; std::string m_label;
}; };
class BasicReporter : public SharedImpl<IReporter> { class BasicReporter : public SharedImpl<IReporter> {
struct SpanInfo { struct SpanInfo {
SpanInfo() SpanInfo()
: emitted( false ) : emitted( false )
{} {}
SpanInfo( const std::string& spanName ) SpanInfo( const std::string& spanName )
: name( spanName ), : name( spanName ),
emitted( false ) emitted( false )
{} {}
SpanInfo( const SpanInfo& other ) SpanInfo( const SpanInfo& other )
: name( other.name ), : name( other.name ),
emitted( other.emitted ) emitted( other.emitted )
{} {}
std::string name; std::string name;
bool emitted; bool emitted;
}; };
public: public:
BasicReporter( const IReporterConfig& config ) BasicReporter( const IReporterConfig& config )
: m_config( config ), : m_config( config ),
m_firstSectionInTestCase( true ), m_firstSectionInTestCase( true ),
m_aborted( false ) m_aborted( false )
{} {}
static std::string getDescription() { static std::string getDescription() {
return "Reports test results as lines of text"; return "Reports test results as lines of text";
} }
private: private:
void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) { void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) {
if( counts.passed ) if( counts.passed )
m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed"; m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed";
else else
m_config.stream() << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed"; m_config.stream() << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed";
} }
void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) { void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) {
if( totals.assertions.total() == 0 ) { if( totals.assertions.total() == 0 ) {
m_config.stream() << "No tests ran"; m_config.stream() << "No tests ran";
} }
else if( totals.assertions.failed ) { else if( totals.assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
ReportCounts( "test case", totals.testCases, allPrefix ); ReportCounts( "test case", totals.testCases, allPrefix );
if( totals.testCases.failed > 0 ) { if( totals.testCases.failed > 0 ) {
m_config.stream() << " ("; m_config.stream() << " (";
ReportCounts( "assertion", totals.assertions, allPrefix ); ReportCounts( "assertion", totals.assertions, allPrefix );
m_config.stream() << ")"; m_config.stream() << ")";
} }
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << allPrefix << "tests passed (" m_config.stream() << allPrefix << "tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in " << pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")"; << pluralise( totals.testCases.passed, "test case" ) << ")";
} }
} }
private: // IReporter private: // IReporter
virtual bool shouldRedirectStdout() const { virtual bool shouldRedirectStdout() const {
return false; return false;
} }
virtual void StartTesting() { virtual void StartTesting() {
m_testingSpan = SpanInfo(); m_testingSpan = SpanInfo();
} }
virtual void Aborted() { virtual void Aborted() {
m_aborted = true; m_aborted = true;
} }
virtual void EndTesting( const Totals& totals ) { virtual void EndTesting( const Totals& totals ) {
// Output the overall test results even if "Started Testing" was not emitted // Output the overall test results even if "Started Testing" was not emitted
if( m_aborted ) { if( m_aborted ) {
m_config.stream() << "\n[Testing aborted. "; m_config.stream() << "\n[Testing aborted. ";
ReportCounts( totals, "The first " ); ReportCounts( totals, "The first " );
} }
else { else {
m_config.stream() << "\n[Testing completed. "; m_config.stream() << "\n[Testing completed. ";
ReportCounts( totals ); ReportCounts( totals );
} }
m_config.stream() << "]\n" << std::endl; m_config.stream() << "]\n" << std::endl;
} }
virtual void StartGroup( const std::string& groupName ) { virtual void StartGroup( const std::string& groupName ) {
m_groupSpan = groupName; m_groupSpan = groupName;
} }
virtual void EndGroup( const std::string& groupName, const Totals& totals ) { virtual void EndGroup( const std::string& groupName, const Totals& totals ) {
if( m_groupSpan.emitted && !groupName.empty() ) { if( m_groupSpan.emitted && !groupName.empty() ) {
m_config.stream() << "[End of group: '" << groupName << "'. "; m_config.stream() << "[End of group: '" << groupName << "'. ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]\n" << std::endl; m_config.stream() << "]\n" << std::endl;
m_groupSpan = SpanInfo(); m_groupSpan = SpanInfo();
} }
} }
virtual void StartTestCase( const TestCaseInfo& testInfo ) { virtual void StartTestCase( const TestCaseInfo& testInfo ) {
m_testSpan = testInfo.getName(); m_testSpan = testInfo.getName();
} }
virtual void StartSection( const std::string& sectionName, const std::string& ) { virtual void StartSection( const std::string& sectionName, const std::string& ) {
m_sectionSpans.push_back( SpanInfo( sectionName ) ); m_sectionSpans.push_back( SpanInfo( sectionName ) );
} }
virtual void EndSection( const std::string& sectionName, const Counts& assertions ) { virtual void EndSection( const std::string& sectionName, const Counts& assertions ) {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( sectionSpan.emitted && !sectionSpan.name.empty() ) { if( sectionSpan.emitted && !sectionSpan.name.empty() ) {
m_config.stream() << "[End of section: '" << sectionName << "' "; m_config.stream() << "[End of section: '" << sectionName << "' ";
if( assertions.failed ) { if( assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
ReportCounts( "assertion", assertions); ReportCounts( "assertion", assertions);
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << ( assertions.passed > 1 ? "All " : "" ) m_config.stream() << ( assertions.passed > 1 ? "All " : "" )
<< pluralise( assertions.passed, "assertion" ) << " passed" ; << pluralise( assertions.passed, "assertion" ) << "passed" ;
} }
m_config.stream() << "]\n" << std::endl; m_config.stream() << "]\n" << std::endl;
} }
m_sectionSpans.pop_back(); m_sectionSpans.pop_back();
} }
virtual void Result( const ResultInfo& resultInfo ) { virtual void Result( const ResultInfo& resultInfo ) {
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok ) if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok )
return; return;
StartSpansLazily(); StartSpansLazily();
if( !resultInfo.getFilename().empty() ) { if( !resultInfo.getFilename().empty() ) {
TextColour colour( TextColour::FileName ); TextColour colour( TextColour::FileName );
m_config.stream() << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); m_config.stream() << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() );
} }
if( resultInfo.hasExpression() ) { if( resultInfo.hasExpression() ) {
TextColour colour( TextColour::OriginalExpression ); TextColour colour( TextColour::OriginalExpression );
m_config.stream() << resultInfo.getExpression(); m_config.stream() << resultInfo.getExpression();
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour successColour( TextColour::Success ); TextColour successColour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream() << " succeeded";
} }
else { else {
TextColour errorColour( TextColour::Error ); TextColour errorColour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream() << " failed";
} }
} }
switch( resultInfo.getResultType() ) { switch( resultInfo.getResultType() ) {
case ResultWas::ThrewException: case ResultWas::ThrewException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " with unexpected"; m_config.stream() << " with unexpected";
else else
m_config.stream() << "Unexpected"; m_config.stream() << "Unexpected";
m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'"; m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " because no exception was thrown where one was expected"; m_config.stream() << " because no exception was thrown where one was expected";
else else
m_config.stream() << "No exception thrown where one was expected"; m_config.stream() << "No exception thrown where one was expected";
} }
break; break;
case ResultWas::Info: case ResultWas::Info:
streamVariableLengthText( "info", resultInfo.getMessage() ); streamVariableLengthText( "info", resultInfo.getMessage() );
break; break;
case ResultWas::Warning: case ResultWas::Warning:
m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'"; m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'";
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'"; m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::Ok: case ResultWas::Ok:
case ResultWas::FailureBit: case ResultWas::FailureBit:
case ResultWas::ExpressionFailed: case ResultWas::ExpressionFailed:
case ResultWas::Exception: case ResultWas::Exception:
default: default:
if( !resultInfo.hasExpression() ) { if( !resultInfo.hasExpression() ) {
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour colour( TextColour::Success ); TextColour colour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream() << " succeeded";
} }
else { else {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream() << " failed";
} }
} }
break; break;
} }
if( resultInfo.hasExpandedExpression() ) { if( resultInfo.hasExpandedExpression() ) {
m_config.stream() << " for: "; m_config.stream() << " for: ";
TextColour colour( TextColour::ReconstructedExpression ); TextColour colour( TextColour::ReconstructedExpression );
m_config.stream() << resultInfo.getExpandedExpression(); m_config.stream() << resultInfo.getExpandedExpression();
} }
m_config.stream() << std::endl; m_config.stream() << std::endl;
} }
virtual void EndTestCase( const TestCaseInfo& testInfo, virtual void EndTestCase( const TestCaseInfo& testInfo,
const Totals& totals, const Totals& totals,
const std::string& stdOut, const std::string& stdOut,
const std::string& stdErr ) { const std::string& stdErr ) {
if( !stdOut.empty() ) { if( !stdOut.empty() ) {
StartSpansLazily(); StartSpansLazily();
streamVariableLengthText( "stdout", stdOut ); streamVariableLengthText( "stdout", stdOut );
} }
if( !stdErr.empty() ) { if( !stdErr.empty() ) {
StartSpansLazily(); StartSpansLazily();
streamVariableLengthText( "stderr", stdErr ); streamVariableLengthText( "stderr", stdErr );
} }
if( m_testSpan.emitted ) { if( m_testSpan.emitted ) {
m_config.stream() << "[Finished: '" << testInfo.getName() << "' "; m_config.stream() << "[Finished: '" << testInfo.getName() << "' ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]" << std::endl; m_config.stream() << "]" << std::endl;
} }
} }
private: // helpers private: // helpers
void StartSpansLazily() { void StartSpansLazily() {
if( !m_testingSpan.emitted ) { if( !m_testingSpan.emitted ) {
if( m_config.getName().empty() ) if( m_config.getName().empty() )
m_config.stream() << "[Started testing]" << std::endl; m_config.stream() << "[Started testing]" << std::endl;
else else
m_config.stream() << "[Started testing: " << m_config.getName() << "]" << std::endl; m_config.stream() << "[Started testing: " << m_config.getName() << "]" << std::endl;
m_testingSpan.emitted = true; m_testingSpan.emitted = true;
} }
if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) { if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) {
m_config.stream() << "[Started group: '" << m_groupSpan.name << "']" << std::endl; m_config.stream() << "[Started group: '" << m_groupSpan.name << "']" << std::endl;
m_groupSpan.emitted = true; m_groupSpan.emitted = true;
} }
if( !m_testSpan.emitted ) { if( !m_testSpan.emitted ) {
m_config.stream() << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl; m_config.stream() << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl;
m_testSpan.emitted = true; m_testSpan.emitted = true;
} }
if( !m_sectionSpans.empty() ) { if( !m_sectionSpans.empty() ) {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( !sectionSpan.emitted && !sectionSpan.name.empty() ) { if( !sectionSpan.emitted && !sectionSpan.name.empty() ) {
if( m_firstSectionInTestCase ) { if( m_firstSectionInTestCase ) {
m_config.stream() << "\n"; m_config.stream() << "\n";
m_firstSectionInTestCase = false; m_firstSectionInTestCase = false;
} }
std::vector<SpanInfo>::iterator it = m_sectionSpans.begin(); std::vector<SpanInfo>::iterator it = m_sectionSpans.begin();
std::vector<SpanInfo>::iterator itEnd = m_sectionSpans.end(); std::vector<SpanInfo>::iterator itEnd = m_sectionSpans.end();
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
SpanInfo& prevSpan = *it; SpanInfo& prevSpan = *it;
if( !prevSpan.emitted && !prevSpan.name.empty() ) { if( !prevSpan.emitted && !prevSpan.name.empty() ) {
m_config.stream() << "[Started section: '" << prevSpan.name << "']" << std::endl; m_config.stream() << "[Started section: '" << prevSpan.name << "']" << std::endl;
prevSpan.emitted = true; prevSpan.emitted = true;
} }
} }
} }
} }
} }
void streamVariableLengthText( const std::string& prefix, const std::string& text ) { void streamVariableLengthText( const std::string& prefix, const std::string& text ) {
std::string trimmed = trim( text ); std::string trimmed = trim( text );
if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) { if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) {
m_config.stream() << "[" << prefix << ": " << trimmed << "]\n"; m_config.stream() << "[" << prefix << ": " << trimmed << "]\n";
} }
else { else {
m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n"; << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
} }
} }
private: private:
const IReporterConfig& m_config; const IReporterConfig& m_config;
bool m_firstSectionInTestCase; bool m_firstSectionInTestCase;
SpanInfo m_testingSpan; SpanInfo m_testingSpan;
SpanInfo m_groupSpan; SpanInfo m_groupSpan;
SpanInfo m_testSpan; SpanInfo m_testSpan;
std::vector<SpanInfo> m_sectionSpans; std::vector<SpanInfo> m_sectionSpans;
bool m_aborted; bool m_aborted;
}; };
} // end namespace Catch } // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED