Added colorised console output for Windows

Also tweaks the output again
This commit is contained in:
Phil Nash 2012-02-25 20:36:22 +00:00
parent f7299fc87b
commit 20df8c5da1
5 changed files with 239 additions and 45 deletions

View File

@ -0,0 +1,50 @@
/*
* catch_console_colour.hpp
* Catch
*
* Created by Phil on 25/2/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_CONSOLE_COLOUR_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED
#include "catch_common.h"
namespace Catch
{
struct ConsoleColourImpl;
class TextColour : NonCopyable
{
public:
enum Colours
{
None,
FileName,
ResultError,
ResultSuccess,
Error,
Success,
OriginalExpression,
ReconstructedExpression
};
TextColour( Colours colour = None );
void set( Colours colour );
~TextColour();
private:
ConsoleColourImpl* m_impl;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED

View File

@ -0,0 +1,102 @@
/*
* catch_console_colour_impl.hpp
* Catch
*
* Created by Phil on 25/2/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_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED
#include "catch_console_colour.hpp"
#ifdef CATCH_PLATFORM_WINDOWS
#include "windows.h"
namespace Catch
{
namespace
{
WORD mapConsoleColour( TextColour::Colours colour )
{
switch( colour )
{
case TextColour::FileName:
return FOREGROUND_INTENSITY; // greyed out
case TextColour::ResultError:
return FOREGROUND_RED | FOREGROUND_INTENSITY; // bright red
case TextColour::ResultSuccess:
return FOREGROUND_GREEN | FOREGROUND_INTENSITY; // bright green
case TextColour::Error:
return FOREGROUND_RED; // dark red
case TextColour::Success:
return FOREGROUND_GREEN; // dark green
case TextColour::OriginalExpression:
return FOREGROUND_BLUE | FOREGROUND_GREEN; // turquoise
case TextColour::ReconstructedExpression:
return FOREGROUND_RED | FOREGROUND_GREEN; // greeny-yellow
default: return 0;
}
}
}
struct ConsoleColourImpl
{
ConsoleColourImpl()
: hStdout( GetStdHandle(STD_OUTPUT_HANDLE) ),
wOldColorAttrs( 0 )
{
GetConsoleScreenBufferInfo( hStdout, &csbiInfo );
wOldColorAttrs = csbiInfo.wAttributes;
}
~ConsoleColourImpl()
{
SetConsoleTextAttribute( hStdout, wOldColorAttrs );
}
void set( TextColour::Colours colour )
{
WORD consoleColour = mapConsoleColour( colour );
if( consoleColour > 0 )
SetConsoleTextAttribute( hStdout, consoleColour );
}
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
WORD wOldColorAttrs;
};
TextColour::TextColour( Colours colour )
: m_impl( new ConsoleColourImpl() )
{
if( colour )
m_impl->set( colour );
}
TextColour::~TextColour()
{
delete m_impl;
}
void TextColour::set( Colours colour )
{
m_impl->set( colour );
}
} // end namespace Catch
#else
namespace Catch
{
TextColour::TextColour( Colours ){}
TextColour::~TextColour(){}
void TextColour::set( Colours ){}
} // end namespace Catch
#endif
#endif // TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED

View File

@ -15,6 +15,7 @@
#include "catch_exception_translator_registry.hpp" #include "catch_exception_translator_registry.hpp"
#include "catch_runner_impl.hpp" #include "catch_runner_impl.hpp"
#include "catch_generators_impl.hpp" #include "catch_generators_impl.hpp"
#include "catch_console_colour_impl.hpp"
#include "catch_stream.hpp" #include "catch_stream.hpp"
namespace Catch namespace Catch

View File

@ -15,6 +15,7 @@
#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"
namespace Catch namespace Catch
{ {
@ -32,11 +33,11 @@ namespace Catch
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 IReporter class BasicReporter : public IReporter
{ {
struct SpanInfo struct SpanInfo
@ -58,7 +59,7 @@ namespace Catch
std::string name; std::string name;
bool emitted; bool emitted;
}; };
public: public:
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
BasicReporter BasicReporter
@ -69,14 +70,14 @@ namespace Catch
m_firstSectionInTestCase( true ) m_firstSectionInTestCase( true )
{ {
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
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:
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -86,25 +87,12 @@ namespace Catch
const Counts& counts const Counts& counts
) )
{ {
if( counts.failed > 0 ) if( counts.passed )
{ m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed";
if( counts.passed > 0 )
m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed";
else
{
if( counts.failed > 1 )
m_config.stream() << "All ";
m_config.stream() << pluralise( counts.failed, label ) << " failed";
}
}
else else
{ m_config.stream() << ( counts.failed > 1 ? "All " : "" ) << pluralise( counts.failed, label ) << " failed";
if( counts.passed > 1 )
m_config.stream() << "All ";
m_config.stream() << pluralise( counts.passed, label ) << " passed";
}
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void ReportCounts void ReportCounts
( (
@ -114,18 +102,29 @@ namespace Catch
if( totals.assertions.total() == 0 ) if( totals.assertions.total() == 0 )
{ {
m_config.stream() << "No tests ran"; m_config.stream() << "No tests ran";
return;
} }
ReportCounts( "test case", totals.testCases ); else if( totals.assertions.failed )
if( totals.testCases.failed > 0 )
{ {
m_config.stream() << ". "; TextColour colour( TextColour::ResultError );
ReportCounts( "assertion", totals.assertions ); ReportCounts( "test case", totals.testCases );
if( totals.testCases.failed > 0 )
{
m_config.stream() << " (";
ReportCounts( "assertion", totals.assertions );
m_config.stream() << ")";
}
}
else
{
TextColour colour( TextColour::ResultSuccess );
m_config.stream() << "All tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")";
} }
} }
private: // IReporter
private: // IReporter
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual bool shouldRedirectStdout virtual bool shouldRedirectStdout
() ()
@ -152,7 +151,7 @@ namespace Catch
ReportCounts( totals); ReportCounts( totals);
m_config.stream() << "]\n" << std::endl; m_config.stream() << "]\n" << std::endl;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void StartGroup virtual void StartGroup
( (
@ -161,7 +160,7 @@ namespace Catch
{ {
m_groupSpan = groupName; m_groupSpan = groupName;
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void EndGroup virtual void EndGroup
( (
@ -207,8 +206,19 @@ namespace Catch
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 << "'";
ReportCounts( "assertion", assertions);
if( assertions.failed )
{
TextColour colour( TextColour::ResultError );
ReportCounts( "assertion", assertions);
}
else
{
TextColour colour( TextColour::ResultSuccess );
m_config.stream() << ( assertions.passed > 1 ? "All " : "" )
<< 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();
@ -226,30 +236,46 @@ namespace Catch
StartSpansLazily(); StartSpansLazily();
if( !resultInfo.getFilename().empty() ) if( !resultInfo.getFilename().empty() )
{
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 );
m_config.stream() << resultInfo.getExpression(); m_config.stream() << resultInfo.getExpression();
if( resultInfo.ok() ) if( resultInfo.ok() )
{
TextColour colour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream() << " succeeded";
}
else else
{
TextColour colour( 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 );
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 );
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() );
@ -258,7 +284,10 @@ namespace Catch
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 );
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:
@ -269,16 +298,24 @@ namespace Catch
if( !resultInfo.hasExpression() ) if( !resultInfo.hasExpression() )
{ {
if( resultInfo.ok() ) if( resultInfo.ok() )
{
TextColour colour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream() << " succeeded";
}
else else
{
TextColour colour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream() << " failed";
}
} }
break; break;
} }
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() && resultInfo.getExpression() != resultInfo.getExpandedExpression() )
{ {
m_config.stream() << " for: " << resultInfo.getExpandedExpression(); m_config.stream() << " for: ";
TextColour colour( TextColour::ReconstructedExpression );
m_config.stream() << resultInfo.getExpandedExpression();
} }
m_config.stream() << std::endl; m_config.stream() << std::endl;
} }
@ -297,21 +334,21 @@ namespace Catch
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
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -362,7 +399,7 @@ namespace Catch
} }
} }
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void streamVariableLengthText void streamVariableLengthText
( (
@ -377,23 +414,23 @@ namespace Catch
} }
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;
}; };
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter ) INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
} // end namespace Catch } // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED

View File

@ -84,6 +84,8 @@
4A6D0C67149B3E3D00DB3EAA /* catch_reporter_junit.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_junit.hpp; sourceTree = "<group>"; }; 4A6D0C67149B3E3D00DB3EAA /* catch_reporter_junit.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_junit.hpp; sourceTree = "<group>"; };
4A6D0C68149B3E3D00DB3EAA /* catch_reporter_xml.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_xml.hpp; sourceTree = "<group>"; }; 4A6D0C68149B3E3D00DB3EAA /* catch_reporter_xml.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_xml.hpp; sourceTree = "<group>"; };
4A7ADB4314F631E10094FE10 /* catch_totals.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_totals.hpp; sourceTree = "<group>"; }; 4A7ADB4314F631E10094FE10 /* catch_totals.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_totals.hpp; sourceTree = "<group>"; };
4AB1C73514F97BDA00F31DF7 /* catch_console_colour_impl.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_console_colour_impl.hpp; sourceTree = "<group>"; };
4AB1C73714F97C1300F31DF7 /* catch_console_colour.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_console_colour.hpp; sourceTree = "<group>"; };
4AE1840A14EE4F230066340D /* catch_self_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_self_test.cpp; path = ../../../SelfTest/catch_self_test.cpp; sourceTree = "<group>"; }; 4AE1840A14EE4F230066340D /* catch_self_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_self_test.cpp; path = ../../../SelfTest/catch_self_test.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@ -189,6 +191,8 @@
4A6D0C63149B3E3D00DB3EAA /* catch_test_registry.hpp */, 4A6D0C63149B3E3D00DB3EAA /* catch_test_registry.hpp */,
4A6D0C64149B3E3D00DB3EAA /* catch_xmlwriter.hpp */, 4A6D0C64149B3E3D00DB3EAA /* catch_xmlwriter.hpp */,
4A7ADB4314F631E10094FE10 /* catch_totals.hpp */, 4A7ADB4314F631E10094FE10 /* catch_totals.hpp */,
4AB1C73514F97BDA00F31DF7 /* catch_console_colour_impl.hpp */,
4AB1C73714F97C1300F31DF7 /* catch_console_colour.hpp */,
); );
name = internal; name = internal;
path = ../../../../include/internal; path = ../../../../include/internal;