From d750da13a8ca2ffcd00ba49d52ee8044287aa31b 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:23:30 +0200 Subject: [PATCH] Split out colour mode string parsing into its own function --- .../internal/catch_reporter_spec_parser.cpp | 19 ++++++++++++++++++- .../internal/catch_reporter_spec_parser.hpp | 5 +++++ .../CmdLineHelpers.tests.cpp | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/catch2/internal/catch_reporter_spec_parser.cpp b/src/catch2/internal/catch_reporter_spec_parser.cpp index 28e3c19b..9f896ae2 100644 --- a/src/catch2/internal/catch_reporter_spec_parser.cpp +++ b/src/catch2/internal/catch_reporter_spec_parser.cpp @@ -8,6 +8,8 @@ #include +#include + namespace Catch { namespace Detail { @@ -59,6 +61,21 @@ namespace Catch { return parts; } - } + + Optional stringToColourMode( StringRef colourMode ) { + if ( colourMode == "default" ) { + return ColourMode::PlatformDefault; + } else if ( colourMode == "ansi" ) { + return ColourMode::ANSI; + } else if ( colourMode == "win32" ) { + return ColourMode::Win32; + } else if ( colourMode == "none" ) { + return ColourMode::None; + } else { + return {}; + } + } + } // namespace Detail + } // namespace Catch diff --git a/src/catch2/internal/catch_reporter_spec_parser.hpp b/src/catch2/internal/catch_reporter_spec_parser.hpp index 74632f25..3dd9f17e 100644 --- a/src/catch2/internal/catch_reporter_spec_parser.hpp +++ b/src/catch2/internal/catch_reporter_spec_parser.hpp @@ -8,6 +8,7 @@ #ifndef CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED #define CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED +#include #include #include @@ -15,9 +16,13 @@ namespace Catch { + enum class ColourMode : std::uint8_t; + namespace Detail { //! Splits the reporter spec into reporter name and kv-pair options std::vector splitReporterSpec( StringRef reporterSpec ); + + Optional stringToColourMode( StringRef colourMode ); } } diff --git a/tests/SelfTest/IntrospectiveTests/CmdLineHelpers.tests.cpp b/tests/SelfTest/IntrospectiveTests/CmdLineHelpers.tests.cpp index b79dcca2..0eacfdf3 100644 --- a/tests/SelfTest/IntrospectiveTests/CmdLineHelpers.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/CmdLineHelpers.tests.cpp @@ -9,6 +9,7 @@ #include #include #include +#include TEST_CASE("Reporter spec splitting", "[reporter-spec][cli][approvals]") { using Catch::Detail::splitReporterSpec; @@ -44,3 +45,20 @@ TEST_CASE("Reporter spec splitting", "[reporter-spec][cli][approvals]") { "key:key=value:value"s } ) ); } } + +TEST_CASE( "Parsing colour mode", "[cli][colour][approvals]" ) { + using Catch::Detail::stringToColourMode; + using Catch::ColourMode; + SECTION("Valid strings") { + REQUIRE( stringToColourMode( "none" ) == ColourMode::None ); + REQUIRE( stringToColourMode( "ansi" ) == ColourMode::ANSI ); + REQUIRE( stringToColourMode( "win32" ) == ColourMode::Win32 ); + REQUIRE( stringToColourMode( "default" ) == + ColourMode::PlatformDefault ); + } + SECTION("Wrong strings") { + REQUIRE_FALSE( stringToColourMode( "NONE" ) ); + REQUIRE_FALSE( stringToColourMode( "-" ) ); + REQUIRE_FALSE( stringToColourMode( "asdbjsdb kasbd" ) ); + } +}