Debian Codegraph @

3rdparty/catch2/docs/other-macros.md

raw symbols SCIP
1 <a id="top"></a>
2 # Other macros
3
4 This page serves as a reference for macros that are not documented
5 elsewhere. For now, these macros are separated into 2 rough categories,
6 "assertion related macros" and "test case related macros".
7
8 ## Assertion related macros
9
10 * `CHECKED_IF` and `CHECKED_ELSE`
11
12 `CHECKED_IF( expr )` is an `if` replacement, that also applies Catch2's
13 stringification machinery to the _expr_ and records the result. As with
14 `if`, the block after a `CHECKED_IF` is entered only if the expression
15 evaluates to `true`. `CHECKED_ELSE( expr )` work similarly, but the block
16 is entered only if the _expr_ evaluated to `false`.
17
18 > `CHECKED_X` macros were changed to not count as failure in Catch2 3.0.1.
19
20 Example:
21 ```cpp
22 int a = ...;
23 int b = ...;
24 CHECKED_IF( a == b ) {
25 // This block is entered when a == b
26 } CHECKED_ELSE ( a == b ) {
27 // This block is entered when a != b
28 }
29 ```
30
31 * `CHECK_NOFAIL`
32
33 `CHECK_NOFAIL( expr )` is a variant of `CHECK` that does not fail the test
34 case if _expr_ evaluates to `false`. This can be useful for checking some
35 assumption, that might be violated without the test necessarily failing.
36
37 Example output:
38 ```
39 main.cpp:6:
40 FAILED - but was ok:
41 CHECK_NOFAIL( 1 == 2 )
42
43 main.cpp:7:
44 PASSED:
45 CHECK( 2 == 2 )
46 ```
47
48 * `SUCCEED`
49
50 `SUCCEED( msg )` is mostly equivalent with `INFO( msg ); REQUIRE( true );`.
51 In other words, `SUCCEED` is for cases where just reaching a certain line
52 means that the test has been a success.
53
54 Example usage:
55 ```cpp
56 TEST_CASE( "SUCCEED showcase" ) {
57 int I = 1;
58 SUCCEED( "I is " << I );
59 }
60 ```
61
62 * `STATIC_REQUIRE` and `STATIC_CHECK`
63
64 > `STATIC_REQUIRE` was [introduced](https://github.com/catchorg/Catch2/issues/1362) in Catch2 2.4.2.
65
66 `STATIC_REQUIRE( expr )` is a macro that can be used the same way as a
67 `static_assert`, but also registers the success with Catch2, so it is
68 reported as a success at runtime. The whole check can also be deferred
69 to the runtime, by defining `CATCH_CONFIG_RUNTIME_STATIC_REQUIRE` before
70 including the Catch2 header.
71
72 Example:
73 ```cpp
74 TEST_CASE("STATIC_REQUIRE showcase", "[traits]") {
75 STATIC_REQUIRE( std::is_void<void>::value );
76 STATIC_REQUIRE_FALSE( std::is_void<int>::value );
77 }
78 ```
79
80 > `STATIC_CHECK` was [introduced](https://github.com/catchorg/Catch2/pull/2318) in Catch2 3.0.1.
81
82 `STATIC_CHECK( expr )` is equivalent to `STATIC_REQUIRE( expr )`, with the
83 difference that when `CATCH_CONFIG_RUNTIME_STATIC_REQUIRE` is defined, it
84 becomes equivalent to `CHECK` instead of `REQUIRE`.
85
86 Example:
87 ```cpp
88 TEST_CASE("STATIC_CHECK showcase", "[traits]") {
89 STATIC_CHECK( std::is_void<void>::value );
90 STATIC_CHECK_FALSE( std::is_void<int>::value );
91 }
92 ```
93
94 ## Test case related macros
95
96 * `METHOD_AS_TEST_CASE`
97
98 `METHOD_AS_TEST_CASE( member-function-pointer, description )` lets you
99 register a member function of a class as a Catch2 test case. The class
100 will be separately instantiated for each method registered in this way.
101
102 ```cpp
103 class TestClass {
104 std::string s;
105
106 public:
107 TestClass()
108 :s( "hello" )
109 {}
110
111 void testCase() {
112 REQUIRE( s == "hello" );
113 }
114 };
115
116
117 METHOD_AS_TEST_CASE( TestClass::testCase, "Use class's method as a test case", "[class]" )
118 ```
119
120 * `REGISTER_TEST_CASE`
121
122 `REGISTER_TEST_CASE( function, description )` let's you register
123 a `function` as a test case. The function has to have `void()` signature,
124 the description can contain both name and tags.
125
126 Example:
127 ```cpp
128 REGISTER_TEST_CASE( someFunction, "ManuallyRegistered", "[tags]" );
129 ```
130
131 _Note that the registration still has to happen before Catch2's session
132 is initiated. This means that it either needs to be done in a global
133 constructor, or before Catch2's session is created in user's own main._
134
135
136 * `DYNAMIC_SECTION`
137
138 > Introduced in Catch2 2.3.0.
139
140 `DYNAMIC_SECTION` is a `SECTION` where the user can use `operator<<` to
141 create the final name for that section. This can be useful with e.g.
142 generators, or when creating a `SECTION` dynamically, within a loop.
143
144 Example:
145 ```cpp
146 TEST_CASE( "looped SECTION tests" ) {
147 int a = 1;
148
149 for( int b = 0; b < 10; ++b ) {
150 DYNAMIC_SECTION( "b is currently: " << b ) {
151 CHECK( b > a );
152 }
153 }
154 }
155 ```