mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-25 09:22:19 +00:00
src/lib.rs code coverage
This commit is contained in:
parent
488e0b400f
commit
ad47444b7a
145
tests/lib.rs
145
tests/lib.rs
@ -7,50 +7,81 @@ use serde::Deserialize;
|
|||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use common::{compare_result, read_contents, read_json, setup};
|
use common::{compare_result, read_contents, read_json, setup};
|
||||||
|
use jsonpath::JsonPathError;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compile() {
|
fn compile() {
|
||||||
|
let compile_object = |path| {
|
||||||
|
let mut template = jsonpath::compile(path);
|
||||||
|
let json_obj = read_json("./benches/data_obj.json");
|
||||||
|
let json = template(&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 mut template = jsonpath::compile(path);
|
||||||
|
let json_obj = read_json("./benches/data_array.json");
|
||||||
|
let json = template(&json_obj).unwrap();
|
||||||
|
let ret = json!([
|
||||||
|
{"id": 2,"name": "Gray Berry"},
|
||||||
|
{"id": 2,"name": "Rosetta Erickson"}
|
||||||
|
]);
|
||||||
|
compare_result(json, ret);
|
||||||
|
};
|
||||||
|
|
||||||
|
fn compile_error() {
|
||||||
|
let mut template = jsonpath::compile("$[");
|
||||||
|
if let Err(JsonPathError::Path(_)) = template(&Value::Null) {
|
||||||
|
assert!(true);
|
||||||
|
} else {
|
||||||
|
assert!(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
let mut template = jsonpath::compile("$..friends[2]");
|
compile_object("$..friends[2]");
|
||||||
let json_obj = read_json("./benches/data_obj.json");
|
compile_array("$..friends[2]");
|
||||||
let json = template(&json_obj).unwrap();
|
compile_error();
|
||||||
let ret = json!([
|
|
||||||
{"id": 2,"name": "Gray Berry"},
|
|
||||||
{"id": 2,"name": "Gray Berry"}
|
|
||||||
]);
|
|
||||||
compare_result(json, ret);
|
|
||||||
|
|
||||||
let json_obj = read_json("./benches/data_array.json");
|
|
||||||
let json = template(&json_obj).unwrap();
|
|
||||||
let ret = json!([
|
|
||||||
{"id": 2,"name": "Gray Berry"},
|
|
||||||
{"id": 2,"name": "Rosetta Erickson"}
|
|
||||||
]);
|
|
||||||
compare_result(json, ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn selector() {
|
fn selector() {
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
let json_obj = read_json("./benches/data_obj.json");
|
fn select<'a, F>(selector: &mut F, path: &'a str, target: Value)
|
||||||
let mut reader = jsonpath::selector(&json_obj);
|
where
|
||||||
let json = reader("$..friends[2]").unwrap();
|
F: FnMut(&'a str) -> Result<Vec<&Value>, JsonPathError>,
|
||||||
let ret = json!([
|
{
|
||||||
{"id": 2,"name": "Gray Berry"},
|
let json = selector(path).unwrap();
|
||||||
{"id": 2,"name": "Gray Berry"}
|
compare_result(json, target);
|
||||||
]);
|
};
|
||||||
compare_result(json, ret);
|
|
||||||
|
|
||||||
let json = reader("$..friends[0]").unwrap();
|
let json_obj = read_json("./benches/data_obj.json");
|
||||||
let ret = json!([
|
let mut selector = jsonpath::selector(&json_obj);
|
||||||
{"id": 0},
|
|
||||||
{"id": 0,"name": "Millicent Norman"}
|
select(
|
||||||
]);
|
&mut selector,
|
||||||
compare_result(json, ret);
|
"$..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]
|
#[test]
|
||||||
@ -61,31 +92,43 @@ fn selector_as() {
|
|||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn select<'a, F>(selector: &mut F, path: &'a str, target: Vec<Friend>)
|
||||||
|
where
|
||||||
|
F: FnMut(&'a str) -> Result<Vec<Friend>, JsonPathError>,
|
||||||
|
{
|
||||||
|
let json = selector(path).unwrap();
|
||||||
|
assert_eq!(json, target);
|
||||||
|
};
|
||||||
|
|
||||||
let json_obj = read_json("./benches/data_obj.json");
|
let json_obj = read_json("./benches/data_obj.json");
|
||||||
let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
|
let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
|
||||||
let json = selector("$..friends[2]").unwrap();
|
|
||||||
|
|
||||||
let ret = vec![
|
select(
|
||||||
Friend {
|
&mut selector,
|
||||||
id: 2,
|
"$..friends[2]",
|
||||||
name: Some("Gray Berry".to_string()),
|
vec![
|
||||||
},
|
Friend {
|
||||||
Friend {
|
id: 2,
|
||||||
id: 2,
|
name: Some("Gray Berry".to_string()),
|
||||||
name: Some("Gray Berry".to_string()),
|
},
|
||||||
},
|
Friend {
|
||||||
];
|
id: 2,
|
||||||
assert_eq!(json, ret);
|
name: Some("Gray Berry".to_string()),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
let json = selector("$..friends[0]").unwrap();
|
select(
|
||||||
let ret = vec![
|
&mut selector,
|
||||||
Friend { id: 0, name: None },
|
"$..friends[0]",
|
||||||
Friend {
|
vec![
|
||||||
id: 0,
|
Friend { id: 0, name: None },
|
||||||
name: Some("Millicent Norman".to_string()),
|
Friend {
|
||||||
},
|
id: 0,
|
||||||
];
|
name: Some("Millicent Norman".to_string()),
|
||||||
assert_eq!(json, ret);
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user