use serde_json::Value;
pub struct PropertyMatcher<'a> {
pairs: Vec<(String, Value)>,
assertion: &'a mut super::base::JsonPathAssertion<'a>,
}
impl<'a> PropertyMatcher<'a> {
pub(crate) fn new(pairs: Vec<(String, Value)>, assertion: &'a mut super::base::JsonPathAssertion<'a>) -> Self {
Self { pairs, assertion }
}
pub fn count(self, expected: usize) -> Self {
assert_eq!(
self.pairs.len(),
expected,
"Expected {} matching properties but found {} at {}",
expected,
self.pairs.len(),
self.assertion.path_str
);
self
}
pub fn all<F>(self, predicate: F) -> Self
where
F: Fn((&str, &Value)) -> bool
{
for (k, v) in &self.pairs {
assert!(
predicate((k, v)),
"Property {:?} did not match predicate at {}",
(k, v),
self.assertion.path_str
);
}
self
}
pub fn collect_values(self) -> Vec<Value> {
self.pairs.into_iter().map(|(_, v)| v).collect()
}
pub fn collect_keys(self) -> Vec<String> {
self.pairs.into_iter().map(|(k, _)| k).collect()
}
pub fn collect_pairs(self) -> Vec<(String, Value)> {
self.pairs
}
pub fn and(self) -> &'a mut super::base::JsonPathAssertion<'a> {
self.assertion
}
}