Skip to content

Commit b4d4beb

Browse files
authored
Merge pull request #107 from R1tschY/vec_output_support
Add support for Vec<u8> as output expectation
2 parents 1ac9d7b + 20a3df8 commit b4d4beb

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

src/assert.rs

+32-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [Output]: https://doc.rust-lang.org/std/process/struct.Output.html
44
5+
use std::borrow::Cow;
56
use std::fmt;
67
use std::process;
78
use std::str;
@@ -723,47 +724,50 @@ where
723724
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
724725
/// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
725726
#[derive(Debug)]
726-
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);
727+
pub struct BytesContentOutputPredicate(Cow<'static, [u8]>);
727728

728729
impl BytesContentOutputPredicate {
729730
pub(crate) fn new(value: &'static [u8]) -> Self {
730-
let pred = predicates::ord::eq(value);
731-
BytesContentOutputPredicate(pred)
732-
}
733-
}
734-
735-
impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {
736-
fn parameters<'a>(
737-
&'a self,
738-
) -> Box<dyn Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
739-
self.0.parameters()
731+
BytesContentOutputPredicate(Cow::from(value))
740732
}
741733

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

739+
impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {}
740+
750741
impl predicates_core::Predicate<[u8]> for BytesContentOutputPredicate {
751742
fn eval(&self, item: &[u8]) -> bool {
752-
self.0.eval(item)
743+
self.0.as_ref() == item
753744
}
754745

755-
fn find_case<'a>(
756-
&'a self,
746+
fn find_case(
747+
&self,
757748
expected: bool,
758749
variable: &[u8],
759-
) -> Option<predicates_core::reflection::Case<'a>> {
760-
self.0.find_case(expected, variable)
750+
) -> Option<predicates_core::reflection::Case> {
751+
let actual = self.eval(variable);
752+
if expected == actual {
753+
Some(predicates_core::reflection::Case::new(Some(self), actual))
754+
} else {
755+
None
756+
}
761757
}
762758
}
763759

764760
impl fmt::Display for BytesContentOutputPredicate {
765761
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
766-
self.0.fmt(f)
762+
predicates::ord::eq(self.0.as_ref()).fmt(f)
763+
}
764+
}
765+
766+
impl IntoOutputPredicate<BytesContentOutputPredicate> for Vec<u8> {
767+
type Predicate = BytesContentOutputPredicate;
768+
769+
fn into_output(self) -> Self::Predicate {
770+
Self::Predicate::from_vec(self)
767771
}
768772
}
769773

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

1024+
#[test]
1025+
fn into_output_from_vec() {
1026+
let pred = convert_output(vec![b'H', b'e', b'l', b'l', b'o']);
1027+
assert!(pred.eval(b"Hello" as &[u8]));
1028+
}
1029+
10201030
#[test]
10211031
fn into_output_from_str() {
10221032
let pred = convert_output("Hello");

tests/assert.rs

+14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ fn stdout_example() {
102102
.assert()
103103
.stdout(b"hello\n" as &[u8]);
104104

105+
Command::cargo_bin("bin_fixture")
106+
.unwrap()
107+
.env("stdout", "hello")
108+
.env("stderr", "world")
109+
.assert()
110+
.stdout(vec![b'h', b'e', b'l', b'l', b'o', b'\n']);
111+
105112
Command::cargo_bin("bin_fixture")
106113
.unwrap()
107114
.env("stdout", "hello")
@@ -133,6 +140,13 @@ fn stderr_example() {
133140
.assert()
134141
.stderr(b"world\n" as &[u8]);
135142

143+
Command::cargo_bin("bin_fixture")
144+
.unwrap()
145+
.env("stdout", "hello")
146+
.env("stderr", "world")
147+
.assert()
148+
.stderr(vec![b'w', b'o', b'r', b'l', b'd', b'\n']);
149+
136150
Command::cargo_bin("bin_fixture")
137151
.unwrap()
138152
.env("stdout", "hello")

0 commit comments

Comments
 (0)