Trait PropertyAssertions

Source
pub trait PropertyAssertions<'a> {
    // Required methods
    fn has_property(&'a mut self, name: &str) -> &'a mut Self;
    fn has_properties<I, S>(&'a mut self, names: I) -> &'a mut Self
       where I: IntoIterator<Item = S>,
             S: AsRef<str>;
    fn has_property_count(&'a mut self, expected: usize) -> &'a mut Self;
    fn has_property_count_matching<F>(
        &'a mut self,
        predicate: F,
        expected: usize,
    ) -> &'a mut Self
       where F: Fn(&str) -> bool;
    fn has_property_value(
        &'a mut self,
        name: &str,
        expected: Value,
    ) -> &'a mut Self;
    fn has_property_matching<F>(
        &'a mut self,
        name: &str,
        predicate: F,
    ) -> &'a mut Self
       where F: Fn(&Value) -> bool;
    fn properties_matching<F>(&'a mut self, predicate: F) -> PropertyMatcher<'a>
       where F: Fn(&str) -> bool;
}
Expand description

Trait providing property testing capabilities for JSON objects.

Required Methods§

Source

fn has_property(&'a mut self, name: &str) -> &'a mut Self

Asserts that the object has the specified property.

§Examples
test.assert_path("$.user")
    .has_property("name");
§Panics
  • Panics if the value is not an object
  • Panics if the property doesn’t exist
Source

fn has_properties<I, S>(&'a mut self, names: I) -> &'a mut Self
where I: IntoIterator<Item = S>, S: AsRef<str>,

Asserts that the object has all the specified properties.

§Examples
test.assert_path("$.user")
    .has_properties(["name", "age"]);
§Panics
  • Panics if the value is not an object
  • Panics if any of the properties don’t exist
Source

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

Asserts that the object has exactly the expected number of properties.

§Examples
test.assert_path("$.user")
    .has_property_count(2);
§Panics
  • Panics if the value is not an object
  • Panics if the number of properties doesn’t match the expected countfn has_property_count(&’a mut self, expected: usize) -> &’a mut Self;
Source

fn has_property_count_matching<F>( &'a mut self, predicate: F, expected: usize, ) -> &'a mut Self
where F: Fn(&str) -> bool,

Asserts that the object has the expected number of properties matching a predicate.

§Examples
test.assert_path("$.user")
    .has_property_count_matching(|key| key.starts_with("meta_"), 2);
§Panics
  • Panics if the value is not an object
  • Panics if the number of matching properties doesn’t equal the expected count
Source

fn has_property_value(&'a mut self, name: &str, expected: Value) -> &'a mut Self

Asserts that a property has the expected value.

§Examples
test.assert_path("$.user")
    .has_property_value("name", json!("John"));
§Panics
  • Panics if the value is not an object
  • Panics if the property doesn’t exist
  • Panics if the property value doesn’t match the expected value
Source

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

Asserts that a property’s value satisfies a predicate.

§Examples
test.assert_path("$.user")
    .has_property_matching("age", |v| v.as_u64().unwrap_or(0) > 18);
§Panics
  • Panics if the value is not an object
  • Panics if the property doesn’t exist
  • Panics if the property value doesn’t satisfy the predicate
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.

§Examples
test.assert_path("$.user")
    .properties_matching(|key| key.starts_with("meta_"))
    .count(2)
    .and()
    .has_property_count(2);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§