use quarb::{Defs, QueryResult, Value};
fn doc() -> quarb_json::JsonAdapter {
quarb_json::JsonAdapter::parse(
r#"{"books":[
{"title":"Iliad","tags":["epic","verse"]},
{"title":"Histories","tags":["history"]}]}"#,
)
.unwrap()
}
fn printed(q: &str) -> String {
quarb::expand(q, &Defs::default()).unwrap_or_else(|e| panic!("{q}: {e}"))
}
fn answers(q: &str) -> Vec<String> {
let run = quarb::Run::ambient();
let out = quarb::Query::parse(q, &doc())
.unwrap_or_else(|e| panic!("{q}: {e}"))
.run(&doc(), &run)
.unwrap_or_else(|e| panic!("{q}: {e}"));
match out.result {
QueryResult::Values(vs) => vs.iter().map(Value::display_form).collect(),
QueryResult::Nodes(ns) => ns.iter().map(|n| format!("{n:?}")).collect(),
}
}
#[test]
fn two_steps_up_keep_their_star_and_read_back() {
for (written, canonical) in [
(
"//books/* | /tags/* | /../.. | :path",
r"//books/* | /tags/* | \*\ | :path",
),
(
r"//books/* | /tags/* | \*\* | :path",
r"//books/* | /tags/* | \*\ | :path",
),
(
"//books/* | /tags/* | /.. | :path",
r"//books/* | /tags/* | \ | :path",
),
(
r"//books/* | /tags/* | \\ | :path",
r"//books/* | /tags/* | \\ | :path",
),
(
r"//books/*/tags/*\\\* @| count",
r"//books/*/tags/*\\*\ @| count",
),
(
r"//books/*/tags/*\*\\ @| count",
r"//books/*/tags/*\*\\ @| count",
),
(
r"//books/*/tags/*(\*\*) | :path",
r"//books/*/tags/*(\*\) | :path",
),
(
r"//books/*/tags/*[\*\*::title]::",
r"//books/*/tags/*[\*\::title]::",
),
] {
let once = printed(written);
assert_eq!(once, canonical, "{written}");
assert_eq!(printed(&once), once, "{written}");
assert_eq!(answers(&once), answers(written), "{written}");
}
assert_eq!(
answers("//books/*[::title = \"Histories\"] | /tags/* | /../.. | :path"),
["/books/2"]
);
assert_eq!(
answers(r#"//books/*[::title = "Histories"] | /tags/* | \\ | :path"#),
["/books/2/tags", "/books/2", "/books", "/"]
);
}
#[test]
fn the_thread_tree_draws_the_two_steps_as_they_read() {
let tree = answers("//books/*[::title = \"Histories\"] | /tags/* | /../.. @| threads");
assert_eq!(tree.len(), 1);
assert!(tree[0].contains(r"└─ \*\"), "{}", tree[0]);
assert!(!tree[0].contains(r"\\"), "{}", tree[0]);
}