Struct JsonPathAssertion

Source
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>

Source

pub fn exists(&'a mut self) -> &'a mut Self

Asserts that the path exists and has at least one value.

§Examples
test.assert_path("$.user.name")
    .exists();
§Panics

Panics if the path does not exist in the JSON structure.

Source

pub fn does_not_exist(&'a mut self) -> &'a mut Self

Asserts that the path does not exist or has no values.

§Examples
test.assert_path("$.user.email")
    .does_not_exist();
§Panics

Panics if the path exists in the JSON structure.

Source

pub fn equals(&'a mut self, expected: Value) -> &'a mut Self

Asserts that the value at the current path equals the expected value.

§Examples
test.assert_path("$.user.name")
    .equals(json!("John"));
§Panics
  • Panics if no value exists at the path
  • Panics if the value doesn’t match the expected value
Source

pub fn is_string(&'a mut self) -> &'a mut Self

Asserts that the value at the current path is a string.

§Examples
test.assert_path("$.message")
    .is_string();
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a string
Source

pub fn contains_string(&'a mut self, substring: &str) -> &'a mut Self

Asserts that the string value contains the given substring.

§Examples
test.assert_path("$.email")
    .contains_string("@example");
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a string
  • Panics if the string does not contain the substring
Source

pub fn starts_with(&'a mut self, prefix: &str) -> &'a mut Self

Asserts that the string value starts with the given prefix.

§Examples
test.assert_path("$.id")
    .starts_with("user_");
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a string
  • Panics if the string does not start with the prefix
Source

pub fn ends_with(&'a mut self, suffix: &str) -> &'a mut Self

Asserts that the string value ends with the given suffix.

§Examples
test.assert_path("$.file")
    .ends_with(".pdf");
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a string
  • Panics if the string does not end with the suffix
Source

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
Source

pub fn is_number(&'a mut self) -> &'a mut Self

Asserts that the value at the current path is a number.

§Examples
test.assert_path("$.count")
    .is_number();
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a number
Source

pub fn is_greater_than(&'a mut self, value: i64) -> &'a mut Self

Asserts that the numeric value is greater than the given value.

§Examples
test.assert_path("$.age")
    .is_greater_than(18);
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a number
  • Panics if the value is not greater than the given value
Source

pub fn is_less_than(&'a mut self, value: i64) -> &'a mut Self

Asserts that the numeric value is less than the given value.

§Examples
test.assert_path("$.temperature")
    .is_less_than(40);
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a number
  • Panics if the value is not less than the given value
Source

pub fn is_between(&'a mut self, min: i64, max: i64) -> &'a mut Self

Asserts that the numeric value is between the given minimum and maximum values (inclusive).

§Examples
test.assert_path("$.score")
    .is_between(0, 100);
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not a number
  • Panics if the value is not between min and max (inclusive)
Source

pub fn is_array(&'a mut self) -> &'a mut Self

Asserts that the value at the current path is an array.

§Examples
test.assert_path("$.tags")
    .is_array();
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not an array
Source

pub fn has_length(&'a mut self, expected: usize) -> &'a mut Self

Asserts that the array has the expected length.

§Examples
test.assert_path("$.tags")
    .is_array()
    .has_length(2);
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not an array
  • Panics if the array length doesn’t match the expected length
Source

pub fn contains(&'a mut self, expected: &Value) -> &'a mut Self

Asserts that the array contains the expected value.

§Examples
test.assert_path("$.roles")
    .is_array()
    .contains(&json!("admin"));
§Panics
  • Panics if no value exists at the path
  • Panics if the value is not an array
  • Panics if the array does not contain the expected value
Source

pub fn matches<F>(&'a mut self, predicate: F) -> &'a mut Self
where F: FnOnce(&Value) -> bool,

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
Source

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
Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PropertyAssertions<'a> for JsonPathAssertion<'a>

Source§

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
where I: IntoIterator<Item = S>, S: AsRef<str>,

Asserts that the object has all the specified properties. Read more
Source§

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
where F: Fn(&str) -> bool,

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

Asserts that a property has the expected value. Read more
Source§

fn has_property_matching<F>(&mut self, name: &str, predicate: F) -> &mut Self
where F: Fn(&Value) -> bool,

Asserts that a property’s value satisfies a predicate. Read more
Source§

fn properties_matching<F>(&'a mut self, predicate: F) -> PropertyMatcher<'a>
where F: Fn(&str) -> bool,

Creates a PropertyMatcher for testing properties that match 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.