expect_json/expects/ops/contains_not/
string_contains_not.rs

1use crate::expects::SerializeExpectOp;
2use crate::internals::objects::StringObject;
3use crate::internals::types::ValueType;
4use crate::internals::Context;
5use crate::internals::JsonExpectOp;
6use crate::internals::JsonValueEqError;
7use crate::internals::JsonValueEqResult;
8use serde::Deserialize;
9use serde::Serialize;
10
11#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
12pub struct StringContainsNot {
13    content: String,
14}
15
16impl StringContainsNot {
17    pub(crate) fn new(content: String) -> Self {
18        Self { content }
19    }
20}
21
22impl JsonExpectOp for StringContainsNot {
23    fn on_string<'a>(self, context: &mut Context<'a>, received: &'a str) -> JsonValueEqResult<()> {
24        if received.contains(&self.content) {
25            return Err(JsonValueEqError::ContainsFound {
26                context: context.to_static(),
27                json_type: ValueType::String,
28                expected: StringObject::from(self.content).into(),
29                received: StringObject::from(received.to_owned()).into(),
30            });
31        }
32
33        Ok(())
34    }
35}
36
37impl From<StringContainsNot> for SerializeExpectOp {
38    fn from(contains: StringContainsNot) -> Self {
39        SerializeExpectOp::StringContainsNot(contains)
40    }
41}
42
43#[cfg(test)]
44mod test_string_contains_not {
45    use crate::expect;
46    use crate::expect_json_eq;
47    use pretty_assertions::assert_eq;
48    use serde_json::json;
49
50    #[test]
51    fn it_should_error_for_identical_strings() {
52        let left = json!("1, 2, 3");
53        let right = json!(expect.not.contains("1, 2, 3"));
54
55        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
56        assert_eq!(
57            output,
58            r#"Json string at root contains value was expecting to not be there:
59    expected string to not contain "1, 2, 3", but it was found.
60    received "1, 2, 3""#
61        );
62    }
63
64    #[test]
65    fn it_should_error_for_partial_matches_in_middle() {
66        let left = json!("0, 1, 2, 3, 4");
67        let right = json!(expect.not.contains("1, 2, 3"));
68
69        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
70        assert_eq!(
71            output,
72            r#"Json string at root contains value was expecting to not be there:
73    expected string to not contain "1, 2, 3", but it was found.
74    received "0, 1, 2, 3, 4""#
75        );
76    }
77
78    #[test]
79    fn it_should_be_ok_for_empty_contains() {
80        let left = json!("0, 1, 2, 3, 4, 5");
81        let right = json!(expect.not.contains(""));
82
83        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
84        assert_eq!(
85            output,
86            r#"Json string at root contains value was expecting to not be there:
87    expected string to not contain "", but it was found.
88    received "0, 1, 2, 3, 4, 5""#
89        );
90    }
91
92    #[test]
93    fn it_should_be_ok_for_totall_different_values() {
94        let left = json!("1, 2, 3");
95        let right = json!(expect.not.contains("a, b, c"));
96
97        let output = expect_json_eq(&left, &right);
98        assert!(output.is_ok());
99    }
100}