create_json_matcher

Macro create_json_matcher 

Source
macro_rules! create_json_matcher {
    (null) => { ... };
    (true) => { ... };
    (false) => { ... };
    ($num:literal) => { ... };
    ($string:literal) => { ... };
    ([ $($item:tt),* $(,)? ]) => { ... };
    ({ $($json:tt)* }) => { ... };
    (@object {$($out:tt)*}) => { ... };
    (@object {$($out:tt)*} $key:literal : { $($value:tt)* } , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : { $($value:tt)* }) => { ... };
    (@object {$($out:tt)*} $key:literal : [ $($value:tt)* ] , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : [ $($value:tt)* ]) => { ... };
    (@object {$($out:tt)*} $key:literal : null , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : null) => { ... };
    (@object {$($out:tt)*} $key:literal : true , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : true) => { ... };
    (@object {$($out:tt)*} $key:literal : false , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : false) => { ... };
    (@object {$($out:tt)*} $key:literal : $value:literal , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : $value:literal) => { ... };
    (@object {$($out:tt)*} $key:ident : null , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:ident : null) => { ... };
    (@object {$($out:tt)*} $key:ident : true , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:ident : true) => { ... };
    (@object {$($out:tt)*} $key:ident : false , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:ident : false) => { ... };
    (@object {$($out:tt)*} $key:ident : $value:literal , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:ident : $value:literal) => { ... };
    (@object {$($out:tt)*} $key:literal : $value:expr , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:literal : $value:expr) => { ... };
    (@object {$($out:tt)*} $key:ident : $value:expr , $($rest:tt)*) => { ... };
    (@object {$($out:tt)*} $key:ident : $value:expr) => { ... };
    ($expr:expr) => { ... };
}
Expand description

Create a json matcher from JSON-like syntax with embedded matchers

use json_matcher::{
    create_json_matcher, BooleanMatcher, JsonMatcher, JsonMatcherError, JsonPath, JsonPathElement,
};
use serde_json::json;

let matcher = create_json_matcher!({
    "name": "John",
    "is_cool": BooleanMatcher::any()
});

let test_data = json!({
    "name": "John",
    "is_cool": "unknown"
});

assert_eq!(
    matcher.json_matches(&test_data),
    vec![JsonMatcherError {
        path: JsonPath::from(vec![
            JsonPathElement::Root,
            JsonPathElement::Key("is_cool".to_string())
        ]),
        message: "Value is not a boolean".to_string()
    }]
);