@@ -335,6 +335,9 @@ pub trait IntoCodePredicate<P>
335
335
where
336
336
P : predicates:: Predicate < i32 > ,
337
337
{
338
+ /// The type of the predicate being returned.
339
+ type Predicate ;
340
+
338
341
/// Convert to a predicate for testing a program's exit code.
339
342
fn into_code ( self ) -> P ;
340
343
}
@@ -343,22 +346,45 @@ impl<P> IntoCodePredicate<P> for P
343
346
where
344
347
P : predicates:: Predicate < i32 > ,
345
348
{
346
- fn into_code ( self ) -> P {
349
+ type Predicate = P ;
350
+
351
+ fn into_code ( self ) -> Self :: Predicate {
347
352
self
348
353
}
349
354
}
350
355
351
356
impl IntoCodePredicate < predicates:: ord:: EqPredicate < i32 > > for i32 {
352
- fn into_code ( self ) -> predicates:: ord:: EqPredicate < i32 > {
357
+ type Predicate = predicates:: ord:: EqPredicate < i32 > ;
358
+
359
+ fn into_code ( self ) -> Self :: Predicate {
353
360
predicates:: ord:: eq ( self )
354
361
}
355
362
}
356
363
364
+ impl IntoCodePredicate < predicates:: iter:: InPredicate < i32 > > for Vec < i32 > {
365
+ type Predicate = predicates:: iter:: InPredicate < i32 > ;
366
+
367
+ fn into_code ( self ) -> Self :: Predicate {
368
+ predicates:: iter:: in_iter ( self )
369
+ }
370
+ }
371
+
372
+ impl IntoCodePredicate < predicates:: iter:: InPredicate < i32 > > for & ' static [ i32 ] {
373
+ type Predicate = predicates:: iter:: InPredicate < i32 > ;
374
+
375
+ fn into_code ( self ) -> Self :: Predicate {
376
+ predicates:: iter:: in_iter ( self . iter ( ) . cloned ( ) )
377
+ }
378
+ }
379
+
357
380
/// Used by `Assert` to convert Self into the needed `Predicate<[u8]>`.
358
381
pub trait IntoOutputPredicate < P >
359
382
where
360
383
P : predicates:: Predicate < [ u8 ] > ,
361
384
{
385
+ /// The type of the predicate being returned.
386
+ type Predicate ;
387
+
362
388
/// Convert to a predicate for testing a path.
363
389
fn into_output ( self ) -> P ;
364
390
}
@@ -367,17 +393,96 @@ impl<P> IntoOutputPredicate<P> for P
367
393
where
368
394
P : predicates:: Predicate < [ u8 ] > ,
369
395
{
370
- fn into_output ( self ) -> P {
396
+ type Predicate = P ;
397
+
398
+ fn into_output ( self ) -> Self :: Predicate {
371
399
self
372
400
}
373
401
}
374
402
375
- impl IntoOutputPredicate < predicates:: str:: Utf8Predicate < predicates:: ord:: EqPredicate < & ' static str > > >
403
+ impl IntoOutputPredicate < predicates:: ord:: EqPredicate < & ' static [ u8 ] > > for & ' static [ u8 ] {
404
+ type Predicate = predicates:: ord:: EqPredicate < & ' static [ u8 ] > ;
405
+
406
+ fn into_output ( self ) -> Self :: Predicate {
407
+ predicates:: ord:: eq ( self )
408
+ }
409
+ }
410
+
411
+ impl IntoOutputPredicate < predicates:: str:: Utf8Predicate < predicates:: str:: DifferencePredicate > >
376
412
for & ' static str
377
413
{
378
- fn into_output (
379
- self ,
380
- ) -> predicates:: str:: Utf8Predicate < predicates:: ord:: EqPredicate < & ' static str > > {
381
- predicates:: ord:: eq ( self ) . from_utf8 ( )
414
+ type Predicate = predicates:: str:: Utf8Predicate < predicates:: str:: DifferencePredicate > ;
415
+
416
+ fn into_output ( self ) -> Self :: Predicate {
417
+ predicates:: str:: similar ( self ) . from_utf8 ( )
418
+ }
419
+ }
420
+
421
+ #[ cfg( test) ]
422
+ mod test {
423
+ use super :: * ;
424
+
425
+ use predicates:: prelude:: * ;
426
+
427
+ // Since IntoCodePredicate exists solely for conversion, test it under that scenario to ensure
428
+ // it works as expected.
429
+ fn convert_code < I , P > ( pred : I ) -> P
430
+ where
431
+ I : IntoCodePredicate < P > ,
432
+ P : predicates:: Predicate < i32 > ,
433
+ {
434
+ pred. into_code ( )
435
+ }
436
+
437
+ #[ test]
438
+ fn into_code_from_pred ( ) {
439
+ let pred = convert_code ( predicate:: eq ( 10 ) ) ;
440
+ assert ! ( pred. eval( & 10 ) ) ;
441
+ }
442
+
443
+ #[ test]
444
+ fn into_code_from_i32 ( ) {
445
+ let pred = convert_code ( 10 ) ;
446
+ assert ! ( pred. eval( & 10 ) ) ;
447
+ }
448
+
449
+ #[ test]
450
+ fn into_code_from_vec ( ) {
451
+ let pred = convert_code ( vec ! [ 3 , 10 ] ) ;
452
+ assert ! ( pred. eval( & 10 ) ) ;
453
+ }
454
+
455
+ #[ test]
456
+ fn into_code_from_array ( ) {
457
+ let pred = convert_code ( & [ 3 , 10 ] as & [ i32 ] ) ;
458
+ assert ! ( pred. eval( & 10 ) ) ;
459
+ }
460
+
461
+ // Since IntoOutputPredicate exists solely for conversion, test it under that scenario to ensure
462
+ // it works as expected.
463
+ fn convert_output < I , P > ( pred : I ) -> P
464
+ where
465
+ I : IntoOutputPredicate < P > ,
466
+ P : predicates:: Predicate < [ u8 ] > ,
467
+ {
468
+ pred. into_output ( )
469
+ }
470
+
471
+ #[ test]
472
+ fn into_output_from_pred ( ) {
473
+ let pred = convert_output ( predicate:: eq ( b"Hello" as & [ u8 ] ) ) ;
474
+ assert ! ( pred. eval( b"Hello" as & [ u8 ] ) ) ;
475
+ }
476
+
477
+ #[ test]
478
+ fn into_output_from_bytes ( ) {
479
+ let pred = convert_output ( b"Hello" as & [ u8 ] ) ;
480
+ assert ! ( pred. eval( b"Hello" as & [ u8 ] ) ) ;
481
+ }
482
+
483
+ #[ test]
484
+ fn into_output_from_str ( ) {
485
+ let pred = convert_output ( "Hello" ) ;
486
+ assert ! ( pred. eval( b"Hello" as & [ u8 ] ) ) ;
382
487
}
383
488
}
0 commit comments