expect_json/expects/ops/contains_not/
array_contains_not.rs

1use crate::expects::SerializeExpectOp;
2use crate::internals::objects::ArrayObject;
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;
10use serde_json::Value;
11use std::collections::HashSet;
12
13#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
14pub struct ArrayContainsNot {
15    values: Vec<Value>,
16}
17
18impl ArrayContainsNot {
19    pub(crate) fn new(values: Vec<Value>) -> Self {
20        Self { values }
21    }
22}
23
24impl JsonExpectOp for ArrayContainsNot {
25    fn on_array<'a>(
26        self,
27        context: &mut Context<'a>,
28        received_values: &'a [Value],
29    ) -> JsonValueEqResult<()> {
30        let received_items_in_set = received_values.iter().collect::<HashSet<&'a Value>>();
31
32        for expected in self.values {
33            if received_items_in_set.contains(&expected) {
34                return Err(JsonValueEqError::ContainsFound {
35                    context: context.to_static(),
36                    json_type: ValueType::Array,
37                    expected: expected.into(),
38                    received: ArrayObject::from(received_values.to_owned()).into(),
39                });
40            }
41        }
42
43        Ok(())
44    }
45}
46
47impl From<ArrayContainsNot> for SerializeExpectOp {
48    fn from(contains: ArrayContainsNot) -> Self {
49        SerializeExpectOp::ArrayContainsNot(contains)
50    }
51}
52
53#[cfg(test)]
54mod test_array_contains_not {
55    use crate::expect;
56    use crate::expect_json_eq;
57    use pretty_assertions::assert_eq;
58    use serde_json::json;
59
60    #[test]
61    fn it_should_error_for_identical_numeric_arrays() {
62        let left = json!([1, 2, 3]);
63        let right = json!(expect.not.contains([1, 2, 3]));
64
65        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
66        assert_eq!(
67            output,
68            r#"Json array at root contains value was expecting to not be there:
69    expected array to not contain 1, but it was found.
70    received [1, 2, 3]"#
71        );
72    }
73
74    #[test]
75    fn it_should_errorfor_reversed_identical_numeric_arrays() {
76        let left = json!([1, 2, 3]);
77        let right = json!(expect.not.contains([3, 2, 1]));
78
79        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
80        assert_eq!(
81            output,
82            r#"Json array at root contains value was expecting to not be there:
83    expected array to not contain 3, but it was found.
84    received [1, 2, 3]"#
85        );
86    }
87
88    #[test]
89    fn it_should_error_for_partial_contains() {
90        let left = json!([0, 1, 2, 3, 4, 5]);
91        let right = json!(expect.not.contains([1, 2, 3]));
92
93        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
94        assert_eq!(
95            output,
96            r#"Json array at root contains value was expecting to not be there:
97    expected array to not contain 1, but it was found.
98    received [0, 1, 2, 3, 4, 5]"#
99        );
100    }
101
102    #[test]
103    fn it_should_be_ok_for_totall_different_values() {
104        let left = json!([0, 1, 2, 3]);
105        let right = json!(expect.not.contains([4, 5, 6]));
106
107        let output = expect_json_eq(&left, &right);
108        assert!(output.is_ok());
109    }
110
111    #[test]
112    fn it_should_be_ok_for_empty_contains() {
113        let left = json!([0, 1, 2, 3]);
114        let right = json!(expect.not.contains(&[] as &'static [u32]));
115
116        let output = expect_json_eq(&left, &right);
117        assert!(output.is_ok());
118    }
119}