diff --git a/src/assert.rs b/src/assert.rs index 652883a..1a9da89 100644 --- a/src/assert.rs +++ b/src/assert.rs @@ -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; @@ -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> + 'a> { - self.0.parameters() + BytesContentOutputPredicate(Cow::from(value)) } - /// Nested `Predicate`s of the current `Predicate`. - fn children<'a>( - &'a self, - ) -> Box> + 'a> { - self.0.children() + pub(crate) fn from_vec(value: Vec) -> 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> { - self.0.find_case(expected, variable) + ) -> Option { + 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 for Vec { + type Predicate = BytesContentOutputPredicate; + + fn into_output(self) -> Self::Predicate { + Self::Predicate::from_vec(self) } } @@ -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"); diff --git a/tests/assert.rs b/tests/assert.rs index 126070f..c2727a4 100644 --- a/tests/assert.rs +++ b/tests/assert.rs @@ -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") @@ -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")