Refactor colour handling to prepare for per-reporter colour modes

This includes always compiling the ANSI and None colour
implementations, as they don't need to touch any platform
specific APIs, and removing their respective compile-time
configuration options.

Because the Win32 colour implementation requires Win32-specific
APIs, it is still hidden behind a compile-time toggle,
`CATCH_CONFIG_COLOUR_WIN32` (renamed from `..._COLOUR_WINDOWS`).

The commandline options for colours were also changed. The
option now uses different name, and allows to select between
different implementations, rather than changing whether
the compiled-in colour implementation is used through
"yes/no/default" options.
This commit is contained in:
Martin Hořeňovský 2022-03-27 23:35:41 +02:00
parent a4e4e82474
commit 1a8a793178
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
27 changed files with 292 additions and 203 deletions

View File

@ -8,9 +8,7 @@ expand_template(
out = "catch2/catch_user_config.hpp",
substitutions = {
"#cmakedefine CATCH_CONFIG_ANDROID_LOGWRITE": "",
"#cmakedefine CATCH_CONFIG_COLOUR_ANSI": "",
"#cmakedefine CATCH_CONFIG_COLOUR_NONE": "",
"#cmakedefine CATCH_CONFIG_COLOUR_WINDOWS": "",
"#cmakedefine CATCH_CONFIG_COLOUR_WIN32": "",
"#cmakedefine CATCH_CONFIG_COUNTER": "",
"#cmakedefine CATCH_CONFIG_CPP11_TO_STRING": "",
"#cmakedefine CATCH_CONFIG_CPP17_BYTE": "",

View File

@ -26,6 +26,7 @@ endmacro()
set(_OverridableOptions
"ANDROID_LOGWRITE"
"COLOUR_WIN32"
"COUNTER"
"CPP11_TO_STRING"
"CPP17_BYTE"
@ -45,9 +46,6 @@ foreach(OptionName ${_OverridableOptions})
endforeach()
set(_OtherConfigOptions
"COLOUR_ANSI"
"COLOUR_NONE"
"COLOUR_WINDOWS"
"DISABLE_EXCEPTIONS"
"DISABLE_EXCEPTIONS_CUSTOM_HANDLER"
"DISABLE"

View File

@ -67,7 +67,7 @@ Click one of the following links to take you straight to that option - or scroll
<a href="#benchmark-confidence-interval"> ` --benchmark-confidence-interval`</a><br />
<a href="#benchmark-no-analysis"> ` --benchmark-no-analysis`</a><br />
<a href="#benchmark-warmup-time"> ` --benchmark-warmup-time`</a><br />
<a href="#use-colour"> ` --use-colour`</a><br />
<a href="#colour-mode"> ` --colour-mode`</a><br />
<a href="#test-sharding"> ` --shard-count`</a><br />
<a href="#test-sharding"> ` --shard-index`</a><br />
<a href=#no-tests-override> ` --allow-running-no-tests`</a><br />
@ -457,16 +457,26 @@ filename it is found in, with any extension stripped, prefixed with the `#` char
So, for example, tests within the file `~\Dev\MyProject\Ferrets.cpp` would be tagged `[#Ferrets]`.
<a id="use-colour"></a>
<a id="colour-mode"></a>
## Override output colouring
<pre>--use-colour &lt;yes|no|auto&gt;</pre>
<pre>--colour-mode &lt;ansi|win32|none|default&gt;</pre>
Catch colours output for terminals, but omits colouring when it detects that
output is being sent to a pipe. This is done to avoid interfering with automated
processing of output.
Catch2 support two different ways of colouring terminal output, and by
default it attempts to make a good guess on which implementation to use
(and whether to even use it, e.g. Catch2 tries to avoid writing colour
codes when writing the results into a file).
`--colour-mode` allows the user to explicitly select what happens.
* `--colour-mode ansi` tells Catch2 to always use ANSI colour codes, even
when writing to a file
* `--colour-mode win32` tells Catch2 to use colour implementation based
on Win32 terminal API
* `--colour-mode none` tells Catch2 to disable colours completely
* `--colour-mode default` lets Catch2 decide
`--colour-mode default` is the default setting.
`--use-colour yes` forces coloured output, `--use-colour no` disables coloured
output. The default behaviour is `--use-colour auto`.
<a id="test-sharding"></a>
## Test Sharding

View File

@ -31,19 +31,18 @@ To keep test code clean and uncluttered Catch uses short macro names (e.g. ```TE
## Terminal colour
CATCH_CONFIG_COLOUR_NONE // completely disables all text colouring
CATCH_CONFIG_COLOUR_WINDOWS // forces the Win32 console API to be used
CATCH_CONFIG_COLOUR_ANSI // forces ANSI colour codes to be used
CATCH_CONFIG_COLOUR_WIN32 // Force enables compiling colouring impl based on Win32 console API
CATCH_CONFIG_NO_COLOUR_WIN32 // Force disables ...
Yes, I am English, so I will continue to spell "colour" with a 'u'.
Yes, Catch2 uses the british spelling of colour.
When sending output to the terminal, if it detects that it can, Catch will use colourised text. On Windows the Win32 API, ```SetConsoleTextAttribute```, is used. On POSIX systems ANSI colour escape codes are inserted into the stream.
Catch2 attempts to autodetect whether the Win32 console colouring API,
`SetConsoleTextAttribute`, is available, and if it is available it compiles
in a console colouring implementation that uses it.
For finer control you can define one of the above identifiers (these are mutually exclusive - but that is not checked so may behave unexpectedly if you mix them):
This option can be used to override Catch2's autodetection and force the
compilation either ON or OFF.
Note that when ANSI colour codes are used "unistd.h" must be includable - along with a definition of ```isatty()```
Typically you should place the ```#define``` before #including "catch.hpp" in your main source file - but if you prefer you can define it for your whole project by whatever your IDE or build system provides for you to do so.
## Console width

View File

@ -179,6 +179,13 @@ v3 releases.
* To support this, the `-r`, `--reporter` flag now also accepts optional output destination
* For full overview of the semantics of using multiple reporters, look into the reporter documentation
* To enable the new syntax, reporter names can no longer contain `::`.
* Console colour support has been rewritten and significantly improved
* The colour implementation based on ANSI colour codes is always available
* Colour implementations respect their associated stream
* previously e.g. Win32 impl would change console colour even if Catch2 was writing to a file
* The colour API is resilient against changing evaluation order of expressions
* The associated CLI flag and compile-time configuration options have changed
* For details see the docs for command-line and compile-time Catch2 configuration
### Fixes

View File

@ -138,7 +138,7 @@ namespace Catch {
uint32_t Config::rngSeed() const { return m_data.rngSeed; }
unsigned int Config::shardCount() const { return m_data.shardCount; }
unsigned int Config::shardIndex() const { return m_data.shardIndex; }
UseColour Config::useColour() const { return m_data.useColour; }
ColourMode Config::colourMode() const { return m_data.colourMode; }
bool Config::shouldDebugBreak() const { return m_data.shouldDebugBreak; }
int Config::abortAfter() const { return m_data.abortAfter; }
bool Config::showInvisibles() const { return m_data.showInvisibles; }

View File

@ -66,7 +66,7 @@ namespace Catch {
ShowDurations showDurations = ShowDurations::DefaultForReporter;
double minDuration = -1;
TestRunOrder runOrder = TestRunOrder::Declared;
UseColour useColour = UseColour::Auto;
ColourMode colourMode = ColourMode::PlatformDefault;
WaitForKeypress::When waitForKeypress = WaitForKeypress::Never;
std::string defaultOutputFilename;
@ -114,7 +114,7 @@ namespace Catch {
uint32_t rngSeed() const override;
unsigned int shardCount() const override;
unsigned int shardIndex() const override;
UseColour useColour() const override;
ColourMode colourMode() const override;
bool shouldDebugBreak() const override;
int abortAfter() const override;
bool showInvisibles() const override;

View File

@ -152,7 +152,8 @@ namespace Catch {
m_startupExceptions = true;
auto errStream = makeStream( "%stderr" );
auto colourImpl = makeColourImpl( &config(), errStream.get() );
auto colourImpl = makeColourImpl(
ColourMode::PlatformDefault, errStream.get() );
auto guard = colourImpl->guardColour( Colour::Red );
errStream->stream() << "Errors occurred during startup!" << '\n';
// iterate over all exceptions and notify user
@ -197,7 +198,7 @@ namespace Catch {
config();
getCurrentMutableContext().setConfig(m_config.get());
auto errStream = makeStream( "%stderr" );
auto colour = makeColourImpl( &config(), errStream.get() );
auto colour = makeColourImpl( ColourMode::PlatformDefault, errStream.get() );
errStream->stream()
<< colour->guardColour( Colour::Red )

View File

@ -32,7 +32,13 @@
# error Cannot force ANDROID_LOGWRITE to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_COLOUR_WIN32
#cmakedefine CATCH_CONFIG_NO_COLOUR_WIN32
#if defined( CATCH_CONFIG_COLOUR_WIN32 ) && \
defined( CATCH_CONFIG_NO_COLOUR_WIN32 )
# error Cannot force COLOUR_WIN32 to be ON and OFF
#endif
#cmakedefine CATCH_CONFIG_COUNTER
#cmakedefine CATCH_CONFIG_NO_COUNTER
@ -159,9 +165,6 @@
// ------
#cmakedefine CATCH_CONFIG_COLOUR_ANSI
#cmakedefine CATCH_CONFIG_COLOUR_NONE
#cmakedefine CATCH_CONFIG_COLOUR_WINDOWS
#cmakedefine CATCH_CONFIG_DISABLE_EXCEPTIONS
#cmakedefine CATCH_CONFIG_DISABLE_EXCEPTIONS_CUSTOM_HANDLER
#cmakedefine CATCH_CONFIG_DISABLE

View File

@ -42,10 +42,15 @@ namespace Catch {
LexicographicallySorted,
Randomized
};
enum class UseColour {
Auto,
Yes,
No
enum class ColourMode : std::uint8_t {
//! Let Catch2 pick implementation based on platform detection
PlatformDefault,
//! Use ANSI colour code escapes
ANSI,
//! Use Win32 console colour API
Win32,
//! Don't use any colour
None
};
struct WaitForKeypress { enum When {
Never,
@ -79,7 +84,7 @@ namespace Catch {
virtual uint32_t rngSeed() const = 0;
virtual unsigned int shardCount() const = 0;
virtual unsigned int shardIndex() const = 0;
virtual UseColour useColour() const = 0;
virtual ColourMode colourMode() const = 0;
virtual std::vector<std::string> const& getSectionsToRun() const = 0;
virtual Verbosity verbosity() const = 0;

View File

@ -13,6 +13,7 @@
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
#include <catch2/interfaces/catch_interfaces_reporter_registry.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_console_colour.hpp>
#include <algorithm>
#include <fstream>
@ -95,17 +96,42 @@ namespace Catch {
return ParserResult::runtimeError("Could not parse '" + seed + "' as seed");
}
};
auto const setColourUsage = [&]( std::string const& useColour ) {
auto mode = toLower( useColour );
auto const setColourMode = [&]( std::string const&
colourMode ) {
auto mode = toLower( colourMode );
if( mode == "yes" )
config.useColour = UseColour::Yes;
else if( mode == "no" )
config.useColour = UseColour::No;
else if( mode == "auto" )
config.useColour = UseColour::Auto;
else
return ParserResult::runtimeError( "colour mode must be one of: auto, yes or no. '" + useColour + "' not recognised" );
if ( mode == "default" ) {
if ( !isColourImplAvailable( ColourMode::PlatformDefault ) ) {
return ParserResult::runtimeError(
"colour mode 'default' is not supported in this "
"binary" );
}
config.colourMode = ColourMode::PlatformDefault;
} else if ( mode == "ansi" ) {
if ( !isColourImplAvailable( ColourMode::ANSI ) ) {
return ParserResult::runtimeError(
"colour mode 'ansi' is not supported in this binary" );
}
config.colourMode = ColourMode::ANSI;
} else if ( mode == "win32" ) {
if ( !isColourImplAvailable( ColourMode::Win32 ) ) {
return ParserResult::runtimeError(
"colour mode 'win32' is not supported in this "
"binary" );
}
config.colourMode = ColourMode::Win32;
} else if ( mode == "none" ) {
if ( !isColourImplAvailable( ColourMode::None ) ) {
return ParserResult::runtimeError(
"colour mode 'none' is not supported in this binary" );
}
config.colourMode = ColourMode::None;
} else {
return ParserResult::runtimeError(
"colour mode must be one of: default, ansi, win32, "
"or none. '" +
colourMode + "' not recognised" );
}
return ParserResult::ok( ParseResultType::Matched );
};
auto const setWaitForKeypress = [&]( std::string const& keypress ) {
@ -298,8 +324,8 @@ namespace Catch {
| Opt( setRngSeed, "'time'|'random-device'|number" )
["--rng-seed"]
( "set a specific seed for random numbers" )
| Opt( setColourUsage, "yes|no|auto" )
["--use-colour"]
| Opt( setColourMode, "ansi|win32|none|default" )
["--colour-mode"]
( "should output be colourised" )
| Opt( config.libIdentify )
["--libidentify"]

View File

@ -116,7 +116,6 @@
#ifdef __OS400__
# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS
# define CATCH_CONFIG_COLOUR_NONE
#endif
////////////////////////////////////////////////////////////////////////////////
@ -164,7 +163,7 @@
// Universal Windows platform does not support SEH
// Or console colours (or console at all...)
# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
# define CATCH_CONFIG_COLOUR_NONE
# define CATCH_INTERNAL_CONFIG_NO_COLOUR_WIN32
# else
# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH
# endif
@ -206,7 +205,7 @@
#if defined(UNDER_RTSS) || defined(RTX64_BUILD)
#define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH
#define CATCH_INTERNAL_CONFIG_NO_ASYNC
#define CATCH_CONFIG_COLOUR_NONE
#define CATCH_INTERNAL_CONFIG_NO_COLOUR_WIN32
#endif
#if !defined(_GLIBCXX_USE_C99_MATH_TR1)
@ -358,4 +357,12 @@
#define CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR
#endif
#if defined( CATCH_PLATFORM_WINDOWS ) && \
!defined( CATCH_CONFIG_COLOUR_WIN32 ) && \
!defined( CATCH_CONFIG_NO_COLOUR_WIN32 ) && \
!defined( CATCH_INTERNAL_CONFIG_NO_COLOUR_WIN32 )
# define CATCH_CONFIG_COLOUR_WIN32
#endif
#endif // CATCH_COMPILER_CAPABILITIES_HPP_INCLUDED

View File

@ -20,6 +20,7 @@
#include <catch2/internal/catch_platform.hpp>
#include <catch2/internal/catch_debugger.hpp>
#include <catch2/internal/catch_windows_h_proxy.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <ostream>
@ -31,20 +32,6 @@ namespace Catch {
return ColourGuard(colourCode, this );
}
namespace {
//! A do-nothing implementation of colour, used as fallback for unknown
//! platforms, and when the user asks to deactivate all colours.
class NoColourImpl : public ColourImpl {
public:
NoColourImpl( IStream const* stream ): ColourImpl( stream ) {}
static bool useColourOnPlatform() { return true; }
private:
void use( Colour::Code ) const override {}
};
} // namespace
void ColourImpl::ColourGuard::engageImpl( std::ostream& stream ) {
assert( &stream == &m_colourImpl->m_stream->stream() &&
"Engaging colour guard for different stream than used by the "
@ -92,18 +79,24 @@ namespace Catch {
return CATCH_MOVE(*this);
}
namespace {
//! A do-nothing implementation of colour, used as fallback for unknown
//! platforms, and when the user asks to deactivate all colours.
class NoColourImpl : public ColourImpl {
public:
NoColourImpl( IStream const* stream ): ColourImpl( stream ) {}
static bool useColourOnPlatform() { return true; }
private:
void use( Colour::Code ) const override {}
};
} // namespace
} // namespace Catch
#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI )
# ifdef CATCH_PLATFORM_WINDOWS
# define CATCH_CONFIG_COLOUR_WINDOWS
# else
# define CATCH_CONFIG_COLOUR_ANSI
# endif
#endif
#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) /////////////////////////////////////////
#if defined ( CATCH_CONFIG_COLOUR_WIN32 ) /////////////////////////////////////////
namespace Catch {
namespace {
@ -174,13 +167,9 @@ namespace {
namespace Catch {
namespace {
// use POSIX/ ANSI console terminal codes
// Thanks to Adam Strzelecki for original contribution
// (http://github.com/nanoant)
// https://github.com/philsquared/Catch/pull/131
class PosixColourImpl : public ColourImpl {
class ANSIColourImpl : public ColourImpl {
public:
PosixColourImpl( IStream const* stream ): ColourImpl( stream ) {}
ANSIColourImpl( IStream const* stream ): ColourImpl( stream ) {}
static bool useColourOnPlatform(IStream const& stream) {
// This is kinda messy due to trying to support a bunch of
@ -239,43 +228,52 @@ namespace {
namespace Catch {
Detail::unique_ptr<ColourImpl> makeColourImpl(IConfig const* config, IStream const* stream) {
UseColour colourMode = config ? config->useColour() : UseColour::Auto;
bool createPlatformInstance = false;
if ( colourMode == UseColour::No ) {
createPlatformInstance = false;
Detail::unique_ptr<ColourImpl> makeColourImpl( ColourMode implSelection,
IStream const* stream ) {
if ( implSelection == ColourMode::None ) {
return Detail::make_unique<NoColourImpl>( stream );
}
if ( colourMode == UseColour::Yes ) {
createPlatformInstance = true;
if ( implSelection == ColourMode::ANSI ) {
return Detail::make_unique<ANSIColourImpl>( stream );
}
#if defined( CATCH_CONFIG_COLOUR_WIN32 )
if ( implSelection == ColourMode::Win32 ) {
return Detail::make_unique<Win32ColourImpl>( stream );
}
if ( colourMode == UseColour::Auto ) {
createPlatformInstance =
#if defined( CATCH_CONFIG_COLOUR_ANSI )
PosixColourImpl::useColourOnPlatform( *stream )
#elif defined( CATCH_CONFIG_COLOUR_WINDOWS )
Win32ColourImpl::useColourOnPlatform( *stream )
#else
false
#endif
;
}
if ( createPlatformInstance ) {
return
#if defined( CATCH_CONFIG_COLOUR_ANSI )
Detail::make_unique<PosixColourImpl>(stream);
#elif defined( CATCH_CONFIG_COLOUR_WINDOWS )
Detail::make_unique<Win32ColourImpl>(stream);
#else
Detail::make_unique<NoColourImpl>(stream);
// todo: check win32 eligibility under ifdef, otherwise ansi
if ( implSelection == ColourMode::PlatformDefault) {
#if defined (CATCH_CONFIG_COLOUR_WIN32)
if ( Win32ColourImpl::useColourOnPlatform( *stream ) ) {
return Detail::make_unique<Win32ColourImpl>( stream );
} else {
return Detail::make_unique<NoColourImpl>( stream );
}
#endif
if ( ANSIColourImpl::useColourOnPlatform( *stream ) ) {
return Detail::make_unique<ANSIColourImpl>( stream );
}
return Detail::make_unique<NoColourImpl>( stream );
}
CATCH_ERROR( "Could not create colour impl for selection " << static_cast<int>(implSelection) );
}
bool isColourImplAvailable( ColourMode colourSelection ) {
switch ( colourSelection ) {
#if defined( CATCH_CONFIG_COLOUR_WIN32 )
case ColourMode::Win32:
#endif
case ColourMode::ANSI:
case ColourMode::None:
case ColourMode::PlatformDefault:
return true;
default:
return false;
}
}
} // end namespace Catch

View File

@ -11,9 +11,11 @@
#include <catch2/internal/catch_unique_ptr.hpp>
#include <iosfwd>
#include <cstdint>
namespace Catch {
enum class ColourMode : std::uint8_t;
struct IConfig;
class IStream;
@ -128,7 +130,11 @@ namespace Catch {
};
//! Provides ColourImpl based on global config and target compilation platform
Detail::unique_ptr<ColourImpl> makeColourImpl( IConfig const* config, IStream const* stream );
Detail::unique_ptr<ColourImpl> makeColourImpl( ColourMode colourSelection,
IStream const* stream );
//! Checks if specific colour impl has been compiled into the binary
bool isColourImplAvailable( ColourMode colourSelection );
} // end namespace Catch

View File

@ -9,10 +9,21 @@
#include <catch2/reporters/catch_reporter_common_base.hpp>
#include <catch2/reporters/catch_reporter_helpers.hpp>
#include <catch2/internal/catch_console_colour.hpp>
#include <catch2/internal/catch_stream.hpp>
namespace Catch {
ReporterBase::ReporterBase( ReporterConfig const& config ):
IEventListener( config.fullConfig() ),
m_wrapped_stream( config.stream() ),
m_stream( m_wrapped_stream->stream() ),
m_colour( makeColourImpl( m_config->colourMode(), m_wrapped_stream ) ) {}
void ReporterBase::listReporters(std::vector<ReporterDescription> const& descriptions) {
ReporterBase::~ReporterBase() = default;
void ReporterBase::listReporters(
std::vector<ReporterDescription> const& descriptions ) {
defaultListReporters(m_stream, descriptions, m_config->verbosity());
}

View File

@ -9,10 +9,10 @@
#define CATCH_REPORTER_COMMON_BASE_HPP_INCLUDED
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <catch2/internal/catch_console_colour.hpp>
namespace Catch {
class ColourImpl;
/**
* This is the base class for all reporters.
*
@ -33,12 +33,8 @@ namespace Catch {
Detail::unique_ptr<ColourImpl> m_colour;
public:
ReporterBase( ReporterConfig const& config ):
IEventListener( config.fullConfig() ),
m_wrapped_stream( config.stream() ),
m_stream( m_wrapped_stream->stream() ),
m_colour( makeColourImpl( config.fullConfig(), m_wrapped_stream ) ) {}
ReporterBase( ReporterConfig const& config );
~ReporterBase() override; // = default;
/**
* Provides a simple default listing of reporters.

View File

@ -473,6 +473,17 @@ set_tests_properties("Reporters::DashAsLocationInReporterSpecSendsOutputToStdout
PASS_REGULAR_EXPRESSION "All tests passed \\(5 assertions in 1 test case\\)"
)
add_test(NAME "Colours::ColourModeCanBeExplicitlySetToAnsi"
COMMAND
$<TARGET_FILE:SelfTest> "Factorials are computed"
--reporter console
--colour-mode ansi
)
set_tests_properties("Colours::ColourModeCanBeExplicitlySetToAnsi"
PROPERTIES
PASS_REGULAR_EXPRESSION "\\[1\;32mAll tests passed"
)
if (CATCH_ENABLE_CONFIGURE_TESTS)
add_test(NAME "CMakeConfig::DefaultReporter"
COMMAND

View File

@ -1381,15 +1381,15 @@ CmdLine.tests.cpp:<line number>: passed: config.abortAfter == 1 for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: config.shouldDebugBreak for: true
CmdLine.tests.cpp:<line number>: passed: config.noThrow == true for: true == true
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Auto for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "auto"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Auto for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "yes"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Yes for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "no"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::No for: 2 == 2
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::PlatformDefault for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse( { "test", "--colour-mode", "default" } ) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::PlatformDefault for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--colour-mode", "ansi"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::ANSI for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--colour-mode", "none"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::None for: 3 == 3
CmdLine.tests.cpp:<line number>: passed: !result for: true
CmdLine.tests.cpp:<line number>: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
CmdLine.tests.cpp:<line number>: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
CmdLine.tests.cpp:<line number>: passed: cli.parse({ "test", "--benchmark-samples=200" }) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.benchmarkSamples == 200 for: 200 == 200
CmdLine.tests.cpp:<line number>: passed: cli.parse({ "test", "--benchmark-resamples=20000" }) for: {?}

View File

@ -1379,15 +1379,15 @@ CmdLine.tests.cpp:<line number>: passed: config.abortAfter == 1 for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: config.shouldDebugBreak for: true
CmdLine.tests.cpp:<line number>: passed: config.noThrow == true for: true == true
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Auto for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "auto"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Auto for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "yes"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::Yes for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--use-colour", "no"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.useColour == UseColour::No for: 2 == 2
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::PlatformDefault for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse( { "test", "--colour-mode", "default" } ) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::PlatformDefault for: 0 == 0
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--colour-mode", "ansi"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::ANSI for: 1 == 1
CmdLine.tests.cpp:<line number>: passed: cli.parse({"test", "--colour-mode", "none"}) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.colourMode == ColourMode::None for: 3 == 3
CmdLine.tests.cpp:<line number>: passed: !result for: true
CmdLine.tests.cpp:<line number>: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
CmdLine.tests.cpp:<line number>: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
CmdLine.tests.cpp:<line number>: passed: cli.parse({ "test", "--benchmark-samples=200" }) for: {?}
CmdLine.tests.cpp:<line number>: passed: config.benchmarkSamples == 200 for: 200 == 200
CmdLine.tests.cpp:<line number>: passed: cli.parse({ "test", "--benchmark-resamples=20000" }) for: {?}

View File

@ -9969,7 +9969,7 @@ with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Auto )
REQUIRE( config.colourMode == ColourMode::PlatformDefault )
with expansion:
0 == 0
@ -9982,12 +9982,12 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "auto"}) )
CHECK( cli.parse( { "test", "--colour-mode", "default" } ) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Auto )
REQUIRE( config.colourMode == ColourMode::PlatformDefault )
with expansion:
0 == 0
@ -10000,12 +10000,12 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "yes"}) )
CHECK( cli.parse({"test", "--colour-mode", "ansi"}) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Yes )
REQUIRE( config.colourMode == ColourMode::ANSI )
with expansion:
1 == 1
@ -10018,14 +10018,14 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "no"}) )
CHECK( cli.parse({"test", "--colour-mode", "none"}) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::No )
REQUIRE( config.colourMode == ColourMode::None )
with expansion:
2 == 2
3 == 3
-------------------------------------------------------------------------------
Process can be configured on command line
@ -10043,8 +10043,8 @@ with expansion:
CmdLine.tests.cpp:<line number>: PASSED:
CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) )
with expansion:
"colour mode must be one of: auto, yes or no. 'wrong' not recognised"
contains: "colour mode must be one of"
"colour mode must be one of: default, ansi, win32, or none. 'wrong' not
recognised" contains: "colour mode must be one of"
-------------------------------------------------------------------------------
Process can be configured on command line

View File

@ -9967,7 +9967,7 @@ with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Auto )
REQUIRE( config.colourMode == ColourMode::PlatformDefault )
with expansion:
0 == 0
@ -9980,12 +9980,12 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "auto"}) )
CHECK( cli.parse( { "test", "--colour-mode", "default" } ) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Auto )
REQUIRE( config.colourMode == ColourMode::PlatformDefault )
with expansion:
0 == 0
@ -9998,12 +9998,12 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "yes"}) )
CHECK( cli.parse({"test", "--colour-mode", "ansi"}) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::Yes )
REQUIRE( config.colourMode == ColourMode::ANSI )
with expansion:
1 == 1
@ -10016,14 +10016,14 @@ CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( cli.parse({"test", "--use-colour", "no"}) )
CHECK( cli.parse({"test", "--colour-mode", "none"}) )
with expansion:
{?}
CmdLine.tests.cpp:<line number>: PASSED:
REQUIRE( config.useColour == UseColour::No )
REQUIRE( config.colourMode == ColourMode::None )
with expansion:
2 == 2
3 == 3
-------------------------------------------------------------------------------
Process can be configured on command line
@ -10041,8 +10041,8 @@ with expansion:
CmdLine.tests.cpp:<line number>: PASSED:
CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) )
with expansion:
"colour mode must be one of: auto, yes or no. 'wrong' not recognised"
contains: "colour mode must be one of"
"colour mode must be one of: default, ansi, win32, or none. 'wrong' not
recognised" contains: "colour mode must be one of"
-------------------------------------------------------------------------------
Process can be configured on command line

View File

@ -2601,23 +2601,23 @@ ok {test-number} - config.noThrow == true for: true == true
# Process can be configured on command line
ok {test-number} - cli.parse({"test"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Auto for: 0 == 0
ok {test-number} - config.colourMode == ColourMode::PlatformDefault for: 0 == 0
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "auto"}) for: {?}
ok {test-number} - cli.parse( { "test", "--colour-mode", "default" } ) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Auto for: 0 == 0
ok {test-number} - config.colourMode == ColourMode::PlatformDefault for: 0 == 0
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "yes"}) for: {?}
ok {test-number} - cli.parse({"test", "--colour-mode", "ansi"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Yes for: 1 == 1
ok {test-number} - config.colourMode == ColourMode::ANSI for: 1 == 1
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "no"}) for: {?}
ok {test-number} - cli.parse({"test", "--colour-mode", "none"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::No for: 2 == 2
ok {test-number} - config.colourMode == ColourMode::None for: 3 == 3
# Process can be configured on command line
ok {test-number} - !result for: true
# Process can be configured on command line
ok {test-number} - result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
ok {test-number} - result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
# Process can be configured on command line
ok {test-number} - cli.parse({ "test", "--benchmark-samples=200" }) for: {?}
# Process can be configured on command line

View File

@ -2599,23 +2599,23 @@ ok {test-number} - config.noThrow == true for: true == true
# Process can be configured on command line
ok {test-number} - cli.parse({"test"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Auto for: 0 == 0
ok {test-number} - config.colourMode == ColourMode::PlatformDefault for: 0 == 0
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "auto"}) for: {?}
ok {test-number} - cli.parse( { "test", "--colour-mode", "default" } ) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Auto for: 0 == 0
ok {test-number} - config.colourMode == ColourMode::PlatformDefault for: 0 == 0
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "yes"}) for: {?}
ok {test-number} - cli.parse({"test", "--colour-mode", "ansi"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::Yes for: 1 == 1
ok {test-number} - config.colourMode == ColourMode::ANSI for: 1 == 1
# Process can be configured on command line
ok {test-number} - cli.parse({"test", "--use-colour", "no"}) for: {?}
ok {test-number} - cli.parse({"test", "--colour-mode", "none"}) for: {?}
# Process can be configured on command line
ok {test-number} - config.useColour == UseColour::No for: 2 == 2
ok {test-number} - config.colourMode == ColourMode::None for: 3 == 3
# Process can be configured on command line
ok {test-number} - !result for: true
# Process can be configured on command line
ok {test-number} - result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
ok {test-number} - result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
# Process can be configured on command line
ok {test-number} - cli.parse({ "test", "--benchmark-samples=200" }) for: {?}
# Process can be configured on command line

View File

@ -12165,7 +12165,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Auto
config.colourMode == ColourMode::PlatformDefault
</Original>
<Expanded>
0 == 0
@ -12179,7 +12179,7 @@ C
<Section name="auto" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "auto"})
cli.parse( { "test", "--colour-mode", "default" } )
</Original>
<Expanded>
{?}
@ -12187,7 +12187,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Auto
config.colourMode == ColourMode::PlatformDefault
</Original>
<Expanded>
0 == 0
@ -12201,7 +12201,7 @@ C
<Section name="yes" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "yes"})
cli.parse({"test", "--colour-mode", "ansi"})
</Original>
<Expanded>
{?}
@ -12209,7 +12209,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Yes
config.colourMode == ColourMode::ANSI
</Original>
<Expanded>
1 == 1
@ -12223,7 +12223,7 @@ C
<Section name="no" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "no"})
cli.parse({"test", "--colour-mode", "none"})
</Original>
<Expanded>
{?}
@ -12231,10 +12231,10 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::No
config.colourMode == ColourMode::None
</Original>
<Expanded>
2 == 2
3 == 3
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
@ -12256,7 +12256,7 @@ C
result.errorMessage(), ContainsSubstring( "colour mode must be one of" )
</Original>
<Expanded>
"colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
"colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>

View File

@ -12165,7 +12165,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Auto
config.colourMode == ColourMode::PlatformDefault
</Original>
<Expanded>
0 == 0
@ -12179,7 +12179,7 @@ C
<Section name="auto" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "auto"})
cli.parse( { "test", "--colour-mode", "default" } )
</Original>
<Expanded>
{?}
@ -12187,7 +12187,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Auto
config.colourMode == ColourMode::PlatformDefault
</Original>
<Expanded>
0 == 0
@ -12201,7 +12201,7 @@ C
<Section name="yes" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "yes"})
cli.parse({"test", "--colour-mode", "ansi"})
</Original>
<Expanded>
{?}
@ -12209,7 +12209,7 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::Yes
config.colourMode == ColourMode::ANSI
</Original>
<Expanded>
1 == 1
@ -12223,7 +12223,7 @@ C
<Section name="no" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
cli.parse({"test", "--use-colour", "no"})
cli.parse({"test", "--colour-mode", "none"})
</Original>
<Expanded>
{?}
@ -12231,10 +12231,10 @@ C
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
config.useColour == UseColour::No
config.colourMode == ColourMode::None
</Original>
<Expanded>
2 == 2
3 == 3
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
@ -12256,7 +12256,7 @@ C
result.errorMessage(), ContainsSubstring( "colour mode must be one of" )
</Original>
<Expanded>
"colour mode must be one of: auto, yes or no. 'wrong' not recognised" contains: "colour mode must be one of"
"colour mode must be one of: default, ansi, win32, or none. 'wrong' not recognised" contains: "colour mode must be one of"
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>

View File

@ -15,6 +15,8 @@
#include <catch2/catch_test_case_info.hpp>
#include <catch2/internal/catch_commandline.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
namespace {
auto fakeTestCase(const char* name, const char* desc = "") { return Catch::makeTestCaseInfo("", { name, desc }, CATCH_INTERNAL_LINEINFO); }
@ -556,34 +558,34 @@ TEST_CASE( "Process can be configured on command line", "[config][command-line]"
SECTION( "use-colour") {
using Catch::UseColour;
using Catch::ColourMode;
SECTION( "without option" ) {
CHECK(cli.parse({"test"}));
REQUIRE( config.useColour == UseColour::Auto );
REQUIRE( config.colourMode == ColourMode::PlatformDefault );
}
SECTION( "auto" ) {
CHECK(cli.parse({"test", "--use-colour", "auto"}));
CHECK( cli.parse( { "test", "--colour-mode", "default" } ) );
REQUIRE( config.useColour == UseColour::Auto );
REQUIRE( config.colourMode == ColourMode::PlatformDefault );
}
SECTION( "yes" ) {
CHECK(cli.parse({"test", "--use-colour", "yes"}));
CHECK(cli.parse({"test", "--colour-mode", "ansi"}));
REQUIRE( config.useColour == UseColour::Yes );
REQUIRE( config.colourMode == ColourMode::ANSI );
}
SECTION( "no" ) {
CHECK(cli.parse({"test", "--use-colour", "no"}));
CHECK(cli.parse({"test", "--colour-mode", "none"}));
REQUIRE( config.useColour == UseColour::No );
REQUIRE( config.colourMode == ColourMode::None );
}
SECTION( "error" ) {
auto result = cli.parse({"test", "--use-colour", "wrong"});
auto result = cli.parse({"test", "--colour-mode", "wrong"});
CHECK( !result );
CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) );
}
@ -712,3 +714,17 @@ TEST_CASE("Various suspicious reporter specs are rejected",
auto result = cli.parse( { "test", "--reporter", spec } );
REQUIRE_FALSE( result );
}
TEST_CASE("Win32 colour implementation is compile-time optional",
"[approvals][cli][colours]") {
Catch::ConfigData config;
auto cli = Catch::makeCommandLineParser( config );
auto result = cli.parse( { "test", "--colour-mode", "win32" } );
#if defined( CATCH_CONFIG_COLOUR_WIN32 )
REQUIRE( result );
#else
REQUIRE_FALSE( result );
#endif
}

View File

@ -64,10 +64,7 @@ TEST_CASE( "The default listing implementation write to provided stream",
{ "fake test name"_catch_sr, "[fakeTestTag]"_catch_sr },
{ "fake-file.cpp", 123456789 } };
std::vector<Catch::TestCaseHandle> tests({ {&fakeInfo, nullptr} });
Catch::ConfigData cd;
cd.useColour = Catch::UseColour::No;
Catch::Config conf(cd);
auto colour = Catch::makeColourImpl( &conf, &sstream);
auto colour = Catch::makeColourImpl( Catch::ColourMode::None, &sstream);
Catch::defaultListTests(sstream.stream(), colour.get(), tests, false, Catch::Verbosity::Normal);
auto listingString = sstream.str();