@@ -7,7 +7,7 @@ use std::str;
77use predicates;
88use predicates:: str:: PredicateStrExt ;
99
10- use cmd :: dump_buffer;
10+ use errors :: dump_buffer;
1111use errors:: output_fmt;
1212
1313/// Assert the state of an `Output`.
@@ -51,7 +51,7 @@ impl OutputAssertExt for process::Output {
5151impl < ' c > OutputAssertExt for & ' c mut process:: Command {
5252 fn assert ( self ) -> Assert {
5353 let output = self . output ( ) . unwrap ( ) ;
54- Assert :: new ( output) . set_cmd ( format ! ( "{:?}" , self ) )
54+ Assert :: new ( output) . append_context ( "command" , format ! ( "{:?}" , self ) )
5555 }
5656}
5757
@@ -71,32 +71,26 @@ impl<'c> OutputAssertExt for &'c mut process::Command {
7171/// .assert()
7272/// .success();
7373/// ```
74- #[ derive( Debug ) ]
7574pub struct Assert {
7675 output : process:: Output ,
77- cmd : Option < String > ,
78- stdin : Option < Vec < u8 > > ,
76+ context : Vec < ( & ' static str , Box < fmt:: Display > ) > ,
7977}
8078
8179impl Assert {
8280 /// Create an `Assert` for a given `Output`.
8381 pub fn new ( output : process:: Output ) -> Self {
8482 Self {
8583 output,
86- cmd : None ,
87- stdin : None ,
84+ context : vec ! [ ] ,
8885 }
8986 }
9087
91- /// Add the command line for additional context.
92- pub fn set_cmd ( mut self , cmd : String ) -> Self {
93- self . cmd = Some ( cmd) ;
94- self
95- }
96-
97- /// Add the `stdn` for additional context.
98- pub fn set_stdin ( mut self , stdin : Vec < u8 > ) -> Self {
99- self . stdin = Some ( stdin) ;
88+ /// Clarify failures with additional context.
89+ pub fn append_context < D > ( mut self , name : & ' static str , context : D ) -> Self
90+ where
91+ D : fmt:: Display + ' static ,
92+ {
93+ self . context . push ( ( name, Box :: new ( context) ) ) ;
10094 self
10195 }
10296
@@ -298,20 +292,21 @@ impl Assert {
298292
299293impl fmt:: Display for Assert {
300294 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
301- if let Some ( ref cmd) = self . cmd {
302- writeln ! ( f, "command=`{}`" , cmd) ?;
303- }
304- if let Some ( ref stdin) = self . stdin {
305- if let Ok ( stdin) = str:: from_utf8 ( stdin) {
306- writeln ! ( f, "stdin=```{}```" , stdin) ?;
307- } else {
308- writeln ! ( f, "stdin=```{:?}```" , stdin) ?;
309- }
295+ for & ( ref name, ref context) in & self . context {
296+ writeln ! ( f, "{}=`{}`" , name, context) ?;
310297 }
311298 output_fmt ( & self . output , f)
312299 }
313300}
314301
302+ impl fmt:: Debug for Assert {
303+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
304+ f. debug_struct ( "Assert" )
305+ . field ( "output" , & self . output )
306+ . finish ( )
307+ }
308+ }
309+
315310/// Used by `Assert::code` to convert `Self` into the needed `Predicate<i32>`.
316311///
317312/// # Examples
0 commit comments