Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Add STATIC_CHECK and STATIC_CHECK_FALSE (#2318)

Commit
f41d76167431eeb0ae8e82703e5597769c0531b8
Author
Morwenn ·
Committer
GitHub
Parents
edc2f6e8a35b

Files · Download .tar.gz

Changes

 docs/other-macros.md                              | 18 ++++++++++++++++--
 src/catch2/catch_test_macros.hpp                  | 12 ++++++++++++
 tests/ExtraTests/X01-PrefixedMacros.cpp           |  2 ++
 tests/ExtraTests/X02-DisabledMacros.cpp           |  4 ++++
 tests/SelfTest/Baselines/compact.sw.approved.txt  |  2 ++
 tests/SelfTest/Baselines/console.std.approved.txt |  2 +-
 tests/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 +-
 tests/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 5e7f0af0..ce33c1d8 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<void>::value );
+    STATIC_CHECK_FALSE( std::is_void<int>::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 918fd4bf..792fdce9 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 43452d76..9110bbba 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<void>::value );
     CATCH_STATIC_REQUIRE_FALSE( std::is_void<int>::value );
+    CATCH_STATIC_CHECK( std::is_void<void>::value );
+    CATCH_STATIC_CHECK_FALSE( std::is_void<int>::value );
     CATCH_FAIL("");
 }
 
diff --git a/tests/ExtraTests/X02-DisabledMacros.cpp b/tests/ExtraTests/X02-DisabledMacros.cpp
index c2d997c9..5a756d70 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 e0dac422..3cd8ba4c 100644
--- a/tests/SelfTest/Baselines/compact.sw.approved.txt
+++ b/tests/SelfTest/Baselines/compact.sw.approved.txt
@@ -948,6 +948,8 @@ Tricky.tests.cpp:<line number>: passed: !False for: true
 Tricky.tests.cpp:<line number>: passed: !(False) for: !{?}
 Compilation.tests.cpp:<line number>: passed: with 1 message: 'std::is_void<void>::value'
 Compilation.tests.cpp:<line number>: passed: with 1 message: '!(std::is_void<int>::value)'
+Compilation.tests.cpp:<line number>: passed: with 1 message: 'std::is_void<void>::value'
+Compilation.tests.cpp:<line number>: passed: with 1 message: '!(std::is_void<int>::value)'
 Condition.tests.cpp:<line number>: failed: data.int_seven > 7 for: 7 > 7
 Condition.tests.cpp:<line number>: failed: data.int_seven < 7 for: 7 < 7
 Condition.tests.cpp:<line number>: 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 db80f75d..17f9d281 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 74c0df93..32654bc3 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:<line number>
 ...............................................................................
 
+Compilation.tests.cpp:<line number>: PASSED:
+with message:
+  std::is_void<void>::value
+
+Compilation.tests.cpp:<line number>: PASSED:
+with message:
+  !(std::is_void<int>::value)
+
 Compilation.tests.cpp:<line number>: PASSED:
 with message:
   std::is_void<void>::value
@@ -17268,5 +17276,5 @@ Misc.tests.cpp:<line number>: 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 f4469f58..0047eb70 100644
--- a/tests/SelfTest/Baselines/junit.sw.approved.txt
+++ b/tests/SelfTest/Baselines/junit.sw.approved.txt
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuitesloose text artifact
 >
-  <testsuite name="<exe-name>" errors="17" failures="129" tests="2164" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
+  <testsuite name="<exe-name>" errors="17" failures="129" tests="2166" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
     <properties>
       <property name="random-seed" value="1"/>
       <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt
index 405d1108..087064df 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<void>::value'
 # Optionally static assertions
 ok {test-number} - with 1 message: '!(std::is_void<int>::value)'
+# Optionally static assertions
+ok {test-number} - with 1 message: 'std::is_void<void>::value'
+# Optionally static assertions
+ok {test-number} - with 1 message: '!(std::is_void<int>::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 2b5e3378..008cd139 100644
--- a/tests/SelfTest/Baselines/xml.sw.approved.txt
+++ b/tests/SelfTest/Baselines/xml.sw.approved.txt
@@ -20238,6 +20238,6 @@ loose text artifact
     </Section>
     <OverallResult success="true"/>
   </TestCase>
-  <OverallResults successes="1991" failures="146" expectedFailures="27"/>
+  <OverallResults successes="1993" failures="146" expectedFailures="27"/>
   <OverallResultsCases successes="283" failures="86" expectedFailures="7"/>
 </Catch2TestRun>
diff --git a/tests/SelfTest/UsageTests/Compilation.tests.cpp b/tests/SelfTest/UsageTests/Compilation.tests.cpp
index c40c09f8..866644e3 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<void>::value );
     STATIC_REQUIRE_FALSE( std::is_void<int>::value );
+    STATIC_CHECK( std::is_void<void>::value );
+    STATIC_CHECK_FALSE( std::is_void<int>::value );
 }
 
 TEST_CASE("#1548", "[compilation]") {