expect_json/expect/ops/expect_string/
expect_string.rs

1use crate::expect::ops::expect_string::ExpectStringSubOp;
2use crate::expect_core::expect_op;
3use crate::expect_core::Context;
4use crate::expect_core::ExpectOp;
5use crate::expect_core::ExpectOpResult;
6use crate::JsonType;
7
8#[expect_op(internal, name = "string")]
9#[derive(Debug, Clone, Default, PartialEq)]
10pub struct ExpectString {
11    sub_ops: Vec<ExpectStringSubOp>,
12}
13
14impl ExpectString {
15    pub(crate) fn new() -> Self {
16        Self { sub_ops: vec![] }
17    }
18
19    pub fn empty(mut self) -> Self {
20        self.sub_ops.push(ExpectStringSubOp::Empty);
21        self
22    }
23
24    pub fn not_empty(mut self) -> Self {
25        self.sub_ops.push(ExpectStringSubOp::NotEmpty);
26        self
27    }
28
29    pub fn len(mut self, len: usize) -> Self {
30        self.sub_ops.push(ExpectStringSubOp::Len(len));
31        self
32    }
33
34    pub fn min_len(mut self, min_len: usize) -> Self {
35        self.sub_ops.push(ExpectStringSubOp::MinLen(min_len));
36        self
37    }
38
39    pub fn max_len(mut self, max_len: usize) -> Self {
40        self.sub_ops.push(ExpectStringSubOp::MaxLen(max_len));
41        self
42    }
43
44    pub fn contains<S>(mut self, expected_sub_string: S) -> Self
45    where
46        S: Into<String>,
47    {
48        self.sub_ops
49            .push(ExpectStringSubOp::Contains(expected_sub_string.into()));
50        self
51    }
52}
53
54impl ExpectOp for ExpectString {
55    fn on_string(&self, context: &mut Context, received: &str) -> ExpectOpResult<()> {
56        for sub_op in &self.sub_ops {
57            sub_op.on_string(self, context, received)?;
58        }
59
60        Ok(())
61    }
62
63    fn debug_supported_types(&self) -> &'static [JsonType] {
64        &[JsonType::String]
65    }
66}
67
68#[cfg(test)]
69mod test_contains {
70    use crate::expect;
71    use crate::expect_json_eq;
72    use pretty_assertions::assert_eq;
73    use serde_json::json;
74
75    #[test]
76    fn it_should_be_equal_for_identical_strings() {
77        let left = json!("1, 2, 3");
78        let right = json!(expect::string().contains("1, 2, 3"));
79
80        let output = expect_json_eq(&left, &right);
81        assert!(output.is_ok());
82    }
83
84    #[test]
85    fn it_should_be_equal_for_partial_matches_in_middle() {
86        let left = json!("0, 1, 2, 3, 4");
87        let right = json!(expect::string().contains("1, 2, 3"));
88
89        let output = expect_json_eq(&left, &right);
90        assert!(output.is_ok());
91    }
92
93    #[test]
94    fn it_should_be_ok_for_empty_contains() {
95        let left = json!("0, 1, 2, 3, 4, 5");
96        let right = json!(expect::string().contains(""));
97
98        let output = expect_json_eq(&left, &right);
99        assert!(output.is_ok());
100    }
101
102    #[test]
103    fn it_should_error_for_totall_different_values() {
104        let left = json!("1, 2, 3");
105        let right = json!(expect::string().contains("a, b, c"));
106
107        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
108        assert_eq!(
109            output,
110            r#"Json string at root does not contain expected value:
111    expected string to contain "a, b, c", but it was not found.
112    received "1, 2, 3""#
113        );
114    }
115}
116
117#[cfg(test)]
118mod test_empty {
119    use crate::expect;
120    use crate::expect_json_eq;
121    use pretty_assertions::assert_eq;
122    use serde_json::json;
123
124    #[test]
125    fn it_should_pass_when_string_is_empty() {
126        let left = json!("");
127        let right = json!(expect::string().empty());
128
129        let output = expect_json_eq(&left, &right);
130        assert!(output.is_ok(), "assertion error: {output:#?}");
131    }
132
133    #[test]
134    fn it_should_fail_when_string_is_not_empty() {
135        let left = json!("🦊");
136        let right = json!(expect::string().empty());
137
138        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
139        assert_eq!(
140            output,
141            r#"Json expect::string() error at root:
142    expected empty string
143    received "🦊""#
144        );
145    }
146}
147
148#[cfg(test)]
149mod test_not_empty {
150    use crate::expect;
151    use crate::expect_json_eq;
152    use pretty_assertions::assert_eq;
153    use serde_json::json;
154
155    #[test]
156    fn it_should_pass_when_string_is_not_empty() {
157        let left = json!("🦊");
158        let right = json!(expect::string().not_empty());
159
160        let output = expect_json_eq(&left, &right);
161        assert!(output.is_ok(), "assertion error: {output:#?}");
162    }
163
164    #[test]
165    fn it_should_fail_when_string_is_empty() {
166        let left = json!("");
167        let right = json!(expect::string().not_empty());
168
169        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
170        assert_eq!(
171            output,
172            r#"Json expect::string() error at root:
173    expected non-empty string
174    received """#
175        );
176    }
177}
178
179#[cfg(test)]
180mod test_len {
181    use crate::expect;
182    use crate::expect_json_eq;
183    use pretty_assertions::assert_eq;
184    use serde_json::json;
185
186    #[test]
187    fn it_should_pass_when_string_has_same_number_of_characters() {
188        let left = json!("123");
189        let right = json!(expect::string().len(3));
190
191        let output = expect_json_eq(&left, &right);
192        assert!(output.is_ok(), "assertion error: {output:#?}");
193    }
194
195    #[test]
196    fn it_should_fail_when_string_is_too_short() {
197        let left = json!("12");
198        let right = json!(expect::string().len(3));
199
200        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
201        assert_eq!(
202            output,
203            r#"Json expect::string() error at root:
204    expected string to have 3 characters, but it has 2,
205    received "12""#
206        );
207    }
208
209    #[test]
210    fn it_should_fail_when_string_is_too_long() {
211        let left = json!("1234");
212        let right = json!(expect::string().len(3));
213
214        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
215        assert_eq!(
216            output,
217            r#"Json expect::string() error at root:
218    expected string to have 3 characters, but it has 4,
219    received "1234""#
220        );
221    }
222}
223
224#[cfg(test)]
225mod test_min_len {
226    use crate::expect;
227    use crate::expect_json_eq;
228    use pretty_assertions::assert_eq;
229    use serde_json::json;
230
231    #[test]
232    fn it_should_pass_when_string_has_exactly_enough_characters() {
233        let left = json!("123");
234        let right = json!(expect::string().min_len(3));
235
236        let output = expect_json_eq(&left, &right);
237        assert!(output.is_ok(), "assertion error: {output:#?}");
238    }
239
240    #[test]
241    fn it_should_pass_when_string_has_more_than_enough_characters() {
242        let left = json!("12345");
243        let right = json!(expect::string().min_len(3));
244
245        let output = expect_json_eq(&left, &right);
246        assert!(output.is_ok(), "assertion error: {output:#?}");
247    }
248
249    #[test]
250    fn it_should_fail_when_string_is_too_short() {
251        let left = json!("12");
252        let right = json!(expect::string().min_len(3));
253
254        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
255        assert_eq!(
256            output,
257            r#"Json expect::string() error at root:
258    expected string to have at least 3 characters, but it has 2,
259    received "12""#
260        );
261    }
262}
263
264#[cfg(test)]
265mod test_max_len {
266    use crate::expect;
267    use crate::expect_json_eq;
268    use pretty_assertions::assert_eq;
269    use serde_json::json;
270
271    #[test]
272    fn it_should_pass_when_string_has_exactly_enough_characters() {
273        let left = json!("123");
274        let right = json!(expect::string().max_len(3));
275
276        let output = expect_json_eq(&left, &right);
277        assert!(output.is_ok(), "assertion error: {output:#?}");
278    }
279
280    #[test]
281    fn it_should_pass_when_string_has_less_than_enough_characters() {
282        let left = json!("12");
283        let right = json!(expect::string().max_len(5));
284
285        let output = expect_json_eq(&left, &right);
286        assert!(output.is_ok(), "assertion error: {output:#?}");
287    }
288
289    #[test]
290    fn it_should_fail_when_string_is_too_long() {
291        let left = json!("🦊🦊🦊🦊🦊🦊");
292        let right = json!(expect::string().max_len(3));
293
294        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
295        assert_eq!(
296            output,
297            r#"Json expect::string() error at root:
298    expected string to have at most 3 characters, but it has 24,
299    received "🦊🦊🦊🦊🦊🦊""#
300        );
301    }
302}