1
+ use core:: fmt:: Display ;
1
2
use libtest_mimic:: Failed ;
2
3
use std:: error:: Error ;
3
4
4
5
/// Result of a test.
5
6
pub type Result = std:: result:: Result < ( ) , Box < dyn Error > > ;
6
7
7
8
#[ doc( hidden) ]
9
+
8
10
pub struct InnerTestError {
9
11
msg : String ,
10
12
}
@@ -21,6 +23,13 @@ impl Display for InnerTestError {
21
23
}
22
24
}
23
25
26
+ #[ cfg( feature = "googletest" ) ]
27
+ impl From < googletest:: internal:: test_outcome:: TestFailure > for InnerTestError {
28
+ fn from ( e : googletest:: internal:: test_outcome:: TestFailure ) -> Self {
29
+ Self { msg : e. to_string ( ) }
30
+ }
31
+ }
32
+
24
33
/// The result of test runned by rustest.
25
34
///
26
35
/// InnerTestResult is necessary as we are somehow between user (`Result`) and libtest_mimic (`LibTestResult`).
@@ -58,13 +67,42 @@ impl IntoError for Result {
58
67
}
59
68
}
60
69
70
+ #[ cfg( feature = "googletest" ) ]
71
+ impl < T > IntoError for googletest:: Result < T > {
72
+ fn into_error ( self ) -> InnerTestResult {
73
+ self . map ( |_v| ( ) )
74
+ . map_err ( |e| InnerTestError :: new ( e. to_string ( ) ) )
75
+ }
76
+ }
77
+
61
78
/// An actual test run by rustest
62
79
pub struct Test {
63
80
name : String ,
64
81
runner : Box < dyn FnOnce ( ) -> InnerTestResult + Send + std:: panic:: UnwindSafe > ,
65
82
xfail : bool ,
66
83
}
67
84
85
+ fn setup_gtest ( ) {
86
+ #[ cfg( feature = "googletest" ) ]
87
+ {
88
+ use googletest:: internal:: test_outcome:: TestOutcome ;
89
+ TestOutcome :: init_current_test_outcome ( ) ;
90
+ }
91
+ }
92
+
93
+ fn collect_gtest ( test_result : InnerTestResult ) -> InnerTestResult {
94
+ #[ cfg( not( feature = "googletest" ) ) ]
95
+ {
96
+ test_result
97
+ }
98
+
99
+ #[ cfg( feature = "googletest" ) ]
100
+ {
101
+ use googletest:: internal:: test_outcome:: TestOutcome ;
102
+ TestOutcome :: close_current_test_outcome ( test_result) . map_err ( |e| e. into ( ) )
103
+ }
104
+ }
105
+
68
106
impl Test {
69
107
/// Build a new test.
70
108
pub fn new < F > ( name : impl Into < String > , xfail : bool , runner : F ) -> Self
@@ -78,6 +116,7 @@ impl Test {
78
116
}
79
117
}
80
118
fn run ( self ) -> LibTestResult {
119
+ setup_gtest ( ) ;
81
120
let unwind_result = std:: panic:: catch_unwind ( self . runner ) ;
82
121
let test_result = match unwind_result {
83
122
Ok ( Ok ( ( ) ) ) => Ok ( ( ) ) ,
@@ -92,6 +131,7 @@ impl Test {
92
131
Err ( InnerTestError :: new ( payload) )
93
132
}
94
133
} ;
134
+ let test_result = collect_gtest ( test_result) ;
95
135
if self . xfail {
96
136
match test_result {
97
137
Ok ( _) => Err ( "Test should fail" . into ( ) ) ,
0 commit comments