Skip to content

Commit f1ca25f

Browse files
hovinenbcopybara-github
authored andcommitted
Rewrite README and crate-level documentation to give a frontline summary of the *_that! assertion macros.
This should make the existence of and difference between these macros clearer up front to the reader. The `assert_that!` and `expect_that!` macros are listed first to nudge users to those rather than `verify_that!`. This change also adds more examples of assertions in the beginning of the top-level documentation and changes the examples in crate-level documentation to use `expect_that!` rather than `verify_that!` when it does not otherwise matter. Also fixes the Tokio dependency in the integration tests to 1.29.1, since the just released 1.32.0 appears to have a higher MSRV than this crate. PiperOrigin-RevId: 558057297
1 parent 599fe7c commit f1ca25f

File tree

3 files changed

+126
-72
lines changed

3 files changed

+126
-72
lines changed

README.md

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,57 @@ version 1.66 for the best developer experience.
3333
3434
## Assertions and matchers
3535

36-
Most assertions are made through the macro [`verify_that!`] which evaluates to a
37-
[`Result<()>`]. It takes two arguments: an actual value to be tested and a
38-
[`Matcher`].
36+
The core of GoogleTest is its *matchers*. Matchers indicate what aspect of an
37+
actual value one is asserting: (in-)equality, containment, regular expression
38+
matching, and so on.
39+
40+
To make an assertion using a matcher, GoogleTest offers three macros:
41+
42+
* [`assert_that!`] panics if the assertion fails, aborting the test.
43+
* [`expect_that!`] logs an assertion failure, marking the test as having
44+
failed, but allows the test to continue running (called a _non-fatal
45+
assertion_). It requires the use of the [`googletest::test`] attribute macro
46+
on the test itself.
47+
* [`verify_that!`] has no side effects and evaluates to a [`Result`] whose
48+
`Err` variant describes the assertion failure, if there is one. In
49+
combination with the
50+
[`?` operator](https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator),
51+
this can be used to abort the test on assertion failure without panicking. It
52+
is also the building block for the other two macros above.
53+
54+
For example:
3955

40-
Unlike the macros used in other test assertion libraries in Rust, `verify_that!`
41-
does not panic when the test assertion fails. Instead, it evaluates to `Result`,
42-
which the caller can choose to handle by:
43-
44-
* Returning immediately from the function with the `?` operator (a *fatal*
45-
assertion), or
46-
* Logging the failure, marking the test as failed, and allowing execution to
47-
continue (see [Non-fatal assertions](#non-fatal-assertions) below).
56+
```rust
57+
use googletest::prelude::*;
4858

49-
Fatal assertions are analogous to the `ASSERT_*` family of macros in GoogleTest.
59+
#[test]
60+
fn fails_and_panics() {
61+
let value = 2;
62+
assert_that!(value, eq(4));
63+
}
5064

51-
For example, for fatal assertions:
65+
#[googletest::test]
66+
fn two_logged_failures() {
67+
let value = 2;
68+
expect_that!(value, eq(4)); // Test now failed, but continues executing.
69+
expect_that!(value, eq(5)); // Second failure is also logged.
70+
}
5271

53-
```rust
54-
use googletest::prelude::*;
72+
#[test]
73+
fn fails_immediately_without_panic() -> Result<()> {
74+
let value = 2;
75+
verify_that!(value, eq(4))?; // Test fails and aborts.
76+
verify_that!(value, eq(2))?; // Never executes.
77+
Ok(())
78+
}
5579

5680
#[test]
57-
fn more_than_one_failure() -> Result<()> {
81+
fn simple_assertion() -> Result<()> {
5882
let value = 2;
59-
verify_that!(value, eq(4))?; // Fails and ends execution of the test.
60-
verify_that!(value, eq(2)) // One can also just return the assertion result.
83+
verify_that!(value, eq(4)) // One can also just return the last assertion.
6184
}
6285
```
6386

64-
> In case one wants behaviour closer to other Rust test libraries, the macro
65-
> [`assert_that!`] has the same parameters as [`verify_that!`] but panics on
66-
> failure.
67-
6887
This library includes a rich set of matchers, covering:
6988

7089
* Equality, numeric inequality, and approximate equality;
@@ -76,10 +95,10 @@ Matchers are composable:
7695
```rust
7796
use googletest::prelude::*;
7897

79-
#[test]
80-
fn contains_at_least_one_item_at_least_3() -> Result<()> {
98+
#[googletest::test]
99+
fn contains_at_least_one_item_at_least_3() {
81100
let value = vec![1, 2, 3];
82-
verify_that!(value, contains(ge(3)))
101+
expect_that!(value, contains(ge(3)));
83102
}
84103
```
85104

@@ -88,10 +107,10 @@ They can also be logically combined:
88107
```rust
89108
use googletest::prelude::*;
90109

91-
#[test]
92-
fn strictly_between_9_and_11() -> Result<()> {
110+
#[googletest::test]
111+
fn strictly_between_9_and_11() {
93112
let value = 10;
94-
verify_that!(value, gt(9).and(not(ge(11))))
113+
expect_that!(value, gt(9).and(not(ge(11))));
95114
}
96115
```
97116

@@ -110,17 +129,17 @@ struct AStruct {
110129
}
111130

112131
#[test]
113-
fn struct_has_expected_values() -> Result<()> {
132+
fn struct_has_expected_values() {
114133
let value = AStruct {
115134
a_field: 10,
116135
another_field: 100,
117136
a_third_field: "A correct value",
118137
};
119-
verify_that!(value, matches_pattern!(AStruct {
138+
expect_that!(value, matches_pattern!(AStruct {
120139
a_field: eq(10),
121140
another_field: gt(50),
122141
a_third_field: contains_substring("correct"),
123-
}))
142+
}));
124143
}
125144
```
126145

@@ -162,12 +181,12 @@ pub fn eq_my_way<T: PartialEq + Debug>(expected: T) -> impl Matcher<ActualT = T>
162181
}
163182
```
164183

165-
The new matcher can then be used in `verify_that!`:
184+
The new matcher can then be used in the assertion macros:
166185

167186
```rust
168-
#[test]
169-
fn should_be_equal_by_my_definition() -> Result<()> {
170-
verify_that!(10, eq_my_way(10))
187+
#[googletest::test]
188+
fn should_be_equal_by_my_definition() {
189+
expect_that!(10, eq_my_way(10));
171190
}
172191
```
173192

googletest/crate_docs.md

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4571
Matchers are composable:
4672

4773
```
4874
use 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

6089
They can also be logically combined:
@@ -63,13 +92,16 @@ They can also be logically combined:
6392
use 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
290325
aborts.
291326

292327
To 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
297332
use googletest::prelude::*;

integration_tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ googletest = { path = "../googletest", version = "0.9.0", features = ["anyhow"]
3232
anyhow = "1"
3333
indoc = "2"
3434
rstest = "0.13.0"
35-
tokio = { version = "1", features = ["time", "macros", "rt"] }
35+
tokio = { version = "=1.29.1", features = ["time", "macros", "rt"] }
3636

3737
[[bin]]
3838
name = "integration_tests"

0 commit comments

Comments
 (0)