@@ -108,17 +108,17 @@ extern crate difference;
108
108
#[ macro_use] extern crate error_chain;
109
109
extern crate rustc_serialize;
110
110
111
- use std:: process:: { Command , Output } ;
112
- use std:: fmt;
113
-
114
- use difference:: Changeset ;
111
+ use std:: process:: Command ;
115
112
116
113
mod errors;
117
114
use errors:: * ;
118
115
119
116
#[ macro_use] mod macros;
120
117
pub use macros:: flatten_escaped_string;
121
118
119
+ mod output;
120
+ use output:: { OutputAssertion , StdErr , StdOut } ;
121
+
122
122
mod diff;
123
123
124
124
/// Assertions for a specific command.
@@ -127,38 +127,8 @@ pub struct Assert {
127
127
cmd : Vec < String > ,
128
128
expect_success : Option < bool > ,
129
129
expect_exit_code : Option < i32 > ,
130
- expect_stdout : Option < OutputAssertion > ,
131
- expect_stderr : Option < OutputAssertion > ,
132
- }
133
-
134
- #[ derive( Debug ) ]
135
- struct OutputAssertion {
136
- expect : String ,
137
- fuzzy : bool ,
138
- }
139
-
140
- #[ derive( Debug , Copy , Clone ) ]
141
- enum OutputType {
142
- StdOut ,
143
- StdErr ,
144
- }
145
-
146
- impl OutputType {
147
- fn select < ' a > ( & self , o : & ' a Output ) -> & ' a [ u8 ] {
148
- match * self {
149
- OutputType :: StdOut => & o. stdout ,
150
- OutputType :: StdErr => & o. stderr ,
151
- }
152
- }
153
- }
154
-
155
- impl fmt:: Display for OutputType {
156
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
157
- match * self {
158
- OutputType :: StdOut => write ! ( f, "stdout" ) ,
159
- OutputType :: StdErr => write ! ( f, "stderr" ) ,
160
- }
161
- }
130
+ expect_stdout : Option < OutputAssertion < StdOut > > ,
131
+ expect_stderr : Option < OutputAssertion < StdErr > > ,
162
132
}
163
133
164
134
impl std:: default:: Default for Assert {
@@ -318,6 +288,7 @@ impl Assert {
318
288
self . expect_stdout = Some ( OutputAssertion {
319
289
expect : output. into ( ) ,
320
290
fuzzy : true ,
291
+ kind : StdOut ,
321
292
} ) ;
322
293
self
323
294
}
@@ -337,6 +308,7 @@ impl Assert {
337
308
self . expect_stdout = Some ( OutputAssertion {
338
309
expect : output. into ( ) ,
339
310
fuzzy : false ,
311
+ kind : StdOut ,
340
312
} ) ;
341
313
self
342
314
}
@@ -358,6 +330,7 @@ impl Assert {
358
330
self . expect_stderr = Some ( OutputAssertion {
359
331
expect : output. into ( ) ,
360
332
fuzzy : true ,
333
+ kind : StdErr ,
361
334
} ) ;
362
335
self
363
336
}
@@ -379,6 +352,7 @@ impl Assert {
379
352
self . expect_stderr = Some ( OutputAssertion {
380
353
expect : output. into ( ) ,
381
354
fuzzy : false ,
355
+ kind : StdErr ,
382
356
} ) ;
383
357
self
384
358
}
@@ -421,52 +395,15 @@ impl Assert {
421
395
) ) ;
422
396
}
423
397
424
- self . assert_output ( OutputType :: StdOut , & output) ?;
425
- self . assert_output ( OutputType :: StdErr , & output) ?;
426
-
427
- Ok ( ( ) )
428
- }
429
-
430
- /// Perform the appropriate output assertion.
431
- fn assert_output ( & self , output_type : OutputType , output : & Output ) -> Result < ( ) > {
432
- let observed = String :: from_utf8_lossy ( output_type. select ( output) ) ;
433
- match * self . expect_output ( output_type) {
434
- Some ( OutputAssertion {
435
- expect : ref expected_output,
436
- fuzzy : true ,
437
- } ) if !observed. contains ( expected_output) => {
438
- bail ! ( ErrorKind :: OutputMismatch (
439
- output_type. to_string( ) ,
440
- self . cmd. clone( ) ,
441
- expected_output. clone( ) ,
442
- observed. into( ) ,
443
- ) ) ;
444
- } ,
445
- Some ( OutputAssertion {
446
- expect : ref expected_output,
447
- fuzzy : false ,
448
- } ) => {
449
- let differences = Changeset :: new ( expected_output. trim ( ) , observed. trim ( ) , "\n " ) ;
450
- if differences. distance > 0 {
451
- let nice_diff = diff:: render ( & differences) ?;
452
- bail ! ( ErrorKind :: ExactOutputMismatch (
453
- output_type. to_string( ) ,
454
- self . cmd. clone( ) ,
455
- nice_diff
456
- ) ) ;
457
- }
458
- } ,
459
- _ => { } ,
398
+ if let Some ( ouput_assertion) = self . expect_stdout {
399
+ ouput_assertion. execute ( & output) ?;
460
400
}
461
- Ok ( ( ) )
462
- }
463
401
464
- /// Return a reference to the appropriate output assertion.
465
- fn expect_output ( & self , output_type : OutputType ) -> & Option < OutputAssertion > {
466
- match output_type {
467
- OutputType :: StdOut => & self . expect_stdout ,
468
- OutputType :: StdErr => & self . expect_stderr ,
402
+ if let Some ( ouput_assertion) = self . expect_stderr {
403
+ ouput_assertion. execute ( & output) ?;
469
404
}
405
+
406
+ Ok ( ( ) )
470
407
}
471
408
472
409
/// Execute the command, check the assertions, and panic when they fail.
0 commit comments