From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/jsonpath_lib/tests/array_filter.rs | 253 ++++++++++++ vendor/jsonpath_lib/tests/common.rs | 56 +++ vendor/jsonpath_lib/tests/filter.rs | 280 +++++++++++++ vendor/jsonpath_lib/tests/jsonpath_examples.rs | 242 +++++++++++ vendor/jsonpath_lib/tests/lib.rs | 193 +++++++++ vendor/jsonpath_lib/tests/op.rs | 376 +++++++++++++++++ vendor/jsonpath_lib/tests/paths.rs | 115 ++++++ vendor/jsonpath_lib/tests/precompile.rs | 41 ++ vendor/jsonpath_lib/tests/readme.rs | 543 +++++++++++++++++++++++++ vendor/jsonpath_lib/tests/return_type.rs | 108 +++++ vendor/jsonpath_lib/tests/selector.rs | 131 ++++++ 11 files changed, 2338 insertions(+) create mode 100644 vendor/jsonpath_lib/tests/array_filter.rs create mode 100644 vendor/jsonpath_lib/tests/common.rs create mode 100644 vendor/jsonpath_lib/tests/filter.rs create mode 100644 vendor/jsonpath_lib/tests/jsonpath_examples.rs create mode 100644 vendor/jsonpath_lib/tests/lib.rs create mode 100644 vendor/jsonpath_lib/tests/op.rs create mode 100644 vendor/jsonpath_lib/tests/paths.rs create mode 100644 vendor/jsonpath_lib/tests/precompile.rs create mode 100644 vendor/jsonpath_lib/tests/readme.rs create mode 100644 vendor/jsonpath_lib/tests/return_type.rs create mode 100644 vendor/jsonpath_lib/tests/selector.rs (limited to 'vendor/jsonpath_lib/tests') diff --git a/vendor/jsonpath_lib/tests/array_filter.rs b/vendor/jsonpath_lib/tests/array_filter.rs new file mode 100644 index 000000000..9ffa79ed5 --- /dev/null +++ b/vendor/jsonpath_lib/tests/array_filter.rs @@ -0,0 +1,253 @@ +#[macro_use] +extern crate serde_json; + +use common::{read_json, select_and_then_compare, setup}; + +mod common; + +#[test] +fn array_range_default() { + setup(); + + select_and_then_compare( + "$.school.friends[1, 2]", + read_json("./benchmark/data_obj.json"), + json!([ + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ]), + ); +} + +#[test] +fn array_range_all() { + setup(); + + select_and_then_compare( + "$[ : ]", + json!(["first", "second"]), + json!(["first", "second"]), + ); +} + +#[test] +fn array_range_step_all() { + setup(); + + select_and_then_compare( + "$[::]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["first", "second", "third", "forth", "fifth"]), + ); +} + +#[test] +fn array_range_step_only_step_value() { + setup(); + + select_and_then_compare( + "$[::2]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["first", "third", "fifth"]), + ); +} + +#[test] +fn array_range_step_only_start_index() { + setup(); + + select_and_then_compare( + "$[1::]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["second", "third", "forth", "fifth"]), + ); +} + +#[test] +fn array_range_step_empty_step_value() { + setup(); + + select_and_then_compare( + "$[1:2:]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["second"]), + ); +} + +#[test] +fn array_range_step_empty_end_index() { + setup(); + + select_and_then_compare( + "$[1::2]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["second", "forth"]), + ); +} + +#[test] +fn array_range_step_by_1() { + setup(); + + select_and_then_compare( + "$[0:3:1]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["first", "second", "third"]), + ); +} + +#[test] +fn array_range_step_by_2() { + setup(); + + select_and_then_compare( + "$[0:3:2]", + json!(["first", "second", "third", "forth", "fifth"]), + json!(["first", "third"]), + ); +} + +#[test] +fn array_range_only_negative_index() { + setup(); + + select_and_then_compare( + "$[-4:]", + json!(["first", "second", "third"]), + json!(["first", "second", "third"]), + ); +} + +#[test] +fn array_range_only_end_index() { + setup(); + + select_and_then_compare( + "$[:4]", + json!(["first", "second", "third"]), + json!(["first", "second", "third"]), + ); +} + +#[test] +fn array_range_only_from_index() { + setup(); + + select_and_then_compare( + "$.school.friends[1: ]", + read_json("./benchmark/data_obj.json"), + json!([ + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ]), + ); +} + +#[test] +fn array_range_only_nagative_end_index() { + setup(); + + select_and_then_compare( + "$.school.friends[:-2]", + read_json("./benchmark/data_obj.json"), + json!([ + {"id": 0, "name": "Millicent Norman"} + ]), + ); +} + +#[test] +fn array_index() { + setup(); + + select_and_then_compare( + "$..friends[2].name", + read_json("./benchmark/data_obj.json"), + json!(["Gray Berry", "Gray Berry"]), + ); +} + +#[test] +fn array_all_index() { + setup(); + + select_and_then_compare( + "$..friends[*].name", + read_json("./benchmark/data_obj.json"), + json!([ + "Vincent Cannon", + "Gray Berry", + "Millicent Norman", + "Vincent Cannon", + "Gray Berry" + ]), + ); +} + +#[test] +fn array_all_and_then_key() { + setup(); + + select_and_then_compare( + "$['school']['friends'][*].['name']", + read_json("./benchmark/data_obj.json"), + json!(["Millicent Norman", "Vincent Cannon", "Gray Berry"]), + ); +} + +#[test] +fn array_index_and_then_key() { + setup(); + + select_and_then_compare( + "$['school']['friends'][0].['name']", + read_json("./benchmark/data_obj.json"), + json!(["Millicent Norman"]), + ); +} + +#[test] +fn array_multiple_key() { + setup(); + + select_and_then_compare( + r#"$.["eyeColor", "name"]"#, + read_json("./benchmark/data_obj.json"), + json!(["blue", "Leonor Herman"]), + ); +} + +#[test] +fn bugs40_bracket_notation_after_recursive_descent() { + setup(); + + select_and_then_compare( + "$..[0]", + json!([ + "first", + { + "key": [ + "first nested", + { + "more": [ + {"nested": ["deepest", "second"]}, + ["more", "values"] + ] + } + ] + } + ]), + json!([ + "first", + "first nested", + { + "nested" : [ + "deepest", + "second" + ] + }, + "deepest", + "more" + ]), + ); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/common.rs b/vendor/jsonpath_lib/tests/common.rs new file mode 100644 index 000000000..1de2604d5 --- /dev/null +++ b/vendor/jsonpath_lib/tests/common.rs @@ -0,0 +1,56 @@ +extern crate env_logger; +extern crate jsonpath_lib as jsonpath; +extern crate serde_json; + +use std::io::Read; + +use serde_json::Value; + +use self::jsonpath::Selector; + +#[allow(dead_code)] +pub fn setup() { + let _ = env_logger::try_init(); +} + +#[allow(dead_code)] +pub fn read_json(path: &str) -> Value { + let mut f = std::fs::File::open(path).unwrap(); + let mut contents = String::new(); + f.read_to_string(&mut contents).unwrap(); + serde_json::from_str(&contents).unwrap() +} + +#[allow(dead_code)] +pub fn read_contents(path: &str) -> String { + let mut f = std::fs::File::open(path).unwrap(); + let mut contents = String::new(); + f.read_to_string(&mut contents).unwrap(); + contents +} + +#[allow(dead_code)] +pub fn select_and_then_compare(path: &str, json: Value, target: Value) { + let mut selector = Selector::default(); + let result = selector + .str_path(path) + .unwrap() + .value(&json) + .select_as::() + .unwrap(); + assert_eq!( + result, + match target { + Value::Array(vec) => vec, + _ => panic!("Give me the Array!"), + }, + "{}", + path + ); +} + +#[allow(dead_code)] +pub fn compare_result(result: Vec<&Value>, target: Value) { + let result = serde_json::to_value(result).unwrap(); + assert_eq!(result, target); +} diff --git a/vendor/jsonpath_lib/tests/filter.rs b/vendor/jsonpath_lib/tests/filter.rs new file mode 100644 index 000000000..c847dab33 --- /dev/null +++ b/vendor/jsonpath_lib/tests/filter.rs @@ -0,0 +1,280 @@ +#[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 filter_parent_paths() { + setup(); + + select_and_then_compare( + "$[?(@.key.subKey == 'subKey2')]", + json!([ + {"key": {"seq": 1, "subKey": "subKey1"}}, + {"key": {"seq": 2, "subKey": "subKey2"}}, + {"key": 42}, + {"some": "value"} + ]), + json!([{"key": {"seq": 2, "subKey": "subKey2"}}]), + ); +} + +#[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 + } + } + } + } + ]), + ); +} + +#[test] +fn bugs38_array_notation_in_filter() { + setup(); + + select_and_then_compare( + "$[?(@['key']==42)]", + json!([ + {"key": 0}, + {"key": 42}, + {"key": -1}, + {"key": 41}, + {"key": 43}, + {"key": 42.0001}, + {"key": 41.9999}, + {"key": 100}, + {"some": "value"} + ]), + json!([{"key": 42}]), + ); + + select_and_then_compare( + "$[?(@['key'].subKey == 'subKey2')]", + json!([ + {"key": {"seq": 1, "subKey": "subKey1"}}, + {"key": {"seq": 2, "subKey": "subKey2"}}, + {"key": 42}, + {"some": "value"} + ]), + json!([{"key": {"seq": 2, "subKey": "subKey2"}}]), + ); + + select_and_then_compare( + "$[?(@['key']['subKey'] == 'subKey2')]", + json!([ + {"key": {"seq": 1, "subKey": "subKey1"}}, + {"key": {"seq": 2, "subKey": "subKey2"}}, + {"key": 42}, + {"some": "value"} + ]), + json!([{"key": {"seq": 2, "subKey": "subKey2"}}]), + ); + + select_and_then_compare( + "$..key[?(@['subKey'] == 'subKey2')]", + json!([ + {"key": {"seq": 1, "subKey": "subKey1"}}, + {"key": {"seq": 2, "subKey": "subKey2"}}, + {"key": 42}, + {"some": "value"} + ]), + json!([{"seq": 2, "subKey": "subKey2"}]), + ); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/jsonpath_examples.rs b/vendor/jsonpath_lib/tests/jsonpath_examples.rs new file mode 100644 index 000000000..35e84a3dd --- /dev/null +++ b/vendor/jsonpath_lib/tests/jsonpath_examples.rs @@ -0,0 +1,242 @@ +#[macro_use] +extern crate serde_json; + +use common::{read_json, select_and_then_compare, setup}; + +mod common; + +#[test] +fn example_authros_of_all_books() { + setup(); + + select_and_then_compare( + r#"$.store.book[*].author"#, + read_json("./benchmark/example.json"), + json!([ + "Nigel Rees", + "Evelyn Waugh", + "Herman Melville", + "J. R. R. Tolkien" + ]), + ); +} + +#[test] +fn all_authors() { + setup(); + + select_and_then_compare( + r#"$..author"#, + read_json("./benchmark/example.json"), + json!([ + "Nigel Rees", + "Evelyn Waugh", + "Herman Melville", + "J. R. R. Tolkien" + ]), + ); +} + +#[test] +fn all_things_both_books_and_bicycles() { + setup(); + + select_and_then_compare( + r#"$.store.*"#, + read_json("./benchmark/example.json"), + json!([ + [ + {"category" : "reference", "author" : "Nigel Rees","title" : "Sayings of the Century", "price" : 8.95}, + {"category" : "fiction", "author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}, + {"category" : "fiction", "author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}, + {"category" : "fiction", "author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99} + ], + {"color" : "red","price" : 19.95}, + ]), + ); +} + +#[test] +fn the_price_of_everything() { + setup(); + + select_and_then_compare( + r#"$.store..price"#, + read_json("./benchmark/example.json"), + json!([8.95, 12.99, 8.99, 22.99, 19.95]), + ); +} + +#[test] +fn the_third_book() { + setup(); + + select_and_then_compare( + r#"$..book[2]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + } + ]), + ); +} + +#[test] +fn the_second_to_last_book() { + setup(); + + select_and_then_compare( + r#"$..book[-2]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + } + ]), + ); +} + +#[test] +fn the_first_two_books() { + setup(); + + select_and_then_compare( + r#"$..book[0, 1]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "reference", + "author" : "Nigel Rees", + "title" : "Sayings of the Century", + "price" : 8.95 + }, + { + "category" : "fiction", + "author" : "Evelyn Waugh", + "title" : "Sword of Honour", + "price" : 12.99 + } + ]), + ); +} + +#[test] +fn all_books_from_index_0_inclusive_until_index_2_exclusive() { + setup(); + + select_and_then_compare( + r#"$..book[:2]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "reference", + "author" : "Nigel Rees", + "title" : "Sayings of the Century", + "price" : 8.95 + }, + { + "category" : "fiction", + "author" : "Evelyn Waugh", + "title" : "Sword of Honour", + "price" : 12.99 + } + ]), + ); +} + +#[test] +fn all_books_from_index_1_inclusive_until_index_2_exclusive() { + setup(); + + select_and_then_compare( + r#"$..book[2:]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + }, + { + "category" : "fiction", + "author" : "J. R. R. Tolkien", + "title" : "The Lord of the Rings", + "isbn" : "0-395-19395-8", + "price" : 22.99 + } + ]), + ); +} + +#[test] +fn all_books_with_an_isbn_number() { + setup(); + + select_and_then_compare( + r#"$..book[?(@.isbn)]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + }, + { + "category" : "fiction", + "author" : "J. R. R. Tolkien", + "title" : "The Lord of the Rings", + "isbn" : "0-395-19395-8", + "price" : 22.99 + } + ]), + ); +} + +#[test] +fn all_books_in_store_cheaper_than_10() { + setup(); + + select_and_then_compare( + r#"$.store.book[?(@.price < 10)]"#, + read_json("./benchmark/example.json"), + json!([ + { + "category" : "reference", + "author" : "Nigel Rees", + "title" : "Sayings of the Century", + "price" : 8.95 + }, + { + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + } + ]), + ); +} + +#[test] +fn give_me_every_thing() { + setup(); + + select_and_then_compare( + r#"$..*"#, + read_json("./benchmark/example.json"), + read_json("./benchmark/giveme_every_thing_result.json"), + ); +} diff --git a/vendor/jsonpath_lib/tests/lib.rs b/vendor/jsonpath_lib/tests/lib.rs new file mode 100644 index 000000000..cacc9254a --- /dev/null +++ b/vendor/jsonpath_lib/tests/lib.rs @@ -0,0 +1,193 @@ +extern crate jsonpath_lib as jsonpath; +extern crate serde; +#[macro_use] +extern crate serde_json; + +use serde::Deserialize; +use serde_json::Value; + +use common::{compare_result, read_contents, read_json, setup}; +use jsonpath::JsonPathError; + +mod common; + +#[test] +fn compile() { + let compile_object = |path| { + let template = jsonpath::Compiled::compile(path).unwrap(); + let json_obj = read_json("./benchmark/data_obj.json"); + let json = template.select(&json_obj).unwrap(); + let ret = json!([ + {"id": 2,"name": "Gray Berry"}, + {"id": 2,"name": "Gray Berry"} + ]); + compare_result(json, ret); + }; + + let compile_array = |path| { + let template = jsonpath::Compiled::compile(path).unwrap(); + let json_obj = read_json("./benchmark/data_array.json"); + let json = template.select(&json_obj).unwrap(); + let ret = json!([ + {"id": 2,"name": "Gray Berry"}, + {"id": 2,"name": "Rosetta Erickson"} + ]); + compare_result(json, ret); + }; + + fn compile_error() { + let template = jsonpath::Compiled::compile("$["); + assert!(template.is_err()); + } + + setup(); + + compile_object("$..friends[2]"); + compile_array("$..friends[2]"); + compile_error(); +} + +#[test] +fn selector() { + setup(); + + fn select<'a, F>(selector: &mut F, path: &'a str, target: Value) + where + F: FnMut(&'a str) -> Result, JsonPathError>, + { + let json = selector(path).unwrap(); + compare_result(json, target); + }; + + let json_obj = read_json("./benchmark/data_obj.json"); + let mut selector = jsonpath::selector(&json_obj); + + select( + &mut selector, + "$..friends[2]", + json!([ + {"id": 2,"name": "Gray Berry"}, + {"id": 2,"name": "Gray Berry"} + ]), + ); + select( + &mut selector, + "$..friends[0]", + json!([ + {"id": 0}, + {"id": 0,"name": "Millicent Norman"} + ]), + ); +} + +#[test] +fn selector_as() { + #[derive(Deserialize, PartialEq, Debug)] + struct Friend { + id: u8, + name: Option, + } + + fn select<'a, F>(selector: &mut F, path: &'a str, target: Vec) + where + F: FnMut(&'a str) -> Result, JsonPathError>, + { + let json = selector(path).unwrap(); + assert_eq!(json, target); + }; + + let json_obj = read_json("./benchmark/data_obj.json"); + let mut selector = jsonpath::selector_as::(&json_obj); + + select( + &mut selector, + "$..friends[2]", + vec![ + Friend { + id: 2, + name: Some("Gray Berry".to_string()), + }, + Friend { + id: 2, + name: Some("Gray Berry".to_string()), + }, + ], + ); + + select( + &mut selector, + "$..friends[0]", + vec![ + Friend { id: 0, name: None }, + Friend { + id: 0, + name: Some("Millicent Norman".to_string()), + }, + ], + ); +} + +#[test] +fn select() { + let json_obj = read_json("./benchmark/example.json"); + let json = jsonpath::select(&json_obj, "$..book[2]").unwrap(); + let ret = json!([{ + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + }]); + compare_result(json, ret); +} + +#[test] +fn select_str() { + let json_str = read_contents("./benchmark/example.json"); + let result_str = jsonpath::select_as_str(&json_str, "$..book[2]").unwrap(); + let ret = json!([{ + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + }]); + let json: Value = serde_json::from_str(&result_str).unwrap(); + assert_eq!(json, ret); +} + +#[test] +fn test_to_struct() { + #[derive(Deserialize, PartialEq, Debug)] + struct Person { + name: String, + age: u8, + phones: Vec, + } + + let ret: Vec = jsonpath::select_as( + r#" + { + "person": + { + "name": "Doe John", + "age": 44, + "phones": [ + "+44 1234567", + "+44 2345678" + ] + } + } + "#, + "$.person", + ) + .unwrap(); + + let person = Person { + name: "Doe John".to_string(), + age: 44, + phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()], + }; + + assert_eq!(vec![person], ret); +} diff --git a/vendor/jsonpath_lib/tests/op.rs b/vendor/jsonpath_lib/tests/op.rs new file mode 100644 index 000000000..1ade0dd01 --- /dev/null +++ b/vendor/jsonpath_lib/tests/op.rs @@ -0,0 +1,376 @@ +#[macro_use] +extern crate serde_json; + +use common::{read_json, select_and_then_compare, setup}; + +mod common; + +#[test] +fn op_object_eq() { + setup(); + + select_and_then_compare( + "$.school[?(@.friends == @.friends)]", + read_json("./benchmark/data_obj.json"), + json!([{ + "friends": [ + {"id": 0, "name": "Millicent Norman"}, + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ] + }]), + ); +} + +#[test] +fn op_object_ge() { + setup(); + + select_and_then_compare( + "$.friends[?(@.id >= 2)]", + read_json("./benchmark/data_obj.json"), + json!([ + { "id" : 2, "name" : "Gray Berry" } + ]), + ); +} + +#[test] +fn op_object_or_default() { + setup(); + + select_and_then_compare( + "$.friends[?(@.id >= 2 || @.id == 1)]", + read_json("./benchmark/data_obj.json"), + json!([ + { "id" : 2, "name" : "Gray Berry" }, + { "id" : 1, "name" : "Vincent Cannon" } + ]), + ); +} + +#[test] +fn op_object_and_or() { + setup(); + + select_and_then_compare( + "$.friends[?( (@.id >= 2 || @.id == 1) && @.id == 0)]", + read_json("./benchmark/data_obj.json"), + json!([]), + ); +} + +#[test] +fn op_result_type() { + setup(); + + select_and_then_compare( + "$..friends[?(@.id == $.index)].id", + read_json("./benchmark/data_obj.json"), + json!([0, 0]), + ); +} + +#[test] +fn op_absolute_path_result_type() { + setup(); + + select_and_then_compare( + "$..book[?($.store.bicycle.price < @.price)].price", + read_json("./benchmark/example.json"), + json!([22.99]), + ); +} + +#[test] +fn op_complicated() { + setup(); + + select_and_then_compare( + "$..book[?( (@.price == 12.99 || @.category == 'reference') && @.price > 10)].price", + read_json("./benchmark/example.json"), + json!([12.99]), + ); +} + +#[test] +fn op_gt() { + setup(); + + select_and_then_compare( + "$..[?(@.age > 40)]", + json!([ + { "name": "이름1", "age": 40, "phone": "+33 12341234" }, + { "name": "이름2", "age": 42, "phone": "++44 12341234" } + ]), + json!([ + { "name" : "이름2", "age" : 42, "phone" : "++44 12341234" } + ]), + ); +} + +#[test] +fn op_ge() { + setup(); + + select_and_then_compare( + "$..[?(@.age >= 30)]", + json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}), + json!([ + { "name" : "친구3", "age" : 30 } + ]), + ); +} + +#[test] +fn op_eq_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a == 1)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + +#[test] +fn op_ne_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a != 2)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + +#[test] +fn op_lt_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a < 2)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + +#[test] +fn op_le_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a <= 1)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + +#[test] +fn op_gt_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a > 0)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + +#[test] +fn op_ge_for_number() { + setup(); + + select_and_then_compare("$.[?(@.a >= 0)]", json!({ "a": 1 }), json!([{ "a": 1 }])); +} + + + +#[test] +fn op_eq_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a == "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]), + ); +} + +#[test] +fn op_ne_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a != "c")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]), + ); + +} + +#[test] +fn op_lt_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a < "b")]"#, json!({ "a": "b" }), json!([]), + ); +} + +#[test] +fn op_le_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a <= "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]), + ); +} + +#[test] +fn op_gt_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a > "b")]"#, json!({ "a": "b" }), json!([]), + ); +} + +#[test] +fn op_ge_for_string_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a >= "b")]"#, json!({ "a": "b" }), json!([{ "a": "b" }]), + ); +} + +#[test] +fn op_eq_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a == @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([{"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}]), + ); +} + +#[test] +fn op_ne_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a != @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([]), + ); +} + +#[test] +fn op_lt_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a < @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([]), + ); +} + +#[test] +fn op_le_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a <= @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([]), + ); +} + +#[test] +fn op_gt_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a > @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([]), + ); +} + +#[test] +fn op_ge_for_object_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a >= @.c)]"#, + json!({"a": { "1": 1 }, "b": { "2": 2 }, "c": { "1": 1 }}), + json!([]), + ); +} + +#[test] +fn op_eq_for_complex_value() { + setup(); + + select_and_then_compare( + r#"$.[?(1 == @.a)]"#, + json!({ "a": { "b": 1 } }), + json!([]), + ); +} + +#[test] +fn op_ne_for_complex_value() { + setup(); + + select_and_then_compare( + r#"$.[?("1" != @.a)]"#, + json!({ "a": { "b": 1 } }), + json!([]), + ); +} + +#[test] +fn op_le_for_complex_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a <= 1)]"#, + json!({ "a": { "b": 1 } }), + json!([]), + ); +} + +#[test] +fn op_gt_for_complex_value() { + setup(); + + select_and_then_compare( + r#"$.[?(@.a > "1")]"#, + json!({ "a": { "b": 1 } }), + json!([]), + ); +} + +#[test] +fn op_compare_different_types() { + setup(); + + for path in [ + r#"$[?("1" == 1)]"#, + r#"$[?(1 == "1")]"#, + r#"$[?(true == 1)]"#, + r#"$[?(@ == 1)]"#, + ] + .iter() + { + select_and_then_compare(path, json!({}), json!([])); + } +} + +#[test] +fn op_for_same_type() { + setup(); + + select_and_then_compare( + r#"$..[?(@.a == 1)]"#, + json!({ + "a": 1, + "b" : {"a": 1}, + "c" : {"a": 1} + }), + json!([ + {"a": 1}, + {"a": 1} + ]), + ); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/paths.rs b/vendor/jsonpath_lib/tests/paths.rs new file mode 100644 index 000000000..61da575ca --- /dev/null +++ b/vendor/jsonpath_lib/tests/paths.rs @@ -0,0 +1,115 @@ +#[macro_use] +extern crate serde_json; + +use common::{select_and_then_compare, setup}; + +mod common; + +#[test] +fn dolla_token_in_path() { + setup(); + + select_and_then_compare( + "$..$ref", + json!({ + "Junk1": "This is a test to illustrate use of '$' in the attr for the expression $..['$ref'] ", + "$ref": "Match Root", + "Subset1":[ + {"Junk2": "Data...", + "$ref": "Match Subset1" + } + ], + "hierachy1":{ + "hierachy2.1":{ + "hierachy2.1.1":{ "$ref":"Match 2.1.1"}, + "hierachy2.1.2":{ "ref":"Match 2.1.2"}, + "hierachy2.1.3":{ "ref":"No Match 2.1.3"}, + "hierachy2.1.4":{ "$ref":"Match 2.1.4"}, + "hierachy2.1.5":{ "ref":"No Match 2.1.5"} + }, + "hierachy2.2":{ + "hierachy2.2.1":{ "ref":"No Match 2.2.1"}, + "hierachy2.2.2":{ "$ref":"Match 2.2.2"}, + "hierachy2.2.3":{ "ref":"No Match 2.2.3"}, + "hierachy2.2.4":{ "ref":"No Match 2.2.5"}, + "hierachy2.2.5":{ "$ref":"Match 2.2.5"} + }, + "hierachy2.3":{ + "hierachy2.3.1":{ "ref":"No Match 2.3.1"}, + "hierachy2.3.2":{ "ref":"No Match 2.3.2"}, + "hierachy2.3.3":{ "ref":"No Match 2.3.3"}, + "hierachy2.3.4":{ "ref":"No Match 2.3.4"}, + "hierachy2.3.5":{ "ref":"No Match 2.3.5"}, + "hierachy2.3.6":{ + "hierachy2.3.6.1":{ "$ref":"Match 2.3.6.1"}, + "hierachy2.3.6.2":{ "ref":"No Match 2.3.6.2"}, + "hierachy2.3.6.3":{ "ref":"No Match 2.3.6.3"}, + "hierachy2.3.6.4":{ "ref":"No Match 2.3.6.4"}, + "hierachy2.3.6.5":{ "ref":"No Match 2.3.6.5"} + } + } + } + }), + json!([ + "Match Root", + "Match Subset1", + "Match 2.1.1", + "Match 2.1.4", + "Match 2.2.2", + "Match 2.2.5", + "Match 2.3.6.1" + ]), + ); + + select_and_then_compare( + "$..['$ref']", + json!({ + "Junk1": "This is a test to illustrate use of '$' in the attr for the expression $..['$ref'] ", + "$ref": "Match Root", + "Subset1":[ + {"Junk2": "Data...", + "$ref": "Match Subset1" + } + ], + "hierachy1":{ + "hierachy2.1":{ + "hierachy2.1.1":{ "$ref":"Match 2.1.1"}, + "hierachy2.1.2":{ "ref":"Match 2.1.2"}, + "hierachy2.1.3":{ "ref":"No Match 2.1.3"}, + "hierachy2.1.4":{ "$ref":"Match 2.1.4"}, + "hierachy2.1.5":{ "ref":"No Match 2.1.5"} + }, + "hierachy2.2":{ + "hierachy2.2.1":{ "ref":"No Match 2.2.1"}, + "hierachy2.2.2":{ "$ref":"Match 2.2.2"}, + "hierachy2.2.3":{ "ref":"No Match 2.2.3"}, + "hierachy2.2.4":{ "ref":"No Match 2.2.5"}, + "hierachy2.2.5":{ "$ref":"Match 2.2.5"} + }, + "hierachy2.3":{ + "hierachy2.3.1":{ "ref":"No Match 2.3.1"}, + "hierachy2.3.2":{ "ref":"No Match 2.3.2"}, + "hierachy2.3.3":{ "ref":"No Match 2.3.3"}, + "hierachy2.3.4":{ "ref":"No Match 2.3.4"}, + "hierachy2.3.5":{ "ref":"No Match 2.3.5"}, + "hierachy2.3.6":{ + "hierachy2.3.6.1":{ "$ref":"Match 2.3.6.1"}, + "hierachy2.3.6.2":{ "ref":"No Match 2.3.6.2"}, + "hierachy2.3.6.3":{ "ref":"No Match 2.3.6.3"}, + "hierachy2.3.6.4":{ "ref":"No Match 2.3.6.4"}, + "hierachy2.3.6.5":{ "ref":"No Match 2.3.6.5"} + } + } + } + }), + json!([ + "Match Root", + "Match Subset1", + "Match 2.1.1", + "Match 2.1.4", + "Match 2.2.2", + "Match 2.2.5", + "Match 2.3.6.1" + ]), + ); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/precompile.rs b/vendor/jsonpath_lib/tests/precompile.rs new file mode 100644 index 000000000..7509ac72a --- /dev/null +++ b/vendor/jsonpath_lib/tests/precompile.rs @@ -0,0 +1,41 @@ +#[macro_use] +extern crate serde_json; +extern crate jsonpath_lib; + +use common::{setup}; +use jsonpath_lib::Compiled; +use serde_json::Value; + +mod common; + +#[test] +fn precompile_test() { + setup(); + + let json = json!({ + "foo": {"bar": "baz"} + }); + + // compile once + + let compiled = Compiled::compile("$.foo.bar"); + + assert!(compiled.is_ok()); + + let compiled = compiled.unwrap(); + + // re-use + + //let result = compiled(&json).unwrap(); + assert_eq!(compiled.select(&json).unwrap().clone(), vec![&Value::String("baz".into())]); + assert_eq!(compiled.select(&json).unwrap().clone(), vec![&Value::String("baz".into())]); +} + +#[test] +fn precompile_failure() { + setup(); + + let compiled = Compiled::compile(""); + + assert!(compiled.is_err()); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/readme.rs b/vendor/jsonpath_lib/tests/readme.rs new file mode 100644 index 000000000..b8a3786d3 --- /dev/null +++ b/vendor/jsonpath_lib/tests/readme.rs @@ -0,0 +1,543 @@ +extern crate jsonpath_lib as jsonpath; +extern crate serde; +#[macro_use] +extern crate serde_json; + +use serde::Deserialize; +use serde_json::Value; + +use jsonpath::{Selector, SelectorMut}; + +mod common; + +#[test] +fn readme() { + let json_obj = json!({ + "store": { + "book": [ + { + "category": "reference", + "author": "Nigel Rees", + "title": "Sayings of the Century", + "price": 8.95 + }, + { + "category": "fiction", + "author": "Evelyn Waugh", + "title": "Sword of Honour", + "price": 12.99 + }, + { + "category": "fiction", + "author": "Herman Melville", + "title": "Moby Dick", + "isbn": "0-553-21311-3", + "price": 8.99 + }, + { + "category": "fiction", + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99 + } + ], + "bicycle": { + "color": "red", + "price": 19.95 + } + }, + "expensive": 10 + }); + + let mut selector = jsonpath::selector(&json_obj); + + assert_eq!( + selector("$.store.book[*].author").unwrap(), + vec![ + "Nigel Rees", + "Evelyn Waugh", + "Herman Melville", + "J. R. R. Tolkien" + ] + ); + + assert_eq!( + selector("$..author").unwrap(), + vec![ + "Nigel Rees", + "Evelyn Waugh", + "Herman Melville", + "J. R. R. Tolkien" + ] + ); + + assert_eq!( + selector("$.store.*").unwrap(), + vec![ + &json!([ + { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, + { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, + { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, + { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } + ]), + &json!({ "color": "red", "price": 19.95 }) + ] + ); + + assert_eq!( + selector("$.store..price").unwrap(), + vec![8.95, 12.99, 8.99, 22.99, 19.95] + ); + + assert_eq!( + selector("$..book[2]").unwrap(), + vec![&json!({ + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + })] + ); + + assert_eq!( + selector("$..book[-2]").unwrap(), + vec![&json!({ + "category" : "fiction", + "author" : "Herman Melville", + "title" : "Moby Dick", + "isbn" : "0-553-21311-3", + "price" : 8.99 + })] + ); + + assert_eq!( + selector("$..book[0,1]").unwrap(), + vec![ + &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}), + &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}) + ] + ); + + assert_eq!( + selector("$..book[:2]").unwrap(), + vec![ + &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}), + &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}) + ] + ); + + assert_eq!( + selector("$..book[:2]").unwrap(), + vec![ + &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}), + &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}) + ] + ); + + assert_eq!( + selector("$..book[?(@.isbn)]").unwrap(), + vec![ + &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}), + &json!({"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99}) + ] + ); + + assert_eq!( + selector("$.store.book[?(@.price < 10)]").unwrap(), + vec![ + &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}), + &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}) + ] + ); +} + +#[test] +fn readme_selector() { + #[derive(Deserialize, PartialEq, Debug)] + struct Friend { + name: String, + age: Option, + } + + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let mut selector = Selector::default(); + + let result = selector + .str_path("$..[?(@.age >= 30)]") + .unwrap() + .value(&json_obj) + .select() + .unwrap(); + + assert_eq!(vec![&json!({"name": "친구3", "age": 30})], result); + + let result = selector.select_as_str().unwrap(); + assert_eq!(r#"[{"name":"친구3","age":30}]"#, result); + + let result = selector.select_as::().unwrap(); + assert_eq!( + vec![Friend { + name: "친구3".to_string(), + age: Some(30) + }], + result + ); +} + +#[test] +fn readme_selector_mut() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let mut selector_mut = SelectorMut::default(); + + let result = selector_mut + .str_path("$..[?(@.age == 20)].age") + .unwrap() + .value(json_obj) + .replace_with(&mut |v| { + let age = if let Value::Number(n) = v { + n.as_u64().unwrap() * 2 + } else { + 0 + }; + + Some(json!(age)) + }) + .unwrap() + .take() + .unwrap(); + + assert_eq!( + result, + json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 40}, + {"name": "친구2", "age": 40} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}) + ); +} + +#[test] +fn readme_select() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap(); + + assert_eq!( + json, + vec![ + &json!({"name": "친구3", "age": 30}), + &json!({"name": "친구1", "age": 20}) + ] + ); +} + +#[test] +fn readme_select_as_str() { + let ret = jsonpath::select_as_str( + r#" + { + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ] + } + "#, + "$..friends[0]", + ) + .unwrap(); + + assert_eq!( + ret, + r#"[{"name":"친구3","age":30},{"name":"친구1","age":20}]"# + ); +} + +#[test] +fn readme_select_as() { + #[derive(Deserialize, PartialEq, Debug)] + struct Person { + name: String, + age: u8, + phones: Vec, + } + + let ret: Vec = jsonpath::select_as( + r#"{ + "person": + { + "name": "Doe John", + "age": 44, + "phones": [ + "+44 1234567", + "+44 2345678" + ] + } + }"#, + "$.person", + ) + .unwrap(); + + let person = Person { + name: "Doe John".to_string(), + age: 44, + phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()], + }; + + assert_eq!(ret[0], person); +} + +#[test] +fn readme_compile() { + let first_firend = jsonpath::Compiled::compile("$..friends[0]").unwrap(); + + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let json = first_firend.select(&json_obj).unwrap(); + + assert_eq!( + json, + vec![ + &json!({"name": "친구3", "age": 30}), + &json!({"name": "친구1", "age": 20}) + ] + ); +} + +#[test] +fn readme_selector_fn() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let mut selector = jsonpath::selector(&json_obj); + + let json = selector("$..friends[0]").unwrap(); + + assert_eq!( + json, + vec![ + &json!({"name": "친구3", "age": 30}), + &json!({"name": "친구1", "age": 20}) + ] + ); + + let json = selector("$..friends[1]").unwrap(); + + assert_eq!( + json, + vec![ + &json!({"name": "친구4"}), + &json!({"name": "친구2", "age": 20}) + ] + ); +} + +#[test] +fn readme_selector_as() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + #[derive(Deserialize, PartialEq, Debug)] + struct Friend { + name: String, + age: Option, + } + + let mut selector = jsonpath::selector_as::(&json_obj); + + let json = selector("$..friends[0]").unwrap(); + + let ret = vec![ + Friend { + name: "친구3".to_string(), + age: Some(30), + }, + Friend { + name: "친구1".to_string(), + age: Some(20), + }, + ]; + assert_eq!(json, ret); + + let json = selector("$..friends[1]").unwrap(); + + let ret = vec![ + Friend { + name: "친구4".to_string(), + age: None, + }, + Friend { + name: "친구2".to_string(), + age: Some(20), + }, + ]; + + assert_eq!(json, ret); +} + +#[test] +fn readme_delete() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let ret = jsonpath::delete(json_obj, "$..[?(20 == @.age)]").unwrap(); + + assert_eq!( + ret, + json!({ + "school": { + "friends": [ + null, + null + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}) + ); +} + +#[test] +fn readme_delete2() { + let json_obj = common::read_json("./benchmark/example.json"); + + let ret = jsonpath::delete(json_obj, "$.store.book").unwrap(); + + assert_eq!( + ret, + json!({ + "store": { + "book": null, + "bicycle": { + "color": "red", + "price": 19.95 + } + }, + "expensive": 10 + }) + ); +} + +#[test] +fn readme_replace_with() { + let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}); + + let result = jsonpath::replace_with(json_obj, "$..[?(@.age == 20)].age", &mut |v| { + let age = if let Value::Number(n) = v { + n.as_u64().unwrap() * 2 + } else { + 0 + }; + + Some(json!(age)) + }) + .unwrap(); + + assert_eq!( + result, + json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 40}, + {"name": "친구2", "age": 40} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} + ]}) + ); +} diff --git a/vendor/jsonpath_lib/tests/return_type.rs b/vendor/jsonpath_lib/tests/return_type.rs new file mode 100644 index 000000000..0139dda01 --- /dev/null +++ b/vendor/jsonpath_lib/tests/return_type.rs @@ -0,0 +1,108 @@ +#[macro_use] +extern crate serde_json; + +use common::{read_json, select_and_then_compare, setup}; + +mod common; + +#[test] +fn return_type_for_single_object() { + setup(); + + select_and_then_compare( + "$.school", + read_json("./benchmark/data_obj.json"), + json!([{ + "friends": [ + {"id": 0, "name": "Millicent Norman"}, + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ] + }]), + ); +} + +#[test] +fn return_type_for_single_object_key_matched() { + setup(); + + select_and_then_compare( + "$.friends[?(@.name)]", + read_json("./benchmark/data_obj.json"), + json!([ + { "id" : 1, "name" : "Vincent Cannon" }, + { "id" : 2, "name" : "Gray Berry" } + ]), + ); +} + +#[test] +fn return_type_for_child_object_matched() { + setup(); + + select_and_then_compare( + "$.school[?(@.friends[0])]", + read_json("./benchmark/data_obj.json"), + json!([{ + "friends": [ + {"id": 0, "name": "Millicent Norman"}, + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ] + }]), + ); +} + +#[test] +fn return_type_for_child_object_not_matched() { + setup(); + + select_and_then_compare( + "$.school[?(@.friends[10])]", + read_json("./benchmark/data_obj.json"), + json!([]), + ); +} + +#[test] +fn return_type_for_object_filter_true() { + setup(); + + select_and_then_compare( + "$.school[?(1==1)]", + read_json("./benchmark/data_obj.json"), + json!([{ + "friends": [ + {"id": 0, "name": "Millicent Norman"}, + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ] + }]), + ); +} + +#[test] +fn return_type_for_array_filter_true() { + setup(); + + select_and_then_compare( + "$.school.friends[?(1==1)]", + read_json("./benchmark/data_obj.json"), + json!([[ + {"id": 0, "name": "Millicent Norman"}, + {"id": 1, "name": "Vincent Cannon" }, + {"id": 2, "name": "Gray Berry"} + ]]), + ); +} + +#[test] +fn return_type_empty() { + setup(); + + select_and_then_compare( + "$[?(@.key==43)]", + json!([{"key": 42}]), + json!([]), + ); +} \ No newline at end of file diff --git a/vendor/jsonpath_lib/tests/selector.rs b/vendor/jsonpath_lib/tests/selector.rs new file mode 100644 index 000000000..113f66ba0 --- /dev/null +++ b/vendor/jsonpath_lib/tests/selector.rs @@ -0,0 +1,131 @@ +extern crate jsonpath_lib as jsonpath; +#[macro_use] +extern crate serde_json; + +use common::{read_json, setup}; +use jsonpath::{Parser, Selector, SelectorMut}; +use serde_json::Value; + +mod common; + +#[test] +fn selector_mut() { + setup(); + + let mut selector_mut = SelectorMut::default(); + + let mut nums = Vec::new(); + let result = selector_mut + .str_path(r#"$.store..price"#) + .unwrap() + .value(read_json("./benchmark/example.json")) + .replace_with(&mut |v| { + if let Value::Number(n) = v { + nums.push(n.as_f64().unwrap()); + } + Some(Value::String("a".to_string())) + }) + .unwrap() + .take() + .unwrap(); + + assert_eq!( + nums, + vec![8.95_f64, 12.99_f64, 8.99_f64, 22.99_f64, 19.95_f64] + ); + + let mut selector = Selector::default(); + let result = selector + .str_path(r#"$.store..price"#) + .unwrap() + .value(&result) + .select() + .unwrap(); + + assert_eq!( + vec![ + &json!("a"), + &json!("a"), + &json!("a"), + &json!("a"), + &json!("a") + ], + result + ); +} + +#[test] +fn selector_node_ref() { + let node = Parser::compile("$.*").unwrap(); + let mut selector = Selector::default(); + selector.compiled_path(&node); + assert!(std::ptr::eq(selector.node_ref().unwrap(), &node)); +} + +#[test] +fn selector_delete() { + setup(); + + let mut selector_mut = SelectorMut::default(); + + let result = selector_mut + .str_path(r#"$.store..price[?(@>13)]"#) + .unwrap() + .value(read_json("./benchmark/example.json")) + .delete() + .unwrap() + .take() + .unwrap(); + + let mut selector = Selector::default(); + let result = selector + .str_path(r#"$.store..price"#) + .unwrap() + .value(&result) + .select() + .unwrap(); + + assert_eq!( + result, + vec![ + &json!(8.95), + &json!(12.99), + &json!(8.99), + &Value::Null, + &Value::Null + ] + ); +} + +#[test] +fn selector_remove() { + setup(); + + let mut selector_mut = SelectorMut::default(); + + let result = selector_mut + .str_path(r#"$.store..price[?(@>13)]"#) + .unwrap() + .value(read_json("./benchmark/example.json")) + .remove() + .unwrap() + .take() + .unwrap(); + + let mut selector = Selector::default(); + let result = selector + .str_path(r#"$.store..price"#) + .unwrap() + .value(&result) + .select() + .unwrap(); + + assert_eq!( + result, + vec![ + &json!(8.95), + &json!(12.99), + &json!(8.99) + ] + ); +} -- cgit v1.2.3