json_test/matchers/
value.rs1use super::JsonMatcher;
2use serde_json::Value;
3
4#[derive(Debug)]
5pub struct ValueMatcher {
6 expected: Value,
7}
8
9impl ValueMatcher {
10 pub fn new(expected: Value) -> Self {
11 Self { expected }
12 }
13
14 pub fn eq(expected: Value) -> Self {
15 Self::new(expected)
16 }
17}
18
19impl JsonMatcher for ValueMatcher {
20 fn matches(&self, value: &Value) -> bool {
21 &self.expected == value
22 }
23
24 fn description(&self) -> String {
25 format!("equals {}", self.expected)
26 }
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use serde_json::json;
33
34 #[test]
35 fn test_value_matching() {
36 let value = json!(42);
37 assert!(ValueMatcher::eq(json!(42)).matches(&value));
38 assert!(!ValueMatcher::eq(json!(43)).matches(&value));
39
40 let obj = json!({"name": "test", "value": 42});
41 assert!(ValueMatcher::eq(json!({"name": "test", "value": 42})).matches(&obj));
42 assert!(!ValueMatcher::eq(json!({"name": "other"})).matches(&obj));
43 }
44
45 #[test]
46 fn test_array_matching() {
47 let arr = json!([1, 2, 3]);
48 assert!(ValueMatcher::eq(json!([1, 2, 3])).matches(&arr));
49 assert!(!ValueMatcher::eq(json!([3, 2, 1])).matches(&arr));
50 }
51
52 #[test]
53 fn test_null_matching() {
54 let null = json!(null);
55 assert!(ValueMatcher::eq(json!(null)).matches(&null));
56 assert!(!ValueMatcher::eq(json!(42)).matches(&null));
57 }
58}