The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

633 lines
21 KiB

2 months ago
  1. # Assertions Reference
  2. This page lists the assertion macros provided by GoogleTest for verifying code
  3. behavior. To use them, include the header `gtest/gtest.h`.
  4. The majority of the macros listed below come as a pair with an `EXPECT_` variant
  5. and an `ASSERT_` variant. Upon failure, `EXPECT_` macros generate nonfatal
  6. failures and allow the current function to continue running, while `ASSERT_`
  7. macros generate fatal failures and abort the current function.
  8. All assertion macros support streaming a custom failure message into them with
  9. the `<<` operator, for example:
  10. ```cpp
  11. EXPECT_TRUE(my_condition) << "My condition is not true";
  12. ```
  13. Anything that can be streamed to an `ostream` can be streamed to an assertion
  14. macro—in particular, C strings and string objects. If a wide string (`wchar_t*`,
  15. `TCHAR*` in `UNICODE` mode on Windows, or `std::wstring`) is streamed to an
  16. assertion, it will be translated to UTF-8 when printed.
  17. ## Explicit Success and Failure {#success-failure}
  18. The assertions in this section generate a success or failure directly instead of
  19. testing a value or expression. These are useful when control flow, rather than a
  20. Boolean expression, determines the test's success or failure, as shown by the
  21. following example:
  22. ```c++
  23. switch(expression) {
  24. case 1:
  25. ... some checks ...
  26. case 2:
  27. ... some other checks ...
  28. default:
  29. FAIL() << "We shouldn't get here.";
  30. }
  31. ```
  32. ### SUCCEED {#SUCCEED}
  33. `SUCCEED()`
  34. Generates a success. This *does not* make the overall test succeed. A test is
  35. considered successful only if none of its assertions fail during its execution.
  36. The `SUCCEED` assertion is purely documentary and currently doesn't generate any
  37. user-visible output. However, we may add `SUCCEED` messages to GoogleTest output
  38. in the future.
  39. ### FAIL {#FAIL}
  40. `FAIL()`
  41. Generates a fatal failure, which returns from the current function.
  42. Can only be used in functions that return `void`. See
  43. [Assertion Placement](../advanced.md#assertion-placement) for more information.
  44. ### ADD_FAILURE {#ADD_FAILURE}
  45. `ADD_FAILURE()`
  46. Generates a nonfatal failure, which allows the current function to continue
  47. running.
  48. ### ADD_FAILURE_AT {#ADD_FAILURE_AT}
  49. `ADD_FAILURE_AT(`*`file_path`*`,`*`line_number`*`)`
  50. Generates a nonfatal failure at the file and line number specified.
  51. ## Generalized Assertion {#generalized}
  52. The following assertion allows [matchers](matchers.md) to be used to verify
  53. values.
  54. ### EXPECT_THAT {#EXPECT_THAT}
  55. `EXPECT_THAT(`*`value`*`,`*`matcher`*`)` \
  56. `ASSERT_THAT(`*`value`*`,`*`matcher`*`)`
  57. Verifies that *`value`* matches the [matcher](matchers.md) *`matcher`*.
  58. For example, the following code verifies that the string `value1` starts with
  59. `"Hello"`, `value2` matches a regular expression, and `value3` is between 5 and
  60. 10:
  61. ```cpp
  62. #include "gmock/gmock.h"
  63. using ::testing::AllOf;
  64. using ::testing::Gt;
  65. using ::testing::Lt;
  66. using ::testing::MatchesRegex;
  67. using ::testing::StartsWith;
  68. ...
  69. EXPECT_THAT(value1, StartsWith("Hello"));
  70. EXPECT_THAT(value2, MatchesRegex("Line \\d+"));
  71. ASSERT_THAT(value3, AllOf(Gt(5), Lt(10)));
  72. ```
  73. Matchers enable assertions of this form to read like English and generate
  74. informative failure messages. For example, if the above assertion on `value1`
  75. fails, the resulting message will be similar to the following:
  76. ```
  77. Value of: value1
  78. Actual: "Hi, world!"
  79. Expected: starts with "Hello"
  80. ```
  81. GoogleTest provides a built-in library of matchers—see the
  82. [Matchers Reference](matchers.md). It is also possible to write your own
  83. matchers—see [Writing New Matchers Quickly](../gmock_cook_book.md#NewMatchers).
  84. The use of matchers makes `EXPECT_THAT` a powerful, extensible assertion.
  85. *The idea for this assertion was borrowed from Joe Walnes' Hamcrest project,
  86. which adds `assertThat()` to JUnit.*
  87. ## Boolean Conditions {#boolean}
  88. The following assertions test Boolean conditions.
  89. ### EXPECT_TRUE {#EXPECT_TRUE}
  90. `EXPECT_TRUE(`*`condition`*`)` \
  91. `ASSERT_TRUE(`*`condition`*`)`
  92. Verifies that *`condition`* is true.
  93. ### EXPECT_FALSE {#EXPECT_FALSE}
  94. `EXPECT_FALSE(`*`condition`*`)` \
  95. `ASSERT_FALSE(`*`condition`*`)`
  96. Verifies that *`condition`* is false.
  97. ## Binary Comparison {#binary-comparison}
  98. The following assertions compare two values. The value arguments must be
  99. comparable by the assertion's comparison operator, otherwise a compiler error
  100. will result.
  101. If an argument supports the `<<` operator, it will be called to print the
  102. argument when the assertion fails. Otherwise, GoogleTest will attempt to print
  103. them in the best way it can—see
  104. [Teaching GoogleTest How to Print Your Values](../advanced.md#teaching-googletest-how-to-print-your-values).
  105. Arguments are always evaluated exactly once, so it's OK for the arguments to
  106. have side effects. However, the argument evaluation order is undefined and
  107. programs should not depend on any particular argument evaluation order.
  108. These assertions work with both narrow and wide string objects (`string` and
  109. `wstring`).
  110. See also the [Floating-Point Comparison](#floating-point) assertions to compare
  111. floating-point numbers and avoid problems caused by rounding.
  112. ### EXPECT_EQ {#EXPECT_EQ}
  113. `EXPECT_EQ(`*`val1`*`,`*`val2`*`)` \
  114. `ASSERT_EQ(`*`val1`*`,`*`val2`*`)`
  115. Verifies that *`val1`*`==`*`val2`*.
  116. Does pointer equality on pointers. If used on two C strings, it tests if they
  117. are in the same memory location, not if they have the same value. Use
  118. [`EXPECT_STREQ`](#EXPECT_STREQ) to compare C strings (e.g. `const char*`) by
  119. value.
  120. When comparing a pointer to `NULL`, use `EXPECT_EQ(`*`ptr`*`, nullptr)` instead
  121. of `EXPECT_EQ(`*`ptr`*`, NULL)`.
  122. ### EXPECT_NE {#EXPECT_NE}
  123. `EXPECT_NE(`*`val1`*`,`*`val2`*`)` \
  124. `ASSERT_NE(`*`val1`*`,`*`val2`*`)`
  125. Verifies that *`val1`*`!=`*`val2`*.
  126. Does pointer equality on pointers. If used on two C strings, it tests if they
  127. are in different memory locations, not if they have different values. Use
  128. [`EXPECT_STRNE`](#EXPECT_STRNE) to compare C strings (e.g. `const char*`) by
  129. value.
  130. When comparing a pointer to `NULL`, use `EXPECT_NE(`*`ptr`*`, nullptr)` instead
  131. of `EXPECT_NE(`*`ptr`*`, NULL)`.
  132. ### EXPECT_LT {#EXPECT_LT}
  133. `EXPECT_LT(`*`val1`*`,`*`val2`*`)` \
  134. `ASSERT_LT(`*`val1`*`,`*`val2`*`)`
  135. Verifies that *`val1`*`<`*`val2`*.
  136. ### EXPECT_LE {#EXPECT_LE}
  137. `EXPECT_LE(`*`val1`*`,`*`val2`*`)` \
  138. `ASSERT_LE(`*`val1`*`,`*`val2`*`)`
  139. Verifies that *`val1`*`<=`*`val2`*.
  140. ### EXPECT_GT {#EXPECT_GT}
  141. `EXPECT_GT(`*`val1`*`,`*`val2`*`)` \
  142. `ASSERT_GT(`*`val1`*`,`*`val2`*`)`
  143. Verifies that *`val1`*`>`*`val2`*.
  144. ### EXPECT_GE {#EXPECT_GE}
  145. `EXPECT_GE(`*`val1`*`,`*`val2`*`)` \
  146. `ASSERT_GE(`*`val1`*`,`*`val2`*`)`
  147. Verifies that *`val1`*`>=`*`val2`*.
  148. ## String Comparison {#c-strings}
  149. The following assertions compare two **C strings**. To compare two `string`
  150. objects, use [`EXPECT_EQ`](#EXPECT_EQ) or [`EXPECT_NE`](#EXPECT_NE) instead.
  151. These assertions also accept wide C strings (`wchar_t*`). If a comparison of two
  152. wide strings fails, their values will be printed as UTF-8 narrow strings.
  153. To compare a C string with `NULL`, use `EXPECT_EQ(`*`c_string`*`, nullptr)` or
  154. `EXPECT_NE(`*`c_string`*`, nullptr)`.
  155. ### EXPECT_STREQ {#EXPECT_STREQ}
  156. `EXPECT_STREQ(`*`str1`*`,`*`str2`*`)` \
  157. `ASSERT_STREQ(`*`str1`*`,`*`str2`*`)`
  158. Verifies that the two C strings *`str1`* and *`str2`* have the same contents.
  159. ### EXPECT_STRNE {#EXPECT_STRNE}
  160. `EXPECT_STRNE(`*`str1`*`,`*`str2`*`)` \
  161. `ASSERT_STRNE(`*`str1`*`,`*`str2`*`)`
  162. Verifies that the two C strings *`str1`* and *`str2`* have different contents.
  163. ### EXPECT_STRCASEEQ {#EXPECT_STRCASEEQ}
  164. `EXPECT_STRCASEEQ(`*`str1`*`,`*`str2`*`)` \
  165. `ASSERT_STRCASEEQ(`*`str1`*`,`*`str2`*`)`
  166. Verifies that the two C strings *`str1`* and *`str2`* have the same contents,
  167. ignoring case.
  168. ### EXPECT_STRCASENE {#EXPECT_STRCASENE}
  169. `EXPECT_STRCASENE(`*`str1`*`,`*`str2`*`)` \
  170. `ASSERT_STRCASENE(`*`str1`*`,`*`str2`*`)`
  171. Verifies that the two C strings *`str1`* and *`str2`* have different contents,
  172. ignoring case.
  173. ## Floating-Point Comparison {#floating-point}
  174. The following assertions compare two floating-point values.
  175. Due to rounding errors, it is very unlikely that two floating-point values will
  176. match exactly, so `EXPECT_EQ` is not suitable. In general, for floating-point
  177. comparison to make sense, the user needs to carefully choose the error bound.
  178. GoogleTest also provides assertions that use a default error bound based on
  179. Units in the Last Place (ULPs). To learn more about ULPs, see the article
  180. [Comparing Floating Point Numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
  181. ### EXPECT_FLOAT_EQ {#EXPECT_FLOAT_EQ}
  182. `EXPECT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)` \
  183. `ASSERT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)`
  184. Verifies that the two `float` values *`val1`* and *`val2`* are approximately
  185. equal, to within 4 ULPs from each other.
  186. ### EXPECT_DOUBLE_EQ {#EXPECT_DOUBLE_EQ}
  187. `EXPECT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)` \
  188. `ASSERT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)`
  189. Verifies that the two `double` values *`val1`* and *`val2`* are approximately
  190. equal, to within 4 ULPs from each other.
  191. ### EXPECT_NEAR {#EXPECT_NEAR}
  192. `EXPECT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)` \
  193. `ASSERT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)`
  194. Verifies that the difference between *`val1`* and *`val2`* does not exceed the
  195. absolute error bound *`abs_error`*.
  196. ## Exception Assertions {#exceptions}
  197. The following assertions verify that a piece of code throws, or does not throw,
  198. an exception. Usage requires exceptions to be enabled in the build environment.
  199. Note that the piece of code under test can be a compound statement, for example:
  200. ```cpp
  201. EXPECT_NO_THROW({
  202. int n = 5;
  203. DoSomething(&n);
  204. });
  205. ```
  206. ### EXPECT_THROW {#EXPECT_THROW}
  207. `EXPECT_THROW(`*`statement`*`,`*`exception_type`*`)` \
  208. `ASSERT_THROW(`*`statement`*`,`*`exception_type`*`)`
  209. Verifies that *`statement`* throws an exception of type *`exception_type`*.
  210. ### EXPECT_ANY_THROW {#EXPECT_ANY_THROW}
  211. `EXPECT_ANY_THROW(`*`statement`*`)` \
  212. `ASSERT_ANY_THROW(`*`statement`*`)`
  213. Verifies that *`statement`* throws an exception of any type.
  214. ### EXPECT_NO_THROW {#EXPECT_NO_THROW}
  215. `EXPECT_NO_THROW(`*`statement`*`)` \
  216. `ASSERT_NO_THROW(`*`statement`*`)`
  217. Verifies that *`statement`* does not throw any exception.
  218. ## Predicate Assertions {#predicates}
  219. The following assertions enable more complex predicates to be verified while
  220. printing a more clear failure message than if `EXPECT_TRUE` were used alone.
  221. ### EXPECT_PRED* {#EXPECT_PRED}
  222. `EXPECT_PRED1(`*`pred`*`,`*`val1`*`)` \
  223. `EXPECT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \
  224. `EXPECT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
  225. `EXPECT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \
  226. `EXPECT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
  227. `ASSERT_PRED1(`*`pred`*`,`*`val1`*`)` \
  228. `ASSERT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \
  229. `ASSERT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
  230. `ASSERT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \
  231. `ASSERT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
  232. Verifies that the predicate *`pred`* returns `true` when passed the given values
  233. as arguments.
  234. The parameter *`pred`* is a function or functor that accepts as many arguments
  235. as the corresponding macro accepts values. If *`pred`* returns `true` for the
  236. given arguments, the assertion succeeds, otherwise the assertion fails.
  237. When the assertion fails, it prints the value of each argument. Arguments are
  238. always evaluated exactly once.
  239. As an example, see the following code:
  240. ```cpp
  241. // Returns true if m and n have no common divisors except 1.
  242. bool MutuallyPrime(int m, int n) { ... }
  243. ...
  244. const int a = 3;
  245. const int b = 4;
  246. const int c = 10;
  247. ...
  248. EXPECT_PRED2(MutuallyPrime, a, b); // Succeeds
  249. EXPECT_PRED2(MutuallyPrime, b, c); // Fails
  250. ```
  251. In the above example, the first assertion succeeds, and the second fails with
  252. the following message:
  253. ```
  254. MutuallyPrime(b, c) is false, where
  255. b is 4
  256. c is 10
  257. ```
  258. Note that if the given predicate is an overloaded function or a function
  259. template, the assertion macro might not be able to determine which version to
  260. use, and it might be necessary to explicitly specify the type of the function.
  261. For example, for a Boolean function `IsPositive()` overloaded to take either a
  262. single `int` or `double` argument, it would be necessary to write one of the
  263. following:
  264. ```cpp
  265. EXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5);
  266. EXPECT_PRED1(static_cast<bool (*)(double)>(IsPositive), 3.14);
  267. ```
  268. Writing simply `EXPECT_PRED1(IsPositive, 5);` would result in a compiler error.
  269. Similarly, to use a template function, specify the template arguments:
  270. ```cpp
  271. template <typename T>
  272. bool IsNegative(T x) {
  273. return x < 0;
  274. }
  275. ...
  276. EXPECT_PRED1(IsNegative<int>, -5); // Must specify type for IsNegative
  277. ```
  278. If a template has multiple parameters, wrap the predicate in parentheses so the
  279. macro arguments are parsed correctly:
  280. ```cpp
  281. ASSERT_PRED2((MyPredicate<int, int>), 5, 0);
  282. ```
  283. ### EXPECT_PRED_FORMAT* {#EXPECT_PRED_FORMAT}
  284. `EXPECT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \
  285. `EXPECT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \
  286. `EXPECT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
  287. `EXPECT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)`
  288. \
  289. `EXPECT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
  290. `ASSERT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \
  291. `ASSERT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \
  292. `ASSERT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \
  293. `ASSERT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)`
  294. \
  295. `ASSERT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)`
  296. Verifies that the predicate *`pred_formatter`* succeeds when passed the given
  297. values as arguments.
  298. The parameter *`pred_formatter`* is a *predicate-formatter*, which is a function
  299. or functor with the signature:
  300. ```cpp
  301. testing::AssertionResult PredicateFormatter(const char* expr1,
  302. const char* expr2,
  303. ...
  304. const char* exprn,
  305. T1 val1,
  306. T2 val2,
  307. ...
  308. Tn valn);
  309. ```
  310. where *`val1`*, *`val2`*, ..., *`valn`* are the values of the predicate
  311. arguments, and *`expr1`*, *`expr2`*, ..., *`exprn`* are the corresponding
  312. expressions as they appear in the source code. The types `T1`, `T2`, ..., `Tn`
  313. can be either value types or reference types; if an argument has type `T`, it
  314. can be declared as either `T` or `const T&`, whichever is appropriate. For more
  315. about the return type `testing::AssertionResult`, see
  316. [Using a Function That Returns an AssertionResult](../advanced.md#using-a-function-that-returns-an-assertionresult).
  317. As an example, see the following code:
  318. ```cpp
  319. // Returns the smallest prime common divisor of m and n,
  320. // or 1 when m and n are mutually prime.
  321. int SmallestPrimeCommonDivisor(int m, int n) { ... }
  322. // Returns true if m and n have no common divisors except 1.
  323. bool MutuallyPrime(int m, int n) { ... }
  324. // A predicate-formatter for asserting that two integers are mutually prime.
  325. testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
  326. const char* n_expr,
  327. int m,
  328. int n) {
  329. if (MutuallyPrime(m, n)) return testing::AssertionSuccess();
  330. return testing::AssertionFailure() << m_expr << " and " << n_expr
  331. << " (" << m << " and " << n << ") are not mutually prime, "
  332. << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n);
  333. }
  334. ...
  335. const int a = 3;
  336. const int b = 4;
  337. const int c = 10;
  338. ...
  339. EXPECT_PRED_FORMAT2(AssertMutuallyPrime, a, b); // Succeeds
  340. EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c); // Fails
  341. ```
  342. In the above example, the final assertion fails and the predicate-formatter
  343. produces the following failure message:
  344. ```
  345. b and c (4 and 10) are not mutually prime, as they have a common divisor 2
  346. ```
  347. ## Windows HRESULT Assertions {#HRESULT}
  348. The following assertions test for `HRESULT` success or failure. For example:
  349. ```cpp
  350. CComPtr<IShellDispatch2> shell;
  351. ASSERT_HRESULT_SUCCEEDED(shell.CoCreateInstance(L"Shell.Application"));
  352. CComVariant empty;
  353. ASSERT_HRESULT_SUCCEEDED(shell->ShellExecute(CComBSTR(url), empty, empty, empty, empty));
  354. ```
  355. The generated output contains the human-readable error message associated with
  356. the returned `HRESULT` code.
  357. ### EXPECT_HRESULT_SUCCEEDED {#EXPECT_HRESULT_SUCCEEDED}
  358. `EXPECT_HRESULT_SUCCEEDED(`*`expression`*`)` \
  359. `ASSERT_HRESULT_SUCCEEDED(`*`expression`*`)`
  360. Verifies that *`expression`* is a success `HRESULT`.
  361. ### EXPECT_HRESULT_FAILED {#EXPECT_HRESULT_FAILED}
  362. `EXPECT_HRESULT_FAILED(`*`expression`*`)` \
  363. `EXPECT_HRESULT_FAILED(`*`expression`*`)`
  364. Verifies that *`expression`* is a failure `HRESULT`.
  365. ## Death Assertions {#death}
  366. The following assertions verify that a piece of code causes the process to
  367. terminate. For context, see [Death Tests](../advanced.md#death-tests).
  368. These assertions spawn a new process and execute the code under test in that
  369. process. How that happens depends on the platform and the variable
  370. `::testing::GTEST_FLAG(death_test_style)`, which is initialized from the
  371. command-line flag `--gtest_death_test_style`.
  372. * On POSIX systems, `fork()` (or `clone()` on Linux) is used to spawn the
  373. child, after which:
  374. * If the variable's value is `"fast"`, the death test statement is
  375. immediately executed.
  376. * If the variable's value is `"threadsafe"`, the child process re-executes
  377. the unit test binary just as it was originally invoked, but with some
  378. extra flags to cause just the single death test under consideration to
  379. be run.
  380. * On Windows, the child is spawned using the `CreateProcess()` API, and
  381. re-executes the binary to cause just the single death test under
  382. consideration to be run - much like the `"threadsafe"` mode on POSIX.
  383. Other values for the variable are illegal and will cause the death test to fail.
  384. Currently, the flag's default value is
  385. **`"fast"`**.
  386. If the death test statement runs to completion without dying, the child process
  387. will nonetheless terminate, and the assertion fails.
  388. Note that the piece of code under test can be a compound statement, for example:
  389. ```cpp
  390. EXPECT_DEATH({
  391. int n = 5;
  392. DoSomething(&n);
  393. }, "Error on line .* of DoSomething()");
  394. ```
  395. ### EXPECT_DEATH {#EXPECT_DEATH}
  396. `EXPECT_DEATH(`*`statement`*`,`*`matcher`*`)` \
  397. `ASSERT_DEATH(`*`statement`*`,`*`matcher`*`)`
  398. Verifies that *`statement`* causes the process to terminate with a nonzero exit
  399. status and produces `stderr` output that matches *`matcher`*.
  400. The parameter *`matcher`* is either a [matcher](matchers.md) for a `const
  401. std::string&`, or a regular expression (see
  402. [Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare
  403. string *`s`* (with no matcher) is treated as
  404. [`ContainsRegex(s)`](matchers.md#string-matchers), **not**
  405. [`Eq(s)`](matchers.md#generic-comparison).
  406. For example, the following code verifies that calling `DoSomething(42)` causes
  407. the process to die with an error message that contains the text `My error`:
  408. ```cpp
  409. EXPECT_DEATH(DoSomething(42), "My error");
  410. ```
  411. ### EXPECT_DEATH_IF_SUPPORTED {#EXPECT_DEATH_IF_SUPPORTED}
  412. `EXPECT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)` \
  413. `ASSERT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)`
  414. If death tests are supported, behaves the same as
  415. [`EXPECT_DEATH`](#EXPECT_DEATH). Otherwise, verifies nothing.
  416. ### EXPECT_DEBUG_DEATH {#EXPECT_DEBUG_DEATH}
  417. `EXPECT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)` \
  418. `ASSERT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)`
  419. In debug mode, behaves the same as [`EXPECT_DEATH`](#EXPECT_DEATH). When not in
  420. debug mode (i.e. `NDEBUG` is defined), just executes *`statement`*.
  421. ### EXPECT_EXIT {#EXPECT_EXIT}
  422. `EXPECT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)` \
  423. `ASSERT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)`
  424. Verifies that *`statement`* causes the process to terminate with an exit status
  425. that satisfies *`predicate`*, and produces `stderr` output that matches
  426. *`matcher`*.
  427. The parameter *`predicate`* is a function or functor that accepts an `int` exit
  428. status and returns a `bool`. GoogleTest provides two predicates to handle common
  429. cases:
  430. ```cpp
  431. // Returns true if the program exited normally with the given exit status code.
  432. ::testing::ExitedWithCode(exit_code);
  433. // Returns true if the program was killed by the given signal.
  434. // Not available on Windows.
  435. ::testing::KilledBySignal(signal_number);
  436. ```
  437. The parameter *`matcher`* is either a [matcher](matchers.md) for a `const
  438. std::string&`, or a regular expression (see
  439. [Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare
  440. string *`s`* (with no matcher) is treated as
  441. [`ContainsRegex(s)`](matchers.md#string-matchers), **not**
  442. [`Eq(s)`](matchers.md#generic-comparison).
  443. For example, the following code verifies that calling `NormalExit()` causes the
  444. process to print a message containing the text `Success` to `stderr` and exit
  445. with exit status code 0:
  446. ```cpp
  447. EXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), "Success");
  448. ```