@@ -41,6 +41,46 @@ impl<T: Write> Write for OutputLocation<T> {
4141 }
4242}
4343
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+
4484pub struct ConsoleTestState {
4585 pub log_out : Option < File > ,
4686 pub total : usize ,
@@ -138,53 +178,44 @@ impl ConsoleTestState {
138178
139179// List the tests to console, and optionally to logfile. Filters are honored.
140180pub fn list_tests_console ( opts : & TestOpts , tests : Vec < TestDescAndFn > ) -> io:: Result < ( ) > {
141- let mut output = match term:: stdout ( ) {
181+ let output = match term:: stdout ( ) {
142182 None => OutputLocation :: Raw ( io:: stdout ( ) . lock ( ) ) ,
143183 Some ( t) => OutputLocation :: Pretty ( t) ,
144184 } ;
145185
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) ?;
151194
195+ out. write_discovery_start ( ) ?;
152196 for test in filter_tests ( opts, tests) . into_iter ( ) {
153197 use crate :: TestFn :: * ;
154198
155- let TestDescAndFn { desc : TestDesc { name , .. } , testfn } = test;
199+ let TestDescAndFn { desc, testfn } = test;
156200
157201 let fntype = match testfn {
158202 StaticTestFn ( ..) | DynTestFn ( ..) => {
159- ntest += 1 ;
203+ st . tests += 1 ;
160204 "test"
161205 }
162206 StaticBenchFn ( ..) | DynBenchFn ( ..) => {
163- nbench += 1 ;
207+ st . benchmarks += 1 ;
164208 "benchmark"
165209 }
166210 } ;
167211
168- writeln ! ( output, "{name}: {fntype}" ) ?;
169- st. write_log ( || format ! ( "{fntype} {name}\n " ) ) ?;
170- }
212+ st. ignored += if desc. ignore { 1 } else { 0 } ;
171213
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) ) ?;
177216 }
178217
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)
188219}
189220
190221// Updates `ConsoleTestState` depending on result of the test execution.
0 commit comments