Struct JsonTest

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

Source

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

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§

Source§

impl<'a> Debug for JsonTest<'a>

Source§

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

Formats the value using the given formatter. Read more

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