00726d5498
yaml: lossless block and flow YAML subset with | and > scalars, comments, tab-indent error, limits (1 MiB, depth 12, 10000 nodes), emitter that round-trips; 9 fixtures from the Obsidian Bases docs (Bases tasks 1; written by Claude Sonnet after two MiniMax attempts produced no code). expr: tokens, AST and precedence-climbing parser with regex literals, arrays, objects, member/index/call, dependency collection, depth and length limits (task 3; MiniMax M3.1 Flash, OpenCode Zen free route). Verified: notescraft-bases 43+ tests pass, clippy -D warnings clean, xtask layers OK. The previous bases commit message claimed a YAML parser that did not exist yet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
185 lines
7.3 KiB
Rust
185 lines
7.3 KiB
Rust
//! YAML subset: fixtures from the Obsidian Bases docs, scalars, limits and robustness.
|
|
|
|
use notescraft_bases::{Error, Node, emit, parse};
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn s(x: &str) -> Node {
|
|
Node::Str(x.to_string())
|
|
}
|
|
|
|
fn get<'a>(n: &'a Node, key: &str) -> &'a Node {
|
|
match n {
|
|
Node::Map(m) => m.iter().find(|(k, _)| k == key).map_or(&Node::Null, |(_, v)| v),
|
|
_ => &Node::Null,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn fixtures_parse_and_round_trip() {
|
|
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
|
|
let mut n = 0;
|
|
for e in std::fs::read_dir(dir).unwrap() {
|
|
let path = e.unwrap().path();
|
|
let text = std::fs::read_to_string(&path).unwrap();
|
|
let a = parse(&text).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
|
let out = emit(&a);
|
|
let b = parse(&out).unwrap_or_else(|e| panic!("{} re-parse: {e}\n{out}", path.display()));
|
|
assert_eq!(a, b, "{}\n{out}", path.display());
|
|
n += 1;
|
|
}
|
|
assert!(n >= 9);
|
|
}
|
|
|
|
#[test]
|
|
fn key_order_and_nesting() {
|
|
let n = parse(include_str!("fixtures/Bases_syntax_1.yaml")).unwrap();
|
|
let Node::Map(m) = &n else { panic!() };
|
|
let keys: Vec<_> = m.iter().map(|(k, _)| k.as_str()).collect();
|
|
assert_eq!(keys, ["filters", "formulas", "properties", "summaries", "views"]);
|
|
assert_eq!(get(get(&n, "formulas"), "ppu"), &s("(price / age).toFixed(2)"));
|
|
let Node::Seq(views) = get(&n, "views") else { panic!() };
|
|
assert_eq!(get(&views[0], "limit"), &Node::Num(10.0));
|
|
assert_eq!(get(get(&views[0], "groupBy"), "direction"), &s("DESC"));
|
|
let emitted = emit(&n);
|
|
let pos = |k: &str| emitted.find(k).unwrap();
|
|
assert!(pos("filters:") < pos("formulas:") && pos("formulas:") < pos("views:"));
|
|
}
|
|
|
|
#[test]
|
|
fn block_scalars_and_chomping() {
|
|
let n = parse("a: |\n x\n y\n\nb: |-\n x\n y\nc: |+\n x\n\nd: >\n one\n two\n\n three\ne: >-\n p\n q\nf: 1\n").unwrap();
|
|
assert_eq!(get(&n, "a"), &s("x\ny\n"));
|
|
assert_eq!(get(&n, "b"), &s("x\ny"));
|
|
assert_eq!(get(&n, "c"), &s("x\n\n"));
|
|
assert_eq!(get(&n, "d"), &s("one two\nthree\n"));
|
|
assert_eq!(get(&n, "e"), &s("p q"));
|
|
assert_eq!(get(&n, "f"), &Node::Num(1.0));
|
|
assert_eq!(parse(&emit(&n)).unwrap(), n);
|
|
}
|
|
|
|
#[test]
|
|
fn quoted_formulas() {
|
|
let n = parse("f: 'if(a == \"x\", 1, 2)'\ng: \"say \\\"hi\\\"\\n\\ttab \\u00e9 \\\\\"\nh: 'it''s'\n- x\n".trim_end_matches("- x\n")).unwrap();
|
|
assert_eq!(get(&n, "f"), &s("if(a == \"x\", 1, 2)"));
|
|
assert_eq!(get(&n, "g"), &s("say \"hi\"\n\ttab \u{e9} \\"));
|
|
assert_eq!(get(&n, "h"), &s("it's"));
|
|
assert_eq!(parse(&emit(&n)).unwrap(), n);
|
|
}
|
|
|
|
#[test]
|
|
fn flow_collections() {
|
|
let n = parse("l: [a, 1, \"c, d\", [x, y], {k: v}]\nm: {a: 1, b: [true, ~], \"q r\": 'z'}\ne: []\nf: {}\n").unwrap();
|
|
assert_eq!(get(&n, "l"), &Node::Seq(vec![s("a"), Node::Num(1.0), s("c, d"), Node::Seq(vec![s("x"), s("y")]), Node::Map(vec![("k".into(), s("v"))])]));
|
|
assert_eq!(get(get(&n, "m"), "q r"), &s("z"));
|
|
assert_eq!(get(&n, "e"), &Node::Seq(vec![]));
|
|
assert_eq!(parse(&emit(&n)).unwrap(), n);
|
|
assert!(matches!(parse("a: [1, 2"), Err(Error::Syntax { line: 1, .. })));
|
|
}
|
|
|
|
#[test]
|
|
fn comments_nulls_and_same_indent_seq() {
|
|
let n = parse("# top\na: 1 # c\nb: ~\nc:\nd: null\ne: \"x # y\"\nf: x#y\nlist:\n- one\n- two # c\n\n# gap\n- three\nafter: 2\n").unwrap();
|
|
assert_eq!(get(&n, "a"), &Node::Num(1.0));
|
|
for k in ["b", "c", "d"] {
|
|
assert_eq!(get(&n, k), &Node::Null);
|
|
}
|
|
assert_eq!(get(&n, "e"), &s("x # y"));
|
|
assert_eq!(get(&n, "f"), &s("x#y"));
|
|
assert_eq!(get(&n, "list"), &Node::Seq(vec![s("one"), s("two"), s("three")]));
|
|
assert_eq!(get(&n, "after"), &Node::Num(2.0));
|
|
assert_eq!(parse(&emit(&n)).unwrap(), n);
|
|
}
|
|
|
|
#[test]
|
|
fn seq_of_maps_and_nested_seqs() {
|
|
let n = parse("- a: 1\n b:\n - x\n - y\n- - p\n - q\n- \n k: v\n- plain\n").unwrap();
|
|
let Node::Seq(items) = &n else { panic!() };
|
|
assert_eq!(items.len(), 4);
|
|
assert_eq!(get(&items[0], "b"), &Node::Seq(vec![s("x"), s("y")]));
|
|
assert_eq!(items[1], Node::Seq(vec![s("p"), s("q")]));
|
|
assert_eq!(get(&items[2], "k"), &s("v"));
|
|
assert_eq!(parse(&emit(&n)).unwrap(), n);
|
|
}
|
|
|
|
#[test]
|
|
fn emit_quotes_only_when_needed() {
|
|
let n = Node::Map(vec![
|
|
("plain".into(), s("hello world")),
|
|
("empty".into(), s("")),
|
|
("num".into(), s("12")),
|
|
("null".into(), s("null")),
|
|
("colon".into(), s("a: b")),
|
|
("hash".into(), s("a #b")),
|
|
("lead".into(), s("- x")),
|
|
("sp".into(), s(" x")),
|
|
("quote".into(), s("a \"q\" b")),
|
|
("a: b".into(), Node::Num(2.5)),
|
|
("int".into(), Node::Num(3.0)),
|
|
("multi".into(), s("l1\nl2\n")),
|
|
("ctrl".into(), s(" lead\nx")),
|
|
]);
|
|
let out = emit(&n);
|
|
assert!(out.contains("plain: hello world\n"), "{out}");
|
|
assert!(out.contains("int: 3\n"), "{out}");
|
|
assert!(out.contains("quote: a \"q\" b\n"), "{out}");
|
|
assert!(out.contains("multi: |\n l1\n l2\n"), "{out}");
|
|
assert_eq!(parse(&out).unwrap(), n, "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn limits() {
|
|
let nest = |n: usize| (0..n - 1).map(|i| " ".repeat(i) + "a:\n").collect::<Vec<_>>().concat() + &" ".repeat(n - 1) + "a: 1\n";
|
|
assert!(parse(&nest(12)).is_ok());
|
|
assert!(matches!(parse(&nest(13)), Err(Error::Limit(_))));
|
|
assert!(matches!(parse(&format!("a: {}1{}\n", "[".repeat(13), "]".repeat(13))), Err(Error::Limit(_))));
|
|
assert!(matches!(parse(&format!("a: \"{}\"\n", "x".repeat(2 * 1024 * 1024))), Err(Error::Limit(_))));
|
|
let many = (0..10_001).map(|i| ["- ", &i.to_string(), "\n"].concat()).collect::<Vec<_>>().concat();
|
|
assert!(matches!(parse(&many), Err(Error::Limit(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn tab_indent_is_an_error() {
|
|
let e = parse("a:\n b: 1\nc:\n\td: 2\n").unwrap_err();
|
|
assert_eq!(e, Error::Syntax { line: 4, column: 1, message: "tab in indentation".into() });
|
|
assert!(matches!(parse("a: 1\n b: 2\n"), Err(Error::Syntax { line: 2, .. })));
|
|
assert!(matches!(parse("a: 1\nstray\n"), Err(Error::Syntax { line: 2, .. })));
|
|
}
|
|
|
|
#[test]
|
|
fn random_bytes_never_panic() {
|
|
let mut x: u64 = 0x2545_F491_4F6C_DD1D;
|
|
let mut next = move || {
|
|
x ^= x << 13;
|
|
x ^= x >> 7;
|
|
x ^= x << 17;
|
|
x
|
|
};
|
|
let alphabet: Vec<char> = "ab :-#\n\t[]{},\"'|>\\~0.1e+\u{e9}".chars().collect();
|
|
let start = Instant::now();
|
|
for i in 0..5000 {
|
|
let len = usize::try_from(next() % 200).unwrap_or(0);
|
|
let text: String = if i % 2 == 0 {
|
|
(0..len).map(|_| alphabet[usize::try_from(next()).unwrap_or(0) % alphabet.len()]).collect()
|
|
} else {
|
|
let bytes: Vec<u8> = (0..len).map(|_| (next() & 0xff) as u8).collect();
|
|
String::from_utf8_lossy(&bytes).into_owned()
|
|
};
|
|
if let Ok(n) = parse(&text) {
|
|
assert_eq!(parse(&emit(&n)).as_ref(), Ok(&n), "{text:?}");
|
|
}
|
|
}
|
|
assert!(start.elapsed() < Duration::from_secs(5));
|
|
}
|
|
|
|
#[test]
|
|
fn big_flow_list_is_fast() {
|
|
let items: Vec<String> = (0..6000).map(|i| format!("\"item-{i:08}\"")).collect();
|
|
let text = format!("l: [{}]\n", items.join(","));
|
|
assert!(text.len() > 80_000);
|
|
let start = Instant::now();
|
|
let n = parse(&text).unwrap();
|
|
assert!(start.elapsed() < Duration::from_secs(1));
|
|
let Node::Seq(v) = get(&n, "l") else { panic!() };
|
|
assert_eq!(v.len(), 6000);
|
|
}
|