File tree Expand file tree Collapse file tree 2 files changed +33
-14
lines changed Expand file tree Collapse file tree 2 files changed +33
-14
lines changed Original file line number Diff line number Diff line change 1- use std:: io:: Write ;
2- use std:: str:: from_utf8;
1+ use std:: path:: Path ;
32
4- use super :: * ;
3+ use crate :: alphabetical:: check_lines;
4+ use crate :: diagnostics:: DiagCtx ;
55
66#[ track_caller]
77fn test ( lines : & str , name : & str , expected_msg : & str , expected_bad : bool ) {
8- let mut actual_msg = Vec :: new ( ) ;
9- let mut actual_bad = false ;
10- let mut err = |args : & _ | {
11- write ! ( & mut actual_msg, "{args}" ) ?;
12- Ok ( ( ) )
13- } ;
14- check_lines ( & name, lines. lines ( ) . enumerate ( ) , & mut err, & mut actual_bad) ;
15- assert_eq ! ( expected_msg, from_utf8( & actual_msg) . unwrap( ) ) ;
16- assert_eq ! ( expected_bad, actual_bad) ;
8+ let diag_ctx = DiagCtx :: new ( Path :: new ( "/" ) , false ) ;
9+ let mut check = diag_ctx. start_check ( "alphabetical-test" ) ;
10+ check_lines ( & name, lines. lines ( ) . enumerate ( ) , & mut check) ;
11+
12+ assert_eq ! ( expected_bad, check. is_bad( ) ) ;
13+ let errors = check. get_errors ( ) ;
14+ if expected_bad {
15+ assert_eq ! ( errors. len( ) , 1 ) ;
16+ assert_eq ! ( expected_msg, errors[ 0 ] ) ;
17+ } else {
18+ assert ! ( errors. is_empty( ) ) ;
19+ }
1720}
1821
1922#[ track_caller]
Original file line number Diff line number Diff line change @@ -35,7 +35,13 @@ impl DiagCtx {
3535 } ;
3636
3737 ctx. start_check ( id. clone ( ) ) ;
38- RunningCheck { id, bad : false , ctx : self . 0 . clone ( ) }
38+ RunningCheck {
39+ id,
40+ bad : false ,
41+ ctx : self . 0 . clone ( ) ,
42+ #[ cfg( test) ]
43+ errors : vec ! [ ] ,
44+ }
3945 }
4046
4147 pub fn into_failed_checks ( self ) -> Vec < FinishedCheck > {
@@ -135,13 +141,18 @@ pub struct RunningCheck {
135141 id : CheckId ,
136142 bad : bool ,
137143 ctx : Arc < Mutex < DiagCtxInner > > ,
144+ #[ cfg( test) ]
145+ errors : Vec < String > ,
138146}
139147
140148impl RunningCheck {
141149 /// Immediately output an error and mark the check as failed.
142150 pub fn error < T : Display > ( & mut self , msg : T ) {
143151 self . mark_as_bad ( ) ;
144- output_message ( & msg. to_string ( ) , Some ( & self . id ) , Some ( COLOR_ERROR ) ) ;
152+ let msg = msg. to_string ( ) ;
153+ output_message ( & msg, Some ( & self . id ) , Some ( COLOR_ERROR ) ) ;
154+ #[ cfg( test) ]
155+ self . errors . push ( msg) ;
145156 }
146157
147158 /// Immediately output a warning.
@@ -171,6 +182,11 @@ impl RunningCheck {
171182 self . ctx . lock ( ) . unwrap ( ) . verbose
172183 }
173184
185+ #[ cfg( test) ]
186+ pub fn get_errors ( & self ) -> Vec < String > {
187+ self . errors . clone ( )
188+ }
189+
174190 fn mark_as_bad ( & mut self ) {
175191 self . bad = true ;
176192 }
You can’t perform that action at this time.
0 commit comments