use quarb::Defs;
use quarb::{expand, is_calculator};
fn canon(q: &str) -> String {
expand(q, &Defs::default()).unwrap()
}
fn refuse(q: &str) -> String {
expand(q, &Defs::default()).unwrap_err().to_string()
}
#[test]
fn head_desugars_to_root_anchor() {
assert_eq!(canon("= 2 + 2"), canon("^ | (2 + 2)"));
canon("= 2 + 2 | rec(\"answer\", $_)");
canon("= (2 + 2) * 3");
canon("= 3 @| count");
}
#[test]
fn head_composes_with_fragments() {
canon("def &twice: | ($_ * 2); = 21 | &twice");
canon("def &four: = 2 + 2; &four");
}
#[test]
fn head_stands_alone() {
assert!(refuse("= 1 || /x").contains("expression head stands alone"));
}
#[test]
fn parens_keep_their_navigational_meanings() {
canon("(/a|/b)");
canon("(/a/(x.rs|y.txt)|/b/z.rs)");
assert_eq!(canon("/a.m | (m)/address"), "/a.m | (m)/address");
assert!(refuse("(|/a)").contains("at least one hop"));
}
#[test]
fn calculator_detection() {
assert!(is_calculator("= 2 + 2"));
assert!(is_calculator("= 2 + 2 | rec(\"x\", $_)"));
assert!(is_calculator("^ | count"));
assert!(is_calculator("def &four: = 2 + 2; &four"));
assert!(!is_calculator("/a"));
assert!(!is_calculator("//x::y"));
assert!(!is_calculator("= 2 || /x"));
assert!(!is_calculator("1 + 1"));
}