assertions.md (10771B)
1 ## Assertion macros 2 3 Most test frameworks have a large collection of assertion macros to capture all possible conditional forms (```_EQUALS```, ```_NOTEQUALS```, ```_GREATER_THAN``` etc). 4 5 **doctest** is different (but it's like [**Catch**](https://github.com/catchorg/Catch2) in this regard). Because it decomposes comparison 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. 6 7 There are 3 levels of assert severity for all assertion macros: 8 9 - ```REQUIRE``` - this level will immediately quit the test case if the assert fails and will mark the test case as failed. 10 - ```CHECK``` - this level will mark the test case as failed if the assert fails but will continue with the test case. 11 - ```WARN``` - this level will only print a message if the assert fails but will not mark the test case as failed. 12 13 The ```CHECK``` level is mostly 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. 14 15 All asserts evaluate the expressions only once and if they fail - the values are [**stringified**](stringification.md) properly. 16 17 Since **doctest** is [**thread-safe**](faq.md#is-doctest-thread-aware) all asserts and [**logging**](logging.md) macros can be used in threads spawned from test cases. 18 19 Note that the ```REQUIRE``` level of asserts uses exceptions to end the current test case. It might be dangerous to use this level of asserts inside destructors of user-defined classes - if a destructor is called during stack unwinding due to an exception and a ```REQUIRE``` assert fails then the program will terminate. Also since C++11 all destructors are by default ```noexcept(true)``` unless specified otherwise so such an assert will lead to ```std::terminate()``` being called. 20 21 ## Expression decomposing asserts 22 23 These are of the form ```CHECK(expression)``` (Same for ```REQUIRE``` and ```WARN```). 24 25 ```expression``` can be a binary comparison like ```a == b``` or just a single thing like ```vec.isEmpty()```. 26 27 If an exception is thrown it is caught, reported, and counted as a failure (unless the assert is of level ```WARN```). 28 29 Examples: 30 31 ```c++ 32 CHECK(flags == state::alive | state::moving); 33 CHECK(thisReturnsTrue()); 34 REQUIRE(i < 42); 35 ``` 36 37 - Negating asserts - ```<LEVEL>_FALSE(expression)``` - evaluates the expression and records the _logical NOT_ of the result. 38 39 These forms exist as a workaround for the fact that ```!``` prefixed expressions cannot be decomposed properly. 40 41 Example: 42 43 ```c++ 44 REQUIRE_FALSE(thisReturnsFalse()); 45 ``` 46 47 - Using the [**```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**](configuration.md#doctest_config_super_fast_asserts) config option can make compilation of asserts up to [**31-63%**](benchmarks.md#cost-of-an-assertion-macro) faster! 48 - These asserts also have a ```_MESSAGE``` form - like ```CHECK_MESSAGE(expression, message)``` which is basically a code block ```{}``` with a scoped [**```INFO()```**](logging.md#info) logging macro together with the ```CHECK``` macro - that way the message will be relevant only to that assert. The binary/unary asserts don't have this variation yet. 49 50 Examples: 51 52 ```c++ 53 INFO("this is relevant to all asserts, and here is some var: ", local); 54 55 CHECK_MESSAGE(a < b, "relevant only to this assert ", other_local, " more text!"); 56 57 CHECK(b < c); // here only the first INFO() will be relevant 58 ``` 59 60 For more information about the ```INFO()``` macro visit the [logging page](logging.md). 61 62 ## Binary and unary asserts 63 64 These asserts don't use templates to decompose the comparison expressions for the left and right parts. 65 66 These have the same guarantees as the expression decomposing ones but [**57-68% faster**](benchmarks.md#cost-of-an-assertion-macro) for compilation. 67 68 ```<LEVEL>``` is one of 3 possible: ```REQUIRE```/```CHECK```/```WARN```. 69 70 - ```<LEVEL>_EQ(left, right)``` - same as ```<LEVEL>(left == right)``` 71 - ```<LEVEL>_NE(left, right)``` - same as ```<LEVEL>(left != right)``` 72 - ```<LEVEL>_GT(left, right)``` - same as ```<LEVEL>(left > right)``` 73 - ```<LEVEL>_LT(left, right)``` - same as ```<LEVEL>(left < right)``` 74 - ```<LEVEL>_GE(left, right)``` - same as ```<LEVEL>(left >= right)``` 75 - ```<LEVEL>_LE(left, right)``` - same as ```<LEVEL>(left <= right)``` 76 - ```<LEVEL>_UNARY(expr)``` - same as ```<LEVEL>(expr)``` 77 - ```<LEVEL>_UNARY_FALSE(expr)``` - same as ```<LEVEL>_FALSE(expr)``` 78 79 Using the [**```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**](configuration.md#doctest_config_super_fast_asserts) config option can make the binary asserts to compile up to [**84-91%**](benchmarks.md#cost-of-an-assertion-macro) faster! 80 81 ## Exceptions 82 83 ```<LEVEL>``` is one of 3 possible: ```REQUIRE```/```CHECK```/```WARN```. 84 85 - ```<LEVEL>_THROWS(expression)``` 86 87 Expects that an exception (of any type) is thrown during evaluation of the expression. 88 89 - ```<LEVEL>_THROWS_AS(expression, exception_type)``` 90 91 Expects that an exception of the _specified type_ is thrown during evaluation of the expression. 92 93 Note that ```const``` and ```&``` are added to the exception type if missing (users shouldn't care) - the standard practice for exceptions in C++ is ```Throw by value, catch by (const) reference```. 94 95 ```c++ 96 CHECK_THROWS_AS(func(), const std::exception&); 97 CHECK_THROWS_AS(func(), std::exception); // same as above 98 ``` 99 100 - ```<LEVEL>_THROWS_WITH(expression, c_string)``` 101 102 Expects that an exception is thrown during evaluation of the expression and is successfully translated to the _specified c string_ (see [**translating exceptions**](stringification.md#translating-exceptions)). 103 104 ```c++ 105 CHECK_THROWS_WITH(func(), "invalid operation!"); 106 ``` 107 108 - ```<LEVEL>_THROWS_WITH_AS(expression, c_string, exception_type)``` 109 110 This is a combination of ```<LEVEL>_THROWS_WITH``` and ```<LEVEL>_THROWS_AS```. 111 112 ```c++ 113 CHECK_THROWS_WITH_AS(func(), "invalid operation!", std::runtime_error); 114 ``` 115 116 - ```<LEVEL>_NOTHROW(expression)``` 117 118 Expects that no exception is thrown during evaluation of the expression. 119 120 Note that these asserts also have a ```_MESSAGE``` form - like ```CHECK_THROWS_MESSAGE(expression, message)``` - these work identically to the ```_MESSAGE``` form of the normal macros (```CHECK_MESSAGE(a < b, "this shouldn't fail")```) described earlier. 121 122 One may use the [**```DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS```**](configuration.md#doctest_config_void_cast_expressions) config identifier to cast the expression in these asserts to void to avoid warnings or other issues - for example nodiscard statements whose result isn't checked. This will however limit the ability to write entire ```{}``` blocks of code as the expression (or multiple statements) but in that case a simple lambda can be used. This should have been the default behavior from day 1 of the framework... 123 124 ## Using asserts out of a testing context 125 126 Asserts can be used outside of a testing context (in code not called from a ```TEST_CASE()```) instead of [```assert()```](https://en.cppreference.com/w/cpp/error/assert). 127 128 A ```doctest::Context``` object still has to be created somewhere and set as the default one using the ```setAsDefaultForAssertsOutOfTestCases()``` method - and then asserts will work. A handler can be registered by calling the ```setAssertHandler()``` method on the context object. If no handler is set then ```std::abort()``` is called on failure. 129 130 The results would be best when using the [**```DOCTEST_CONFIG_SUPER_FAST_ASSERTS```**](configuration.md#doctest_config_super_fast_asserts) config identifier. 131 132 Checkout the [**example**](../../examples/all_features/asserts_used_outside_of_tests.cpp) showcasing how that is done. For more information see the [**issue for the feature request**](https://github.com/doctest/doctest/issues/114). 133 134 Currently [**logging macros**](logging.md) cannot be used for extra context for asserts outside of a test run. That means that the ```_MESSAGE``` variants of asserts are also not usable - since they are just a packed ```INFO()``` with an assert right after it. 135 136 ## String containment 137 138 ```doctest::Contains``` can be used to check whether the string passed to its constructor is contained within the string it is compared with. Here's a simple example: 139 140 ```c++ 141 REQUIRE("foobar" == doctest::Contains("foo")); 142 ``` 143 144 It can also be used with the ```THROWS_WITH``` family of assertion macros to check whether the thrown exception [translated to a string](stringification.md#translating-exceptions) contains the provided string. Here's another example: 145 ```c++ 146 REQUIRE_THROWS_WITH(func(), doctest::Contains("Oopsie")); 147 ``` 148 149 ## Floating point comparisons 150 151 When comparing floating point numbers - especially if at least one of them has been computed - great care must be taken to allow for rounding errors and inexact representations. 152 153 **doctest** provides a way to perform tolerant comparisons of floating point values through the use of a wrapper class called ```doctest::Approx```. ```doctest::Approx``` can be used on either side of a comparison expression. It overloads the comparisons operators to take a relative tolerance into account. Here's a simple example: 154 155 ```c++ 156 REQUIRE(performComputation() == doctest::Approx(2.1)); 157 ``` 158 159 By default a small epsilon value (relative - in percentages) is used that covers many simple cases of rounding errors. When this is insufficient the epsilon value (the amount within which a difference either way is ignored) can be specified by calling the ```epsilon()``` method on the ```doctest::Approx``` instance. e.g.: 160 161 ```c++ 162 REQUIRE(22.0/7 == doctest::Approx(3.141).epsilon(0.01)); // allow for a 1% error 163 ``` 164 165 When dealing with very large or very small numbers it can be useful to specify a scale, which can be achieved by calling the ```scale()``` method on the ```doctest::Approx``` instance. 166 167 ## NaN checking 168 169 Two NaN floating point numbers do not compare equal to each other. This makes it quite inconvenient to check for NaN while capturing the value. 170 ```c++ 171 CHECK(std::isnan(performComputation()); // does not capture the result of the call 172 ``` 173 174 **doctest** provides `doctest::IsNaN` which can be used in assertions to check if a float (or any other floating point fundamental type) is indeed NaN, outputting the actual value if it is not. 175 ```c++ 176 CHECK(doctest::IsNaN(performComputation()); // captures the result! 177 ``` 178 179 `IsNaN` is able to capture the value, even if negated via `!`. 180 181 -------- 182 183 - Check out the [**example**](../../examples/all_features/assertion_macros.cpp) which shows many of these macros 184 - Do not wrap assertion macros in ```try```/```catch``` - the REQUIRE macros throw exceptions to end the test case execution! 185 186 --------------- 187 188 [Home](readme.md#reference) 189 190 <p align="center"><img src="../../scripts/data/logo/icon_2.svg"></p>