macro_rules! pat {
({ $($key:literal : $val:expr),* $(,)? }) => { ... };
({ $($key:literal : $val:expr),* , .. }) => { ... };
}
Expand description
Matches a JSON object by specifying a pattern of key-value matchers, similar to
GoogleTest’s matches_pattern!
macro for Rust structs.
This macro is used for asserting that a serde_json::Value
representing a JSON object
contains the specified fields, with each field matching the corresponding matcher. Extra
fields are rejected unless the pattern ends with ..
.
§Examples
Basic usage:
let value = json!({ "name": "Alice", "age": 30 });
assert_that!(
value,
json::pat!({
"name": eq("Alice"),
"age": ge(29),
.. // allows additional fields
})
);
Nested matching:
let value = json!({
"user": {
"id": 1,
"active": true
}
});
assert_that!(
value,
json::pat!({
"user": json::pat!({
"id": eq(1),
"active": is_true(),
})
})
);
§Alias
This macro is reexported as json::pat!
.