Skip to content

Commit

Permalink
Improve error messages for TEST_CASE tag parsing errors
Browse files Browse the repository at this point in the history
Also removes a duplicated test case checking for empty tag error.

Related to #2650
  • Loading branch information
horenmar committed May 20, 2023
1 parent 06c0e1c commit 498704a
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 75 deletions.
24 changes: 20 additions & 4 deletions src/catch2/catch_test_case_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,20 @@ namespace Catch {
for (size_t idx = 0; idx < originalTags.size(); ++idx) {
auto c = originalTags[idx];
if (c == '[') {
assert(!inTag);
CATCH_ENFORCE(
!inTag,
"Found '[' inside a tag while registering test case '"
<< _nameAndTags.name << "' at " << _lineInfo );

inTag = true;
tagStart = idx;
}
if (c == ']') {
assert(inTag);
CATCH_ENFORCE(
inTag,
"Found unmatched ']' while registering test case '"
<< _nameAndTags.name << "' at " << _lineInfo );

inTag = false;
tagEnd = idx;
assert(tagStart < tagEnd);
Expand All @@ -153,7 +161,11 @@ namespace Catch {
// it over to backing storage and actually reference the
// backing storage in the saved tags
StringRef tagStr = originalTags.substr(tagStart+1, tagEnd - tagStart - 1);
CATCH_ENFORCE(!tagStr.empty(), "Empty tags are not allowed");
CATCH_ENFORCE( !tagStr.empty(),
"Found an empty tag while registering test case '"
<< _nameAndTags.name << "' at "
<< _lineInfo );

enforceNotReservedTag(tagStr, lineInfo);
properties |= parseSpecialTag(tagStr);
// When copying a tag to the backing storage, we need to
Expand All @@ -167,8 +179,12 @@ namespace Catch {
// the tags.
internalAppendTag(tagStr);
}
(void)inTag; // Silence "set-but-unused" warning in release mode.
}
CATCH_ENFORCE( !inTag,
"Found an unclosed tag while registering test case '"
<< _nameAndTags.name << "' at " << _lineInfo );


// Add [.] if relevant
if (isHidden()) {
internalAppendTag("."_sr);
Expand Down
2 changes: 1 addition & 1 deletion tests/SelfTest/Baselines/automake.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ Nor would this
:test-result: PASS Default scale is invisible to comparison
:test-result: PASS Directly creating an EnumInfo
:test-result: PASS Empty stream name opens cout stream
:test-result: PASS Empty tag is not allowed
:test-result: FAIL EndsWith string matcher
:test-result: PASS Enums can quickly have stringification enabled using REGISTER_ENUM
:test-result: PASS Enums in namespaces can quickly have stringification enabled using REGISTER_ENUM
Expand Down Expand Up @@ -178,6 +177,7 @@ Nor would this
:test-result: PASS Matchers can be negated (Not) with the ! operator
:test-result: FAIL Matchers can be negated (Not) with the ! operator - failing
:test-result: XFAIL Mayfail test case with nested sections
:test-result: FAIL Mismatched square brackets in tags are caught and reported
:test-result: FAIL Mismatching exception messages failing the test
:test-result: PASS Multireporter calls reporters and listeners in correct order
:test-result: PASS Multireporter updates ReporterPreferences properly
Expand Down
2 changes: 1 addition & 1 deletion tests/SelfTest/Baselines/automake.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
:test-result: PASS Default scale is invisible to comparison
:test-result: PASS Directly creating an EnumInfo
:test-result: PASS Empty stream name opens cout stream
:test-result: PASS Empty tag is not allowed
:test-result: FAIL EndsWith string matcher
:test-result: PASS Enums can quickly have stringification enabled using REGISTER_ENUM
:test-result: PASS Enums in namespaces can quickly have stringification enabled using REGISTER_ENUM
Expand Down Expand Up @@ -176,6 +175,7 @@
:test-result: PASS Matchers can be negated (Not) with the ! operator
:test-result: FAIL Matchers can be negated (Not) with the ! operator - failing
:test-result: XFAIL Mayfail test case with nested sections
:test-result: FAIL Mismatched square brackets in tags are caught and reported
:test-result: FAIL Mismatching exception messages failing the test
:test-result: PASS Multireporter calls reporters and listeners in correct order
:test-result: PASS Multireporter updates ReporterPreferences properly
Expand Down
6 changes: 4 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,6 @@ ToString.tests.cpp:<line number>: passed: enumInfo->lookup(3) == "{** unexpected
==
"{** unexpected enum value **}"
Stream.tests.cpp:<line number>: passed: Catch::makeStream( "" )->isConsole() for: true
Tag.tests.cpp:<line number>: passed: Catch::TestCaseInfo( "", { "fake test name", "[]" }, dummySourceLineInfo )
Matchers.tests.cpp:<line number>: failed: testStringForMatching(), EndsWith( "Substring" ) for: "this string contains 'abc' as a substring" ends with: "Substring"
Matchers.tests.cpp:<line number>: failed: testStringForMatching(), EndsWith( "this", Catch::CaseSensitive::No ) for: "this string contains 'abc' as a substring" ends with: "this" (case insensitive)
EnumToString.tests.cpp:<line number>: passed: stringify( EnumClass3::Value1 ) == "Value1" for: "Value1" == "Value1"
Expand Down Expand Up @@ -994,6 +993,9 @@ Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with unclosed tag'") for: "Found an unclosed tag while registering test case 'test with unclosed tag' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with unclosed tag'"
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with nested tags", "[abc[def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with nested tags'") for: "Found '[' inside a tag while registering test case 'test with nested tags' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with nested tags'"
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with superfluous close tags", "[abc][def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with superfluous close tags'") for: "Found unmatched ']' while registering test case 'test with superfluous close tags' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with superfluous close tags'"
Exception.tests.cpp:<line number>: passed: thisThrows(), "expected exception" for: "expected exception" equals: "expected exception"
Exception.tests.cpp:<line number>: failed: thisThrows(), "should fail" for: "expected exception" equals: "should fail"
Reporters.tests.cpp:<line number>: passed: records == expected for: { "Hello", "world", "Goodbye", "world" }
Expand Down Expand Up @@ -2539,6 +2541,6 @@ InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: passed:
test cases: 409 | 309 passed | 84 failed | 5 skipped | 11 failed as expected
assertions: 2226 | 2049 passed | 145 failed | 32 failed as expected
assertions: 2228 | 2051 passed | 145 failed | 32 failed as expected


6 changes: 4 additions & 2 deletions tests/SelfTest/Baselines/compact.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,6 @@ ToString.tests.cpp:<line number>: passed: enumInfo->lookup(3) == "{** unexpected
==
"{** unexpected enum value **}"
Stream.tests.cpp:<line number>: passed: Catch::makeStream( "" )->isConsole() for: true
Tag.tests.cpp:<line number>: passed: Catch::TestCaseInfo( "", { "fake test name", "[]" }, dummySourceLineInfo )
Matchers.tests.cpp:<line number>: failed: testStringForMatching(), EndsWith( "Substring" ) for: "this string contains 'abc' as a substring" ends with: "Substring"
Matchers.tests.cpp:<line number>: failed: testStringForMatching(), EndsWith( "this", Catch::CaseSensitive::No ) for: "this string contains 'abc' as a substring" ends with: "this" (case insensitive)
EnumToString.tests.cpp:<line number>: passed: stringify( EnumClass3::Value1 ) == "Value1" for: "Value1" == "Value1"
Expand Down Expand Up @@ -992,6 +991,9 @@ Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Condition.tests.cpp:<line number>: failed: explicitly
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with unclosed tag'") for: "Found an unclosed tag while registering test case 'test with unclosed tag' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with unclosed tag'"
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with nested tags", "[abc[def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with nested tags'") for: "Found '[' inside a tag while registering test case 'test with nested tags' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with nested tags'"
Tag.tests.cpp:<line number>: passed: TestCaseInfo( "", { "test with superfluous close tags", "[abc][def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with superfluous close tags'") for: "Found unmatched ']' while registering test case 'test with superfluous close tags' at Tag.tests.cpp:<line number>" contains: "registering test case 'test with superfluous close tags'"
Exception.tests.cpp:<line number>: passed: thisThrows(), "expected exception" for: "expected exception" equals: "expected exception"
Exception.tests.cpp:<line number>: failed: thisThrows(), "should fail" for: "expected exception" equals: "should fail"
Reporters.tests.cpp:<line number>: passed: records == expected for: { "Hello", "world", "Goodbye", "world" }
Expand Down Expand Up @@ -2528,6 +2530,6 @@ InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
Misc.tests.cpp:<line number>: passed:
Misc.tests.cpp:<line number>: passed:
test cases: 409 | 309 passed | 84 failed | 5 skipped | 11 failed as expected
assertions: 2226 | 2049 passed | 145 failed | 32 failed as expected
assertions: 2228 | 2051 passed | 145 failed | 32 failed as expected


17 changes: 15 additions & 2 deletions tests/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,19 @@ Condition.tests.cpp:<line number>

Condition.tests.cpp:<line number>: FAILED:

-------------------------------------------------------------------------------
Mismatched square brackets in tags are caught and reported
-------------------------------------------------------------------------------
Tag.tests.cpp:<line number>
...............................................................................

Tag.tests.cpp:<line number>: FAILED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), "registering test case 'test with unclosed tag'" )
with expansion:
"Found an unclosed tag while registering test case 'test with unclosed tag'
at Tag.tests.cpp:<line number>"
equals: "registering test case 'test with unclosed tag'"

-------------------------------------------------------------------------------
Mismatching exception messages failing the test
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1533,6 +1546,6 @@ due to unexpected exception with message:
Why would you throw a std::string?

===============================================================================
test cases: 409 | 323 passed | 69 failed | 6 skipped | 11 failed as expected
assertions: 2209 | 2049 passed | 128 failed | 32 failed as expected
test cases: 409 | 322 passed | 70 failed | 6 skipped | 11 failed as expected
assertions: 2209 | 2048 passed | 129 failed | 32 failed as expected

38 changes: 28 additions & 10 deletions tests/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3967,15 +3967,6 @@ Stream.tests.cpp:<line number>: PASSED:
with expansion:
true

-------------------------------------------------------------------------------
Empty tag is not allowed
-------------------------------------------------------------------------------
Tag.tests.cpp:<line number>
...............................................................................

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS( Catch::TestCaseInfo( "", { "fake test name", "[]" }, dummySourceLineInfo ) )

-------------------------------------------------------------------------------
EndsWith string matcher
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -7397,6 +7388,33 @@ Condition.tests.cpp:<line number>

Condition.tests.cpp:<line number>: FAILED:

-------------------------------------------------------------------------------
Mismatched square brackets in tags are caught and reported
-------------------------------------------------------------------------------
Tag.tests.cpp:<line number>
...............................................................................

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with unclosed tag'") )
with expansion:
"Found an unclosed tag while registering test case 'test with unclosed tag'
at Tag.tests.cpp:<line number>"
contains: "registering test case 'test with unclosed tag'"

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with nested tags", "[abc[def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with nested tags'") )
with expansion:
"Found '[' inside a tag while registering test case 'test with nested tags'
at Tag.tests.cpp:<line number>"
contains: "registering test case 'test with nested tags'"

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with superfluous close tags", "[abc][def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with superfluous close tags'") )
with expansion:
"Found unmatched ']' while registering test case 'test with superfluous close
tags' at tests/<exe-name>/IntrospectiveTests/Tag.tests.
cpp:46" contains: "registering test case 'test with superfluous close tags'"

-------------------------------------------------------------------------------
Mismatching exception messages failing the test
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -18232,5 +18250,5 @@ Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 409 | 309 passed | 84 failed | 5 skipped | 11 failed as expected
assertions: 2226 | 2049 passed | 145 failed | 32 failed as expected
assertions: 2228 | 2051 passed | 145 failed | 32 failed as expected

38 changes: 28 additions & 10 deletions tests/SelfTest/Baselines/console.sw.multi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3965,15 +3965,6 @@ Stream.tests.cpp:<line number>: PASSED:
with expansion:
true

-------------------------------------------------------------------------------
Empty tag is not allowed
-------------------------------------------------------------------------------
Tag.tests.cpp:<line number>
...............................................................................

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS( Catch::TestCaseInfo( "", { "fake test name", "[]" }, dummySourceLineInfo ) )

-------------------------------------------------------------------------------
EndsWith string matcher
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -7395,6 +7386,33 @@ Condition.tests.cpp:<line number>

Condition.tests.cpp:<line number>: FAILED:

-------------------------------------------------------------------------------
Mismatched square brackets in tags are caught and reported
-------------------------------------------------------------------------------
Tag.tests.cpp:<line number>
...............................................................................

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with unclosed tag'") )
with expansion:
"Found an unclosed tag while registering test case 'test with unclosed tag'
at Tag.tests.cpp:<line number>"
contains: "registering test case 'test with unclosed tag'"

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with nested tags", "[abc[def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with nested tags'") )
with expansion:
"Found '[' inside a tag while registering test case 'test with nested tags'
at Tag.tests.cpp:<line number>"
contains: "registering test case 'test with nested tags'"

Tag.tests.cpp:<line number>: PASSED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with superfluous close tags", "[abc][def]]" }, dummySourceLineInfo ), ContainsSubstring("registering test case 'test with superfluous close tags'") )
with expansion:
"Found unmatched ']' while registering test case 'test with superfluous close
tags' at tests/<exe-name>/IntrospectiveTests/Tag.tests.
cpp:46" contains: "registering test case 'test with superfluous close tags'"

-------------------------------------------------------------------------------
Mismatching exception messages failing the test
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -18221,5 +18239,5 @@ Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 409 | 309 passed | 84 failed | 5 skipped | 11 failed as expected
assertions: 2226 | 2049 passed | 145 failed | 32 failed as expected
assertions: 2228 | 2051 passed | 145 failed | 32 failed as expected

14 changes: 12 additions & 2 deletions tests/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="17" failures="128" skipped="11" tests="2237" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="129" skipped="11" tests="2237" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties>
<property name="random-seed" value="1"/>
<property name="filters" value="&quot;*&quot; ~[!nonportable] ~[!benchmark] ~[approvals]"/>
Expand Down Expand Up @@ -463,7 +463,6 @@ at Exception.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Default scale is invisible to comparison" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Directly creating an EnumInfo" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Empty stream name opens cout stream" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Empty tag is not allowed" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="EndsWith string matcher" time="{duration}" status="run">
<failure message="testStringForMatching(), EndsWith( &quot;Substring&quot; )" type="CHECK_THAT">
FAILED:
Expand Down Expand Up @@ -883,6 +882,17 @@ FAILED:
at Condition.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Mismatched square brackets in tags are caught and reported" time="{duration}" status="run">
<failure message="TestCaseInfo( &quot;&quot;, { &quot;test with unclosed tag&quot;, &quot;[abc&quot; }, dummySourceLineInfo ), &quot;registering test case 'test with unclosed tag'&quot;" type="REQUIRE_THROWS_WITH">
FAILED:
REQUIRE_THROWS_WITH( TestCaseInfo( "", { "test with unclosed tag", "[abc" }, dummySourceLineInfo ), "registering test case 'test with unclosed tag'" )
with expansion:
"Found an unclosed tag while registering test case 'test with unclosed tag'
at Tag.tests.cpp:<line number>"
equals: "registering test case 'test with unclosed tag'"
at Tag.tests.cpp:<line number>
</failure>
</testcase>
<testcase classname="<exe-name>.global" name="Mismatching exception messages failing the test" time="{duration}" status="run">
<failure message="thisThrows(), &quot;should fail&quot;" type="REQUIRE_THROWS_WITH">
FAILED:
Expand Down

0 comments on commit 498704a

Please sign in to comment.