use serde_json::{Value, json};
use super::{Doc, Kind, Style, parse};
fn style_bits(style: Style) -> u32 {
u32::from(style.tagged)
| u32::from(style.double) << 1
| u32::from(style.single) << 2
| u32::from(style.literal) << 3
| u32::from(style.folded) << 4
| u32::from(style.flow) << 5
}
fn kind_number(kind: Kind) -> u32 {
match kind {
Kind::Document => 1,
Kind::Sequence => 2,
Kind::Mapping => 4,
Kind::Scalar => 8,
Kind::Alias => 16,
}
}
fn dump_node(doc: &Doc, id: usize) -> Value {
let node = &doc.nodes[id];
let alias = node.alias.map_or(String::new(), |target| {
format!("{}:{}", doc.nodes[target].line, doc.nodes[target].column)
});
json!({
"kind": kind_number(node.kind),
"style": style_bits(node.style),
"tag": node.tag,
"value": node.value,
"anchor": node.anchor,
"alias": alias,
"head": node.head_comment,
"lc": node.line_comment,
"foot": node.foot_comment,
"ln": node.line,
"col": node.column,
"content": node.content.iter().map(|&child| dump_node(doc, child)).collect::<Vec<_>>(),
})
}
pub(super) fn dump(input: &str) -> Value {
match parse(input) {
Err(error) => json!({ "error": error.to_string() }),
Ok(doc) => match doc.root {
Some(root) => json!({ "node": dump_node(&doc, root) }),
None => json!({ "node": {
"kind": 0, "style": 0, "tag": "", "value": "", "anchor": "", "alias": "",
"head": "", "lc": "", "foot": "", "ln": 0, "col": 0, "content": [],
} }),
},
}
}
#[test]
fn corpus_matches_go() {
let dir =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/schema-gen/yaml");
let mut checked = 0;
for entry in std::fs::read_dir(&dir).expect("corpus dir") {
let path = entry.expect("entry").path();
if path.extension().is_none_or(|e| e != "yaml") {
continue;
}
let input = std::fs::read(&path).expect("input");
if path.file_name().is_some_and(|name| name == "crlf.yaml") {
assert!(
input.windows(2).any(|pair| pair == b"\r\n"),
"crlf.yaml lost its CRLF"
);
}
let expected =
std::fs::read_to_string(format!("{}.go.json", path.display())).expect("go dump");
let expected: Value = serde_json::from_str(&expected).expect("json");
let actual = dump(&String::from_utf8_lossy(&input));
assert_eq!(actual, expected, "{}", path.display());
checked += 1;
}
assert!(checked >= 25, "the corpus is missing");
}
const RESOLVED: [(&str, &str, &str); 27] = [
("", "a", "Str(\"a\")"),
("", "~", "Null"),
("", "True", "Bool(true)"),
("", "yes", "Str(\"yes\")"),
("", "0x1F", "Int(31)"),
("", "0o17", "Int(15)"),
("", "017", "Int(15)"),
("", "0b101", "Int(5)"),
("", "1_000", "Int(1000)"),
("", "-12", "Int(-12)"),
("", "1e3", "Float(1000.0)"),
("", ".5", "Float(0.5)"),
("", ".inf", "Float(inf)"),
("", "-.inf", "Float(-inf)"),
("", "12345678901234567890", "Uint(12345678901234567890)"),
("", "18446744073709551616", "Float(1.8446744073709552e19)"),
("", "2001-12-14", "2001-12-14T00:00:00Z"),
(
"",
"2001-12-14t21:59:43.10-05:00",
"2001-12-14T21:59:43.1-05:00",
),
("", "2001-12-14 21:59:43", "2001-12-14T21:59:43Z"),
(
"",
"2001-12-14T21:59:43.123456789Z",
"2001-12-14T21:59:43.123456789Z",
),
(
"",
"2001-12-14 21:59:43.10 -5",
"Str(\"2001-12-14 21:59:43.10 -5\")",
),
("!!str", "12", "Str(\"12\")"),
("!!int", "0x10", "Int(16)"),
("!!float", "1", "Float(1.0)"),
("!!timestamp", "2001-12-14", "2001-12-14T00:00:00Z"),
("!!str", "true", "Str(\"true\")"),
("!!seq", "x", "Str(\"x\")"),
];
const MISFITS: [(&str, &str, &str); 4] = [
("!!int", "abc", "cannot decode !!str `abc` as a !!int"),
("!!bool", "yes", "cannot decode !!str `yes` as a !!bool"),
("!!null", "x", "cannot decode !!str `x` as a !!null"),
(
"!!timestamp",
"nope",
"cannot decode !!str `nope` as a !!timestamp",
),
];
#[test]
fn scalars_resolve_to_the_values_go_decodes() {
for (tag, input, expected) in RESOLVED {
let (_, value) = super::resolve::resolve(tag, input).expect("resolves");
let shown = match value {
super::resolve::Resolved::Timestamp(stamp) => stamp.to_string(),
other => format!("{other:?}"),
};
assert_eq!(shown, expected, "{tag} {input}");
}
}
#[test]
fn explicit_tags_that_do_not_fit_fail_like_go() {
for (tag, input, expected) in MISFITS {
assert_eq!(
super::resolve::resolve(tag, input).expect_err("fails"),
expected,
"{tag} {input}"
);
}
}