From f41d76167431eeb0ae8e82703e5597769c0531b8 Mon Sep 17 00:00:00 2001 From: Morwenn Date: Mon, 15 Nov 2021 00:28:27 +0100 Subject: [PATCH] Add STATIC_CHECK and STATIC_CHECK_FALSE (#2318) --- docs/other-macros.md | 18 ++++++++++++++++-- src/catch2/catch_test_macros.hpp | 12 ++++++++++++ tests/ExtraTests/X01-PrefixedMacros.cpp | 2 ++ tests/ExtraTests/X02-DisabledMacros.cpp | 4 ++++ .../SelfTest/Baselines/compact.sw.approved.txt | 2 ++ .../Baselines/console.std.approved.txt | 2 +- .../SelfTest/Baselines/console.sw.approved.txt | 10 +++++++++- tests/SelfTest/Baselines/junit.sw.approved.txt | 2 +- tests/SelfTest/Baselines/tap.sw.approved.txt | 6 +++++- tests/SelfTest/Baselines/xml.sw.approved.txt | 2 +- .../SelfTest/UsageTests/Compilation.tests.cpp | 2 ++ 11 files changed, 55 insertions(+), 7 deletions(-) diff --git a/docs/other-macros.md b/docs/other-macros.md index 5e7f0af09f..ce33c1d851 100644 --- a/docs/other-macros.md +++ b/docs/other-macros.md @@ -59,9 +59,9 @@ TEST_CASE( "SUCCEED showcase" ) { } ``` -* `STATIC_REQUIRE` +* `STATIC_REQUIRE` and `STATIC_CHECK` -> [Introduced](https://github.com/catchorg/Catch2/issues/1362) in Catch2 2.4.2. +> `STATIC_REQUIRE` was [introduced](https://github.com/catchorg/Catch2/issues/1362) in Catch2 2.4.2. `STATIC_REQUIRE( expr )` is a macro that can be used the same way as a `static_assert`, but also registers the success with Catch2, so it is @@ -77,6 +77,20 @@ TEST_CASE("STATIC_REQUIRE showcase", "[traits]") { } ``` +> `STATIC_CHECK` was [introduced](https://github.com/catchorg/Catch2/pull/2318) in Catch2 X.Y.Z. + +`STATIC_CHECK( expr )` is equivalent to `STATIC_REQUIRE( expr )`, with the +difference that when `CATCH_CONFIG_RUNTIME_STATIC_REQUIRE` is defined, it +becomes equivalent to `CHECK` instead of `REQUIRE`. + +Example: +```cpp +TEST_CASE("STATIC_CHECK showcase", "[traits]") { + STATIC_CHECK( std::is_void::value ); + STATIC_CHECK_FALSE( std::is_void::value ); +} +``` + ## Test case related macros * `METHOD_AS_TEST_CASE` diff --git a/src/catch2/catch_test_macros.hpp b/src/catch2/catch_test_macros.hpp index 918fd4bfff..792fdce96e 100644 --- a/src/catch2/catch_test_macros.hpp +++ b/src/catch2/catch_test_macros.hpp @@ -54,9 +54,13 @@ #if !defined(CATCH_CONFIG_RUNTIME_STATIC_REQUIRE) #define CATCH_STATIC_REQUIRE( ... ) static_assert( __VA_ARGS__ , #__VA_ARGS__ ); CATCH_SUCCEED( #__VA_ARGS__ ) #define CATCH_STATIC_REQUIRE_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); CATCH_SUCCEED( #__VA_ARGS__ ) + #define CATCH_STATIC_CHECK( ... ) static_assert( __VA_ARGS__ , #__VA_ARGS__ ); CATCH_SUCCEED( #__VA_ARGS__ ) + #define CATCH_STATIC_CHECK_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); CATCH_SUCCEED( #__VA_ARGS__ ) #else #define CATCH_STATIC_REQUIRE( ... ) CATCH_REQUIRE( __VA_ARGS__ ) #define CATCH_STATIC_REQUIRE_FALSE( ... ) CATCH_REQUIRE_FALSE( __VA_ARGS__ ) + #define CATCH_STATIC_CHECK( ... ) CATCH_CHECK( __VA_ARGS__ ) + #define CATCH_STATIC_CHECK_FALSE( ... ) CATCH_CHECK_FALSE( __VA_ARGS__ ) #endif @@ -101,6 +105,8 @@ #define CATCH_STATIC_REQUIRE( ... ) (void)(0) #define CATCH_STATIC_REQUIRE_FALSE( ... ) (void)(0) + #define CATCH_STATIC_CHECK( ... ) (void)(0) + #define CATCH_STATIC_CHECK_FALSE( ... ) (void)(0) // "BDD-style" convenience wrappers #define CATCH_SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ )) @@ -145,9 +151,13 @@ #if !defined(CATCH_CONFIG_RUNTIME_STATIC_REQUIRE) #define STATIC_REQUIRE( ... ) static_assert( __VA_ARGS__, #__VA_ARGS__ ); SUCCEED( #__VA_ARGS__ ) #define STATIC_REQUIRE_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); SUCCEED( "!(" #__VA_ARGS__ ")" ) + #define STATIC_CHECK( ... ) static_assert( __VA_ARGS__, #__VA_ARGS__ ); SUCCEED( #__VA_ARGS__ ) + #define STATIC_CHECK_FALSE( ... ) static_assert( !(__VA_ARGS__), "!(" #__VA_ARGS__ ")" ); SUCCEED( "!(" #__VA_ARGS__ ")" ) #else #define STATIC_REQUIRE( ... ) REQUIRE( __VA_ARGS__ ) #define STATIC_REQUIRE_FALSE( ... ) REQUIRE_FALSE( __VA_ARGS__ ) + #define STATIC_CHECK( ... ) CHECK( __VA_ARGS__ ) + #define STATIC_CHECK_FALSE( ... ) CHECK_FALSE( __VA_ARGS__ ) #endif // "BDD-style" convenience wrappers @@ -191,6 +201,8 @@ #define STATIC_REQUIRE( ... ) (void)(0) #define STATIC_REQUIRE_FALSE( ... ) (void)(0) + #define STATIC_CHECK( ... ) (void)(0) + #define STATIC_CHECK_FALSE( ... ) (void)(0) // "BDD-style" convenience wrappers #define SCENARIO( ... ) INTERNAL_CATCH_TESTCASE_NO_REGISTRATION(INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ) ) diff --git a/tests/ExtraTests/X01-PrefixedMacros.cpp b/tests/ExtraTests/X01-PrefixedMacros.cpp index 43452d7669..9110bbba5b 100644 --- a/tests/ExtraTests/X01-PrefixedMacros.cpp +++ b/tests/ExtraTests/X01-PrefixedMacros.cpp @@ -62,6 +62,8 @@ CATCH_TEST_CASE("PrefixedMacros") { CATCH_STATIC_REQUIRE( std::is_void::value ); CATCH_STATIC_REQUIRE_FALSE( std::is_void::value ); + CATCH_STATIC_CHECK( std::is_void::value ); + CATCH_STATIC_CHECK_FALSE( std::is_void::value ); CATCH_FAIL(""); } diff --git a/tests/ExtraTests/X02-DisabledMacros.cpp b/tests/ExtraTests/X02-DisabledMacros.cpp index c2d997c9cc..5a756d701e 100644 --- a/tests/ExtraTests/X02-DisabledMacros.cpp +++ b/tests/ExtraTests/X02-DisabledMacros.cpp @@ -32,6 +32,10 @@ foo f; TEST_CASE( "Disabled Macros" ) { std::cout << "This should not happen\n"; FAIL(); + + // Test that static assertions don't fire when macros are disabled + STATIC_CHECK( 0 == 1 ); + STATIC_REQUIRE( !true ); } #if defined(__clang__) diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index e0dac42263..3cd8ba4c28 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -948,6 +948,8 @@ Tricky.tests.cpp:: passed: !False for: true Tricky.tests.cpp:: passed: !(False) for: !{?} Compilation.tests.cpp:: passed: with 1 message: 'std::is_void::value' Compilation.tests.cpp:: passed: with 1 message: '!(std::is_void::value)' +Compilation.tests.cpp:: passed: with 1 message: 'std::is_void::value' +Compilation.tests.cpp:: passed: with 1 message: '!(std::is_void::value)' Condition.tests.cpp:: failed: data.int_seven > 7 for: 7 > 7 Condition.tests.cpp:: failed: data.int_seven < 7 for: 7 < 7 Condition.tests.cpp:: failed: data.int_seven > 8 for: 7 > 8 diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index db80f75dd9..17f9d28159 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1427,5 +1427,5 @@ due to unexpected exception with message: =============================================================================== test cases: 376 | 299 passed | 70 failed | 7 failed as expected -assertions: 2147 | 1991 passed | 129 failed | 27 failed as expected +assertions: 2149 | 1993 passed | 129 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index 74c0df93c6..32654bc3e6 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -7312,6 +7312,14 @@ Optionally static assertions Compilation.tests.cpp: ............................................................................... +Compilation.tests.cpp:: PASSED: +with message: + std::is_void::value + +Compilation.tests.cpp:: PASSED: +with message: + !(std::is_void::value) + Compilation.tests.cpp:: PASSED: with message: std::is_void::value @@ -17268,5 +17276,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 376 | 283 passed | 86 failed | 7 failed as expected -assertions: 2164 | 1991 passed | 146 failed | 27 failed as expected +assertions: 2166 | 1993 passed | 146 failed | 27 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index f4469f58d2..0047eb703d 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 405d11081d..087064dfd2 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -1878,6 +1878,10 @@ ok {test-number} - !(False) for: !{?} ok {test-number} - with 1 message: 'std::is_void::value' # Optionally static assertions ok {test-number} - with 1 message: '!(std::is_void::value)' +# Optionally static assertions +ok {test-number} - with 1 message: 'std::is_void::value' +# Optionally static assertions +ok {test-number} - with 1 message: '!(std::is_void::value)' # Ordering comparison checks that should fail not ok {test-number} - data.int_seven > 7 for: 7 > 7 # Ordering comparison checks that should fail @@ -4330,5 +4334,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..2164 +1..2166 diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 2b5e3378d8..008cd1393d 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -20238,6 +20238,6 @@ loose text artifact - + diff --git a/tests/SelfTest/UsageTests/Compilation.tests.cpp b/tests/SelfTest/UsageTests/Compilation.tests.cpp index c40c09f81a..866644e372 100644 --- a/tests/SelfTest/UsageTests/Compilation.tests.cpp +++ b/tests/SelfTest/UsageTests/Compilation.tests.cpp @@ -172,6 +172,8 @@ TEST_CASE("#1403", "[compilation]") { TEST_CASE("Optionally static assertions", "[compilation]") { STATIC_REQUIRE( std::is_void::value ); STATIC_REQUIRE_FALSE( std::is_void::value ); + STATIC_CHECK( std::is_void::value ); + STATIC_CHECK_FALSE( std::is_void::value ); } TEST_CASE("#1548", "[compilation]") {