@@ -8,6 +8,8 @@ use std::str;
8
8
9
9
use predicates;
10
10
use predicates:: str:: PredicateStrExt ;
11
+ use predicates_core;
12
+ use predicates_tree:: CaseTreeExt ;
11
13
12
14
use errors:: dump_buffer;
13
15
use errors:: output_fmt;
@@ -185,23 +187,15 @@ impl Assert {
185
187
/// ```
186
188
pub fn failure ( self ) -> Self {
187
189
if self . output . status . success ( ) {
188
- panic ! (
189
- "Unexpected success\n stdout=```{}```\n {}" ,
190
- dump_buffer( & self . output. stdout) ,
191
- self
192
- ) ;
190
+ panic ! ( "Unexpected success\n {}" , self ) ;
193
191
}
194
192
self
195
193
}
196
194
197
195
/// Ensure the command aborted before returning a code.
198
196
pub fn interrupted ( self ) -> Self {
199
197
if self . output . status . code ( ) . is_some ( ) {
200
- panic ! (
201
- "Unexpected completion\n stdout=```{}```\n {}" ,
202
- dump_buffer( & self . output. stdout) ,
203
- self
204
- ) ;
198
+ panic ! ( "Unexpected completion\n {}" , self ) ;
205
199
}
206
200
self
207
201
}
@@ -210,40 +204,47 @@ impl Assert {
210
204
///
211
205
/// # Examples
212
206
///
213
- /// ```rust
207
+ /// ```rust,ignore
214
208
/// use assert_cmd::prelude::*;
215
209
///
216
210
/// use std::process::Command;
211
+ /// use predicates::prelude::*;
212
+ ///
213
+ /// Command::main_binary()
214
+ /// .unwrap()
215
+ /// .env("exit", "42")
216
+ /// .assert()
217
+ /// .code(predicate::eq(42));
217
218
///
219
+ /// // which can be shortened to:
218
220
/// Command::main_binary()
219
221
/// .unwrap()
220
222
/// .env("exit", "42")
221
223
/// .assert()
222
224
/// .code(42);
223
225
/// ```
226
+ ///
227
+ /// See [`IntoCodePredicate`] for other built-in conversions.
228
+ ///
229
+ /// [IntoCodePredicate]: trait.IntoCodePredicate.html
224
230
pub fn code < I , P > ( self , pred : I ) -> Self
225
231
where
226
232
I : IntoCodePredicate < P > ,
227
- P : predicates :: Predicate < i32 > ,
233
+ P : predicates_core :: Predicate < i32 > ,
228
234
{
229
235
self . code_impl ( & pred. into_code ( ) )
230
236
}
231
237
232
- fn code_impl ( self , pred : & predicates :: Predicate < i32 > ) -> Self {
238
+ fn code_impl ( self , pred : & predicates_core :: Predicate < i32 > ) -> Self {
233
239
let actual_code = self . output . status . code ( ) . unwrap_or_else ( || {
234
240
panic ! (
235
241
"Command interrupted\n stderr=```{}```\n {}" ,
236
242
dump_buffer( & self . output. stderr) ,
237
243
self
238
244
)
239
245
} ) ;
240
- if !pred. eval ( & actual_code) {
241
- panic ! (
242
- "Unexpected return code\n stdout=```{}```\n stderr=```{}```\n {}" ,
243
- dump_buffer( & self . output. stdout) ,
244
- dump_buffer( & self . output. stderr) ,
245
- self
246
- ) ;
246
+ if let Some ( case) = pred. find_case ( false , & actual_code) {
247
+ panic ! ( "Unexpected return code, failed {}\n {}" , case. tree( ) , self ) ;
247
248
}
248
249
self
249
250
}
@@ -252,31 +253,44 @@ impl Assert {
252
253
///
253
254
/// # Examples
254
255
///
255
- /// ```rust
256
+ /// ```rust,ignore
256
257
/// use assert_cmd::prelude::*;
257
258
///
258
259
/// use std::process::Command;
260
+ /// use predicates::prelude::*;
259
261
///
260
262
/// Command::main_binary()
261
263
/// .unwrap()
262
264
/// .env("stdout", "hello")
263
265
/// .env("stderr", "world")
264
266
/// .assert()
267
+ /// .stdout(predicate::str::similar("hello\n").from_utf8());
268
+ ///
269
+ /// // which can be shortened to:
270
+ /// Command::main_binary()
271
+ /// .unwrap()
272
+ /// .env("stdout", "hello")
273
+ /// .env("stderr", "world")
274
+ /// .assert()
265
275
/// .stdout("hello\n");
266
276
/// ```
277
+ ///
278
+ /// See [`IntoOutputPredicate`] for other built-in conversions.
279
+ ///
280
+ /// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
267
281
pub fn stdout < I , P > ( self , pred : I ) -> Self
268
282
where
269
283
I : IntoOutputPredicate < P > ,
270
- P : predicates :: Predicate < [ u8 ] > ,
284
+ P : predicates_core :: Predicate < [ u8 ] > ,
271
285
{
272
286
self . stdout_impl ( & pred. into_output ( ) )
273
287
}
274
288
275
- fn stdout_impl ( self , pred : & predicates :: Predicate < [ u8 ] > ) -> Self {
289
+ fn stdout_impl ( self , pred : & predicates_core :: Predicate < [ u8 ] > ) -> Self {
276
290
{
277
291
let actual = & self . output . stdout ;
278
- if ! pred. eval ( actual) {
279
- panic ! ( "Unexpected stdout\n {}" , self ) ;
292
+ if let Some ( case ) = pred. find_case ( false , & actual) {
293
+ panic ! ( "Unexpected stdout, failed {} \n {}" , case . tree ( ) , self ) ;
280
294
}
281
295
}
282
296
self
@@ -286,31 +300,44 @@ impl Assert {
286
300
///
287
301
/// # Examples
288
302
///
289
- /// ```rust
303
+ /// ```rust,ignore
290
304
/// use assert_cmd::prelude::*;
291
305
///
292
306
/// use std::process::Command;
307
+ /// use predicates::prelude::*;
308
+ ///
309
+ /// Command::main_binary()
310
+ /// .unwrap()
311
+ /// .env("stdout", "hello")
312
+ /// .env("stderr", "world")
313
+ /// .assert()
314
+ /// .stderr(predicate::str::similar("world\n").from_utf8());
293
315
///
316
+ /// // which can be shortened to:
294
317
/// Command::main_binary()
295
318
/// .unwrap()
296
319
/// .env("stdout", "hello")
297
320
/// .env("stderr", "world")
298
321
/// .assert()
299
322
/// .stderr("world\n");
300
323
/// ```
324
+ ///
325
+ /// See [`IntoOutputPredicate`] for other built-in conversions.
326
+ ///
327
+ /// [IntoOutputPredicate]: trait.IntoOutputPredicate.html
301
328
pub fn stderr < I , P > ( self , pred : I ) -> Self
302
329
where
303
330
I : IntoOutputPredicate < P > ,
304
- P : predicates :: Predicate < [ u8 ] > ,
331
+ P : predicates_core :: Predicate < [ u8 ] > ,
305
332
{
306
333
self . stderr_impl ( & pred. into_output ( ) )
307
334
}
308
335
309
- fn stderr_impl ( self , pred : & predicates :: Predicate < [ u8 ] > ) -> Self {
336
+ fn stderr_impl ( self , pred : & predicates_core :: Predicate < [ u8 ] > ) -> Self {
310
337
{
311
338
let actual = & self . output . stderr ;
312
- if ! pred. eval ( actual) {
313
- panic ! ( "Unexpected stderr\n {}" , self ) ;
339
+ if let Some ( case ) = pred. find_case ( false , & actual) {
340
+ panic ! ( "Unexpected stderr, failed {} \n \n {}" , case . tree ( ) , self ) ;
314
341
}
315
342
}
316
343
self
@@ -343,25 +370,27 @@ impl fmt::Debug for Assert {
343
370
/// use assert_cmd::prelude::*;
344
371
///
345
372
/// use std::process::Command;
373
+ /// use predicates::prelude::*;
346
374
///
347
375
/// Command::main_binary()
348
376
/// .unwrap()
349
377
/// .env("exit", "42")
350
378
/// .assert()
351
- /// .code(42);
352
- /// // which is equivalent to
379
+ /// .code(predicate::eq(42));
380
+ ///
381
+ /// // which can be shortened to:
353
382
/// Command::main_binary()
354
383
/// .unwrap()
355
384
/// .env("exit", "42")
356
385
/// .assert()
357
- /// .code(predicates::ord::eq(42) );
386
+ /// .code(42 );
358
387
/// ```
359
388
///
360
389
/// [`Assert::code`]: struct.Assert.html#method.code
361
- /// [`Predicate<i32>`]: https://docs.rs/predicates/0.5.2/predicates /trait.Predicate.html
390
+ /// [`Predicate<i32>`]: https://docs.rs/predicates-core /0.9.0/predicates_core /trait.Predicate.html
362
391
pub trait IntoCodePredicate < P >
363
392
where
364
- P : predicates :: Predicate < i32 > ,
393
+ P : predicates_core :: Predicate < i32 > ,
365
394
{
366
395
/// The type of the predicate being returned.
367
396
type Predicate ;
@@ -372,7 +401,7 @@ where
372
401
373
402
impl < P > IntoCodePredicate < P > for P
374
403
where
375
- P : predicates :: Predicate < i32 > ,
404
+ P : predicates_core :: Predicate < i32 > ,
376
405
{
377
406
type Predicate = P ;
378
407
@@ -408,12 +437,36 @@ impl IntoCodePredicate<predicates::iter::InPredicate<i32>> for &'static [i32] {
408
437
/// Used by [`Assert::stdout`] and [`Assert::stderr`] to convert Self
409
438
/// into the needed [`Predicate<[u8]>`].
410
439
///
440
+ /// # Examples
441
+ ///
442
+ /// ```rust,ignore
443
+ /// use assert_cmd::prelude::*;
444
+ ///
445
+ /// use std::process::Command;
446
+ /// use predicates::prelude::*;
447
+ ///
448
+ /// Command::main_binary()
449
+ /// .unwrap()
450
+ /// .env("stdout", "hello")
451
+ /// .env("stderr", "world")
452
+ /// .assert()
453
+ /// .stdout(predicate::str::similar("hello\n").from_utf8());
454
+ ///
455
+ /// // which can be shortened to:
456
+ /// Command::main_binary()
457
+ /// .unwrap()
458
+ /// .env("stdout", "hello")
459
+ /// .env("stderr", "world")
460
+ /// .assert()
461
+ /// .stdout("hello\n");
462
+ /// ```
463
+ ///
411
464
/// [`Assert::stdout`]: struct.Assert.html#method.stdout
412
465
/// [`Assert::stderr`]: struct.Assert.html#method.stderr
413
- /// [`Predicate<[u8]>`]: https://docs.rs/predicates/0.5.2/predicates /trait.Predicate.html
466
+ /// [`Predicate<[u8]>`]: https://docs.rs/predicates-core /0.9.0/predicates_core /trait.Predicate.html
414
467
pub trait IntoOutputPredicate < P >
415
468
where
416
- P : predicates :: Predicate < [ u8 ] > ,
469
+ P : predicates_core :: Predicate < [ u8 ] > ,
417
470
{
418
471
/// The type of the predicate being returned.
419
472
type Predicate ;
@@ -424,7 +477,7 @@ where
424
477
425
478
impl < P > IntoOutputPredicate < P > for P
426
479
where
427
- P : predicates :: Predicate < [ u8 ] > ,
480
+ P : predicates_core :: Predicate < [ u8 ] > ,
428
481
{
429
482
type Predicate = P ;
430
483
@@ -437,7 +490,7 @@ where
437
490
/// [Predicate] used by [`IntoOutputPredicate`] for bytes.
438
491
///
439
492
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
440
- /// [Predicate]: https://docs.rs/predicates/0.5.2/predicates /trait.Predicate.html
493
+ /// [Predicate]: https://docs.rs/predicates-core /0.9.0/predicates_core /trait.Predicate.html
441
494
#[ derive( Debug ) ]
442
495
pub struct BytesContentOutputPredicate ( predicates:: ord:: EqPredicate < & ' static [ u8 ] > ) ;
443
496
@@ -448,10 +501,31 @@ impl BytesContentOutputPredicate {
448
501
}
449
502
}
450
503
451
- impl predicates:: Predicate < [ u8 ] > for BytesContentOutputPredicate {
504
+ impl predicates_core:: reflection:: PredicateReflection for BytesContentOutputPredicate {
505
+ fn parameters < ' a > (
506
+ & ' a self ,
507
+ ) -> Box < Iterator < Item = predicates_core:: reflection:: Parameter < ' a > > + ' a > {
508
+ self . 0 . parameters ( )
509
+ }
510
+
511
+ /// Nested `Predicate`s of the current `Predicate`.
512
+ fn children < ' a > ( & ' a self ) -> Box < Iterator < Item = predicates_core:: reflection:: Child < ' a > > + ' a > {
513
+ self . 0 . children ( )
514
+ }
515
+ }
516
+
517
+ impl predicates_core:: Predicate < [ u8 ] > for BytesContentOutputPredicate {
452
518
fn eval ( & self , item : & [ u8 ] ) -> bool {
453
519
self . 0 . eval ( item)
454
520
}
521
+
522
+ fn find_case < ' a > (
523
+ & ' a self ,
524
+ expected : bool ,
525
+ variable : & [ u8 ] ,
526
+ ) -> Option < predicates_core:: reflection:: Case < ' a > > {
527
+ self . 0 . find_case ( expected, variable)
528
+ }
455
529
}
456
530
457
531
impl fmt:: Display for BytesContentOutputPredicate {
@@ -472,7 +546,7 @@ impl IntoOutputPredicate<BytesContentOutputPredicate> for &'static [u8] {
472
546
/// [Predicate] used by [`IntoOutputPredicate`] for [`str`].
473
547
///
474
548
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
475
- /// [Predicate]: https://docs.rs/predicates/0.5.2/predicates /trait.Predicate.html
549
+ /// [Predicate]: https://docs.rs/predicates-core /0.9.0/predicates_core /trait.Predicate.html
476
550
/// [`str`]: https://doc.rust-lang.org/std/primitive.str.html
477
551
#[ derive( Debug , Clone ) ]
478
552
pub struct StrContentOutputPredicate (
@@ -486,10 +560,31 @@ impl StrContentOutputPredicate {
486
560
}
487
561
}
488
562
489
- impl predicates:: Predicate < [ u8 ] > for StrContentOutputPredicate {
563
+ impl predicates_core:: reflection:: PredicateReflection for StrContentOutputPredicate {
564
+ fn parameters < ' a > (
565
+ & ' a self ,
566
+ ) -> Box < Iterator < Item = predicates_core:: reflection:: Parameter < ' a > > + ' a > {
567
+ self . 0 . parameters ( )
568
+ }
569
+
570
+ /// Nested `Predicate`s of the current `Predicate`.
571
+ fn children < ' a > ( & ' a self ) -> Box < Iterator < Item = predicates_core:: reflection:: Child < ' a > > + ' a > {
572
+ self . 0 . children ( )
573
+ }
574
+ }
575
+
576
+ impl predicates_core:: Predicate < [ u8 ] > for StrContentOutputPredicate {
490
577
fn eval ( & self , item : & [ u8 ] ) -> bool {
491
578
self . 0 . eval ( item)
492
579
}
580
+
581
+ fn find_case < ' a > (
582
+ & ' a self ,
583
+ expected : bool ,
584
+ variable : & [ u8 ] ,
585
+ ) -> Option < predicates_core:: reflection:: Case < ' a > > {
586
+ self . 0 . find_case ( expected, variable)
587
+ }
493
588
}
494
589
495
590
impl fmt:: Display for StrContentOutputPredicate {
@@ -517,7 +612,7 @@ mod test {
517
612
fn convert_code < I , P > ( pred : I ) -> P
518
613
where
519
614
I : IntoCodePredicate < P > ,
520
- P : predicates :: Predicate < i32 > ,
615
+ P : predicates_core :: Predicate < i32 > ,
521
616
{
522
617
pred. into_code ( )
523
618
}
@@ -551,7 +646,7 @@ mod test {
551
646
fn convert_output < I , P > ( pred : I ) -> P
552
647
where
553
648
I : IntoOutputPredicate < P > ,
554
- P : predicates :: Predicate < [ u8 ] > ,
649
+ P : predicates_core :: Predicate < [ u8 ] > ,
555
650
{
556
651
pred. into_output ( )
557
652
}
0 commit comments