From 8ac86495de5bf249fd50775335f62eedb77815d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 4 Apr 2022 15:38:25 +0200 Subject: [PATCH] Cleanup ColourMode parsing in CLI --- src/catch2/internal/catch_commandline.cpp | 41 +++++-------------- .../Baselines/compact.sw.approved.txt | 2 +- .../Baselines/compact.sw.multi.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 2 +- .../Baselines/console.sw.multi.approved.txt | 2 +- tests/SelfTest/Baselines/tap.sw.approved.txt | 2 +- .../Baselines/tap.sw.multi.approved.txt | 2 +- tests/SelfTest/Baselines/xml.sw.approved.txt | 2 +- .../Baselines/xml.sw.multi.approved.txt | 2 +- 9 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/catch2/internal/catch_commandline.cpp b/src/catch2/internal/catch_commandline.cpp index 6363e47a..5095f1db 100644 --- a/src/catch2/internal/catch_commandline.cpp +++ b/src/catch2/internal/catch_commandline.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -98,40 +99,20 @@ namespace Catch { }; auto const setColourMode = [&]( std::string const& colourMode ) { - auto mode = toLower( colourMode ); - - 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 { + Optional maybeMode = Catch::Detail::stringToColourMode(toLower( colourMode )); + if ( !maybeMode ) { return ParserResult::runtimeError( "colour mode must be one of: default, ansi, win32, " "or none. '" + - colourMode + "' not recognised" ); + colourMode + "' is not recognised" ); } + auto mode = *maybeMode; + if ( !isColourImplAvailable( mode ) ) { + return ParserResult::runtimeError( + "colour mode '" + colourMode + + "' is not supported in this binary" ); + } + config.colourMode = mode; return ParserResult::ok( ParseResultType::Matched ); }; auto const setWaitForKeypress = [&]( std::string const& keypress ) { diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index f8c74732..e2ac5551 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1407,7 +1407,7 @@ CmdLine.tests.cpp:: passed: config.colourMode == ColourMode::ANSI f CmdLine.tests.cpp:: passed: cli.parse({"test", "--colour-mode", "none"}) for: {?} CmdLine.tests.cpp:: passed: config.colourMode == ColourMode::None for: 3 == 3 CmdLine.tests.cpp:: passed: !result for: true -CmdLine.tests.cpp:: 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:: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of" CmdLine.tests.cpp:: passed: cli.parse({ "test", "--benchmark-samples=200" }) for: {?} CmdLine.tests.cpp:: passed: config.benchmarkSamples == 200 for: 200 == 200 CmdLine.tests.cpp:: passed: cli.parse({ "test", "--benchmark-resamples=20000" }) for: {?} diff --git a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt index b9bfabd0..b46512ed 100644 --- a/tests/SelfTest/Baselines/compact.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.multi.approved.txt @@ -1405,7 +1405,7 @@ CmdLine.tests.cpp:: passed: config.colourMode == ColourMode::ANSI f CmdLine.tests.cpp:: passed: cli.parse({"test", "--colour-mode", "none"}) for: {?} CmdLine.tests.cpp:: passed: config.colourMode == ColourMode::None for: 3 == 3 CmdLine.tests.cpp:: passed: !result for: true -CmdLine.tests.cpp:: 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:: passed: result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) for: "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of" CmdLine.tests.cpp:: passed: cli.parse({ "test", "--benchmark-samples=200" }) for: {?} CmdLine.tests.cpp:: passed: config.benchmarkSamples == 200 for: 200 == 200 CmdLine.tests.cpp:: passed: cli.parse({ "test", "--benchmark-resamples=20000" }) for: {?} diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index d3476456..3091da60 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -10124,7 +10124,7 @@ with expansion: CmdLine.tests.cpp:: PASSED: CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) ) with expansion: - "colour mode must be one of: default, ansi, win32, or none. 'wrong' not + "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of" ------------------------------------------------------------------------------- diff --git a/tests/SelfTest/Baselines/console.sw.multi.approved.txt b/tests/SelfTest/Baselines/console.sw.multi.approved.txt index 3cfef5e2..3847cfc9 100644 --- a/tests/SelfTest/Baselines/console.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.multi.approved.txt @@ -10122,7 +10122,7 @@ with expansion: CmdLine.tests.cpp:: PASSED: CHECK_THAT( result.errorMessage(), ContainsSubstring( "colour mode must be one of" ) ) with expansion: - "colour mode must be one of: default, ansi, win32, or none. 'wrong' not + "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of" ------------------------------------------------------------------------------- diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index d7d9a9cf..1cb2209b 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2629,7 +2629,7 @@ 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: default, ansi, win32, or none. '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' is 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 diff --git a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt index cf36b3d9..f3e52353 100644 --- a/tests/SelfTest/Baselines/tap.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.multi.approved.txt @@ -2627,7 +2627,7 @@ 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: default, ansi, win32, or none. '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' is 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 diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 79245e38..434f8dbe 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -12337,7 +12337,7 @@ C result.errorMessage(), ContainsSubstring( "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" + "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of" diff --git a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt index 2daf4989..781d986c 100644 --- a/tests/SelfTest/Baselines/xml.sw.multi.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.multi.approved.txt @@ -12337,7 +12337,7 @@ C result.errorMessage(), ContainsSubstring( "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" + "colour mode must be one of: default, ansi, win32, or none. 'wrong' is not recognised" contains: "colour mode must be one of"