Skip to content

Commit ba77526

Browse files
Merge pull request #290 from google:support-googletest-test-returning-unit
PiperOrigin-RevId: 553749719
2 parents 4e3d04a + b0de4d7 commit ba77526

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,21 @@ This is analogous to the `EXPECT_*` family of macros in GoogleTest.
181181

182182
To make a non-fatal assertion, use the macro [`expect_that!`]. The test must
183183
also be marked with [`googletest::test`] instead of the Rust-standard `#[test]`.
184-
It must return [`Result<()>`].
184+
185+
```rust
186+
use googletest::prelude::*;
187+
188+
#[googletest::test]
189+
fn three_non_fatal_assertions() {
190+
let value = 2;
191+
expect_that!(value, eq(2)); // Passes; test still considered passing.
192+
expect_that!(value, eq(3)); // Fails; logs failure and marks the test failed.
193+
expect_that!(value, eq(4)); // A second failure, also logged.
194+
}
195+
```
196+
197+
This can be used in the same tests as `verify_that!`, in which case the test
198+
function must also return [`Result<()>`]:
185199

186200
```rust
187201
use googletest::prelude::*;

googletest/crate_docs.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,22 @@ aborts.
291291

292292
To make a non-fatal assertion, use the macro [`expect_that!`]. The test must
293293
also be marked with [`googletest::test`][test] instead of the Rust-standard
294-
`#[test]`. It must return [`Result<()>`].
294+
`#[test]`.
295+
296+
```no_run
297+
use googletest::prelude::*;
298+
299+
#[googletest::test]
300+
fn three_non_fatal_assertions() {
301+
let value = 2;
302+
expect_that!(value, eq(2)); // Passes; test still considered passing.
303+
expect_that!(value, eq(3)); // Fails; logs failure and marks the test failed.
304+
expect_that!(value, eq(4)); // A second failure, also logged.
305+
}
306+
```
307+
308+
This can be used in the same tests as `verify_that!`, in which case the test
309+
function must also return [`Result<()>`]:
295310

296311
```no_run
297312
use googletest::prelude::*;

googletest_macro/src/lib.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,22 @@ use syn::{parse_macro_input, Attribute, ItemFn, ReturnType};
2121
///
2222
/// ```ignore
2323
/// #[googletest::test]
24-
/// fn should_work() -> googletest::Result {
24+
/// fn should_work() {
2525
/// ...
26-
/// Ok(())
2726
/// }
2827
/// ```
2928
///
30-
/// The test function should return [`googletest::Result`] so that one can use
31-
/// `verify_that!` with the question mark operator to abort execution. The last
32-
/// line of the test should return `Ok(())`.
33-
///
34-
/// Any function your test invokes which contains a `verify_that!` call should
35-
/// be invoked with the `?` operator so that a failure in the subroutine aborts
36-
/// the rest of the test execution:
29+
/// The test function is not required to have a return type. If it does have a
30+
/// return type, that type must be [`googletest::Result`]. One may do this if
31+
/// one wishes to use both fatal and non-fatal assertions in the same test. For
32+
/// example:
3733
///
3834
/// ```ignore
3935
/// #[googletest::test]
40-
/// fn should_work() -> googletest::Result {
41-
/// ...
42-
/// assert_that_everything_is_okay()?;
43-
/// do_some_more_stuff(); // Will not be executed if assert failed.
44-
/// Ok(())
45-
/// }
46-
///
47-
/// fn assert_that_everything_is_okay() -> googletest::Result {
48-
/// verify_that!(...)
36+
/// fn should_work() -> googletest::Result<()> {
37+
/// let value = 2;
38+
/// expect_that!(value, gt(0));
39+
/// verify_that!(value, eq(2))
4940
/// }
5041
/// ```
5142
///
@@ -59,14 +50,8 @@ pub fn test(
5950
let attrs = parsed_fn.attrs.drain(..).collect::<Vec<_>>();
6051
let (mut sig, block) = (parsed_fn.sig, parsed_fn.block);
6152
let output_type = match sig.output.clone() {
62-
ReturnType::Type(_, output_type) => output_type,
63-
_ => {
64-
return quote! {
65-
compile_error!(
66-
"Test function with the #[googletest::test] attribute must return googletest::Result<()>"
67-
);
68-
}.into()
69-
}
53+
ReturnType::Type(_, output_type) => Some(output_type),
54+
ReturnType::Default => None,
7055
};
7156
sig.output = ReturnType::Default;
7257
let (maybe_closure, invocation) = if sig.asyncness.is_some() {
@@ -93,14 +78,27 @@ pub fn test(
9378
},
9479
)
9580
};
96-
let function = quote! {
97-
#(#attrs)*
98-
#sig -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
99-
#maybe_closure
100-
use googletest::internal::test_outcome::TestOutcome;
101-
TestOutcome::init_current_test_outcome();
102-
let result: #output_type = #invocation;
103-
TestOutcome::close_current_test_outcome(result)
81+
let function = if let Some(output_type) = output_type {
82+
quote! {
83+
#(#attrs)*
84+
#sig -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
85+
#maybe_closure
86+
use googletest::internal::test_outcome::TestOutcome;
87+
TestOutcome::init_current_test_outcome();
88+
let result: #output_type = #invocation;
89+
TestOutcome::close_current_test_outcome(result)
90+
}
91+
}
92+
} else {
93+
quote! {
94+
#(#attrs)*
95+
#sig -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
96+
#maybe_closure
97+
use googletest::internal::test_outcome::TestOutcome;
98+
TestOutcome::init_current_test_outcome();
99+
#invocation;
100+
TestOutcome::close_current_test_outcome(googletest::Result::Ok(()))
101+
}
104102
}
105103
};
106104
let output = if attrs.iter().any(is_test_attribute) {

integration_tests/src/integration_tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ mod tests {
5050
Ok(())
5151
}
5252

53+
#[googletest::test]
54+
fn should_pass_with_expect_that_returning_unit() {
55+
let value = 2;
56+
expect_that!(value, eq(2));
57+
}
58+
5359
#[test]
5460
fn should_fail_on_assertion_failure() -> Result<()> {
5561
let status = run_external_process("simple_assertion_failure").status()?;

0 commit comments

Comments
 (0)