#![cfg(feature = "json")]
use std::borrow::Cow;
use std::fmt;
use crate::core::Matcher;
use crate::matchers::strings::JsonMatcher;
use super::MismatchFormat;
pub fn match_json<'a, Actual>(json: impl Into<Cow<'a, str>>) -> Matcher<'a, Actual, Actual>
where
Actual: fmt::Debug + AsRef<str> + 'a,
{
Matcher::new(
JsonMatcher::new(json),
MismatchFormat::new("to be equivalent JSON to", "to not be equivalent JSON to"),
)
}
#[cfg(test)]
mod tests {
use super::match_json;
use crate::expect;
fn expected() -> &'static str {
r#"
{
"key1": "value1",
"key2": "value2"
}
"#
}
fn actual() -> &'static str {
r#"{"key2":"value2","key1":"value1"}"#
}
#[test]
fn succeeds_when_matches_json() {
expect!(actual()).to(match_json(expected()));
}
#[test]
fn succeeds_when_not_matches_json() {
expect!(actual()).to_not(match_json(r#""different json""#));
}
#[test]
#[should_panic]
fn fails_when_matches_json() {
expect!(actual()).to_not(match_json(expected()));
}
#[test]
#[should_panic]
fn fails_when_not_matches_json() {
expect!(actual()).to(match_json(r#""different json""#));
}
}