CmdLineHelpers.tests.cpp 4.8 KB

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