Skip to content

Commit

Permalink
Merge pull request #107 from R1tschY/vec_output_support
Browse files Browse the repository at this point in the history
Add support for Vec<u8> as output expectation
  • Loading branch information
epage authored Oct 13, 2020
2 parents 1ac9d7b + 20a3df8 commit b4d4beb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! [Output]: https://doc.rust-lang.org/std/process/struct.Output.html

use std::borrow::Cow;
use std::fmt;
use std::process;
use std::str;
Expand Down Expand Up @@ -723,47 +724,50 @@ where
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
#[derive(Debug)]
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);
pub struct BytesContentOutputPredicate(Cow<'static, [u8]>);

impl BytesContentOutputPredicate {
pub(crate) fn new(value: &'static [u8]) -> Self {
let pred = predicates::ord::eq(value);
BytesContentOutputPredicate(pred)
}
}

impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {
fn parameters<'a>(
&'a self,
) -> Box<dyn Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
self.0.parameters()
BytesContentOutputPredicate(Cow::from(value))
}

/// Nested `Predicate`s of the current `Predicate`.
fn children<'a>(
&'a self,
) -> Box<dyn Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
self.0.children()
pub(crate) fn from_vec(value: Vec<u8>) -> Self {
BytesContentOutputPredicate(Cow::from(value))
}
}

impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {}

impl predicates_core::Predicate<[u8]> for BytesContentOutputPredicate {
fn eval(&self, item: &[u8]) -> bool {
self.0.eval(item)
self.0.as_ref() == item
}

fn find_case<'a>(
&'a self,
fn find_case(
&self,
expected: bool,
variable: &[u8],
) -> Option<predicates_core::reflection::Case<'a>> {
self.0.find_case(expected, variable)
) -> Option<predicates_core::reflection::Case> {
let actual = self.eval(variable);
if expected == actual {
Some(predicates_core::reflection::Case::new(Some(self), actual))
} else {
None
}
}
}

impl fmt::Display for BytesContentOutputPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
predicates::ord::eq(self.0.as_ref()).fmt(f)
}
}

impl IntoOutputPredicate<BytesContentOutputPredicate> for Vec<u8> {
type Predicate = BytesContentOutputPredicate;

fn into_output(self) -> Self::Predicate {
Self::Predicate::from_vec(self)
}
}

Expand Down Expand Up @@ -1017,6 +1021,12 @@ mod test {
assert!(pred.eval(b"Hello" as &[u8]));
}

#[test]
fn into_output_from_vec() {
let pred = convert_output(vec![b'H', b'e', b'l', b'l', b'o']);
assert!(pred.eval(b"Hello" as &[u8]));
}

#[test]
fn into_output_from_str() {
let pred = convert_output("Hello");
Expand Down
14 changes: 14 additions & 0 deletions tests/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ fn stdout_example() {
.assert()
.stdout(b"hello\n" as &[u8]);

Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(vec![b'h', b'e', b'l', b'l', b'o', b'\n']);

Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
Expand Down Expand Up @@ -133,6 +140,13 @@ fn stderr_example() {
.assert()
.stderr(b"world\n" as &[u8]);

Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(vec![b'w', b'o', b'r', b'l', b'd', b'\n']);

Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
Expand Down

0 comments on commit b4d4beb

Please sign in to comment.