|
2 | 2 | //!
|
3 | 3 | //! [Output]: https://doc.rust-lang.org/std/process/struct.Output.html
|
4 | 4 |
|
| 5 | +use std::borrow::Cow; |
5 | 6 | use std::fmt;
|
6 | 7 | use std::process;
|
7 | 8 | use std::str;
|
@@ -723,47 +724,50 @@ where
|
723 | 724 | /// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
|
724 | 725 | /// [Predicate]: https://docs.rs/predicates-core/1.0.0/predicates_core/trait.Predicate.html
|
725 | 726 | #[derive(Debug)]
|
726 |
| -pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>); |
| 727 | +pub struct BytesContentOutputPredicate(Cow<'static, [u8]>); |
727 | 728 |
|
728 | 729 | impl BytesContentOutputPredicate {
|
729 | 730 | 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)) |
740 | 732 | }
|
741 | 733 |
|
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)) |
747 | 736 | }
|
748 | 737 | }
|
749 | 738 |
|
| 739 | +impl predicates_core::reflection::PredicateReflection for BytesContentOutputPredicate {} |
| 740 | + |
750 | 741 | impl predicates_core::Predicate<[u8]> for BytesContentOutputPredicate {
|
751 | 742 | fn eval(&self, item: &[u8]) -> bool {
|
752 |
| - self.0.eval(item) |
| 743 | + self.0.as_ref() == item |
753 | 744 | }
|
754 | 745 |
|
755 |
| - fn find_case<'a>( |
756 |
| - &'a self, |
| 746 | + fn find_case( |
| 747 | + &self, |
757 | 748 | expected: bool,
|
758 | 749 | 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 | + } |
761 | 757 | }
|
762 | 758 | }
|
763 | 759 |
|
764 | 760 | impl fmt::Display for BytesContentOutputPredicate {
|
765 | 761 | 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) |
767 | 771 | }
|
768 | 772 | }
|
769 | 773 |
|
@@ -1017,6 +1021,12 @@ mod test {
|
1017 | 1021 | assert!(pred.eval(b"Hello" as &[u8]));
|
1018 | 1022 | }
|
1019 | 1023 |
|
| 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 | + |
1020 | 1030 | #[test]
|
1021 | 1031 | fn into_output_from_str() {
|
1022 | 1032 | let pred = convert_output("Hello");
|
|
0 commit comments