pub struct JsonPathAssertion<'a> { /* private fields */ }
Expand description
Provides assertions for JSON values accessed via JSONPath expressions.
This struct is created by JsonTest::assert_path()
and enables a fluent API
for testing JSON values. All assertion methods follow a builder pattern,
returning &mut Self
for chaining.
§Examples
use json_test::{JsonTest, PropertyAssertions};
use serde_json::json;
let data = json!({
"user": {
"name": "John",
"age": 30
}
});
let mut test = JsonTest::new(&data);
test.assert_path("$.user")
.exists()
.has_property("name")
.has_property_value("age", json!(30));
Implementations§
Source§impl<'a> JsonPathAssertion<'a>
impl<'a> JsonPathAssertion<'a>
Sourcepub fn does_not_exist(&'a mut self) -> &'a mut Self
pub fn does_not_exist(&'a mut self) -> &'a mut Self
Sourcepub fn contains_string(&'a mut self, substring: &str) -> &'a mut Self
pub fn contains_string(&'a mut self, substring: &str) -> &'a mut Self
Sourcepub fn starts_with(&'a mut self, prefix: &str) -> &'a mut Self
pub fn starts_with(&'a mut self, prefix: &str) -> &'a mut Self
Sourcepub fn matches_pattern(&'a mut self, pattern: &str) -> &'a mut Self
pub fn matches_pattern(&'a mut self, pattern: &str) -> &'a mut Self
Asserts that the string value matches the given regular expression pattern.
§Examples
test.assert_path("$.email")
.matches_pattern(r"^[^@]+@[^@]+\.[^@]+$");
§Panics
- Panics if no value exists at the path
- Panics if the value is not a string
- Panics if the pattern is invalid
- Panics if the string does not match the pattern
Sourcepub fn is_greater_than(&'a mut self, value: i64) -> &'a mut Self
pub fn is_greater_than(&'a mut self, value: i64) -> &'a mut Self
Sourcepub fn is_less_than(&'a mut self, value: i64) -> &'a mut Self
pub fn is_less_than(&'a mut self, value: i64) -> &'a mut Self
Sourcepub fn is_between(&'a mut self, min: i64, max: i64) -> &'a mut Self
pub fn is_between(&'a mut self, min: i64, max: i64) -> &'a mut Self
Sourcepub fn has_length(&'a mut self, expected: usize) -> &'a mut Self
pub fn has_length(&'a mut self, expected: usize) -> &'a mut Self
Sourcepub fn matches<F>(&'a mut self, predicate: F) -> &'a mut Self
pub fn matches<F>(&'a mut self, predicate: F) -> &'a mut Self
Asserts that the value matches a custom predicate.
This method allows for complex value validation using custom logic.
§Examples
test.assert_path("$.timestamp")
.matches(|value| {
value.as_str()
.map(|s| s.contains("T") && s.ends_with("Z"))
.unwrap_or(false)
});
§Panics
- Panics if no value exists at the path
- Panics if the value doesn’t satisfy the predicate
Sourcepub fn assert_object(&self) -> Map<String, Value>
pub fn assert_object(&self) -> Map<String, Value>
Asserts that the value is an object and returns it for further testing.
This method is primarily used internally by property assertions.
§Examples
let obj = test.assert_path("$.user")
.assert_object();
assert!(obj.contains_key("name"));
§Panics
- Panics if no value exists at the path
- Panics if the value is not an object
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 a different path while maintaining the test context.
This method enables chaining assertions across different paths.
§Examples
test.assert_path("$.user")
.has_property("name")
.assert_path("$.settings")
.has_property("theme");
§Panics
- Panics if called on an assertion without test context
Trait Implementations§
Source§impl<'a> Debug for JsonPathAssertion<'a>
impl<'a> Debug for JsonPathAssertion<'a>
Source§impl<'a> PropertyAssertions<'a> for JsonPathAssertion<'a>
impl<'a> PropertyAssertions<'a> for JsonPathAssertion<'a>
Source§fn has_property(&'a mut self, name: &str) -> &'a mut Self
fn has_property(&'a mut self, name: &str) -> &'a mut Self
Asserts that the object has the specified property. Read more
Source§fn has_properties<I, S>(&mut self, names: I) -> &mut Self
fn has_properties<I, S>(&mut self, names: I) -> &mut Self
Asserts that the object has all the specified properties. Read more
Source§fn has_property_count(&mut self, expected: usize) -> &mut Self
fn has_property_count(&mut self, expected: usize) -> &mut Self
Asserts that the object has exactly the expected number of properties. Read more
Source§fn has_property_count_matching<F>(
&mut self,
predicate: F,
expected: usize,
) -> &mut Self
fn has_property_count_matching<F>( &mut self, predicate: F, expected: usize, ) -> &mut Self
Asserts that the object has the expected number of properties matching a predicate. Read more
Source§fn has_property_value(&mut self, name: &str, expected: Value) -> &mut Self
fn has_property_value(&mut self, name: &str, expected: Value) -> &mut Self
Asserts that a property has the expected value. Read more
Source§fn has_property_matching<F>(&mut self, name: &str, predicate: F) -> &mut Self
fn has_property_matching<F>(&mut self, name: &str, predicate: F) -> &mut Self
Asserts that a property’s value satisfies a predicate. Read more
Auto Trait Implementations§
impl<'a> Freeze for JsonPathAssertion<'a>
impl<'a> RefUnwindSafe for JsonPathAssertion<'a>
impl<'a> Send for JsonPathAssertion<'a>
impl<'a> Sync for JsonPathAssertion<'a>
impl<'a> Unpin for JsonPathAssertion<'a>
impl<'a> !UnwindSafe for JsonPathAssertion<'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