A set of matcher macros for ergonomic JSON testing with googletest-rust.
These tiny, focused matchers make it effortless to assert on serde_json::Value
in Rust tests.
Features
- Value: Match JSON primitive values (
string
,number
(i64/f64),bool
) using thejson::value!
macro, and matchnull
values withjson::is_null()
. - Object: Pattern-match JSON objects by fields using the
json::matches_pattern!{...}
macro; supports both * strict* mode ( all fields must match and no extra fields) and non-strict mode (extra fields allowed via..
). - Array: Match arrays element-by-element (ordered, supports heterogeneous types) using the
json::elements_are![...]
macro. - Unordered array: Match arrays where order does not matter using the
json::unordered_elements_are![...]
macro. - Contains-each (arrays): Require that each expected matcher matches a unique element in any order, allowing extra
elements, with the
json::contains_each![...]
macro. - Containment (arrays): Assert that an array is a subset of the expected matchers (i.e., every actual element is
accounted for) using the
json::is_contained_in![...]
macro. - Clear, structured diagnostic failure messages: When a match fails, detailed explanations show which part of the JSON structure did not match and why.
Getting Started
Add to your Cargo.toml
:
[]
= "0.14"
= "1"
= "0.1" # replace with the latest version on crates.io
This crate is typically only needed as a dev-dependency.
In tests:
use *;
use json;
use json as j;
The crate re-exports a
json
namespace with everything you need:
json::value!(...)
– match a value inside a JSONValue
json::matches_pattern!
– explicitly match JSON objects by fields (withjson::pat!
as an alias)json::elements_are![...]
– match arrays element-by-element (ordered)json::unordered_elements_are![...]
– match arrays element-by-element (unordered)json::is_contained_in![...]
– assert containment of JSON elements
Examples
Values
Match JSON values with json::value
# use *;
# use json;
# use json as j;
Objects
Match JSON objects with json::matches_pattern!
Strict match:
# use *;
# use json;
# use json as j;
Non-strict match (with ..
):
# use *;
# use json;
# use json as j;
Arrays
Match arrays with json::elements_are!
(ordered)
# use *;
# use json;
# use json as j;
Match arrays with json::unordered_elements_are!
(unordered)
# use *;
# use json;
# use json as j;
Assert containment with json::is_contained_in!
# use *;
# use json;
# use json as j;
Assert each matcher finds a unique element with json::contains_each!
# use *;
# use json;
# use json as j;
Array Matcher Quick Reference
Here’s a quick reference matrix comparing the array matchers:
Matcher | Order Matters | Extra Elements OK | Missing Elements OK | Use Case |
---|---|---|---|---|
elements_are! |
Yes | No | No | Exact ordered match of all elements |
unordered_elements_are! |
No | No | No | Exact unordered match of all elements |
contains_each! |
No | Yes | No | Require each matcher to match a unique element, extra allowed |
is_contained_in! |
No | No | Yes | Actual elements are subset of expected |
Combined example
Compose all together for complex structures. Here is a Rust struct with a JSON field, using matches_pattern!
for the
struct and nested JSON matchers for the field:
# use *;
# use matches_pattern;
# use json;
# use json as j;
Acknowledgements
Parts of this crate are adapted from googletest-rust, which is licensed under Apache 2.0.