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_runner_impl.hpp"
#include "catch_generators_impl.hpp"
#include "catch_console_colour_impl.hpp"
#include "catch_stream.hpp"
namespace Catch

View File

@ -15,6 +15,7 @@
#include "../internal/catch_capture.hpp"
#include "../internal/catch_interfaces_reporter.h"
#include "../internal/catch_reporter_registrars.hpp"
#include "../internal/catch_console_colour.hpp"
namespace Catch
{
@ -86,23 +87,10 @@ namespace Catch
const Counts& counts
)
{
if( counts.failed > 0 )
{
if( counts.passed > 0 )
if( counts.passed )
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
{
if( counts.passed > 1 )
m_config.stream() << "All ";
m_config.stream() << pluralise( counts.passed, label ) << " passed";
}
m_config.stream() << ( counts.failed > 1 ? "All " : "" ) << pluralise( counts.failed, label ) << " failed";
}
///////////////////////////////////////////////////////////////////////////
@ -114,13 +102,24 @@ namespace Catch
if( totals.assertions.total() == 0 )
{
m_config.stream() << "No tests ran";
return;
}
else if( totals.assertions.failed )
{
TextColour colour( TextColour::ResultError );
ReportCounts( "test case", totals.testCases );
if( totals.testCases.failed > 0 )
{
m_config.stream() << ". ";
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" ) << ")";
}
}
@ -207,8 +206,19 @@ namespace Catch
SpanInfo& sectionSpan = m_sectionSpans.back();
if( sectionSpan.emitted && !sectionSpan.name.empty() )
{
m_config.stream() << "[End of section: '" << sectionName << "'. ";
m_config.stream() << "[End of section: '" << sectionName << "'";
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_sectionSpans.pop_back();
@ -226,30 +236,46 @@ namespace Catch
StartSpansLazily();
if( !resultInfo.getFilename().empty() )
{
TextColour colour( TextColour::FileName );
m_config.stream() << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() );
}
if( resultInfo.hasExpression() )
{
TextColour colour( TextColour::OriginalExpression );
m_config.stream() << resultInfo.getExpression();
if( resultInfo.ok() )
{
TextColour colour( TextColour::Success );
m_config.stream() << " succeeded";
}
else
{
TextColour colour( TextColour::Error );
m_config.stream() << " failed";
}
}
switch( resultInfo.getResultType() )
{
case ResultWas::ThrewException:
{
TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() )
m_config.stream() << " with unexpected";
else
m_config.stream() << "Unexpected";
m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'";
}
break;
case ResultWas::DidntThrowException:
{
TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() )
m_config.stream() << " because no exception was thrown where one was expected";
else
m_config.stream() << "No exception thrown where one was expected";
}
break;
case ResultWas::Info:
streamVariableLengthText( "info", resultInfo.getMessage() );
@ -258,7 +284,10 @@ namespace Catch
m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'";
break;
case ResultWas::ExplicitFailure:
{
TextColour colour( TextColour::Error );
m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'";
}
break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::Ok:
@ -269,16 +298,24 @@ namespace Catch
if( !resultInfo.hasExpression() )
{
if( resultInfo.ok() )
{
TextColour colour( TextColour::Success );
m_config.stream() << " succeeded";
}
else
{
TextColour colour( TextColour::Error );
m_config.stream() << " failed";
}
}
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;
}
@ -306,7 +343,7 @@ namespace Catch
if( m_testSpan.emitted )
{
m_config.stream() << "[Finished: " << testInfo.getName() << " ";
m_config.stream() << "[Finished: '" << testInfo.getName() << "' ";
ReportCounts( totals );
m_config.stream() << "]" << std::endl;
}

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>"; };
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>"; };
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>"; };
/* End PBXFileReference section */
@ -189,6 +191,8 @@
4A6D0C63149B3E3D00DB3EAA /* catch_test_registry.hpp */,
4A6D0C64149B3E3D00DB3EAA /* catch_xmlwriter.hpp */,
4A7ADB4314F631E10094FE10 /* catch_totals.hpp */,
4AB1C73514F97BDA00F31DF7 /* catch_console_colour_impl.hpp */,
4AB1C73714F97C1300F31DF7 /* catch_console_colour.hpp */,
);
name = internal;
path = ../../../../include/internal;