@@ -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) {
0 commit comments