diff --git a/docs/assertions.md b/docs/assertions.md index 509abf8a..187b7ea8 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -6,6 +6,7 @@ [Exceptions](#exceptions)
[Matcher expressions](#matcher-expressions)
[Thread Safety](#thread-safety)
+[Expressions with commas](#expressions-with-commas)
Most test frameworks have a large collection of assertion macros to capture all possible conditional forms (```_EQUALS```, ```_NOTEQUALS```, ```_GREATER_THAN``` etc). @@ -155,6 +156,34 @@ Matchers can be composed using `&&`, `||` and `!` operators. Currently assertions in Catch are not thread safe. For more details, along with workarounds, see the section on [the limitations page](limitations.md#thread-safe-assertions). +## Expressions with commas + +Because the preprocessor parses code using different rules than the +compiler, multiple-argument assertions (e.g. `REQUIRE_THROWS_AS`) have +problems with commas inside the provided expressions. As an example +`REQUIRE_THROWS_AS(std::pair(1, 2), std::invalid_argument);` +will fails to compile, because the preprocessor sees 3 arguments provided, +but the macro accepts only 2. There are two possible workarounds. + +1) Use typedef: +```cpp +using int_pair = std::pair; +REQUIRE_THROWS_AS(int_pair(1, 2), std::invalid_argument); +``` + +This solution is always applicable, but makes the meaning of the code +less clear. + +2) Parenthesize the expression: +```cpp +TEST_CASE_METHOD((Fixture), "foo", "[bar]") { + SUCCEED(); +} +``` + +This solution is not always applicable, because it might require extra +changes on the Catch's side to work. + --- [Home](Readme.md#top) diff --git a/include/internal/catch_test_registry.h b/include/internal/catch_test_registry.h index a192a531..f7fe3258 100644 --- a/include/internal/catch_test_registry.h +++ b/include/internal/catch_test_registry.h @@ -47,12 +47,17 @@ struct AutoReg : NonCopyable { } // end namespace Catch +#define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) +#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ +#define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ +#define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF + #if defined(CATCH_CONFIG_DISABLE) #define INTERNAL_CATCH_TESTCASE_NO_REGISTRATION( TestName, ... ) \ static void TestName() #define INTERNAL_CATCH_TESTCASE_METHOD_NO_REGISTRATION( TestName, ClassName, ... ) \ namespace{ \ - struct TestName : ClassName { \ + struct TestName : INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF ClassName) { \ void test(); \ }; \ } \ @@ -80,7 +85,7 @@ struct AutoReg : NonCopyable { #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\ CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ namespace{ \ - struct TestName : ClassName{ \ + struct TestName : INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF ClassName) { \ void test(); \ }; \ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); /* NOLINT */ \ diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 12b4f730..6247761c 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -12,6 +12,7 @@ Compilation.tests.cpp:: passed: t1 >= t2 for: {?} >= {?} Misc.tests.cpp:: passed: Compilation.tests.cpp:: passed: std::memcmp(uarr, "123", sizeof(uarr)) == 0 for: 0 == 0 with 2 messages: 'uarr := "123"' and 'sarr := "456"' Compilation.tests.cpp:: passed: std::memcmp(sarr, "456", sizeof(sarr)) == 0 for: 0 == 0 with 2 messages: 'uarr := "123"' and 'sarr := "456"' +Compilation.tests.cpp:: passed: Exception.tests.cpp:: failed: unexpected exception with message: 'answer := 42' with 1 message: 'expected exception' Exception.tests.cpp:: failed: unexpected exception with message: 'answer := 42'; expression was: thisThrows() with 1 message: 'expected exception' Exception.tests.cpp:: passed: thisThrows() with 1 message: 'answer := 42' diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index b4dd6129..79e1bf89 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1084,6 +1084,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 205 | 152 passed | 49 failed | 4 failed as expected -assertions: 1060 | 932 passed | 107 failed | 21 failed as expected +test cases: 206 | 153 passed | 49 failed | 4 failed as expected +assertions: 1061 | 933 passed | 107 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 085b2ea8..5a62613d 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -126,6 +126,15 @@ with messages: uarr := "123" sarr := "456" +------------------------------------------------------------------------------- +#1245 +------------------------------------------------------------------------------- +Compilation.tests.cpp: +............................................................................... + +Compilation.tests.cpp:: +PASSED: + ------------------------------------------------------------------------------- #748 - captures with unexpected exceptions outside assertions @@ -8941,6 +8950,6 @@ Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 205 | 139 passed | 62 failed | 4 failed as expected -assertions: 1074 | 932 passed | 121 failed | 21 failed as expected +test cases: 206 | 140 passed | 62 failed | 4 failed as expected +assertions: 1075 | 933 passed | 121 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.swa4.approved.txt b/projects/SelfTest/Baselines/console.swa4.approved.txt index b8effce5..d69844d8 100644 --- a/projects/SelfTest/Baselines/console.swa4.approved.txt +++ b/projects/SelfTest/Baselines/console.swa4.approved.txt @@ -126,6 +126,15 @@ with messages: uarr := "123" sarr := "456" +------------------------------------------------------------------------------- +#1245 +------------------------------------------------------------------------------- +Compilation.tests.cpp: +............................................................................... + +Compilation.tests.cpp:: +PASSED: + ------------------------------------------------------------------------------- #748 - captures with unexpected exceptions outside assertions @@ -332,6 +341,6 @@ with expansion: !true =============================================================================== -test cases: 13 | 10 passed | 1 failed | 2 failed as expected -assertions: 37 | 30 passed | 4 failed | 3 failed as expected +test cases: 14 | 11 passed | 1 failed | 2 failed as expected +assertions: 38 | 31 passed | 4 failed | 3 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 2691a27e..02579344 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,13 +1,14 @@ - + + expected exception diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 0c5747ac..001cb0e1 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -127,6 +127,9 @@ + + +
@@ -9889,7 +9892,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/UsageTests/Compilation.tests.cpp b/projects/SelfTest/UsageTests/Compilation.tests.cpp index 3b5f101b..2e518ef2 100644 --- a/projects/SelfTest/UsageTests/Compilation.tests.cpp +++ b/projects/SelfTest/UsageTests/Compilation.tests.cpp @@ -88,6 +88,9 @@ namespace { namespace CompilationTests { #pragma clang diagnostic pop #endif + template + struct Fixture_1245 {}; + #endif TEST_CASE("#809") { @@ -147,4 +150,8 @@ namespace { namespace CompilationTests { REQUIRE(std::memcmp(sarr, "456", sizeof(sarr)) == 0); } + TEST_CASE_METHOD((Fixture_1245), "#1245", "[compilation]") { + SUCCEED(); + } + }} // namespace CompilationTests