use quarb::{QueryResult, Value};
fn doc() -> quarb_json::JsonAdapter {
quarb_json::JsonAdapter::parse(
r#"{"books":[{"k":"a","title":"Emma"},
{"k":"a","title":"Ivanhoe"},
{"k":"b","title":"Persuasion"}]}"#,
)
.unwrap()
}
fn run(q: &str) -> quarb::Result<Vec<String>> {
let run = quarb::Run::ambient();
let out = quarb::Query::parse(q, &doc())?.run(&doc(), &run)?;
Ok(match out.result {
QueryResult::Values(vs) => vs.iter().map(Value::display_form).collect(),
QueryResult::Nodes(ns) => ns.iter().map(|n| format!("{n:?}")).collect(),
})
}
fn rows(q: &str) -> Vec<String> {
run(q).unwrap_or_else(|e| panic!("{q}: {e}"))
}
fn one(q: &str) -> String {
let out = rows(q);
assert_eq!(out.len(), 1, "{q}: {out:?}");
out.into_iter().next().unwrap()
}
const FIVE: &str = r#"= @(2; 2.0; "2"; "2."; "02")"#;
#[test]
fn the_three_commands_take_a_text_as_a_text() {
assert_eq!(one(&format!("{FIVE} | unique")), r#"@(2; "2"; "2."; "02")"#);
assert_eq!(one(&format!("{FIVE} | unique(--as=number)")), "@(2)");
assert_eq!(
one(&format!("{FIVE} | unique(--as=text)")),
r#"@(2; 2.0; "2."; "02")"#
);
assert_eq!(
one(&format!("{FIVE} | frequencies")),
r#"@%(@(value; n); %(2; 2); %("02"; 1); %("2"; 1); %("2."; 1))"#
);
assert_eq!(
one(&format!("{FIVE} | frequencies(--as=number)")),
"@(%(value = 2; n = 5))"
);
assert_eq!(
one(&format!("{FIVE} | frequencies(--as=text)")),
r#"@%(@(value; n); %(2; 2); %("02"; 1); %("2."; 1); %(2.0; 1))"#
);
assert_eq!(one(&format!("{FIVE} | ttr")), "0.8");
assert_eq!(one(&format!("{FIVE} | ttr(--as=number)")), "0.2");
assert_eq!(one(&format!("{FIVE} | ttr(--as=text)")), "0.8");
assert_eq!(
one(r#"= @("+1"; "1."; "1"; 1) | unique"#),
r#"@("+1"; "1."; "1"; 1)"#
);
assert_eq!(
one(r#"= @("+1"; "1."; "1"; 1) | unique(--as=number)"#),
r#"@("+1")"#
);
assert_eq!(
one(r#"= @(5_km; "5 km"; 5000_m) | unique"#),
r#"@(5_km; "5 km")"#
);
assert_eq!(
one(r#"= @(5_km; "5 km"; 5000_m) | unique(--as=auto)"#),
"@(5_km)"
);
assert_eq!(
one(r#"= @(5_km; "5 km"; 5000_m) | unique(--as=text)"#),
r#"@(5_km; "5 km"; 5000_m)"#
);
assert_eq!(one(r#"= @(null; ""; null) | unique"#), r#"@(null; "")"#);
}
#[test]
fn a_structure_is_told_by_its_output_form_and_a_table_cell_by_cell() {
assert_eq!(
one(r#"= @(@(1; 2); @(1; "2"); @(1; 2)) | unique"#),
r#"@(@(1; 2); @(1; "2"))"#
);
assert_eq!(
one(r#"= @(%(a = 2); %(a = "2"); %(a = 2)) | unique"#),
r#"@%(@(a); %(2); %("2"))"#
);
let t = "= #(@(a; b); %(1; 5_km); %(1; 5000_m); %(1; 5_km))";
assert_eq!(one(&format!("{t} | unique")), "#(@(a; b);\n %(1; 5_km))");
assert_eq!(
one(&format!("{t} | unique(--as=text)")),
"#(@(a; b);\n %(1; 5_km);\n %(1; 5000_m))"
);
}
#[test]
fn over_threads_a_text_is_a_text_and_a_node_what_it_is() {
assert_eq!(
rows(&format!("{FIVE} | ... @| unique")),
["2", "2", "2.", "02"]
);
assert_eq!(rows(&format!("{FIVE} | ... @| unique(--as=number)")), ["2"]);
assert_eq!(
rows(&format!("{FIVE} | ... @| unique(--as=text)")),
["2", "2.0", "2.", "02"]
);
assert_eq!(rows("/books/*::k @| unique"), ["a", "b"]);
assert_eq!(one("/books/* @| unique @| count"), "3");
assert_eq!(one("/books/* @| unique(--as=text) @| count"), "3");
}
#[test]
fn the_retired_option_is_no_option() {
for q in [
"= @(1; 2) | unique(--text)",
"= @(1; 2) | frequencies(-t)",
"= @(1; 2) | ttr(--text)",
] {
let e = run(q).unwrap_err().to_string();
assert!(e.contains("has no option"), "{q}: {e}");
}
}