Split out colour mode string parsing into its own function

This commit is contained in:
Martin Hořeňovský 2022-04-04 15:23:30 +02:00
parent c045733d05
commit d750da13a8
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
3 changed files with 41 additions and 1 deletions

View File

@ -8,6 +8,8 @@
#include <catch2/internal/catch_reporter_spec_parser.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
namespace Catch {
namespace Detail {
@ -59,6 +61,21 @@ namespace Catch {
return parts;
}
}
Optional<ColourMode> 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

View File

@ -8,6 +8,7 @@
#ifndef CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED
#define CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED
#include <catch2/internal/catch_optional.hpp>
#include <catch2/internal/catch_stringref.hpp>
#include <vector>
@ -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<std::string> splitReporterSpec( StringRef reporterSpec );
Optional<ColourMode> stringToColourMode( StringRef colourMode );
}
}

View File

@ -9,6 +9,7 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/internal/catch_reporter_spec_parser.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
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" ) );
}
}