Add STATIC_CHECK and STATIC_CHECK_FALSE (#2318)

This commit is contained in:
Morwenn 2021-11-15 00:28:27 +01:00 committed by GitHub
parent edc2f6e8a3
commit f41d761674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 7 deletions

View File

@ -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`

View File

@ -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_ ) )

View File

@ -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("");
}

View File

@ -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__)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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] *"/>

View File

@ -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

View File

@ -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>

View File

@ -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]") {