@@ -9,52 +9,81 @@ This library provides:
99
1010## Assertions and matchers
1111
12- Most assertions are made through the macro [ ` verify_that! ` ] . It takes two
13- arguments: an actual value to be tested and a [ ` Matcher ` ] .
12+ The core of GoogleTest is its * matchers* . Matchers indicate what aspect of an
13+ actual value one is asserting: (in-)equality, containment, regular expression
14+ matching, and so on.
15+
16+ To make an assertion using a matcher, GoogleTest offers three macros:
17+
18+ * [ ` assert_that! ` ] panics if the assertion fails, aborting the test.
19+ * [ ` expect_that! ` ] logs an assertion failure, marking the test as having
20+ failed, but allows the test to continue running (called a _ non-fatal
21+ assertion_ ). It requires the use of the [ ` googletest::test ` ] [ crate::test ]
22+ attribute macro on the test itself.
23+ * [ ` verify_that! ` ] has no side effects and evaluates to a [ ` Result ` ] whose
24+ ` Err ` variant describes the assertion failure, if there is one. In
25+ combination with the
26+ [ ` ? ` operator] ( https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator ) ,
27+ this can be used to abort the test on assertion failure without panicking. It
28+ is also the building block for the other two macros above.
29+
30+ For example:
1431
15- Unlike the macros used in other test assertion libraries in Rust,
16- ` verify_that! ` does not panic when the test assertion fails. Instead, it
17- evaluates to [ ` googletest::Result<()> ` ] [ Result ] , which the caller can choose
18- to handle by:
32+ ```
33+ use googletest::prelude::*;
1934
20- * Returning immediately from the function with the ` ? ` operator (a * fatal*
21- assertion), or
22- * Logging the failure, marking the test as failed, and allowing execution to
23- continue (see [ Non-fatal assertions] ( #non-fatal-assertions ) below).
35+ # /* The attribute macro would prevent the function from being compiled in a doctest.
36+ #[test]
37+ # */
38+ fn fails_and_panics() {
39+ let value = 2;
40+ assert_that!(value, eq(4));
41+ }
2442
25- For example, for fatal assertions:
43+ # /* The attribute macro would prevent the function from being compiled in a doctest.
44+ #[googletest::test]
45+ # */
46+ fn two_logged_failures() {
47+ let value = 2;
48+ expect_that!(value, eq(4)); // Test now failed, but continues executing.
49+ expect_that!(value, eq(5)); // Second failure is also logged.
50+ }
2651
27- ```
28- use googletest::prelude::*;
52+ # /* The attribute macro would prevent the function from being compiled in a doctest.
53+ #[test]
54+ # */
55+ fn fails_immediately_without_panic() -> Result<()> {
56+ let value = 2;
57+ verify_that!(value, eq(4))?; // Test fails and aborts.
58+ verify_that!(value, eq(2))?; // Never executes.
59+ Ok(())
60+ }
2961
3062# /* The attribute macro would prevent the function from being compiled in a doctest.
3163#[test]
3264# */
33- fn more_than_one_failure () -> Result<()> {
65+ fn simple_assertion () -> Result<()> {
3466 let value = 2;
35- verify_that!(value, eq(4))?; // Fails and ends execution of the test.
36- verify_that!(value, eq(2)) // One can also just return the assertion result.
67+ verify_that!(value, eq(4)) // One can also just return the last assertion.
3768}
38- # more_than_one_failure().unwrap_err();
3969```
4070
41- > In case one wants behaviour closer to other Rust test libraries, the macro
42- > [ ` assert_that! ` ] has the same parameters as [ ` verify_that! ` ] but panics on
43- > failure.
44-
4571Matchers are composable:
4672
4773```
4874use googletest::prelude::*;
4975
5076# /* The attribute macro would prevent the function from being compiled in a doctest.
51- #[test]
77+ #[googletest:: test]
5278# */
53- fn contains_at_least_one_item_at_least_3() -> Result<()> {
79+ fn contains_at_least_one_item_at_least_3() {
80+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
5481 let value = vec![1, 2, 3];
55- verify_that!(value, contains(ge(3)))
82+ expect_that!(value, contains(ge(3)));
83+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
84+ # .unwrap();
5685}
57- # contains_at_least_one_item_at_least_3().unwrap() ;
86+ # contains_at_least_one_item_at_least_3();
5887```
5988
6089They can also be logically combined:
@@ -63,13 +92,16 @@ They can also be logically combined:
6392use googletest::prelude::*;
6493
6594# /* The attribute macro would prevent the function from being compiled in a doctest.
66- #[test]
95+ #[googletest:: test]
6796# */
68- fn strictly_between_9_and_11() -> Result<()> {
97+ fn strictly_between_9_and_11() {
98+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
6999 let value = 10;
70- verify_that!(value, gt(9).and(not(ge(11))))
100+ expect_that!(value, gt(9).and(not(ge(11))));
101+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
102+ # .unwrap();
71103}
72- # strictly_between_9_and_11().unwrap() ;
104+ # strictly_between_9_and_11();
73105```
74106
75107## Available matchers
@@ -236,7 +268,7 @@ impl<T: PartialEq + Debug> Matcher for MyEqMatcher<T> {
236268 }
237269 ```
238270
239- The new matcher can then be used in ` verify_that! ` :
271+ The new matcher can then be used in the assertion macros :
240272
241273```
242274# use googletest::prelude::*;
@@ -274,12 +306,15 @@ impl<T: PartialEq + Debug> Matcher for MyEqMatcher<T> {
274306# MyEqMatcher { expected }
275307# }
276308# /* The attribute macro would prevent the function from being compiled in a doctest.
277- #[test]
309+ #[googletest:: test]
278310# */
279- fn should_be_equal_by_my_definition() -> Result<()> {
280- verify_that!(10, eq_my_way(10))
311+ fn should_be_equal_by_my_definition() {
312+ # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
313+ expect_that!(10, eq_my_way(10));
314+ # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
315+ # .unwrap();
281316}
282- # should_be_equal_by_my_definition().unwrap() ;
317+ # should_be_equal_by_my_definition();
283318```
284319
285320## Non-fatal assertions
@@ -290,8 +325,8 @@ having failed, but execution continues until the test completes or otherwise
290325aborts.
291326
292327To make a non-fatal assertion, use the macro [ ` expect_that! ` ] . The test must
293- also be marked with [ ` googletest::test ` ] [ test ] instead of the Rust-standard
294- ` #[test] ` .
328+ also be marked with [ ` googletest::test ` ] [ crate:: test] instead of the
329+ Rust-standard ` #[test] ` .
295330
296331``` no_run
297332use googletest::prelude::*;
0 commit comments