| 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/matchers/catch_matchers_string.hpp> |
| 10 | #include <catch2/matchers/catch_matchers_vector.hpp> |
| 11 | #include <catch2/internal/catch_tag_alias_registry.hpp> |
| 12 | #include <catch2/catch_test_macros.hpp> |
| 13 | #include <catch2/catch_test_case_info.hpp> |
| 14 | |
| 15 | TEST_CASE( "Tag alias can be registered against tag patterns" ) { |
| 16 | |
| 17 | Catch::TagAliasRegistry registry; |
| 18 | |
| 19 | registry.add( "[@zzz]", "[one][two]", Catch::SourceLineInfo( "file", 2 ) ); |
| 20 | |
| 21 | SECTION( "The same tag alias can only be registered once" ) { |
| 22 | |
| 23 | try { |
| 24 | registry.add( "[@zzz]", "[one][two]", Catch::SourceLineInfo( "file", 10 ) ); |
| 25 | FAIL( "expected exception" ); |
| 26 | } |
| 27 | catch( std::exception& ex ) { |
| 28 | std::string what = ex.what(); |
| 29 | using namespace Catch::Matchers; |
| 30 | CHECK_THAT( what, ContainsSubstring( "[@zzz]" ) ); |
| 31 | CHECK_THAT( what, ContainsSubstring( "file" ) ); |
| 32 | CHECK_THAT( what, ContainsSubstring( "2" ) ); |
| 33 | CHECK_THAT( what, ContainsSubstring( "10" ) ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | SECTION( "Tag aliases must be of the form [@name]" ) { |
| 38 | CHECK_THROWS( registry.add( "[no ampersat]", "", Catch::SourceLineInfo( "file", 3 ) ) ); |
| 39 | CHECK_THROWS( registry.add( "[the @ is not at the start]", "", Catch::SourceLineInfo( "file", 3 ) ) ); |
| 40 | CHECK_THROWS( registry.add( "@no square bracket at start]", "", Catch::SourceLineInfo( "file", 3 ) ) ); |
| 41 | CHECK_THROWS( registry.add( "[@no square bracket at end", "", Catch::SourceLineInfo( "file", 3 ) ) ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Dummy line info for creating dummy test cases below |
| 46 | static constexpr Catch::SourceLineInfo dummySourceLineInfo = CATCH_INTERNAL_LINEINFO; |
| 47 | |
| 48 | TEST_CASE("shortened hide tags are split apart", "[tags]") { |
| 49 | using Catch::StringRef; |
| 50 | using Catch::Tag; |
| 51 | using Catch::Matchers::VectorContains; |
| 52 | |
| 53 | Catch::TestCaseInfo testcase("", {"fake test name", "[.magic-tag]"}, dummySourceLineInfo); |
| 54 | REQUIRE_THAT( testcase.tags, VectorContains( Tag( "magic-tag" ) ) |
| 55 | && VectorContains( Tag( "."_catch_sr ) ) ); |
| 56 | } |
| 57 | |
| 58 | TEST_CASE("tags with dots in later positions are not parsed as hidden", "[tags]") { |
| 59 | using Catch::StringRef; |
| 60 | using Catch::Matchers::VectorContains; |
| 61 | Catch::TestCaseInfo testcase("", { "fake test name", "[magic.tag]" }, dummySourceLineInfo); |
| 62 | |
| 63 | REQUIRE(testcase.tags.size() == 1); |
| 64 | REQUIRE(testcase.tags[0].original == "magic.tag"_catch_sr); |
| 65 | } |
| 66 | |
| 67 | TEST_CASE( "empty tags are not allowed", "[tags]" ) { |
| 68 | REQUIRE_THROWS( |
| 69 | Catch::TestCaseInfo("", { "test with an empty tag", "[]" }, dummySourceLineInfo) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | TEST_CASE( "Tags with spaces and non-alphanumerical characters are accepted", |
| 74 | "[tags]" ) { |
| 75 | using Catch::Tag; |
| 76 | using Catch::Matchers::VectorContains; |
| 77 | |
| 78 | Catch::TestCaseInfo testCase( |
| 79 | "", |
| 80 | { "fake test name", "[tag with spaces][I said \"good day\" sir!]" }, |
| 81 | dummySourceLineInfo ); |
| 82 | |
| 83 | REQUIRE( testCase.tags.size() == 2 ); |
| 84 | REQUIRE_THAT( testCase.tags, |
| 85 | VectorContains( Tag( "tag with spaces" ) ) && |
| 86 | VectorContains( Tag( "I said \"good day\" sir!"_catch_sr ) ) ); |
| 87 | } |
| 88 | |
| 89 | TEST_CASE( "Test case with identical tags keeps just one", "[tags]" ) { |
| 90 | using Catch::Tag; |
| 91 | |
| 92 | Catch::TestCaseInfo testCase( |
| 93 | "", |
| 94 | { "fake test name", "[TaG1][tAg1][TAG1][tag1]" }, |
| 95 | dummySourceLineInfo ); |
| 96 | |
| 97 | REQUIRE( testCase.tags.size() == 1 ); |
| 98 | REQUIRE( testCase.tags[0] == Tag( "tag1" ) ); |
| 99 | } |
| 100 | |
| 101 | TEST_CASE("Mismatched square brackets in tags are caught and reported", |
| 102 | "[tags][approvals]") { |
| 103 | using Catch::TestCaseInfo; |
| 104 | using Catch::Matchers::ContainsSubstring; |
| 105 | REQUIRE_THROWS_WITH( TestCaseInfo( "", |
| 106 | { "test with unclosed tag", "[abc" }, |
| 107 | dummySourceLineInfo ), |
| 108 | ContainsSubstring("registering test case 'test with unclosed tag'") ); |
| 109 | REQUIRE_THROWS_WITH( TestCaseInfo( "", |
| 110 | { "test with nested tags", "[abc[def]]" }, |
| 111 | dummySourceLineInfo ), |
| 112 | ContainsSubstring("registering test case 'test with nested tags'") ); |
| 113 | REQUIRE_THROWS_WITH( TestCaseInfo( "", |
| 114 | { "test with superfluous close tags", "[abc][def]]" }, |
| 115 | dummySourceLineInfo ), |
| 116 | ContainsSubstring("registering test case 'test with superfluous close tags'") ); |
| 117 | } |