src/lib.rs code coverage

This commit is contained in:
freestrings 2019-06-20 14:03:09 +09:00
parent 488e0b400f
commit ad47444b7a

View File

@ -7,14 +7,14 @@ 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() {
setup(); let compile_object = |path| {
let mut template = jsonpath::compile(path);
let mut template = jsonpath::compile("$..friends[2]");
let json_obj = read_json("./benches/data_obj.json"); let json_obj = read_json("./benches/data_obj.json");
let json = template(&json_obj).unwrap(); let json = template(&json_obj).unwrap();
let ret = json!([ let ret = json!([
@ -22,7 +22,10 @@ fn compile() {
{"id": 2,"name": "Gray Berry"} {"id": 2,"name": "Gray Berry"}
]); ]);
compare_result(json, ret); 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_obj = read_json("./benches/data_array.json");
let json = template(&json_obj).unwrap(); let json = template(&json_obj).unwrap();
let ret = json!([ let ret = json!([
@ -30,27 +33,55 @@ fn compile() {
{"id": 2,"name": "Rosetta Erickson"} {"id": 2,"name": "Rosetta Erickson"}
]); ]);
compare_result(json, ret); 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();
compile_object("$..friends[2]");
compile_array("$..friends[2]");
compile_error();
} }
#[test] #[test]
fn selector() { fn selector() {
setup(); setup();
fn select<'a, F>(selector: &mut F, path: &'a str, target: Value)
where
F: FnMut(&'a str) -> Result<Vec<&Value>, JsonPathError>,
{
let json = selector(path).unwrap();
compare_result(json, target);
};
let json_obj = read_json("./benches/data_obj.json"); let json_obj = read_json("./benches/data_obj.json");
let mut reader = jsonpath::selector(&json_obj); let mut selector = jsonpath::selector(&json_obj);
let json = reader("$..friends[2]").unwrap();
let ret = json!([ select(
&mut selector,
"$..friends[2]",
json!([
{"id": 2,"name": "Gray Berry"}, {"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Gray Berry"} {"id": 2,"name": "Gray Berry"}
]); ]),
compare_result(json, ret); );
select(
let json = reader("$..friends[0]").unwrap(); &mut selector,
let ret = json!([ "$..friends[0]",
json!([
{"id": 0}, {"id": 0},
{"id": 0,"name": "Millicent Norman"} {"id": 0,"name": "Millicent Norman"}
]); ]),
compare_result(json, ret); );
} }
#[test] #[test]
@ -61,11 +92,21 @@ 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(
&mut selector,
"$..friends[2]",
vec![
Friend { Friend {
id: 2, id: 2,
name: Some("Gray Berry".to_string()), name: Some("Gray Berry".to_string()),
@ -74,18 +115,20 @@ fn selector_as() {
id: 2, id: 2,
name: Some("Gray Berry".to_string()), name: Some("Gray Berry".to_string()),
}, },
]; ],
assert_eq!(json, ret); );
let json = selector("$..friends[0]").unwrap(); select(
let ret = vec![ &mut selector,
"$..friends[0]",
vec![
Friend { id: 0, name: None }, Friend { id: 0, name: None },
Friend { Friend {
id: 0, id: 0,
name: Some("Millicent Norman".to_string()), name: Some("Millicent Norman".to_string()),
}, },
]; ],
assert_eq!(json, ret); );
} }
#[test] #[test]