pub struct JsonTest<'a> { /* private fields */ }
Expand description
Main entry point for JSON testing.
JsonTest
provides methods to create assertions on JSON values using JSONPath expressions.
It maintains a reference to the JSON being tested and enables creation of chainable assertions.
§Examples
use json_test::{JsonTest, PropertyAssertions};
use serde_json::json;
let data = json!({
"user": {
"name": "John",
"settings": {
"theme": "dark"
}
}
});
let mut test = JsonTest::new(&data);
// Test a single path with chained assertions
test.assert_path("$.user")
.has_property("name")
.has_property("settings")
.has_property_value("name", json!("John"));
Implementations§
Source§impl<'a> JsonTest<'a>
impl<'a> JsonTest<'a>
Sourcepub fn new(json: &'a Value) -> Self
pub fn new(json: &'a Value) -> Self
Creates a new JSON test instance.
Takes a reference to a JSON value that will be tested. The JSON value must live at least as long as the test instance.
§Examples
let data = json!({"key": "value"});
let test = JsonTest::new(&data);
Sourcepub fn assert_path(&'a mut self, path: &str) -> JsonPathAssertion<'a>
pub fn assert_path(&'a mut self, path: &str) -> JsonPathAssertion<'a>
Creates a new assertion for the given JSONPath expression.
The path must be a valid JSONPath expression. Invalid paths will cause a panic with a descriptive error message.
§Examples
let data = json!({
"users": [
{"name": "John", "role": "admin"},
{"name": "Jane", "role": "user"}
]
});
let mut test = JsonTest::new(&data);
// Test array element with chained assertions
test.assert_path("$.users[0]")
.has_property("name")
.has_property_value("role", json!("admin"));
§Panics
Panics if the JSONPath expression is invalid. This is appropriate for testing scenarios where invalid paths indicate test specification errors.
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for JsonTest<'a>
impl<'a> RefUnwindSafe for JsonTest<'a>
impl<'a> Send for JsonTest<'a>
impl<'a> Sync for JsonTest<'a>
impl<'a> Unpin for JsonTest<'a>
impl<'a> UnwindSafe for JsonTest<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more