@@ -41,6 +41,46 @@ impl<T: Write> Write for OutputLocation<T> {
41
41
}
42
42
}
43
43
44
+ pub struct ConsoleTestDiscoveryState {
45
+ pub log_out : Option < File > ,
46
+ pub tests : usize ,
47
+ pub benchmarks : usize ,
48
+ pub ignored : usize ,
49
+ pub options : Options ,
50
+ }
51
+
52
+ impl ConsoleTestDiscoveryState {
53
+ pub fn new ( opts : & TestOpts ) -> io:: Result < ConsoleTestDiscoveryState > {
54
+ let log_out = match opts. logfile {
55
+ Some ( ref path) => Some ( File :: create ( path) ?) ,
56
+ None => None ,
57
+ } ;
58
+
59
+ Ok ( ConsoleTestDiscoveryState {
60
+ log_out,
61
+ tests : 0 ,
62
+ benchmarks : 0 ,
63
+ ignored : 0 ,
64
+ options : opts. options ,
65
+ } )
66
+ }
67
+
68
+ pub fn write_log < F , S > ( & mut self , msg : F ) -> io:: Result < ( ) >
69
+ where
70
+ S : AsRef < str > ,
71
+ F : FnOnce ( ) -> S ,
72
+ {
73
+ match self . log_out {
74
+ None => Ok ( ( ) ) ,
75
+ Some ( ref mut o) => {
76
+ let msg = msg ( ) ;
77
+ let msg = msg. as_ref ( ) ;
78
+ o. write_all ( msg. as_bytes ( ) )
79
+ }
80
+ }
81
+ }
82
+ }
83
+
44
84
pub struct ConsoleTestState {
45
85
pub log_out : Option < File > ,
46
86
pub total : usize ,
@@ -138,53 +178,44 @@ impl ConsoleTestState {
138
178
139
179
// List the tests to console, and optionally to logfile. Filters are honored.
140
180
pub fn list_tests_console ( opts : & TestOpts , tests : Vec < TestDescAndFn > ) -> io:: Result < ( ) > {
141
- let mut output = match term:: stdout ( ) {
181
+ let output = match term:: stdout ( ) {
142
182
None => OutputLocation :: Raw ( io:: stdout ( ) . lock ( ) ) ,
143
183
Some ( t) => OutputLocation :: Pretty ( t) ,
144
184
} ;
145
185
146
- let quiet = opts. format == OutputFormat :: Terse ;
147
- let mut st = ConsoleTestState :: new ( opts) ?;
148
-
149
- let mut ntest = 0 ;
150
- let mut nbench = 0 ;
186
+ let mut out: Box < dyn OutputFormatter > = match opts. format {
187
+ OutputFormat :: Pretty | OutputFormat :: Junit => {
188
+ Box :: new ( PrettyFormatter :: new ( output, false , 0 , false , None ) )
189
+ }
190
+ OutputFormat :: Terse => Box :: new ( TerseFormatter :: new ( output, false , 0 , false ) ) ,
191
+ OutputFormat :: Json => Box :: new ( JsonFormatter :: new ( output) ) ,
192
+ } ;
193
+ let mut st = ConsoleTestDiscoveryState :: new ( opts) ?;
151
194
195
+ out. write_discovery_start ( ) ?;
152
196
for test in filter_tests ( opts, tests) . into_iter ( ) {
153
197
use crate :: TestFn :: * ;
154
198
155
- let TestDescAndFn { desc : TestDesc { name , .. } , testfn } = test;
199
+ let TestDescAndFn { desc, testfn } = test;
156
200
157
201
let fntype = match testfn {
158
202
StaticTestFn ( ..) | DynTestFn ( ..) => {
159
- ntest += 1 ;
203
+ st . tests += 1 ;
160
204
"test"
161
205
}
162
206
StaticBenchFn ( ..) | DynBenchFn ( ..) => {
163
- nbench += 1 ;
207
+ st . benchmarks += 1 ;
164
208
"benchmark"
165
209
}
166
210
} ;
167
211
168
- writeln ! ( output, "{name}: {fntype}" ) ?;
169
- st. write_log ( || format ! ( "{fntype} {name}\n " ) ) ?;
170
- }
212
+ st. ignored += if desc. ignore { 1 } else { 0 } ;
171
213
172
- fn plural ( count : u32 , s : & str ) -> String {
173
- match count {
174
- 1 => format ! ( "1 {s}" ) ,
175
- n => format ! ( "{n} {s}s" ) ,
176
- }
214
+ out. write_test_discovered ( & desc, fntype) ?;
215
+ st. write_log ( || format ! ( "{fntype} {}\n " , desc. name) ) ?;
177
216
}
178
217
179
- if !quiet {
180
- if ntest != 0 || nbench != 0 {
181
- writeln ! ( output) ?;
182
- }
183
-
184
- writeln ! ( output, "{}, {}" , plural( ntest, "test" ) , plural( nbench, "benchmark" ) ) ?;
185
- }
186
-
187
- Ok ( ( ) )
218
+ out. write_discovery_finish ( & st)
188
219
}
189
220
190
221
// Updates `ConsoleTestState` depending on result of the test execution.
0 commit comments