Skip to content

Commit

Permalink
Add STATIC_CHECK and STATIC_CHECK_FALSE (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Nov 14, 2021
1 parent edc2f6e commit f41d761
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 7 deletions.
18 changes: 16 additions & 2 deletions docs/other-macros.md
Expand Up @@ -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
Expand All @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions src/catch2/catch_test_macros.hpp
Expand Up @@ -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


Expand Down Expand Up @@ -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_ ))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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_ ) )
Expand Down
2 changes: 2 additions & 0 deletions tests/ExtraTests/X01-PrefixedMacros.cpp
Expand Up @@ -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("");
}

Expand Down
4 changes: 4 additions & 0 deletions tests/ExtraTests/X02-DisabledMacros.cpp
Expand Up @@ -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__)
Expand Down
2 changes: 2 additions & 0 deletions tests/SelfTest/Baselines/compact.sw.approved.txt
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/SelfTest/Baselines/console.std.approved.txt
Expand Up @@ -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

10 changes: 9 additions & 1 deletion tests/SelfTest/Baselines/console.sw.approved.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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

2 changes: 1 addition & 1 deletion 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] *"/>
Expand Down
6 changes: 5 additions & 1 deletion tests/SelfTest/Baselines/tap.sw.approved.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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

2 changes: 1 addition & 1 deletion tests/SelfTest/Baselines/xml.sw.approved.txt
Expand Up @@ -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>
2 changes: 2 additions & 0 deletions tests/SelfTest/UsageTests/Compilation.tests.cpp
Expand Up @@ -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]") {
Expand Down

0 comments on commit f41d761

Please sign in to comment.