@@ -21,31 +21,22 @@ use syn::{parse_macro_input, Attribute, ItemFn, ReturnType};
21
21
///
22
22
/// ```ignore
23
23
/// #[googletest::test]
24
- /// fn should_work() -> googletest::Result {
24
+ /// fn should_work() {
25
25
/// ...
26
- /// Ok(())
27
26
/// }
28
27
/// ```
29
28
///
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:
37
33
///
38
34
/// ```ignore
39
35
/// #[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))
49
40
/// }
50
41
/// ```
51
42
///
@@ -59,14 +50,8 @@ pub fn test(
59
50
let attrs = parsed_fn. attrs . drain ( ..) . collect :: < Vec < _ > > ( ) ;
60
51
let ( mut sig, block) = ( parsed_fn. sig , parsed_fn. block ) ;
61
52
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 ,
70
55
} ;
71
56
sig. output = ReturnType :: Default ;
72
57
let ( maybe_closure, invocation) = if sig. asyncness . is_some ( ) {
@@ -93,14 +78,27 @@ pub fn test(
93
78
} ,
94
79
)
95
80
} ;
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
+ }
104
102
}
105
103
} ;
106
104
let output = if attrs. iter ( ) . any ( is_test_attribute) {
0 commit comments