xref: /freebsd-src/contrib/googletest/docs/reference/assertions.md (revision 5ca8c28cd8c725b81781201cfdb5f9969396f934)
128f6c2f2SEnji Cooper# Assertions Reference
228f6c2f2SEnji Cooper
328f6c2f2SEnji CooperThis page lists the assertion macros provided by GoogleTest for verifying code
4*5ca8c28cSEnji Cooperbehavior. To use them, add `#include <gtest/gtest.h>`.
528f6c2f2SEnji Cooper
628f6c2f2SEnji CooperThe majority of the macros listed below come as a pair with an `EXPECT_` variant
728f6c2f2SEnji Cooperand an `ASSERT_` variant. Upon failure, `EXPECT_` macros generate nonfatal
828f6c2f2SEnji Cooperfailures and allow the current function to continue running, while `ASSERT_`
928f6c2f2SEnji Coopermacros generate fatal failures and abort the current function.
1028f6c2f2SEnji Cooper
1128f6c2f2SEnji CooperAll assertion macros support streaming a custom failure message into them with
1228f6c2f2SEnji Cooperthe `<<` operator, for example:
1328f6c2f2SEnji Cooper
1428f6c2f2SEnji Cooper```cpp
1528f6c2f2SEnji CooperEXPECT_TRUE(my_condition) << "My condition is not true";
1628f6c2f2SEnji Cooper```
1728f6c2f2SEnji Cooper
1828f6c2f2SEnji CooperAnything that can be streamed to an `ostream` can be streamed to an assertion
1928f6c2f2SEnji Coopermacro—in particular, C strings and string objects. If a wide string (`wchar_t*`,
2028f6c2f2SEnji Cooper`TCHAR*` in `UNICODE` mode on Windows, or `std::wstring`) is streamed to an
2128f6c2f2SEnji Cooperassertion, it will be translated to UTF-8 when printed.
2228f6c2f2SEnji Cooper
2328f6c2f2SEnji Cooper## Explicit Success and Failure {#success-failure}
2428f6c2f2SEnji Cooper
2528f6c2f2SEnji CooperThe assertions in this section generate a success or failure directly instead of
2628f6c2f2SEnji Coopertesting a value or expression. These are useful when control flow, rather than a
2728f6c2f2SEnji CooperBoolean expression, determines the test's success or failure, as shown by the
2828f6c2f2SEnji Cooperfollowing example:
2928f6c2f2SEnji Cooper
3028f6c2f2SEnji Cooper```c++
3128f6c2f2SEnji Cooperswitch(expression) {
3228f6c2f2SEnji Cooper  case 1:
3328f6c2f2SEnji Cooper    ... some checks ...
3428f6c2f2SEnji Cooper  case 2:
3528f6c2f2SEnji Cooper    ... some other checks ...
3628f6c2f2SEnji Cooper  default:
3728f6c2f2SEnji Cooper    FAIL() << "We shouldn't get here.";
3828f6c2f2SEnji Cooper}
3928f6c2f2SEnji Cooper```
4028f6c2f2SEnji Cooper
4128f6c2f2SEnji Cooper### SUCCEED {#SUCCEED}
4228f6c2f2SEnji Cooper
4328f6c2f2SEnji Cooper`SUCCEED()`
4428f6c2f2SEnji Cooper
4528f6c2f2SEnji CooperGenerates a success. This *does not* make the overall test succeed. A test is
4628f6c2f2SEnji Cooperconsidered successful only if none of its assertions fail during its execution.
4728f6c2f2SEnji Cooper
4828f6c2f2SEnji CooperThe `SUCCEED` assertion is purely documentary and currently doesn't generate any
4928f6c2f2SEnji Cooperuser-visible output. However, we may add `SUCCEED` messages to GoogleTest output
5028f6c2f2SEnji Cooperin the future.
5128f6c2f2SEnji Cooper
5228f6c2f2SEnji Cooper### FAIL {#FAIL}
5328f6c2f2SEnji Cooper
5428f6c2f2SEnji Cooper`FAIL()`
5528f6c2f2SEnji Cooper
5628f6c2f2SEnji CooperGenerates a fatal failure, which returns from the current function.
5728f6c2f2SEnji Cooper
5828f6c2f2SEnji CooperCan only be used in functions that return `void`. See
5928f6c2f2SEnji Cooper[Assertion Placement](../advanced.md#assertion-placement) for more information.
6028f6c2f2SEnji Cooper
6128f6c2f2SEnji Cooper### ADD_FAILURE {#ADD_FAILURE}
6228f6c2f2SEnji Cooper
6328f6c2f2SEnji Cooper`ADD_FAILURE()`
6428f6c2f2SEnji Cooper
6528f6c2f2SEnji CooperGenerates a nonfatal failure, which allows the current function to continue
6628f6c2f2SEnji Cooperrunning.
6728f6c2f2SEnji Cooper
6828f6c2f2SEnji Cooper### ADD_FAILURE_AT {#ADD_FAILURE_AT}
6928f6c2f2SEnji Cooper
7028f6c2f2SEnji Cooper`ADD_FAILURE_AT(`*`file_path`*`,`*`line_number`*`)`
7128f6c2f2SEnji Cooper
7228f6c2f2SEnji CooperGenerates a nonfatal failure at the file and line number specified.
7328f6c2f2SEnji Cooper
7428f6c2f2SEnji Cooper## Generalized Assertion {#generalized}
7528f6c2f2SEnji Cooper
7628f6c2f2SEnji CooperThe following assertion allows [matchers](matchers.md) to be used to verify
7728f6c2f2SEnji Coopervalues.
7828f6c2f2SEnji Cooper
7928f6c2f2SEnji Cooper### EXPECT_THAT {#EXPECT_THAT}
8028f6c2f2SEnji Cooper
8128f6c2f2SEnji Cooper`EXPECT_THAT(`*`value`*`,`*`matcher`*`)` \
8228f6c2f2SEnji Cooper`ASSERT_THAT(`*`value`*`,`*`matcher`*`)`
8328f6c2f2SEnji Cooper
8428f6c2f2SEnji CooperVerifies that *`value`* matches the [matcher](matchers.md) *`matcher`*.
8528f6c2f2SEnji Cooper
8628f6c2f2SEnji CooperFor example, the following code verifies that the string `value1` starts with
8728f6c2f2SEnji Cooper`"Hello"`, `value2` matches a regular expression, and `value3` is between 5 and
8828f6c2f2SEnji Cooper10:
8928f6c2f2SEnji Cooper
9028f6c2f2SEnji Cooper```cpp
9128f6c2f2SEnji Cooper#include <gmock/gmock.h>
9228f6c2f2SEnji Cooper
9328f6c2f2SEnji Cooperusing ::testing::AllOf;
9428f6c2f2SEnji Cooperusing ::testing::Gt;
9528f6c2f2SEnji Cooperusing ::testing::Lt;
9628f6c2f2SEnji Cooperusing ::testing::MatchesRegex;
9728f6c2f2SEnji Cooperusing ::testing::StartsWith;
9828f6c2f2SEnji Cooper
9928f6c2f2SEnji Cooper...
10028f6c2f2SEnji CooperEXPECT_THAT(value1, StartsWith("Hello"));
10128f6c2f2SEnji CooperEXPECT_THAT(value2, MatchesRegex("Line \\d+"));
10228f6c2f2SEnji CooperASSERT_THAT(value3, AllOf(Gt(5), Lt(10)));
10328f6c2f2SEnji Cooper```
10428f6c2f2SEnji Cooper
10528f6c2f2SEnji CooperMatchers enable assertions of this form to read like English and generate
10628f6c2f2SEnji Cooperinformative failure messages. For example, if the above assertion on `value1`
10728f6c2f2SEnji Cooperfails, the resulting message will be similar to the following:
10828f6c2f2SEnji Cooper
10928f6c2f2SEnji Cooper```
11028f6c2f2SEnji CooperValue of: value1
11128f6c2f2SEnji Cooper  Actual: "Hi, world!"
11228f6c2f2SEnji CooperExpected: starts with "Hello"
11328f6c2f2SEnji Cooper```
11428f6c2f2SEnji Cooper
11528f6c2f2SEnji CooperGoogleTest provides a built-in library of matchers—see the
11628f6c2f2SEnji Cooper[Matchers Reference](matchers.md). It is also possible to write your own
11728f6c2f2SEnji Coopermatchers—see [Writing New Matchers Quickly](../gmock_cook_book.md#NewMatchers).
11828f6c2f2SEnji CooperThe use of matchers makes `EXPECT_THAT` a powerful, extensible assertion.
11928f6c2f2SEnji Cooper
12028f6c2f2SEnji Cooper*The idea for this assertion was borrowed from Joe Walnes' Hamcrest project,
12128f6c2f2SEnji Cooperwhich adds `assertThat()` to JUnit.*
12228f6c2f2SEnji Cooper
12328f6c2f2SEnji Cooper## Boolean Conditions {#boolean}
12428f6c2f2SEnji Cooper
12528f6c2f2SEnji CooperThe following assertions test Boolean conditions.
12628f6c2f2SEnji Cooper
12728f6c2f2SEnji Cooper### EXPECT_TRUE {#EXPECT_TRUE}
12828f6c2f2SEnji Cooper
12928f6c2f2SEnji Cooper`EXPECT_TRUE(`*`condition`*`)` \
13028f6c2f2SEnji Cooper`ASSERT_TRUE(`*`condition`*`)`
13128f6c2f2SEnji Cooper
13228f6c2f2SEnji CooperVerifies that *`condition`* is true.
13328f6c2f2SEnji Cooper
13428f6c2f2SEnji Cooper### EXPECT_FALSE {#EXPECT_FALSE}
13528f6c2f2SEnji Cooper
13628f6c2f2SEnji Cooper`EXPECT_FALSE(`*`condition`*`)` \
13728f6c2f2SEnji Cooper`ASSERT_FALSE(`*`condition`*`)`
13828f6c2f2SEnji Cooper
13928f6c2f2SEnji CooperVerifies that *`condition`* is false.
14028f6c2f2SEnji Cooper
14128f6c2f2SEnji Cooper## Binary Comparison {#binary-comparison}
14228f6c2f2SEnji Cooper
14328f6c2f2SEnji CooperThe following assertions compare two values. The value arguments must be
14428f6c2f2SEnji Coopercomparable by the assertion's comparison operator, otherwise a compiler error
14528f6c2f2SEnji Cooperwill result.
14628f6c2f2SEnji Cooper
14728f6c2f2SEnji CooperIf an argument supports the `<<` operator, it will be called to print the
14828f6c2f2SEnji Cooperargument when the assertion fails. Otherwise, GoogleTest will attempt to print
14928f6c2f2SEnji Cooperthem in the best way it can—see
15028f6c2f2SEnji Cooper[Teaching GoogleTest How to Print Your Values](../advanced.md#teaching-googletest-how-to-print-your-values).
15128f6c2f2SEnji Cooper
15228f6c2f2SEnji CooperArguments are always evaluated exactly once, so it's OK for the arguments to
15328f6c2f2SEnji Cooperhave side effects. However, the argument evaluation order is undefined and
15428f6c2f2SEnji Cooperprograms should not depend on any particular argument evaluation order.
15528f6c2f2SEnji Cooper
15628f6c2f2SEnji CooperThese assertions work with both narrow and wide string objects (`string` and
15728f6c2f2SEnji Cooper`wstring`).
15828f6c2f2SEnji Cooper
15928f6c2f2SEnji CooperSee also the [Floating-Point Comparison](#floating-point) assertions to compare
16028f6c2f2SEnji Cooperfloating-point numbers and avoid problems caused by rounding.
16128f6c2f2SEnji Cooper
16228f6c2f2SEnji Cooper### EXPECT_EQ {#EXPECT_EQ}
16328f6c2f2SEnji Cooper
16428f6c2f2SEnji Cooper`EXPECT_EQ(`*`val1`*`,`*`val2`*`)` \
16528f6c2f2SEnji Cooper`ASSERT_EQ(`*`val1`*`,`*`val2`*`)`
16628f6c2f2SEnji Cooper
16728f6c2f2SEnji CooperVerifies that *`val1`*`==`*`val2`*.
16828f6c2f2SEnji Cooper
16928f6c2f2SEnji CooperDoes pointer equality on pointers. If used on two C strings, it tests if they
17028f6c2f2SEnji Cooperare in the same memory location, not if they have the same value. Use
17128f6c2f2SEnji Cooper[`EXPECT_STREQ`](#EXPECT_STREQ) to compare C strings (e.g. `const char*`) by
17228f6c2f2SEnji Coopervalue.
17328f6c2f2SEnji Cooper
17428f6c2f2SEnji CooperWhen comparing a pointer to `NULL`, use `EXPECT_EQ(`*`ptr`*`, nullptr)` instead
17528f6c2f2SEnji Cooperof `EXPECT_EQ(`*`ptr`*`, NULL)`.
17628f6c2f2SEnji Cooper
17728f6c2f2SEnji Cooper### EXPECT_NE {#EXPECT_NE}
17828f6c2f2SEnji Cooper
17928f6c2f2SEnji Cooper`EXPECT_NE(`*`val1`*`,`*`val2`*`)` \
18028f6c2f2SEnji Cooper`ASSERT_NE(`*`val1`*`,`*`val2`*`)`
18128f6c2f2SEnji Cooper
18228f6c2f2SEnji CooperVerifies that *`val1`*`!=`*`val2`*.
18328f6c2f2SEnji Cooper
18428f6c2f2SEnji CooperDoes pointer equality on pointers. If used on two C strings, it tests if they
18528f6c2f2SEnji Cooperare in different memory locations, not if they have different values. Use
18628f6c2f2SEnji Cooper[`EXPECT_STRNE`](#EXPECT_STRNE) to compare C strings (e.g. `const char*`) by
18728f6c2f2SEnji Coopervalue.
18828f6c2f2SEnji Cooper
18928f6c2f2SEnji CooperWhen comparing a pointer to `NULL`, use `EXPECT_NE(`*`ptr`*`, nullptr)` instead
19028f6c2f2SEnji Cooperof `EXPECT_NE(`*`ptr`*`, NULL)`.
19128f6c2f2SEnji Cooper
19228f6c2f2SEnji Cooper### EXPECT_LT {#EXPECT_LT}
19328f6c2f2SEnji Cooper
19428f6c2f2SEnji Cooper`EXPECT_LT(`*`val1`*`,`*`val2`*`)` \
19528f6c2f2SEnji Cooper`ASSERT_LT(`*`val1`*`,`*`val2`*`)`
19628f6c2f2SEnji Cooper
19728f6c2f2SEnji CooperVerifies that *`val1`*`<`*`val2`*.
19828f6c2f2SEnji Cooper
19928f6c2f2SEnji Cooper### EXPECT_LE {#EXPECT_LE}
20028f6c2f2SEnji Cooper
20128f6c2f2SEnji Cooper`EXPECT_LE(`*`val1`*`,`*`val2`*`)` \
20228f6c2f2SEnji Cooper`ASSERT_LE(`*`val1`*`,`*`val2`*`)`
20328f6c2f2SEnji Cooper
20428f6c2f2SEnji CooperVerifies that *`val1`*`<=`*`val2`*.
20528f6c2f2SEnji Cooper
20628f6c2f2SEnji Cooper### EXPECT_GT {#EXPECT_GT}
20728f6c2f2SEnji Cooper
20828f6c2f2SEnji Cooper`EXPECT_GT(`*`val1`*`,`*`val2`*`)` \
20928f6c2f2SEnji Cooper`ASSERT_GT(`*`val1`*`,`*`val2`*`)`
21028f6c2f2SEnji Cooper
21128f6c2f2SEnji CooperVerifies that *`val1`*`>`*`val2`*.
21228f6c2f2SEnji Cooper
21328f6c2f2SEnji Cooper### EXPECT_GE {#EXPECT_GE}
21428f6c2f2SEnji Cooper
21528f6c2f2SEnji Cooper`EXPECT_GE(`*`val1`*`,`*`val2`*`)` \
21628f6c2f2SEnji Cooper`ASSERT_GE(`*`val1`*`,`*`val2`*`)`
21728f6c2f2SEnji Cooper
21828f6c2f2SEnji CooperVerifies that *`val1`*`>=`*`val2`*.
21928f6c2f2SEnji Cooper
22028f6c2f2SEnji Cooper## String Comparison {#c-strings}
22128f6c2f2SEnji Cooper
22228f6c2f2SEnji CooperThe following assertions compare two **C strings**. To compare two `string`
22328f6c2f2SEnji Cooperobjects, use [`EXPECT_EQ`](#EXPECT_EQ) or [`EXPECT_NE`](#EXPECT_NE) instead.
22428f6c2f2SEnji Cooper
22528f6c2f2SEnji CooperThese assertions also accept wide C strings (`wchar_t*`). If a comparison of two
22628f6c2f2SEnji Cooperwide strings fails, their values will be printed as UTF-8 narrow strings.
22728f6c2f2SEnji Cooper
22828f6c2f2SEnji CooperTo compare a C string with `NULL`, use `EXPECT_EQ(`*`c_string`*`, nullptr)` or
22928f6c2f2SEnji Cooper`EXPECT_NE(`*`c_string`*`, nullptr)`.
23028f6c2f2SEnji Cooper
23128f6c2f2SEnji Cooper### EXPECT_STREQ {#EXPECT_STREQ}
23228f6c2f2SEnji Cooper
23328f6c2f2SEnji Cooper`EXPECT_STREQ(`*`str1`*`,`*`str2`*`)` \
23428f6c2f2SEnji Cooper`ASSERT_STREQ(`*`str1`*`,`*`str2`*`)`
23528f6c2f2SEnji Cooper
23628f6c2f2SEnji CooperVerifies that the two C strings *`str1`* and *`str2`* have the same contents.
23728f6c2f2SEnji Cooper
23828f6c2f2SEnji Cooper### EXPECT_STRNE {#EXPECT_STRNE}
23928f6c2f2SEnji Cooper
24028f6c2f2SEnji Cooper`EXPECT_STRNE(`*`str1`*`,`*`str2`*`)` \
24128f6c2f2SEnji Cooper`ASSERT_STRNE(`*`str1`*`,`*`str2`*`)`
24228f6c2f2SEnji Cooper
24328f6c2f2SEnji CooperVerifies that the two C strings *`str1`* and *`str2`* have different contents.
24428f6c2f2SEnji Cooper
24528f6c2f2SEnji Cooper### EXPECT_STRCASEEQ {#EXPECT_STRCASEEQ}
24628f6c2f2SEnji Cooper
24728f6c2f2SEnji Cooper`EXPECT_STRCASEEQ(`*`str1`*`,`*`str2`*`)` \
24828f6c2f2SEnji Cooper`ASSERT_STRCASEEQ(`*`str1`*`,`*`str2`*`)`
24928f6c2f2SEnji Cooper
25028f6c2f2SEnji CooperVerifies that the two C strings *`str1`* and *`str2`* have the same contents,
25128f6c2f2SEnji Cooperignoring case.
25228f6c2f2SEnji Cooper
25328f6c2f2SEnji Cooper### EXPECT_STRCASENE {#EXPECT_STRCASENE}
25428f6c2f2SEnji Cooper
25528f6c2f2SEnji Cooper`EXPECT_STRCASENE(`*`str1`*`,`*`str2`*`)` \
25628f6c2f2SEnji Cooper`ASSERT_STRCASENE(`*`str1`*`,`*`str2`*`)`
25728f6c2f2SEnji Cooper
25828f6c2f2SEnji CooperVerifies that the two C strings *`str1`* and *`str2`* have different contents,
25928f6c2f2SEnji Cooperignoring case.
26028f6c2f2SEnji Cooper
26128f6c2f2SEnji Cooper## Floating-Point Comparison {#floating-point}
26228f6c2f2SEnji Cooper
26328f6c2f2SEnji CooperThe following assertions compare two floating-point values.
26428f6c2f2SEnji Cooper
26528f6c2f2SEnji CooperDue to rounding errors, it is very unlikely that two floating-point values will
26628f6c2f2SEnji Coopermatch exactly, so `EXPECT_EQ` is not suitable. In general, for floating-point
26728f6c2f2SEnji Coopercomparison to make sense, the user needs to carefully choose the error bound.
26828f6c2f2SEnji Cooper
26928f6c2f2SEnji CooperGoogleTest also provides assertions that use a default error bound based on
27028f6c2f2SEnji CooperUnits in the Last Place (ULPs). To learn more about ULPs, see the article
27128f6c2f2SEnji Cooper[Comparing Floating Point Numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
27228f6c2f2SEnji Cooper
27328f6c2f2SEnji Cooper### EXPECT_FLOAT_EQ {#EXPECT_FLOAT_EQ}
27428f6c2f2SEnji Cooper
27528f6c2f2SEnji Cooper`EXPECT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)` \
27628f6c2f2SEnji Cooper`ASSERT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)`
27728f6c2f2SEnji Cooper
27828f6c2f2SEnji CooperVerifies that the two `float` values *`val1`* and *`val2`* are approximately
27928f6c2f2SEnji Cooperequal, to within 4 ULPs from each other.
28028f6c2f2SEnji Cooper
28128f6c2f2SEnji Cooper### EXPECT_DOUBLE_EQ {#EXPECT_DOUBLE_EQ}
28228f6c2f2SEnji Cooper
28328f6c2f2SEnji Cooper`EXPECT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)` \
28428f6c2f2SEnji Cooper`ASSERT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)`
28528f6c2f2SEnji Cooper
28628f6c2f2SEnji CooperVerifies that the two `double` values *`val1`* and *`val2`* are approximately
28728f6c2f2SEnji Cooperequal, to within 4 ULPs from each other.
28828f6c2f2SEnji Cooper
28928f6c2f2SEnji Cooper### EXPECT_NEAR {#EXPECT_NEAR}
29028f6c2f2SEnji Cooper
29128f6c2f2SEnji Cooper`EXPECT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)` \
29228f6c2f2SEnji Cooper`ASSERT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)`
29328f6c2f2SEnji Cooper
29428f6c2f2SEnji CooperVerifies that the difference between *`val1`* and *`val2`* does not exceed the
29528f6c2f2SEnji Cooperabsolute error bound *`abs_error`*.
29628f6c2f2SEnji Cooper
29728f6c2f2SEnji Cooper## Exception Assertions {#exceptions}
29828f6c2f2SEnji Cooper
29928f6c2f2SEnji CooperThe following assertions verify that a piece of code throws, or does not throw,
30028f6c2f2SEnji Cooperan exception. Usage requires exceptions to be enabled in the build environment.
30128f6c2f2SEnji Cooper
30228f6c2f2SEnji CooperNote that the piece of code under test can be a compound statement, for example:
30328f6c2f2SEnji Cooper
30428f6c2f2SEnji Cooper```cpp
30528f6c2f2SEnji CooperEXPECT_NO_THROW({
30628f6c2f2SEnji Cooper  int n = 5;
30728f6c2f2SEnji Cooper  DoSomething(&n);
30828f6c2f2SEnji Cooper});
30928f6c2f2SEnji Cooper```
31028f6c2f2SEnji Cooper
31128f6c2f2SEnji Cooper### EXPECT_THROW {#EXPECT_THROW}
31228f6c2f2SEnji Cooper
31328f6c2f2SEnji Cooper`EXPECT_THROW(`*`statement`*`,`*`exception_type`*`)` \
31428f6c2f2SEnji Cooper`ASSERT_THROW(`*`statement`*`,`*`exception_type`*`)`
31528f6c2f2SEnji Cooper
31628f6c2f2SEnji CooperVerifies that *`statement`* throws an exception of type *`exception_type`*.
31728f6c2f2SEnji Cooper
31828f6c2f2SEnji Cooper### EXPECT_ANY_THROW {#EXPECT_ANY_THROW}
31928f6c2f2SEnji Cooper
32028f6c2f2SEnji Cooper`EXPECT_ANY_THROW(`*`statement`*`)` \
32128f6c2f2SEnji Cooper`ASSERT_ANY_THROW(`*`statement`*`)`
32228f6c2f2SEnji Cooper
32328f6c2f2SEnji CooperVerifies that *`statement`* throws an exception of any type.
32428f6c2f2SEnji Cooper
32528f6c2f2SEnji Cooper### EXPECT_NO_THROW {#EXPECT_NO_THROW}
32628f6c2f2SEnji Cooper
32728f6c2f2SEnji Cooper`EXPECT_NO_THROW(`*`statement`*`)` \
32828f6c2f2SEnji Cooper`ASSERT_NO_THROW(`*`statement`*`)`
32928f6c2f2SEnji Cooper
33028f6c2f2SEnji CooperVerifies that *`statement`* does not throw any exception.
33128f6c2f2SEnji Cooper
33228f6c2f2SEnji Cooper## Predicate Assertions {#predicates}
33328f6c2f2SEnji Cooper
33428f6c2f2SEnji CooperThe following assertions enable more complex predicates to be verified while
33528f6c2f2SEnji Cooperprinting a more clear failure message than if `EXPECT_TRUE` were used alone.
33628f6c2f2SEnji Cooper
33728f6c2f2SEnji Cooper### EXPECT_PRED* {#EXPECT_PRED}
33828f6c2f2SEnji Cooper
33928f6c2f2SEnji Cooper`EXPECT_PRED1(`*`pred`*`,`*`val1`*`)` \
34028f6c2f2SEnji Cooper`EXPECT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \
34128f6c2f2SEnji Cooper`EXPECT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
34228f6c2f2SEnji Cooper`EXPECT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \
34328f6c2f2SEnji Cooper`EXPECT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
34428f6c2f2SEnji Cooper
34528f6c2f2SEnji Cooper`ASSERT_PRED1(`*`pred`*`,`*`val1`*`)` \
34628f6c2f2SEnji Cooper`ASSERT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \
34728f6c2f2SEnji Cooper`ASSERT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
34828f6c2f2SEnji Cooper`ASSERT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \
34928f6c2f2SEnji Cooper`ASSERT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
35028f6c2f2SEnji Cooper
35128f6c2f2SEnji CooperVerifies that the predicate *`pred`* returns `true` when passed the given values
35228f6c2f2SEnji Cooperas arguments.
35328f6c2f2SEnji Cooper
35428f6c2f2SEnji CooperThe parameter *`pred`* is a function or functor that accepts as many arguments
35528f6c2f2SEnji Cooperas the corresponding macro accepts values. If *`pred`* returns `true` for the
35628f6c2f2SEnji Coopergiven arguments, the assertion succeeds, otherwise the assertion fails.
35728f6c2f2SEnji Cooper
35828f6c2f2SEnji CooperWhen the assertion fails, it prints the value of each argument. Arguments are
35928f6c2f2SEnji Cooperalways evaluated exactly once.
36028f6c2f2SEnji Cooper
36128f6c2f2SEnji CooperAs an example, see the following code:
36228f6c2f2SEnji Cooper
36328f6c2f2SEnji Cooper```cpp
36428f6c2f2SEnji Cooper// Returns true if m and n have no common divisors except 1.
36528f6c2f2SEnji Cooperbool MutuallyPrime(int m, int n) { ... }
36628f6c2f2SEnji Cooper...
36728f6c2f2SEnji Cooperconst int a = 3;
36828f6c2f2SEnji Cooperconst int b = 4;
36928f6c2f2SEnji Cooperconst int c = 10;
37028f6c2f2SEnji Cooper...
37128f6c2f2SEnji CooperEXPECT_PRED2(MutuallyPrime, a, b);  // Succeeds
37228f6c2f2SEnji CooperEXPECT_PRED2(MutuallyPrime, b, c);  // Fails
37328f6c2f2SEnji Cooper```
37428f6c2f2SEnji Cooper
37528f6c2f2SEnji CooperIn the above example, the first assertion succeeds, and the second fails with
37628f6c2f2SEnji Cooperthe following message:
37728f6c2f2SEnji Cooper
37828f6c2f2SEnji Cooper```
37928f6c2f2SEnji CooperMutuallyPrime(b, c) is false, where
38028f6c2f2SEnji Cooperb is 4
38128f6c2f2SEnji Cooperc is 10
38228f6c2f2SEnji Cooper```
38328f6c2f2SEnji Cooper
38428f6c2f2SEnji CooperNote that if the given predicate is an overloaded function or a function
38528f6c2f2SEnji Coopertemplate, the assertion macro might not be able to determine which version to
38628f6c2f2SEnji Cooperuse, and it might be necessary to explicitly specify the type of the function.
38728f6c2f2SEnji CooperFor example, for a Boolean function `IsPositive()` overloaded to take either a
38828f6c2f2SEnji Coopersingle `int` or `double` argument, it would be necessary to write one of the
38928f6c2f2SEnji Cooperfollowing:
39028f6c2f2SEnji Cooper
39128f6c2f2SEnji Cooper```cpp
39228f6c2f2SEnji CooperEXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5);
39328f6c2f2SEnji CooperEXPECT_PRED1(static_cast<bool (*)(double)>(IsPositive), 3.14);
39428f6c2f2SEnji Cooper```
39528f6c2f2SEnji Cooper
39628f6c2f2SEnji CooperWriting simply `EXPECT_PRED1(IsPositive, 5);` would result in a compiler error.
39728f6c2f2SEnji CooperSimilarly, to use a template function, specify the template arguments:
39828f6c2f2SEnji Cooper
39928f6c2f2SEnji Cooper```cpp
40028f6c2f2SEnji Coopertemplate <typename T>
40128f6c2f2SEnji Cooperbool IsNegative(T x) {
40228f6c2f2SEnji Cooper  return x < 0;
40328f6c2f2SEnji Cooper}
40428f6c2f2SEnji Cooper...
40528f6c2f2SEnji CooperEXPECT_PRED1(IsNegative<int>, -5);  // Must specify type for IsNegative
40628f6c2f2SEnji Cooper```
40728f6c2f2SEnji Cooper
40828f6c2f2SEnji CooperIf a template has multiple parameters, wrap the predicate in parentheses so the
40928f6c2f2SEnji Coopermacro arguments are parsed correctly:
41028f6c2f2SEnji Cooper
41128f6c2f2SEnji Cooper```cpp
41228f6c2f2SEnji CooperASSERT_PRED2((MyPredicate<int, int>), 5, 0);
41328f6c2f2SEnji Cooper```
41428f6c2f2SEnji Cooper
41528f6c2f2SEnji Cooper### EXPECT_PRED_FORMAT* {#EXPECT_PRED_FORMAT}
41628f6c2f2SEnji Cooper
41728f6c2f2SEnji Cooper`EXPECT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \
41828f6c2f2SEnji Cooper`EXPECT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \
41928f6c2f2SEnji Cooper`EXPECT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
42028f6c2f2SEnji Cooper`EXPECT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)`
42128f6c2f2SEnji Cooper\
42228f6c2f2SEnji Cooper`EXPECT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
42328f6c2f2SEnji Cooper
42428f6c2f2SEnji Cooper`ASSERT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \
42528f6c2f2SEnji Cooper`ASSERT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \
42628f6c2f2SEnji Cooper`ASSERT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
42728f6c2f2SEnji Cooper`ASSERT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)`
42828f6c2f2SEnji Cooper\
42928f6c2f2SEnji Cooper`ASSERT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
43028f6c2f2SEnji Cooper
43128f6c2f2SEnji CooperVerifies that the predicate *`pred_formatter`* succeeds when passed the given
43228f6c2f2SEnji Coopervalues as arguments.
43328f6c2f2SEnji Cooper
43428f6c2f2SEnji CooperThe parameter *`pred_formatter`* is a *predicate-formatter*, which is a function
43528f6c2f2SEnji Cooperor functor with the signature:
43628f6c2f2SEnji Cooper
43728f6c2f2SEnji Cooper```cpp
43828f6c2f2SEnji Coopertesting::AssertionResult PredicateFormatter(const char* expr1,
43928f6c2f2SEnji Cooper                                            const char* expr2,
44028f6c2f2SEnji Cooper                                            ...
44128f6c2f2SEnji Cooper                                            const char* exprn,
44228f6c2f2SEnji Cooper                                            T1 val1,
44328f6c2f2SEnji Cooper                                            T2 val2,
44428f6c2f2SEnji Cooper                                            ...
44528f6c2f2SEnji Cooper                                            Tn valn);
44628f6c2f2SEnji Cooper```
44728f6c2f2SEnji Cooper
44828f6c2f2SEnji Cooperwhere *`val1`*, *`val2`*, ..., *`valn`* are the values of the predicate
44928f6c2f2SEnji Cooperarguments, and *`expr1`*, *`expr2`*, ..., *`exprn`* are the corresponding
45028f6c2f2SEnji Cooperexpressions as they appear in the source code. The types `T1`, `T2`, ..., `Tn`
45128f6c2f2SEnji Coopercan be either value types or reference types; if an argument has type `T`, it
45228f6c2f2SEnji Coopercan be declared as either `T` or `const T&`, whichever is appropriate. For more
45328f6c2f2SEnji Cooperabout the return type `testing::AssertionResult`, see
45428f6c2f2SEnji Cooper[Using a Function That Returns an AssertionResult](../advanced.md#using-a-function-that-returns-an-assertionresult).
45528f6c2f2SEnji Cooper
45628f6c2f2SEnji CooperAs an example, see the following code:
45728f6c2f2SEnji Cooper
45828f6c2f2SEnji Cooper```cpp
45928f6c2f2SEnji Cooper// Returns the smallest prime common divisor of m and n,
46028f6c2f2SEnji Cooper// or 1 when m and n are mutually prime.
46128f6c2f2SEnji Cooperint SmallestPrimeCommonDivisor(int m, int n) { ... }
46228f6c2f2SEnji Cooper
46328f6c2f2SEnji Cooper// Returns true if m and n have no common divisors except 1.
46428f6c2f2SEnji Cooperbool MutuallyPrime(int m, int n) { ... }
46528f6c2f2SEnji Cooper
46628f6c2f2SEnji Cooper// A predicate-formatter for asserting that two integers are mutually prime.
46728f6c2f2SEnji Coopertesting::AssertionResult AssertMutuallyPrime(const char* m_expr,
46828f6c2f2SEnji Cooper                                             const char* n_expr,
46928f6c2f2SEnji Cooper                                             int m,
47028f6c2f2SEnji Cooper                                             int n) {
47128f6c2f2SEnji Cooper  if (MutuallyPrime(m, n)) return testing::AssertionSuccess();
47228f6c2f2SEnji Cooper
47328f6c2f2SEnji Cooper  return testing::AssertionFailure() << m_expr << " and " << n_expr
47428f6c2f2SEnji Cooper      << " (" << m << " and " << n << ") are not mutually prime, "
47528f6c2f2SEnji Cooper      << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n);
47628f6c2f2SEnji Cooper}
47728f6c2f2SEnji Cooper
47828f6c2f2SEnji Cooper...
47928f6c2f2SEnji Cooperconst int a = 3;
48028f6c2f2SEnji Cooperconst int b = 4;
48128f6c2f2SEnji Cooperconst int c = 10;
48228f6c2f2SEnji Cooper...
48328f6c2f2SEnji CooperEXPECT_PRED_FORMAT2(AssertMutuallyPrime, a, b);  // Succeeds
48428f6c2f2SEnji CooperEXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);  // Fails
48528f6c2f2SEnji Cooper```
48628f6c2f2SEnji Cooper
48728f6c2f2SEnji CooperIn the above example, the final assertion fails and the predicate-formatter
48828f6c2f2SEnji Cooperproduces the following failure message:
48928f6c2f2SEnji Cooper
49028f6c2f2SEnji Cooper```
49128f6c2f2SEnji Cooperb and c (4 and 10) are not mutually prime, as they have a common divisor 2
49228f6c2f2SEnji Cooper```
49328f6c2f2SEnji Cooper
49428f6c2f2SEnji Cooper## Windows HRESULT Assertions {#HRESULT}
49528f6c2f2SEnji Cooper
49628f6c2f2SEnji CooperThe following assertions test for `HRESULT` success or failure. For example:
49728f6c2f2SEnji Cooper
49828f6c2f2SEnji Cooper```cpp
49928f6c2f2SEnji CooperCComPtr<IShellDispatch2> shell;
50028f6c2f2SEnji CooperASSERT_HRESULT_SUCCEEDED(shell.CoCreateInstance(L"Shell.Application"));
50128f6c2f2SEnji CooperCComVariant empty;
50228f6c2f2SEnji CooperASSERT_HRESULT_SUCCEEDED(shell->ShellExecute(CComBSTR(url), empty, empty, empty, empty));
50328f6c2f2SEnji Cooper```
50428f6c2f2SEnji Cooper
50528f6c2f2SEnji CooperThe generated output contains the human-readable error message associated with
50628f6c2f2SEnji Cooperthe returned `HRESULT` code.
50728f6c2f2SEnji Cooper
50828f6c2f2SEnji Cooper### EXPECT_HRESULT_SUCCEEDED {#EXPECT_HRESULT_SUCCEEDED}
50928f6c2f2SEnji Cooper
51028f6c2f2SEnji Cooper`EXPECT_HRESULT_SUCCEEDED(`*`expression`*`)` \
51128f6c2f2SEnji Cooper`ASSERT_HRESULT_SUCCEEDED(`*`expression`*`)`
51228f6c2f2SEnji Cooper
51328f6c2f2SEnji CooperVerifies that *`expression`* is a success `HRESULT`.
51428f6c2f2SEnji Cooper
51528f6c2f2SEnji Cooper### EXPECT_HRESULT_FAILED {#EXPECT_HRESULT_FAILED}
51628f6c2f2SEnji Cooper
51728f6c2f2SEnji Cooper`EXPECT_HRESULT_FAILED(`*`expression`*`)` \
51828f6c2f2SEnji Cooper`ASSERT_HRESULT_FAILED(`*`expression`*`)`
51928f6c2f2SEnji Cooper
52028f6c2f2SEnji CooperVerifies that *`expression`* is a failure `HRESULT`.
52128f6c2f2SEnji Cooper
52228f6c2f2SEnji Cooper## Death Assertions {#death}
52328f6c2f2SEnji Cooper
52428f6c2f2SEnji CooperThe following assertions verify that a piece of code causes the process to
52528f6c2f2SEnji Cooperterminate. For context, see [Death Tests](../advanced.md#death-tests).
52628f6c2f2SEnji Cooper
52728f6c2f2SEnji CooperThese assertions spawn a new process and execute the code under test in that
52828f6c2f2SEnji Cooperprocess. How that happens depends on the platform and the variable
52928f6c2f2SEnji Cooper`::testing::GTEST_FLAG(death_test_style)`, which is initialized from the
53028f6c2f2SEnji Coopercommand-line flag `--gtest_death_test_style`.
53128f6c2f2SEnji Cooper
53228f6c2f2SEnji Cooper*   On POSIX systems, `fork()` (or `clone()` on Linux) is used to spawn the
53328f6c2f2SEnji Cooper    child, after which:
53428f6c2f2SEnji Cooper    *   If the variable's value is `"fast"`, the death test statement is
53528f6c2f2SEnji Cooper        immediately executed.
53628f6c2f2SEnji Cooper    *   If the variable's value is `"threadsafe"`, the child process re-executes
53728f6c2f2SEnji Cooper        the unit test binary just as it was originally invoked, but with some
53828f6c2f2SEnji Cooper        extra flags to cause just the single death test under consideration to
53928f6c2f2SEnji Cooper        be run.
54028f6c2f2SEnji Cooper*   On Windows, the child is spawned using the `CreateProcess()` API, and
54128f6c2f2SEnji Cooper    re-executes the binary to cause just the single death test under
54228f6c2f2SEnji Cooper    consideration to be run - much like the `"threadsafe"` mode on POSIX.
54328f6c2f2SEnji Cooper
54428f6c2f2SEnji CooperOther values for the variable are illegal and will cause the death test to fail.
54528f6c2f2SEnji CooperCurrently, the flag's default value is
54628f6c2f2SEnji Cooper**`"fast"`**.
54728f6c2f2SEnji Cooper
54828f6c2f2SEnji CooperIf the death test statement runs to completion without dying, the child process
54928f6c2f2SEnji Cooperwill nonetheless terminate, and the assertion fails.
55028f6c2f2SEnji Cooper
55128f6c2f2SEnji CooperNote that the piece of code under test can be a compound statement, for example:
55228f6c2f2SEnji Cooper
55328f6c2f2SEnji Cooper```cpp
55428f6c2f2SEnji CooperEXPECT_DEATH({
55528f6c2f2SEnji Cooper  int n = 5;
55628f6c2f2SEnji Cooper  DoSomething(&n);
55728f6c2f2SEnji Cooper}, "Error on line .* of DoSomething()");
55828f6c2f2SEnji Cooper```
55928f6c2f2SEnji Cooper
56028f6c2f2SEnji Cooper### EXPECT_DEATH {#EXPECT_DEATH}
56128f6c2f2SEnji Cooper
56228f6c2f2SEnji Cooper`EXPECT_DEATH(`*`statement`*`,`*`matcher`*`)` \
56328f6c2f2SEnji Cooper`ASSERT_DEATH(`*`statement`*`,`*`matcher`*`)`
56428f6c2f2SEnji Cooper
56528f6c2f2SEnji CooperVerifies that *`statement`* causes the process to terminate with a nonzero exit
56628f6c2f2SEnji Cooperstatus and produces `stderr` output that matches *`matcher`*.
56728f6c2f2SEnji Cooper
56828f6c2f2SEnji CooperThe parameter *`matcher`* is either a [matcher](matchers.md) for a `const
56928f6c2f2SEnji Cooperstd::string&`, or a regular expression (see
57028f6c2f2SEnji Cooper[Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare
57128f6c2f2SEnji Cooperstring *`s`* (with no matcher) is treated as
57228f6c2f2SEnji Cooper[`ContainsRegex(s)`](matchers.md#string-matchers), **not**
57328f6c2f2SEnji Cooper[`Eq(s)`](matchers.md#generic-comparison).
57428f6c2f2SEnji Cooper
57528f6c2f2SEnji CooperFor example, the following code verifies that calling `DoSomething(42)` causes
57628f6c2f2SEnji Cooperthe process to die with an error message that contains the text `My error`:
57728f6c2f2SEnji Cooper
57828f6c2f2SEnji Cooper```cpp
57928f6c2f2SEnji CooperEXPECT_DEATH(DoSomething(42), "My error");
58028f6c2f2SEnji Cooper```
58128f6c2f2SEnji Cooper
58228f6c2f2SEnji Cooper### EXPECT_DEATH_IF_SUPPORTED {#EXPECT_DEATH_IF_SUPPORTED}
58328f6c2f2SEnji Cooper
58428f6c2f2SEnji Cooper`EXPECT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)` \
58528f6c2f2SEnji Cooper`ASSERT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)`
58628f6c2f2SEnji Cooper
58728f6c2f2SEnji CooperIf death tests are supported, behaves the same as
58828f6c2f2SEnji Cooper[`EXPECT_DEATH`](#EXPECT_DEATH). Otherwise, verifies nothing.
58928f6c2f2SEnji Cooper
59028f6c2f2SEnji Cooper### EXPECT_DEBUG_DEATH {#EXPECT_DEBUG_DEATH}
59128f6c2f2SEnji Cooper
59228f6c2f2SEnji Cooper`EXPECT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)` \
59328f6c2f2SEnji Cooper`ASSERT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)`
59428f6c2f2SEnji Cooper
59528f6c2f2SEnji CooperIn debug mode, behaves the same as [`EXPECT_DEATH`](#EXPECT_DEATH). When not in
59628f6c2f2SEnji Cooperdebug mode (i.e. `NDEBUG` is defined), just executes *`statement`*.
59728f6c2f2SEnji Cooper
59828f6c2f2SEnji Cooper### EXPECT_EXIT {#EXPECT_EXIT}
59928f6c2f2SEnji Cooper
60028f6c2f2SEnji Cooper`EXPECT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)` \
60128f6c2f2SEnji Cooper`ASSERT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)`
60228f6c2f2SEnji Cooper
60328f6c2f2SEnji CooperVerifies that *`statement`* causes the process to terminate with an exit status
60428f6c2f2SEnji Cooperthat satisfies *`predicate`*, and produces `stderr` output that matches
60528f6c2f2SEnji Cooper*`matcher`*.
60628f6c2f2SEnji Cooper
60728f6c2f2SEnji CooperThe parameter *`predicate`* is a function or functor that accepts an `int` exit
60828f6c2f2SEnji Cooperstatus and returns a `bool`. GoogleTest provides two predicates to handle common
60928f6c2f2SEnji Coopercases:
61028f6c2f2SEnji Cooper
61128f6c2f2SEnji Cooper```cpp
61228f6c2f2SEnji Cooper// Returns true if the program exited normally with the given exit status code.
61328f6c2f2SEnji Cooper::testing::ExitedWithCode(exit_code);
61428f6c2f2SEnji Cooper
61528f6c2f2SEnji Cooper// Returns true if the program was killed by the given signal.
61628f6c2f2SEnji Cooper// Not available on Windows.
61728f6c2f2SEnji Cooper::testing::KilledBySignal(signal_number);
61828f6c2f2SEnji Cooper```
61928f6c2f2SEnji Cooper
62028f6c2f2SEnji CooperThe parameter *`matcher`* is either a [matcher](matchers.md) for a `const
62128f6c2f2SEnji Cooperstd::string&`, or a regular expression (see
62228f6c2f2SEnji Cooper[Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare
62328f6c2f2SEnji Cooperstring *`s`* (with no matcher) is treated as
62428f6c2f2SEnji Cooper[`ContainsRegex(s)`](matchers.md#string-matchers), **not**
62528f6c2f2SEnji Cooper[`Eq(s)`](matchers.md#generic-comparison).
62628f6c2f2SEnji Cooper
62728f6c2f2SEnji CooperFor example, the following code verifies that calling `NormalExit()` causes the
62828f6c2f2SEnji Cooperprocess to print a message containing the text `Success` to `stderr` and exit
62928f6c2f2SEnji Cooperwith exit status code 0:
63028f6c2f2SEnji Cooper
63128f6c2f2SEnji Cooper```cpp
63228f6c2f2SEnji CooperEXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), "Success");
63328f6c2f2SEnji Cooper```
634