#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic,
dead_code
)]
use oneyaml::{Document, Value};
pub fn agrees_with_its_text(doc: &Document) {
let text = doc.to_string();
let s = doc.settings();
let a = Value::try_from(doc);
let b = oneyaml::from_str_with::<Value>(&text, s);
match (&a, &b) {
(Ok(a), Ok(b)) => assert_eq!(a, b, "meaning differs for {text:?}"),
(Err(a), Err(b)) => assert_eq!(a.code(), b.code(), "{text:?}"),
_ => panic!("tree {a:?} vs text {b:?} for {text:?}"),
}
if doc.has_errors() {
let re = Document::parse_recovering_with(text.clone(), s);
assert_eq!(re.value().diagnostics(), doc.diagnostics(), "{text:?}");
} else {
let re = Document::parse_with(text.clone(), s)
.unwrap_or_else(|e| panic!("edited text does not parse: {e} in {text:?}"));
assert_eq!(re.diagnostics(), doc.diagnostics(), "{text:?}");
assert_eq!(re.to_string(), text);
}
}