use quarb::{QueryResult, Value};
fn doc() -> quarb_json::JsonAdapter {
quarb_json::JsonAdapter::parse(
r#"{"books":[
{"title":"Pride and Prejudice","year":1813,"tags":["love","wit"]},
{"title":"Hamlet","year":1603,"tags":["ghost"]},
{"title":"Tom Sawyer","year":1876,"tags":[]}]}"#,
)
.unwrap()
}
fn text(q: &str) -> 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) => match vs.as_slice() {
[Value::Str(s)] => s.clone(),
other => panic!("{q}: {other:?}"),
},
QueryResult::Nodes(ns) => panic!("{q}: nodes {ns:?}"),
}
}
#[test]
fn every_step_is_a_level_and_a_fold_a_paragraph() {
assert_eq!(
text("/books/* | ::year | [$_ > 1800] | .y @|{.y} count @| threads"),
"\
root (/; ∅)
└─ /books/*
├─ (…/0; ∅)
│ └─ ::year (…; 1813)
│ └─ [$_ > 1800] (…; 1813)
│ └─ .y (…; 1813)
├─ (…/1; ∅)
│ └─ ::year (…; 1603)
└─ (…/2; ∅)
└─ ::year (…; 1876)
└─ [$_ > 1800] (…; 1876)
└─ .y (…; 1876)
@|{.y} count
├─ .y = 1813 (∅; 1)
└─ .y = 1876 (∅; 1)"
);
}
#[test]
fn hops_within_a_branch_and_an_exhausted_thread() {
assert_eq!(
text("/books/* .b /tags/* @| threads"),
"\
root (/; ∅)
└─ /books/*
├─ (…/0; ∅)
│ └─ .b (…; ∅)
│ └─ /tags/*
│ ├─ (…/0; ∅)
│ └─ (…/1; ∅)
├─ (…/1; ∅)
│ └─ .b (…; ∅)
│ └─ /tags/*
│ └─ (…/0; ∅)
└─ (…/2; ∅)
└─ .b (…; ∅)"
);
}
#[test]
fn a_reduction_is_a_paragraph_with_one_answer() {
assert_eq!(
text("/books/* | ::year @| sum | .s @| threads"),
"\
root (/; ∅)
└─ /books/*
├─ (…/0; ∅)
│ └─ ::year (…; 1813)
├─ (…/1; ∅)
│ └─ ::year (…; 1603)
└─ (…/2; ∅)
└─ ::year (…; 1876)
@| sum
└─ (∅; 5292)
└─ .s (∅; 5292)"
);
}
#[test]
fn alike_threads_merge_at_a_hop() {
assert_eq!(
text("/books/0/tags/* | \\ @| threads"),
"\
root (/; ∅)
└─ /books/0/tags/*
├─ (…/0; ∅)
│ └─ \\
│ └─ (/books/0/tags; ∅) (2 merged)
└─ (…/1; ∅)
└─ \\
└─ (/books/0/tags; ∅) ⤴ merged"
);
}
#[test]
fn depth_and_capsa_options() {
assert_eq!(
text("/books/* | ::year | .y @| threads(-d=1)"),
"\
root (/; ∅)
└─ /books/*
├─ (…/0; ∅)
├─ (…/1; ∅)
└─ (…/2; ∅)"
);
assert_eq!(
text("/books/0 | .t(::title) | ::year @| threads(-c)"),
"\
root (/; ∅)
└─ /books/0
└─ (…; ∅)
└─ .t(::title) (…; ∅)
└─ ::year (…; 1813) [.t = \"Pride and Prejudice\"]"
);
}