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 ;
3
2
4
- use super :: * ;
3
+ use crate :: alphabetical:: check_lines;
4
+ use crate :: diagnostics:: DiagCtx ;
5
5
6
6
#[ track_caller]
7
7
fn 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
+ }
17
20
}
18
21
19
22
#[ track_caller]
Original file line number Diff line number Diff line change @@ -35,7 +35,13 @@ impl DiagCtx {
35
35
} ;
36
36
37
37
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
+ }
39
45
}
40
46
41
47
pub fn into_failed_checks ( self ) -> Vec < FinishedCheck > {
@@ -135,13 +141,18 @@ pub struct RunningCheck {
135
141
id : CheckId ,
136
142
bad : bool ,
137
143
ctx : Arc < Mutex < DiagCtxInner > > ,
144
+ #[ cfg( test) ]
145
+ errors : Vec < String > ,
138
146
}
139
147
140
148
impl RunningCheck {
141
149
/// Immediately output an error and mark the check as failed.
142
150
pub fn error < T : Display > ( & mut self , msg : T ) {
143
151
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) ;
145
156
}
146
157
147
158
/// Immediately output a warning.
@@ -171,6 +182,11 @@ impl RunningCheck {
171
182
self . ctx . lock ( ) . unwrap ( ) . verbose
172
183
}
173
184
185
+ #[ cfg( test) ]
186
+ pub fn get_errors ( & self ) -> Vec < String > {
187
+ self . errors . clone ( )
188
+ }
189
+
174
190
fn mark_as_bad ( & mut self ) {
175
191
self . bad = true ;
176
192
}
You can’t perform that action at this time.
0 commit comments