|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use crate::matcher::{Matcher, MatcherResult}; |
| 16 | +use std::{fmt::Debug, marker::PhantomData}; |
| 17 | + |
| 18 | +/// Matches a byte sequence which is a UTF-8 encoded string matched by `inner`. |
| 19 | +/// |
| 20 | +/// The matcher reports no match if either the string is not UTF-8 encoded or if |
| 21 | +/// `inner` does not match on the decoded string. |
| 22 | +/// |
| 23 | +/// The input may be a slice `&[u8]` or a `Vec` of bytes. |
| 24 | +/// |
| 25 | +/// ``` |
| 26 | +/// # use googletest::prelude::*; |
| 27 | +/// # fn should_pass() -> Result<()> { |
| 28 | +/// let bytes: &[u8] = "A string".as_bytes(); |
| 29 | +/// verify_that!(bytes, is_utf8_string(eq("A string")))?; // Passes |
| 30 | +/// let bytes: Vec<u8> = "A string".as_bytes().to_vec(); |
| 31 | +/// verify_that!(bytes, is_utf8_string(eq("A string")))?; // Passes |
| 32 | +/// # Ok(()) |
| 33 | +/// # } |
| 34 | +/// # fn should_fail_1() -> Result<()> { |
| 35 | +/// # let bytes: &[u8] = "A string".as_bytes(); |
| 36 | +/// verify_that!(bytes, is_utf8_string(eq("Another string")))?; // Fails (inner matcher does not match) |
| 37 | +/// # Ok(()) |
| 38 | +/// # } |
| 39 | +/// # fn should_fail_2() -> Result<()> { |
| 40 | +/// let bytes: Vec<u8> = vec![255, 64, 128, 32]; |
| 41 | +/// verify_that!(bytes, is_utf8_string(anything()))?; // Fails (not UTF-8 encoded) |
| 42 | +/// # Ok(()) |
| 43 | +/// # } |
| 44 | +/// # should_pass().unwrap(); |
| 45 | +/// # should_fail_1().unwrap_err(); |
| 46 | +/// # should_fail_2().unwrap_err(); |
| 47 | +/// ``` |
| 48 | +pub fn is_utf8_string<'a, ActualT: AsRef<[u8]> + Debug + 'a, InnerMatcherT>( |
| 49 | + inner: InnerMatcherT, |
| 50 | +) -> impl Matcher<ActualT = ActualT> |
| 51 | +where |
| 52 | + InnerMatcherT: Matcher<ActualT = String>, |
| 53 | +{ |
| 54 | + IsEncodedStringMatcher { inner, phantom: Default::default() } |
| 55 | +} |
| 56 | + |
| 57 | +struct IsEncodedStringMatcher<ActualT, InnerMatcherT> { |
| 58 | + inner: InnerMatcherT, |
| 59 | + phantom: PhantomData<ActualT>, |
| 60 | +} |
| 61 | + |
| 62 | +impl<'a, ActualT: AsRef<[u8]> + Debug + 'a, InnerMatcherT> Matcher |
| 63 | + for IsEncodedStringMatcher<ActualT, InnerMatcherT> |
| 64 | +where |
| 65 | + InnerMatcherT: Matcher<ActualT = String>, |
| 66 | +{ |
| 67 | + type ActualT = ActualT; |
| 68 | + |
| 69 | + fn matches(&self, actual: &Self::ActualT) -> MatcherResult { |
| 70 | + String::from_utf8(actual.as_ref().to_vec()) |
| 71 | + .map(|s| self.inner.matches(&s)) |
| 72 | + .unwrap_or(MatcherResult::NoMatch) |
| 73 | + } |
| 74 | + |
| 75 | + fn describe(&self, matcher_result: MatcherResult) -> String { |
| 76 | + match matcher_result { |
| 77 | + MatcherResult::Match => format!( |
| 78 | + "is a UTF-8 encoded string which {}", |
| 79 | + self.inner.describe(MatcherResult::Match) |
| 80 | + ), |
| 81 | + MatcherResult::NoMatch => format!( |
| 82 | + "is not a UTF-8 encoded string which {}", |
| 83 | + self.inner.describe(MatcherResult::Match) |
| 84 | + ), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn explain_match(&self, actual: &Self::ActualT) -> String { |
| 89 | + match String::from_utf8(actual.as_ref().to_vec()) { |
| 90 | + Ok(s) => format!("which is a UTF-8 encoded string {}", self.inner.explain_match(&s)), |
| 91 | + Err(e) => format!("which is not a UTF-8 encoded string: {e}"), |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use crate::matcher::MatcherResult; |
| 99 | + use crate::prelude::*; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn matches_string_as_byte_slice() -> Result<()> { |
| 103 | + verify_that!("A string".as_bytes(), is_utf8_string(eq("A string"))) |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn matches_string_as_byte_vec() -> Result<()> { |
| 108 | + verify_that!("A string".as_bytes().to_vec(), is_utf8_string(eq("A string"))) |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn matches_string_with_utf_8_encoded_sequences() -> Result<()> { |
| 113 | + verify_that!("äöüÄÖÜ".as_bytes().to_vec(), is_utf8_string(eq("äöüÄÖÜ"))) |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn does_not_match_non_equal_string() -> Result<()> { |
| 118 | + verify_that!("äöüÄÖÜ".as_bytes().to_vec(), not(is_utf8_string(eq("A string")))) |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn does_not_match_non_utf_8_encoded_byte_sequence() -> Result<()> { |
| 123 | + verify_that!(&[192, 64, 255, 32], not(is_utf8_string(eq("A string")))) |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn has_correct_description_in_matched_case() -> Result<()> { |
| 128 | + let matcher = is_utf8_string::<&[u8], _>(eq("A string")); |
| 129 | + |
| 130 | + verify_that!( |
| 131 | + matcher.describe(MatcherResult::Match), |
| 132 | + eq("is a UTF-8 encoded string which is equal to \"A string\"") |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn has_correct_description_in_not_matched_case() -> Result<()> { |
| 138 | + let matcher = is_utf8_string::<&[u8], _>(eq("A string")); |
| 139 | + |
| 140 | + verify_that!( |
| 141 | + matcher.describe(MatcherResult::NoMatch), |
| 142 | + eq("is not a UTF-8 encoded string which is equal to \"A string\"") |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn has_correct_explanation_in_matched_case() -> Result<()> { |
| 148 | + let explanation = is_utf8_string(eq("A string")).explain_match(&"A string".as_bytes()); |
| 149 | + |
| 150 | + verify_that!( |
| 151 | + explanation, |
| 152 | + eq("which is a UTF-8 encoded string which is equal to \"A string\"") |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn has_correct_explanation_when_byte_array_is_not_utf8_encoded() -> Result<()> { |
| 158 | + let explanation = is_utf8_string(eq("A string")).explain_match(&&[192, 128, 0, 64]); |
| 159 | + |
| 160 | + verify_that!(explanation, starts_with("which is not a UTF-8 encoded string: ")) |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn has_correct_explanation_when_inner_matcher_does_not_match() -> Result<()> { |
| 165 | + let explanation = |
| 166 | + is_utf8_string(eq("A string")).explain_match(&"Another string".as_bytes()); |
| 167 | + |
| 168 | + verify_that!( |
| 169 | + explanation, |
| 170 | + eq("which is a UTF-8 encoded string which isn't equal to \"A string\"") |
| 171 | + ) |
| 172 | + } |
| 173 | +} |
0 commit comments