jsonpath/tests/filter.rs
2020-02-23 14:45:14 +09:00

210 lines
4.2 KiB
Rust

#[macro_use]
extern crate serde_json;
use common::{read_json, select_and_then_compare, setup};
mod common;
#[test]
fn quote() {
setup();
select_and_then_compare(
r#"$['single\'quote']"#,
json!({"single'quote":"value"}),
json!(["value"]),
);
select_and_then_compare(
r#"$["double\"quote"]"#,
json!({"double\"quote":"value"}),
json!(["value"]),
);
}
#[test]
fn filter_next_all() {
setup();
for path in &[r#"$.*"#, r#"$[*]"#] {
select_and_then_compare(
path,
json!(["string", 42, { "key": "value" }, [0, 1]]),
json!(["string", 42, { "key": "value" }, [0, 1]]),
);
}
}
#[test]
fn filter_all() {
setup();
for path in &[r#"$..*"#, r#"$..[*]"#] {
select_and_then_compare(
path,
json!(["string", 42, { "key": "value" }, [0, 1]]),
json!([ "string", 42, { "key" : "value" }, [ 0, 1 ], "value", 0, 1 ]),
);
}
}
#[test]
fn filter_array_next_all() {
setup();
for path in &[r#"$.*.*"#, r#"$[*].*"#, r#"$.*[*]"#, r#"$[*][*]"#] {
select_and_then_compare(
path,
json!(["string", 42, { "key": "value" }, [0, 1]]),
json!(["value", 0, 1]),
);
}
}
#[test]
fn filter_all_complex() {
setup();
for path in &[r#"$..friends.*"#, r#"$[*].friends.*"#] {
select_and_then_compare(
path,
read_json("./benchmark/data_array.json"),
json!([
{ "id" : 0, "name" : "Millicent Norman" },
{ "id" : 1, "name" : "Vincent Cannon" },
{ "id" : 2, "name" : "Gray Berry" },
{ "id" : 0, "name" : "Tillman Mckay" },
{ "id" : 1, "name" : "Rivera Berg" },
{ "id" : 2, "name" : "Rosetta Erickson" }
]),
);
}
}
#[test]
fn filter_parent_with_matched_child() {
setup();
select_and_then_compare(
"$.a[?(@.b.c == 1)]",
json!({
"a": {
"b": {
"c": 1
}
}
}),
json!([
{
"b" : {
"c" : 1
}
}
]),
);
}
#[test]
fn filter_parent_exist_child() {
setup();
select_and_then_compare(
"$.a[?(@.b.c)]",
json!({
"a": {
"b": {
"c": 1
}
}
}),
json!([
{
"b" : {
"c" : 1
}
}
]),
);
}
#[test]
fn bugs33_exist_in_all() {
setup();
select_and_then_compare(
"$..[?(@.first.second)]",
json!({
"foo": {
"first": { "second": "value" }
},
"foo2": {
"first": {}
},
"foo3": {
}
}),
json!([
{
"first": {
"second": "value"
}
}
]),
);
}
#[test]
fn bugs33_exist_left_in_all_with_and_condition() {
setup();
select_and_then_compare(
"$..[?(@.first && @.first.second)]",
json!({
"foo": {
"first": { "second": "value" }
},
"foo2": {
"first": {}
},
"foo3": {
}
}),
json!([
{
"first": {
"second": "value"
}
}
]),
);
}
#[test]
fn bugs33_exist_right_in_all_with_and_condition() {
setup();
select_and_then_compare(
"$..[?(@.b.c.d && @.b)]",
json!({
"a": {
"b": {
"c": {
"d" : {
"e" : 1
}
}
}
}
}),
json!([
{
"b" : {
"c" : {
"d" : {
"e" : 1
}
}
}
}
]),
);
}