mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
Added colorised console output for Windows
Also tweaks the output again
This commit is contained in:
parent
f7299fc87b
commit
20df8c5da1
50
include/internal/catch_console_colour.hpp
Normal file
50
include/internal/catch_console_colour.hpp
Normal 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
|
102
include/internal/catch_console_colour_impl.hpp
Normal file
102
include/internal/catch_console_colour_impl.hpp
Normal 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
|
@ -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
|
||||
|
@ -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
|
||||
{
|
||||
@ -32,11 +33,11 @@ namespace Catch
|
||||
os << "s";
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
std::size_t m_count;
|
||||
std::string m_label;
|
||||
};
|
||||
|
||||
|
||||
class BasicReporter : public IReporter
|
||||
{
|
||||
struct SpanInfo
|
||||
@ -58,7 +59,7 @@ namespace Catch
|
||||
std::string name;
|
||||
bool emitted;
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
BasicReporter
|
||||
@ -69,14 +70,14 @@ namespace Catch
|
||||
m_firstSectionInTestCase( true )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
static std::string getDescription
|
||||
()
|
||||
{
|
||||
return "Reports test results as lines of text";
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -86,25 +87,12 @@ namespace Catch
|
||||
const Counts& counts
|
||||
)
|
||||
{
|
||||
if( counts.failed > 0 )
|
||||
{
|
||||
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";
|
||||
}
|
||||
}
|
||||
if( counts.passed )
|
||||
m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s 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";
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
void ReportCounts
|
||||
(
|
||||
@ -114,18 +102,29 @@ namespace Catch
|
||||
if( totals.assertions.total() == 0 )
|
||||
{
|
||||
m_config.stream() << "No tests ran";
|
||||
return;
|
||||
}
|
||||
ReportCounts( "test case", totals.testCases );
|
||||
if( totals.testCases.failed > 0 )
|
||||
else if( totals.assertions.failed )
|
||||
{
|
||||
m_config.stream() << ". ";
|
||||
ReportCounts( "assertion", totals.assertions );
|
||||
TextColour colour( TextColour::ResultError );
|
||||
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
|
||||
()
|
||||
@ -152,7 +151,7 @@ namespace Catch
|
||||
ReportCounts( totals);
|
||||
m_config.stream() << "]\n" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual void StartGroup
|
||||
(
|
||||
@ -161,7 +160,7 @@ namespace Catch
|
||||
{
|
||||
m_groupSpan = groupName;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual void EndGroup
|
||||
(
|
||||
@ -207,8 +206,19 @@ namespace Catch
|
||||
SpanInfo& sectionSpan = m_sectionSpans.back();
|
||||
if( sectionSpan.emitted && !sectionSpan.name.empty() )
|
||||
{
|
||||
m_config.stream() << "[End of section: '" << sectionName << "'. ";
|
||||
ReportCounts( "assertion", assertions);
|
||||
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;
|
||||
}
|
||||
@ -297,21 +334,21 @@ namespace Catch
|
||||
StartSpansLazily();
|
||||
streamVariableLengthText( "stdout", stdOut );
|
||||
}
|
||||
|
||||
|
||||
if( !stdErr.empty() )
|
||||
{
|
||||
StartSpansLazily();
|
||||
streamVariableLengthText( "stderr", stdErr );
|
||||
}
|
||||
|
||||
|
||||
if( m_testSpan.emitted )
|
||||
{
|
||||
m_config.stream() << "[Finished: " << testInfo.getName() << " ";
|
||||
m_config.stream() << "[Finished: '" << testInfo.getName() << "' ";
|
||||
ReportCounts( totals );
|
||||
m_config.stream() << "]" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private: // helpers
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
@ -362,7 +399,7 @@ namespace Catch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
void streamVariableLengthText
|
||||
(
|
||||
@ -377,23 +414,23 @@ namespace Catch
|
||||
}
|
||||
else
|
||||
{
|
||||
m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
|
||||
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
|
||||
m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
|
||||
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const IReporterConfig& m_config;
|
||||
bool m_firstSectionInTestCase;
|
||||
|
||||
|
||||
SpanInfo m_testingSpan;
|
||||
SpanInfo m_groupSpan;
|
||||
SpanInfo m_testSpan;
|
||||
std::vector<SpanInfo> m_sectionSpans;
|
||||
};
|
||||
|
||||
|
||||
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user