| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <catch2/internal/catch_reporter_spec_parser.hpp> |
| 11 | #include <catch2/matchers/catch_matchers_vector.hpp> |
| 12 | #include <catch2/interfaces/catch_interfaces_config.hpp> |
| 13 | |
| 14 | TEST_CASE("Reporter spec splitting", "[reporter-spec][cli][approvals]") { |
| 15 | using Catch::Detail::splitReporterSpec; |
| 16 | using Catch::Matchers::Equals; |
| 17 | using namespace std::string_literals; |
| 18 | |
| 19 | SECTION("Various edge cases") { |
| 20 | REQUIRE_THAT( splitReporterSpec( "" ), |
| 21 | Equals( std::vector<std::string>{ ""s } ) ); |
| 22 | REQUIRE_THAT( splitReporterSpec( "::" ), |
| 23 | Equals( std::vector<std::string>{ "", "" } ) ); |
| 24 | REQUIRE_THAT( splitReporterSpec( "::rep" ), |
| 25 | Equals( std::vector<std::string>{ "", "rep" } ) ); |
| 26 | REQUIRE_THAT( splitReporterSpec( "rep::" ), |
| 27 | Equals( std::vector<std::string>{ "rep", "" } ) ); |
| 28 | |
| 29 | } |
| 30 | |
| 31 | SECTION("Validish specs") { |
| 32 | REQUIRE_THAT( splitReporterSpec( "newReporter" ), |
| 33 | Equals( std::vector<std::string>{ "newReporter"s } ) ); |
| 34 | REQUIRE_THAT( |
| 35 | splitReporterSpec( "foo-reporter::key1=value1::key2=value with " |
| 36 | "space::key with space=some-value" ), |
| 37 | Equals( |
| 38 | std::vector<std::string>{ "foo-reporter"s, |
| 39 | "key1=value1"s, |
| 40 | "key2=value with space"s, |
| 41 | "key with space=some-value"s } ) ); |
| 42 | REQUIRE_THAT( |
| 43 | splitReporterSpec( "spaced reporter name::key:key=value:value" ), |
| 44 | Equals( std::vector<std::string>{ "spaced reporter name"s, |
| 45 | "key:key=value:value"s } ) ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST_CASE( "Parsing colour mode", "[cli][colour][approvals]" ) { |
| 50 | using Catch::Detail::stringToColourMode; |
| 51 | using Catch::ColourMode; |
| 52 | SECTION("Valid strings") { |
| 53 | REQUIRE( stringToColourMode( "none" ) == ColourMode::None ); |
| 54 | REQUIRE( stringToColourMode( "ansi" ) == ColourMode::ANSI ); |
| 55 | REQUIRE( stringToColourMode( "win32" ) == ColourMode::Win32 ); |
| 56 | REQUIRE( stringToColourMode( "default" ) == |
| 57 | ColourMode::PlatformDefault ); |
| 58 | } |
| 59 | SECTION("Wrong strings") { |
| 60 | REQUIRE_FALSE( stringToColourMode( "NONE" ) ); |
| 61 | REQUIRE_FALSE( stringToColourMode( "-" ) ); |
| 62 | REQUIRE_FALSE( stringToColourMode( "asdbjsdb kasbd" ) ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | TEST_CASE("Parsing reporter specs", "[cli][reporter-spec][approvals]") { |
| 68 | using Catch::parseReporterSpec; |
| 69 | using Catch::ReporterSpec; |
| 70 | using namespace std::string_literals; |
| 71 | |
| 72 | SECTION( "Correct specs" ) { |
| 73 | REQUIRE( parseReporterSpec( "someReporter" ) == |
| 74 | ReporterSpec( "someReporter"s, {}, {}, {} ) ); |
| 75 | REQUIRE( parseReporterSpec( "otherReporter::Xk=v::out=c:\\blah" ) == |
| 76 | ReporterSpec( |
| 77 | "otherReporter"s, "c:\\blah"s, {}, { { "Xk"s, "v"s } } ) ); |
| 78 | REQUIRE( parseReporterSpec( "diffReporter::Xk1=v1::Xk2==v2" ) == |
| 79 | ReporterSpec( "diffReporter", |
| 80 | {}, |
| 81 | {}, |
| 82 | { { "Xk1"s, "v1"s }, { "Xk2"s, "=v2"s } } ) ); |
| 83 | REQUIRE( parseReporterSpec( |
| 84 | "Foo:bar:reporter::colour-mode=ansi::Xk 1=v 1::Xk2=v:3" ) == |
| 85 | ReporterSpec( "Foo:bar:reporter", |
| 86 | {}, |
| 87 | Catch::ColourMode::ANSI, |
| 88 | { { "Xk 1"s, "v 1"s }, { "Xk2"s, "v:3"s } } ) ); |
| 89 | } |
| 90 | |
| 91 | SECTION( "Bad specs" ) { |
| 92 | REQUIRE_FALSE( parseReporterSpec( "::" ) ); |
| 93 | // Unknown Catch2 arg (should be "out") |
| 94 | REQUIRE_FALSE( parseReporterSpec( "reporter::output=filename" ) ); |
| 95 | // Wrong colour spec |
| 96 | REQUIRE_FALSE( parseReporterSpec( "reporter::colour-mode=custom" ) ); |
| 97 | // Duplicated colour spec |
| 98 | REQUIRE_FALSE( parseReporterSpec( "reporter::colour-mode=ansi::colour-mode=ansi" ) ); |
| 99 | // Duplicated out arg |
| 100 | REQUIRE_FALSE( parseReporterSpec( "reporter::out=f.txt::out=z.txt" ) ); |
| 101 | // Duplicated custom arg |
| 102 | REQUIRE_FALSE( parseReporterSpec( "reporter::Xa=foo::Xa=bar" ) ); |
| 103 | // Empty key |
| 104 | REQUIRE_FALSE( parseReporterSpec( "reporter::X=foo" ) ); |
| 105 | REQUIRE_FALSE( parseReporterSpec( "reporter::=foo" ) ); |
| 106 | // Empty value |
| 107 | REQUIRE_FALSE( parseReporterSpec( "reporter::Xa=" ) ); |
| 108 | // non-key value later field |
| 109 | REQUIRE_FALSE( parseReporterSpec( "reporter::Xab" ) ); |
| 110 | } |
| 111 | } |