mod common;
use common::oracle::{same, to_json};
use common::spec;
#[test]
fn the_comparator_keeps_signed_zero_apart() {
let neg: serde_json::Value = serde_json::from_str("-0").expect("parses");
let pos: serde_json::Value = serde_json::from_str("0").expect("parses");
assert!(!same(&neg, &pos), "-0 and 0 must not compare equal");
assert!(same(&neg, &neg) && same(&pos, &pos), "each equals itself");
let int: serde_json::Value = serde_json::from_str("1").expect("parses");
let float: serde_json::Value = serde_json::from_str("1.0").expect("parses");
assert!(same(&int, &float), "1 and 1.0 are the same number");
}
#[test]
fn spec() {
let mut failures = Vec::new();
for file in spec::files() {
for row in spec::rows(&file) {
let at = format!("{}:{}", row.file, row.line);
let result = tabnas_json::parse(&row.input);
if let Some(code) = row.expected.strip_prefix("ERROR:") {
if serde_json::from_str::<serde_json::Value>(&row.input).is_ok() {
failures.push(format!(
"{at}: fixture expects {code}, but serde_json accepts this input"
));
}
match result {
Ok(value) => failures.push(format!(
"{at}: expected error {code}, parsed {:?}",
to_json(&value)
)),
Err(error) if error.code != code => {
failures.push(format!("{at}: expected error {code}, got {}", error.code))
}
Err(_) => {}
}
continue;
}
let value = match result {
Ok(value) => value,
Err(error) => {
failures.push(format!(
"{at}: expected {}, got error {}",
row.expected,
error.to_string().lines().next().unwrap_or_default()
));
continue;
}
};
let got = to_json(&value);
match serde_json::from_str::<serde_json::Value>(&row.expected) {
Ok(want) if same(&got, &want) => {}
Ok(want) => {
failures.push(format!("{at}: got {got}, fixture expects {want}"));
continue;
}
Err(e) => {
failures.push(format!("{at}: fixture expected value is not JSON: {e}"));
continue;
}
}
match serde_json::from_str::<serde_json::Value>(&row.input) {
Ok(oracle) if same(&got, &oracle) => {}
Ok(oracle) => failures.push(format!(
"{at}: disagrees with serde_json: {got} != {oracle}"
)),
Err(e) => failures.push(format!("{at}: serde_json rejected a valid fixture: {e}")),
}
}
}
spec::report(failures);
}