json_matcher/matchers/
null.rs1use serde_json::Value;
2
3use crate::{JsonMatcher, JsonMatcherError};
4
5pub struct NullMatcher {}
6
7impl Default for NullMatcher {
8 fn default() -> Self {
9 Self::new()
10 }
11}
12
13impl NullMatcher {
14 pub fn new() -> Self {
15 Self {}
16 }
17}
18
19impl JsonMatcher for NullMatcher {
20 fn json_matches(&self, value: &Value) -> Vec<JsonMatcherError> {
21 match value {
22 Value::Null => vec![],
23 _ => vec![JsonMatcherError::at_root("Value is not null")],
24 }
25 }
26}
27
28impl JsonMatcher for () {
29 fn json_matches(&self, value: &Value) -> Vec<JsonMatcherError> {
30 NullMatcher::new().json_matches(value)
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use crate::assert_jm;
37
38 use super::*;
39
40 #[test]
41 fn test_null_matcher() {
42 let matcher = NullMatcher::new();
43 assert_jm!(Value::Null, matcher);
44 assert_eq!(
45 *std::panic::catch_unwind(|| {
46 assert_jm!(Value::String("world".to_string()), matcher)
47 })
48 .err()
49 .unwrap()
50 .downcast::<String>()
51 .unwrap(),
52 r#"
53Json matcher failed:
54 - $: Value is not null
55
56Actual:
57"world""#
58 );
59 }
60
61 #[test]
62 fn test_raw_implementations() {
63 assert_eq!(().json_matches(&Value::Null), vec![]);
64 assert_eq!(
65 ().json_matches(&Value::Bool(true))
66 .into_iter()
67 .map(|x| x.to_string())
68 .collect::<String>(),
69 "$: Value is not null"
70 );
71 }
72}