use std::path::{Path, PathBuf};
fn fixture(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("quarb-emptyred-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("b.json"),
r#"{"books":[{"title":"Emma","pages":500,"tags":["a","c"]},
{"title":"Iliad","pages":200,"tags":["c"]},
{"title":"Odyssey","pages":300,"tags":["a"]}]}"#,
)
.unwrap();
dir
}
fn qua(dir: &Path, query: &str) -> (String, String, bool) {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_qua"))
.env("QUARB_LOCALE", "C")
.current_dir(dir)
.args([query, "b.json"])
.stdin(std::process::Stdio::null())
.output()
.unwrap();
(
String::from_utf8_lossy(&out.stdout)
.trim()
.replace('\n', " "),
String::from_utf8_lossy(&out.stderr).trim().to_string(),
out.status.success(),
)
}
fn ok(dir: &Path, query: &str) -> String {
let (out, err, ran) = qua(dir, query);
assert!(ran, "{query}: {err}");
assert_eq!(err, "", "{query}");
out
}
#[test]
fn a_reduction_of_no_thread_keeps_the_bodys_capsa() {
let d = fixture("body");
assert_eq!(
ok(
&d,
r#"/books/* | .n(::pages) | %(::title; r = (/tags/*[:: = "c"] @| count | $_ * 100 div $.n | round(2)))"#
),
r#"%(title = "Emma"; r = 0.2) %(title = "Iliad"; r = 0.5) %(title = "Odyssey"; r = 0.0)"#
);
assert_eq!(
ok(
&d,
r#"/books/* | .n(::pages) | .r(/tags/*[:: = "c"] @| count | $_ * 100 div $.n) | $.r"#
),
"0.2 0.5 0.0"
);
assert_eq!(
ok(
&d,
r#"/books/*[3] | .n(::pages) | %(r = (/tags/*[:: = "c"] @| %(k = count; p = $.n)))"#
),
"%(r = %(k = 0; p = 300))"
);
assert_eq!(
ok(
&d,
r#"/books/*[3] | .n(::pages) | %(s = (/tags/*[:: = "c"] @| count | %.))"#
),
"%(s = %(n = 300))"
);
assert_eq!(
ok(
&d,
r#"/books/*[1] | .n(::pages) | %(s = (/tags/* @| count | %.))"#
),
"%(s = %(n = 500))"
);
}
#[test]
fn at_the_top_of_a_query_nothing_changes() {
let d = fixture("top");
let (out, err, ran) = qua(
&d,
r#"/books/* | .n(::pages) | /tags/*[:: = "zz"] @| count | $.n"#,
);
assert!(!ran, "{out}");
assert!(
err.starts_with(
"error UNBOUND_REGULA ($.n): refused: the thread holds no regula '.n' where '$.n' is \
read; it holds none"
),
"{err}"
);
assert_eq!(
ok(
&d,
r#"/books/*[::pages > 9999] | .n(::pages) @| count | ($.n ?? "none")"#
),
"none"
);
}