Debian Codegraph @

3rdparty/catch2/docs/assertions.md

raw symbols SCIP
1 <a id="top"></a>
2 # Assertion Macros
3
4 **Contents**<br>
5 [Natural Expressions](#natural-expressions)<br>
6 [Floating point comparisons](#floating-point-comparisons)<br>
7 [Exceptions](#exceptions)<br>
8 [Matcher expressions](#matcher-expressions)<br>
9 [Thread Safety](#thread-safety)<br>
10 [Expressions with commas](#expressions-with-commas)<br>
11
12 Most test frameworks have a large collection of assertion macros to capture all possible conditional forms (```_EQUALS```, ```_NOTEQUALS```, ```_GREATER_THAN``` etc).
13
14 Catch is different. Because it decomposes natural C-style conditional expressions most of these forms are reduced to one or two that you will use all the time. That said there is a rich set of auxiliary macros as well. We'll describe all of these here.
15
16 Most of these macros come in two forms:
17
18 ## Natural Expressions
19
20 The ```REQUIRE``` family of macros tests an expression and aborts the test case if it fails.
21 The ```CHECK``` family are equivalent but execution continues in the same test case even if the assertion fails. This is useful if you have a series of essentially orthogonal assertions and it is useful to see all the results rather than stopping at the first failure.
22
23 * **REQUIRE(** _expression_ **)** and
24 * **CHECK(** _expression_ **)**
25
26 Evaluates the expression and records the result. If an exception is thrown, it is caught, reported, and counted as a failure. These are the macros you will use most of the time.
27
28 Examples:
29 ```
30 CHECK( str == "string value" );
31 CHECK( thisReturnsTrue() );
32 REQUIRE( i == 42 );
33 ```
34
35 Expressions prefixed with `!` cannot be decomposed. If you have a type
36 that is convertible to bool and you want to assert that it evaluates to
37 false, use the two forms below:
38
39
40 * **REQUIRE_FALSE(** _expression_ **)** and
41 * **CHECK_FALSE(** _expression_ **)**
42
43 Note that there is no reason to use these forms for plain bool variables,
44 because there is no added value in decomposing them.
45
46 Example:
47 ```cpp
48 Status ret = someFunction();
49 REQUIRE_FALSE(ret); // ret must evaluate to false, and Catch2 will print
50 // out the value of ret if possibly
51 ```
52
53
54 ### Other limitations
55
56 Note that expressions containing either of the binary logical operators,
57 `&&` or `||`, cannot be decomposed and will not compile. The reason behind
58 this is that it is impossible to overload `&&` and `||` in a way that
59 keeps their short-circuiting semantics, and expression decomposition
60 relies on overloaded operators to work.
61
62 Simple example of an issue with overloading binary logical operators
63 is a common pointer idiom, `p && p->foo == 2`. Using the built-in `&&`
64 operator, `p` is only dereferenced if it is not null. With overloaded
65 `&&`, `p` is always dereferenced, thus causing a segfault if
66 `p == nullptr`.
67
68 If you want to test expression that contains `&&` or `||`, you have two
69 options.
70
71 1) Enclose it in parentheses. Parentheses force evaluation of the expression
72 before the expression decomposition can touch it, and thus it cannot
73 be used.
74
75 2) Rewrite the expression. `REQUIRE(a == 1 && b == 2)` can always be split
76 into `REQUIRE(a == 1); REQUIRE(b == 2);`. Alternatively, if this is a
77 common pattern in your tests, think about using [Matchers](#matcher-expressions).
78 instead. There is no simple rewrite rule for `||`, but I generally
79 believe that if you have `||` in your test expression, you should rethink
80 your tests.
81
82
83 ## Floating point comparisons
84
85 Comparing floating point numbers is complex, and [so it has its own
86 documentation page](comparing-floating-point-numbers.md#top).
87
88
89 ## Exceptions
90
91 * **REQUIRE_NOTHROW(** _expression_ **)** and
92 * **CHECK_NOTHROW(** _expression_ **)**
93
94 Expects that no exception is thrown during evaluation of the expression.
95
96 * **REQUIRE_THROWS(** _expression_ **)** and
97 * **CHECK_THROWS(** _expression_ **)**
98
99 Expects that an exception (of any type) is be thrown during evaluation of the expression.
100
101 * **REQUIRE_THROWS_AS(** _expression_, _exception type_ **)** and
102 * **CHECK_THROWS_AS(** _expression_, _exception type_ **)**
103
104 Expects that an exception of the _specified type_ is thrown during evaluation of the expression. Note that the _exception type_ is extended with `const&` and you should not include it yourself.
105
106 * **REQUIRE_THROWS_WITH(** _expression_, _string or string matcher_ **)** and
107 * **CHECK_THROWS_WITH(** _expression_, _string or string matcher_ **)**
108
109 Expects that an exception is thrown that, when converted to a string, matches the _string_ or _string matcher_ provided (see next section for Matchers).
110
111 e.g.
112 ```cpp
113 REQUIRE_THROWS_WITH( openThePodBayDoors(), Contains( "afraid" ) && Contains( "can't do that" ) );
114 REQUIRE_THROWS_WITH( dismantleHal(), "My mind is going" );
115 ```
116
117 * **REQUIRE_THROWS_MATCHES(** _expression_, _exception type_, _matcher for given exception type_ **)** and
118 * **CHECK_THROWS_MATCHES(** _expression_, _exception type_, _matcher for given exception type_ **)**
119
120 Expects that exception of _exception type_ is thrown and it matches provided matcher (see the [documentation for Matchers](matchers.md#top)).
121
122
123 _Please note that the `THROW` family of assertions expects to be passed a single expression, not a statement or series of statements. If you want to check a more complicated sequence of operations, you can use a C++11 lambda function._
124
125 ```cpp
126 REQUIRE_NOTHROW([&](){
127 int i = 1;
128 int j = 2;
129 auto k = i + j;
130 if (k == 3) {
131 throw 1;
132 }
133 }());
134 ```
135
136
137
138 ## Matcher expressions
139
140 To support Matchers a slightly different form is used. Matchers have [their own documentation](matchers.md#top).
141
142 * **REQUIRE_THAT(** _lhs_, _matcher expression_ **)** and
143 * **CHECK_THAT(** _lhs_, _matcher expression_ **)**
144
145 Matchers can be composed using `&&`, `||` and `!` operators.
146
147 ## Thread Safety
148
149 Currently assertions in Catch are not thread safe.
150 For more details, along with workarounds, see the section on [the limitations page](limitations.md#thread-safe-assertions).
151
152 ## Expressions with commas
153
154 Because the preprocessor parses code using different rules than the
155 compiler, multiple-argument assertions (e.g. `REQUIRE_THROWS_AS`) have
156 problems with commas inside the provided expressions. As an example
157 `REQUIRE_THROWS_AS(std::pair<int, int>(1, 2), std::invalid_argument);`
158 will fail to compile, because the preprocessor sees 3 arguments provided,
159 but the macro accepts only 2. There are two possible workarounds.
160
161 1) Use typedef:
162 ```cpp
163 using int_pair = std::pair<int, int>;
164 REQUIRE_THROWS_AS(int_pair(1, 2), std::invalid_argument);
165 ```
166
167 This solution is always applicable, but makes the meaning of the code
168 less clear.
169
170 2) Parenthesize the expression:
171 ```cpp
172 TEST_CASE_METHOD((Fixture<int, int>), "foo", "[bar]") {
173 SUCCEED();
174 }
175 ```
176
177 This solution is not always applicable, because it might require extra
178 changes on the Catch's side to work.
179
180 ---
181
182 [Home](Readme.md#top)