Skip to content

Commit cb89674

Browse files
authored
Merge pull request #18 from epage/convert
Expand short-hand predicates
2 parents 6507f9f + 111abca commit cb89674

File tree

1 file changed

+113
-8
lines changed

1 file changed

+113
-8
lines changed

src/assert.rs

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ pub trait IntoCodePredicate<P>
335335
where
336336
P: predicates::Predicate<i32>,
337337
{
338+
/// The type of the predicate being returned.
339+
type Predicate;
340+
338341
/// Convert to a predicate for testing a program's exit code.
339342
fn into_code(self) -> P;
340343
}
@@ -343,22 +346,45 @@ impl<P> IntoCodePredicate<P> for P
343346
where
344347
P: predicates::Predicate<i32>,
345348
{
346-
fn into_code(self) -> P {
349+
type Predicate = P;
350+
351+
fn into_code(self) -> Self::Predicate {
347352
self
348353
}
349354
}
350355

351356
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 {
353360
predicates::ord::eq(self)
354361
}
355362
}
356363

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+
357380
/// Used by `Assert` to convert Self into the needed `Predicate<[u8]>`.
358381
pub trait IntoOutputPredicate<P>
359382
where
360383
P: predicates::Predicate<[u8]>,
361384
{
385+
/// The type of the predicate being returned.
386+
type Predicate;
387+
362388
/// Convert to a predicate for testing a path.
363389
fn into_output(self) -> P;
364390
}
@@ -367,17 +393,96 @@ impl<P> IntoOutputPredicate<P> for P
367393
where
368394
P: predicates::Predicate<[u8]>,
369395
{
370-
fn into_output(self) -> P {
396+
type Predicate = P;
397+
398+
fn into_output(self) -> Self::Predicate {
371399
self
372400
}
373401
}
374402

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>>
376412
for &'static str
377413
{
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]));
382487
}
383488
}

0 commit comments

Comments
 (0)