code coverage 90%

This commit is contained in:
freestrings
2019-06-26 15:07:49 +09:00
parent cab5177811
commit 8f01598e05
5 changed files with 248 additions and 42 deletions

63
tests/selector.rs Normal file
View File

@ -0,0 +1,63 @@
extern crate jsonpath_lib as jsonpath;
#[macro_use]
extern crate serde_json;
use common::{read_json, setup};
use jsonpath::{Selector, SelectorMut, Parser};
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("./benches/example.json"))
.replace_with(&mut |v| {
if let Value::Number(n) = v {
nums.push(n.as_f64().unwrap());
}
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));
}