diff --git a/.travis.yml b/.travis.yml index 66bee5a..efdaf02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ branches: matrix: include: - - rust: nightly + - rust: stable os: linux env: RUST_BACKTRACE=1 before_cache: | @@ -31,6 +31,7 @@ matrix: - cargo test --verbose --all - cd wasm && cargo clippy -- -D warnings -A clippy::suspicious_else_formatting - cd ../nodejs/native && cargo clippy -- -D warnings + - cd ../../ after_success: | cargo tarpaulin --exclude-files nodejs wasm parser/mod.rs --out Xml bash <(curl -s https://codecov.io/bash) @@ -78,4 +79,40 @@ matrix: - npm -v - npm install script: - - npm test \ No newline at end of file + - npm test + - language: node_js + os: linux + node_js: + - '11' + - '10' + - '9' + - '8' + before_install: + - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh + - sh /tmp/rustup.sh -y + - export PATH="$HOME/.cargo/bin:$PATH" + - source "$HOME/.cargo/env" + - node -v + - npm -v + before_script: + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - bash ./build-wasm.sh + - language: node_js + os: osx + node_js: + - '11' + - '10' + - '9' + - '8' + before_install: + - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh + - sh /tmp/rustup.sh -y + - export PATH="$HOME/.cargo/bin:$PATH" + - source "$HOME/.cargo/env" + - node -v + - npm -v + before_script: + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - bash ./build-wasm.sh \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 63d167d..857d030 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,9 +24,6 @@ serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", features = ["preserve_order"] } array_tool = "1.0.3" -[dev-dependencies] -bencher = "0.1.5" - [lib] name = "jsonpath_lib" path = "src/lib.rs" diff --git a/bench.sh b/bench.sh new file mode 100755 index 0000000..669778c --- /dev/null +++ b/bench.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +# +# rustup default nightly +# + +cargo bench --manifest-path ./benchmark/Cargo.toml diff --git a/benchmark/.gitignore b/benchmark/.gitignore new file mode 100644 index 0000000..3343429 --- /dev/null +++ b/benchmark/.gitignore @@ -0,0 +1,4 @@ +.idea/* +.vscode +/target/ +Cargo.lock \ No newline at end of file diff --git a/benchmark/Cargo.toml b/benchmark/Cargo.toml new file mode 100644 index 0000000..fd5f6b2 --- /dev/null +++ b/benchmark/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "jsonpath_lib_benches" +version = "0.1.0" +authors = ["Changseok Han "] +description = "jsonpath_lib benchmark" +license = "MIT" +[dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0", features = ["preserve_order"] } +jsonpath_lib = { path = "../" } + +[dev-dependencies] +bencher = "0.1.5" + +[[bin]] +name = "jsonpath_lib_benches" +path = "src/main.rs" \ No newline at end of file diff --git a/benches/bench_bin/.gitignore b/benchmark/bench_bin/.gitignore similarity index 100% rename from benches/bench_bin/.gitignore rename to benchmark/bench_bin/.gitignore diff --git a/benches/bench_bin/Cargo.toml b/benchmark/bench_bin/Cargo.toml similarity index 100% rename from benches/bench_bin/Cargo.toml rename to benchmark/bench_bin/Cargo.toml diff --git a/benches/bench_bin/bench.sh b/benchmark/bench_bin/bench.sh similarity index 100% rename from benches/bench_bin/bench.sh rename to benchmark/bench_bin/bench.sh diff --git a/benches/bench_bin/src/main.rs b/benchmark/bench_bin/src/main.rs similarity index 100% rename from benches/bench_bin/src/main.rs rename to benchmark/bench_bin/src/main.rs diff --git a/benches/bench_native.sh b/benchmark/bench_native.sh similarity index 100% rename from benches/bench_native.sh rename to benchmark/bench_native.sh diff --git a/benches/bench_wasm.sh b/benchmark/bench_wasm.sh similarity index 100% rename from benches/bench_wasm.sh rename to benchmark/bench_wasm.sh diff --git a/benches/bench.rs b/benchmark/benches/bench.rs similarity index 98% rename from benches/bench.rs rename to benchmark/benches/bench.rs index b2907b8..9cea0ea 100644 --- a/benches/bench.rs +++ b/benchmark/benches/bench.rs @@ -22,7 +22,7 @@ fn read_json(path: &str) -> String { } fn get_string() -> String { - read_json("./benches/example.json") + read_json("./example.json") } fn get_json() -> Value { diff --git a/benches/bench_example.rs b/benchmark/benches/bench_example.rs similarity index 96% rename from benches/bench_example.rs rename to benchmark/benches/bench_example.rs index 3cd3764..f91b7c2 100644 --- a/benches/bench_example.rs +++ b/benchmark/benches/bench_example.rs @@ -19,7 +19,7 @@ fn read_json(path: &str) -> String { } fn get_string() -> String { - read_json("./benches/example.json") + read_json("./example.json") } fn get_json() -> Value { diff --git a/benches/data_array.json b/benchmark/data_array.json similarity index 100% rename from benches/data_array.json rename to benchmark/data_array.json diff --git a/benches/data_obj.json b/benchmark/data_obj.json similarity index 100% rename from benches/data_obj.json rename to benchmark/data_obj.json diff --git a/benches/example.json b/benchmark/example.json similarity index 100% rename from benches/example.json rename to benchmark/example.json diff --git a/benches/giveme_every_thing_result.json b/benchmark/giveme_every_thing_result.json similarity index 100% rename from benches/giveme_every_thing_result.json rename to benchmark/giveme_every_thing_result.json diff --git a/benches/javascript/.gitignore b/benchmark/javascript/.gitignore similarity index 100% rename from benches/javascript/.gitignore rename to benchmark/javascript/.gitignore diff --git a/benches/javascript/bench.js b/benchmark/javascript/bench.js similarity index 100% rename from benches/javascript/bench.js rename to benchmark/javascript/bench.js diff --git a/benches/javascript/bench.sh b/benchmark/javascript/bench.sh similarity index 100% rename from benches/javascript/bench.sh rename to benchmark/javascript/bench.sh diff --git a/benches/javascript/package-lock.json b/benchmark/javascript/package-lock.json similarity index 100% rename from benches/javascript/package-lock.json rename to benchmark/javascript/package-lock.json diff --git a/benches/javascript/package.json b/benchmark/javascript/package.json similarity index 100% rename from benches/javascript/package.json rename to benchmark/javascript/package.json diff --git a/benches/package-lock.json b/benchmark/package-lock.json similarity index 100% rename from benches/package-lock.json rename to benchmark/package-lock.json diff --git a/benchmark/src/main.rs b/benchmark/src/main.rs new file mode 100644 index 0000000..e71fdf5 --- /dev/null +++ b/benchmark/src/main.rs @@ -0,0 +1 @@ +fn main() {} \ No newline at end of file diff --git a/build.sh b/build.sh index b9fbf00..cbb9198 100755 --- a/build.sh +++ b/build.sh @@ -11,7 +11,7 @@ WASM_BROWSER_PKG="${WASM}"/browser_pkg WASM_NODEJS_PKG="${WASM}"/nodejs_pkg WASM_ALL_PKG="${WASM}"/all_pkg WASM_TEST="${WASM}"/tests -BENCHES="${DIR}"/benches +BENCHES="${DIR}"/benchmark BENCHES_JS="${BENCHES}"/javascript NODEJS="${DIR}"/nodejs DOCS="${DIR}"/docs diff --git a/src/lib.rs b/src/lib.rs index 80e9cbb..d7caebb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -412,13 +412,8 @@ pub fn select_as( /// ``` pub fn delete(value: Value, path: &str) -> Result { let mut selector = SelectorMut::default(); - let ret = selector - .str_path(path)? - .value(value) - .delete()? - .take() - .unwrap_or(Value::Null); - Ok(ret) + let value = selector.str_path(path)?.value(value).delete()?; + Ok(value.take().unwrap_or(Value::Null)) } /// Select JSON properties using a jsonpath and transform the result and then replace it. via closure that implements `FnMut` you can transform the selected results. @@ -468,11 +463,6 @@ where F: FnMut(&Value) -> Value, { let mut selector = SelectorMut::default(); - let ret = selector - .str_path(path)? - .value(value) - .replace_with(fun)? - .take() - .unwrap_or(Value::Null); - Ok(ret) + let value = selector.str_path(path)?.value(value).replace_with(fun)?; + Ok(value.take().unwrap_or(Value::Null)) } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 59080f8..4a44b3d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -193,13 +193,14 @@ impl Parser { fn boolean(tokenizer: &mut TokenReader) -> ParseResult { debug!("#boolean"); + + fn validation_bool_value(v: &str) -> bool { + let b = v.as_bytes(); + !b.is_empty() && (b[0] == b't' || b[0] == b'T' || b[0] == b'f' || b[0] == b'F') + } + match tokenizer.next_token() { - Ok(Token::Key(_, ref v)) - if { - let b = v.as_bytes(); - !b.is_empty() && (b[0] == b't' || b[0] == b'T' || b[0] == b'f' || b[0] == b'F') - } => - { + Ok(Token::Key(_, ref v)) if validation_bool_value(v) => { Ok(Self::node(ParseToken::Bool(v.eq_ignore_ascii_case("true")))) } _ => Err(tokenizer.err_msg()), @@ -229,10 +230,10 @@ impl Parser { debug!("#array_quote_value"); match tokenizer.next_token() { Ok(Token::SingleQuoted(_, val)) | Ok(Token::DoubleQuoted(_, val)) => { - if !tokenizer.peek_is(COMMA) { - Ok(Self::node(ParseToken::Key(val))) - } else { + if tokenizer.peek_is(COMMA) { Self::array_keys(tokenizer, val) + } else { + Ok(Self::node(ParseToken::Key(val))) } } _ => Err(tokenizer.err_msg()), diff --git a/src/parser/tokenizer.rs b/src/parser/tokenizer.rs index bd5a15f..b2e04bb 100644 --- a/src/parser/tokenizer.rs +++ b/src/parser/tokenizer.rs @@ -94,6 +94,7 @@ impl Token { self.to_simple() == str_token } + #[cfg_attr(tarpaulin, skip)] fn to_simple(&self) -> &'static str { match self { Token::Absolute(_) => ABSOLUTE, diff --git a/src/select/mod.rs b/src/select/mod.rs index 3772ba7..b240b11 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -157,8 +157,8 @@ impl Cmp for CmpAnd { v1 && v2 } - fn cmp_f64(&self, v1: f64, v2: f64) -> bool { - v1 > 0_f64 && v2 > 0_f64 + fn cmp_f64(&self, _v1: f64, _v2: f64) -> bool { + true } fn cmp_string(&self, v1: &str, v2: &str) -> bool { @@ -177,8 +177,8 @@ impl Cmp for CmpOr { v1 || v2 } - fn cmp_f64(&self, v1: f64, v2: f64) -> bool { - v1 > 0_f64 || v2 > 0_f64 + fn cmp_f64(&self, _v1: f64, _v2: f64) -> bool { + true } fn cmp_string(&self, v1: &str, v2: &str) -> bool { @@ -190,7 +190,7 @@ impl Cmp for CmpOr { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] enum ExprTerm<'a> { String(String), Number(Number), @@ -434,7 +434,7 @@ fn abs_index(n: isize, len: usize) -> usize { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] enum FilterKey { String(String), All, @@ -482,28 +482,25 @@ impl<'a, 'b> Selector<'a, 'b> { pub fn str_path(&mut self, path: &str) -> Result<&mut Self, JsonPathError> { debug!("path : {}", path); - - if self.node_ref.is_some() { - self.node_ref.take(); - } - + self.node_ref.take(); self.node = Some(Parser::compile(path).map_err(JsonPathError::Path)?); Ok(self) } pub fn node_ref(&self) -> Option<&Node> { if let Some(node) = &self.node { - Some(node) - } else { - None + return Some(node); } + + if let Some(node) = &self.node_ref { + return Some(*node); + } + + None } pub fn compiled_path(&mut self, node: &'b Node) -> &mut Self { - if self.node.is_some() { - self.node.take(); - } - + self.node.take(); self.node_ref = Some(node); self } @@ -1195,3 +1192,212 @@ impl SelectorMut { Ok(self) } } + +#[cfg(test)] +mod select_inner_tests { + use serde_json::{Number, Value}; + + use select::{Cmp, CmpAnd, CmpEq, CmpGe, CmpGt, CmpLe, CmpLt, CmpNe, CmpOr, ExprTerm}; + + #[test] + fn to_f64_i64() { + let number = 0_i64; + let v: Value = serde_json::from_str(&format!("{}", number)).unwrap(); + if let Value::Number(n) = v { + assert_eq!((super::to_f64(&n) - number as f64).abs() == 0_f64, true); + } else { + panic!(); + } + } + + #[test] + fn to_f64_f64() { + let number = 0.1_f64; + let v: Value = serde_json::from_str(&format!("{}", number)).unwrap(); + if let Value::Number(n) = v { + assert_eq!((super::to_f64(&n) - number).abs() == 0_f64, true); + } else { + panic!(); + } + } + + #[test] + fn to_f64_u64() { + let number = u64::max_value(); + let v: Value = serde_json::from_str(&format!("{}", number)).unwrap(); + if let Value::Number(n) = v { + assert_eq!((super::to_f64(&n) - number as f64).abs() == 0_f64, true); + } else { + panic!(); + } + } + + #[test] + fn cmp_eq() { + let cmp_fn = CmpEq; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), false); + assert_eq!(cmp_fn.cmp_bool(true, true), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.1), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), false); + assert_eq!(cmp_fn.cmp_string("1", "1"), true); + assert_eq!(cmp_fn.cmp_string("1", "2"), false); + } + + #[test] + fn cmp_ne() { + let cmp_fn = CmpNe; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), true); + assert_eq!(cmp_fn.cmp_bool(true, true), false); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.1), false); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), true); + assert_eq!(cmp_fn.cmp_string("1", "1"), false); + assert_eq!(cmp_fn.cmp_string("1", "2"), true); + } + + #[test] + fn cmp_gt() { + let cmp_fn = CmpGt; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), true); + assert_eq!(cmp_fn.cmp_bool(true, true), false); + assert_eq!(cmp_fn.cmp_f64(0.2, 0.1), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), false); + assert_eq!(cmp_fn.cmp_string("a", "a"), false); + assert_eq!(cmp_fn.cmp_string("b", "a"), true); + assert_eq!(cmp_fn.cmp_string("1", "2"), false); + } + + #[test] + fn cmp_ge() { + let cmp_fn = CmpGe; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), true); + assert_eq!(cmp_fn.cmp_bool(true, true), true); + assert_eq!(cmp_fn.cmp_f64(0.2, 0.1), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.1), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), false); + assert_eq!(cmp_fn.cmp_string("1", "1"), true); + assert_eq!(cmp_fn.cmp_string("ab", "a"), true); + assert_eq!(cmp_fn.cmp_string("1", "2"), false); + } + + #[test] + fn cmp_lt() { + let cmp_fn = CmpLt; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), false); + assert_eq!(cmp_fn.cmp_bool(false, true), true); + assert_eq!(cmp_fn.cmp_bool(true, true), false); + assert_eq!(cmp_fn.cmp_bool(false, false), false); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.1), false); + assert_eq!(cmp_fn.cmp_f64(0.2, 0.1), false); + assert_eq!(cmp_fn.cmp_string("a", "a"), false); + assert_eq!(cmp_fn.cmp_string("ab", "b"), true); + assert_eq!(cmp_fn.cmp_string("1", "2"), true); + } + + #[test] + fn cmp_le() { + let cmp_fn = CmpLe; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), false); + assert_eq!(cmp_fn.cmp_bool(false, true), true); + assert_eq!(cmp_fn.cmp_bool(true, true), true); + assert_eq!(cmp_fn.cmp_bool(false, false), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.2), true); + assert_eq!(cmp_fn.cmp_f64(0.1, 0.1), true); + assert_eq!(cmp_fn.cmp_f64(0.2, 0.1), false); + assert_eq!(cmp_fn.cmp_string("a", "a"), true); + assert_eq!(cmp_fn.cmp_string("ab", "b"), true); + assert_eq!(cmp_fn.cmp_string("abd", "abc"), false); + assert_eq!(cmp_fn.cmp_string("1", "2"), true); + } + + #[test] + fn cmp_and() { + let cmp_fn = CmpAnd; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), false); + assert_eq!(cmp_fn.cmp_bool(false, true), false); + assert_eq!(cmp_fn.cmp_bool(true, true), true); + assert_eq!(cmp_fn.cmp_bool(false, false), false); + assert_eq!(cmp_fn.cmp_f64(0.0, 0.0), true); + assert_eq!(cmp_fn.cmp_string("a", "a"), true); + } + + #[test] + fn cmp_or() { + let cmp_fn = CmpOr; + assert_eq!(cmp_fn.default(), false); + assert_eq!(cmp_fn.cmp_bool(true, false), true); + assert_eq!(cmp_fn.cmp_bool(false, true), true); + assert_eq!(cmp_fn.cmp_bool(true, true), true); + assert_eq!(cmp_fn.cmp_bool(false, false), false); + assert_eq!(cmp_fn.cmp_f64(0.0, 0.0), true); + assert_eq!(cmp_fn.cmp_string("a", "a"), true); + } + + #[test] + fn cmp_json() { + let v1 = Value::Bool(true); + let v2 = Value::String("1".to_string()); + let left = [&v1, &v2]; + let right = [&v1, &v2]; + let empty: Vec<&Value> = Vec::new(); + + assert_eq!(CmpEq.cmp_json(&left, &right), left.to_vec()); + assert_eq!(CmpNe.cmp_json(&left, &right), left.to_vec()); + assert_eq!(CmpGt.cmp_json(&left, &right), empty); + assert_eq!(CmpGe.cmp_json(&left, &right), empty); + assert_eq!(CmpLt.cmp_json(&left, &right), empty); + assert_eq!(CmpLe.cmp_json(&left, &right), empty); + assert_eq!(CmpAnd.cmp_json(&left, &right), left.to_vec()); + assert_eq!(CmpOr.cmp_json(&left, &right), left.to_vec()); + + assert_eq!( + CmpEq.cmp_json(&[&Value::Bool(true)], &[&Value::Bool(true)]), + vec![&Value::Bool(true)] + ); + assert_eq!( + CmpEq.cmp_json(&[&Value::Bool(true)], &[&Value::Bool(false)]), + empty + ); + assert_eq!( + CmpNe.cmp_json(&[&Value::Bool(true)], &[&Value::Bool(true)]), + empty + ); + assert_eq!( + CmpNe.cmp_json(&[&Value::Bool(false)], &[&Value::Bool(true)]), + vec![&Value::Bool(false)] + ); + assert_eq!( + CmpAnd.cmp_json(&[&Value::Bool(true)], &[&Value::Bool(true)]), + vec![&Value::Bool(true)] + ); + assert_eq!( + CmpOr.cmp_json(&[&Value::Bool(true)], &[&Value::Bool(false)]), + vec![&Value::Bool(true), &Value::Bool(false)] + ); + } + + #[test] + fn value_vec_into() { + let v = Value::Bool(true); + let vec = &vec![&v]; + let term: ExprTerm = vec.into(); + assert_eq!(term, ExprTerm::Bool(true)); + + let v = Value::String("a".to_string()); + let vec = &vec![&v]; + let term: ExprTerm = vec.into(); + assert_eq!(term, ExprTerm::String("a".to_string())); + + let v = serde_json::from_str("1.0").unwrap(); + let vec = &vec![&v]; + let term: ExprTerm = vec.into(); + assert_eq!(term, ExprTerm::Number(Number::from_f64(1.0).unwrap())); + } +} diff --git a/tests/filter.rs b/tests/filter.rs index 4540039..112cfd3 100644 --- a/tests/filter.rs +++ b/tests/filter.rs @@ -13,7 +13,7 @@ fn array() { select_and_then_compare( "$.school.friends[1, 2]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ {"id": 1, "name": "Vincent Cannon" }, {"id": 2, "name": "Gray Berry"} @@ -22,7 +22,7 @@ fn array() { select_and_then_compare( "$.school.friends[1: ]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ {"id": 1, "name": "Vincent Cannon" }, {"id": 2, "name": "Gray Berry"} @@ -31,7 +31,7 @@ fn array() { select_and_then_compare( "$.school.friends[:-2]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ {"id": 0, "name": "Millicent Norman"} ]), @@ -39,13 +39,13 @@ fn array() { select_and_then_compare( "$..friends[2].name", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!(["Gray Berry", "Gray Berry"]), ); select_and_then_compare( "$..friends[*].name", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ "Vincent Cannon", "Gray Berry", @@ -57,19 +57,19 @@ fn array() { select_and_then_compare( "$['school']['friends'][*].['name']", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!(["Millicent Norman", "Vincent Cannon", "Gray Berry"]), ); select_and_then_compare( "$['school']['friends'][0].['name']", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!(["Millicent Norman"]), ); select_and_then_compare( r#"$.["eyeColor", "name"]"#, - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!(["blue", "Leonor Herman"]), ); } @@ -80,7 +80,7 @@ fn return_type() { select_and_then_compare( "$.school", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([{ "friends": [ {"id": 0, "name": "Millicent Norman"}, @@ -92,7 +92,7 @@ fn return_type() { select_and_then_compare( "$.school[?(@.friends[0])]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([{ "friends": [ {"id": 0, "name": "Millicent Norman"}, @@ -104,7 +104,7 @@ fn return_type() { select_and_then_compare( "$.school[?(@.friends[10])]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([{ "friends": [ {"id": 0, "name": "Millicent Norman"}, @@ -116,7 +116,7 @@ fn return_type() { select_and_then_compare( "$.school[?(1==1)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([{ "friends": [ {"id": 0, "name": "Millicent Norman"}, @@ -128,7 +128,7 @@ fn return_type() { select_and_then_compare( "$.school.friends[?(1==1)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([[ {"id": 0, "name": "Millicent Norman"}, {"id": 1, "name": "Vincent Cannon" }, @@ -143,7 +143,7 @@ fn op_default() { select_and_then_compare( "$.school[?(@.friends == @.friends)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([{ "friends": [ {"id": 0, "name": "Millicent Norman"}, @@ -155,7 +155,7 @@ fn op_default() { select_and_then_compare( "$.friends[?(@.name)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ { "id" : 1, "name" : "Vincent Cannon" }, { "id" : 2, "name" : "Gray Berry" } @@ -164,7 +164,7 @@ fn op_default() { select_and_then_compare( "$.friends[?(@.id >= 2)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ { "id" : 2, "name" : "Gray Berry" } ]), @@ -172,7 +172,7 @@ fn op_default() { select_and_then_compare( "$.friends[?(@.id >= 2 || @.id == 1)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([ { "id" : 2, "name" : "Gray Berry" }, { "id" : 1, "name" : "Vincent Cannon" } @@ -181,25 +181,25 @@ fn op_default() { select_and_then_compare( "$.friends[?( (@.id >= 2 || @.id == 1) && @.id == 0)]", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([Value::Null]), ); select_and_then_compare( "$..friends[?(@.id == $.index)].id", - read_json("./benches/data_obj.json"), + read_json("./benchmark/data_obj.json"), json!([0, 0]), ); select_and_then_compare( "$..book[?($.store.bicycle.price < @.price)].price", - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([22.99]), ); select_and_then_compare( "$..book[?( (@.price == 12.99 || @.category == 'reference') && @.price > 10)].price", - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([12.99]), ); @@ -365,7 +365,7 @@ fn example() { select_and_then_compare( r#"$.store.book[*].author"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ "Nigel Rees", "Evelyn Waugh", @@ -376,7 +376,7 @@ fn example() { select_and_then_compare( r#"$..author"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ "Nigel Rees", "Evelyn Waugh", @@ -387,7 +387,7 @@ fn example() { select_and_then_compare( r#"$.store.*"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ [ {"category" : "reference", "author" : "Nigel Rees","title" : "Sayings of the Century", "price" : 8.95}, @@ -401,13 +401,13 @@ fn example() { select_and_then_compare( r#"$.store..price"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([8.95, 12.99, 8.99, 22.99, 19.95]), ); select_and_then_compare( r#"$..book[2]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "fiction", @@ -421,7 +421,7 @@ fn example() { select_and_then_compare( r#"$..book[-2]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "fiction", @@ -435,7 +435,7 @@ fn example() { select_and_then_compare( r#"$..book[0, 1]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "reference", @@ -454,7 +454,7 @@ fn example() { select_and_then_compare( r#"$..book[:2]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "reference", @@ -473,7 +473,7 @@ fn example() { select_and_then_compare( r#"$..book[2:]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "fiction", @@ -494,7 +494,7 @@ fn example() { select_and_then_compare( r#"$..book[?(@.isbn)]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "fiction", @@ -515,7 +515,7 @@ fn example() { select_and_then_compare( r#"$.store.book[?(@.price < 10)]"#, - read_json("./benches/example.json"), + read_json("./benchmark/example.json"), json!([ { "category" : "reference", @@ -535,8 +535,8 @@ fn example() { select_and_then_compare( r#"$..*"#, - read_json("./benches/example.json"), - read_json("./benches/giveme_every_thing_result.json"), + read_json("./benchmark/example.json"), + read_json("./benchmark/giveme_every_thing_result.json"), ); } @@ -651,7 +651,7 @@ fn all_filter() { for path in &[r#"$..friends.*"#, r#"$[*].friends.*"#] { select_and_then_compare( path, - read_json("./benches/data_array.json"), + read_json("./benchmark/data_array.json"), json!([ { "id" : 0, "name" : "Millicent Norman" }, { "id" : 1, "name" : "Vincent Cannon" }, diff --git a/tests/lib.rs b/tests/lib.rs index 1ee8780..5ed8fc4 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -15,7 +15,7 @@ mod common; fn compile() { let compile_object = |path| { let mut template = jsonpath::compile(path); - let json_obj = read_json("./benches/data_obj.json"); + let json_obj = read_json("./benchmark/data_obj.json"); let json = template(&json_obj).unwrap(); let ret = json!([ {"id": 2,"name": "Gray Berry"}, @@ -26,7 +26,7 @@ fn compile() { let compile_array = |path| { let mut template = jsonpath::compile(path); - let json_obj = read_json("./benches/data_array.json"); + let json_obj = read_json("./benchmark/data_array.json"); let json = template(&json_obj).unwrap(); let ret = json!([ {"id": 2,"name": "Gray Berry"}, @@ -59,7 +59,7 @@ fn selector() { compare_result(json, target); }; - let json_obj = read_json("./benches/data_obj.json"); + let json_obj = read_json("./benchmark/data_obj.json"); let mut selector = jsonpath::selector(&json_obj); select( @@ -96,7 +96,7 @@ fn selector_as() { assert_eq!(json, target); }; - let json_obj = read_json("./benches/data_obj.json"); + let json_obj = read_json("./benchmark/data_obj.json"); let mut selector = jsonpath::selector_as::(&json_obj); select( @@ -129,7 +129,7 @@ fn selector_as() { #[test] fn select() { - let json_obj = read_json("./benches/example.json"); + let json_obj = read_json("./benchmark/example.json"); let json = jsonpath::select(&json_obj, "$..book[2]").unwrap(); let ret = json!([{ "category" : "fiction", @@ -143,7 +143,7 @@ fn select() { #[test] fn select_str() { - let json_str = read_contents("./benches/example.json"); + 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", diff --git a/tests/readme.rs b/tests/readme.rs index 2ca2274..ae2a02c 100644 --- a/tests/readme.rs +++ b/tests/readme.rs @@ -482,7 +482,7 @@ fn readme_delete() { #[test] fn readme_delete2() { - let json_obj = common::read_json("./benches/example.json"); + let json_obj = common::read_json("./benchmark/example.json"); let ret = jsonpath::delete(json_obj, "$.store.book").unwrap(); diff --git a/tests/mutable.rs b/tests/selector.rs similarity index 76% rename from tests/mutable.rs rename to tests/selector.rs index 6ae6205..c8f2da8 100644 --- a/tests/mutable.rs +++ b/tests/selector.rs @@ -3,7 +3,7 @@ extern crate jsonpath_lib as jsonpath; extern crate serde_json; use common::{read_json, setup}; -use jsonpath::{Selector, SelectorMut}; +use jsonpath::{Selector, SelectorMut, Parser}; use serde_json::Value; mod common; @@ -18,7 +18,7 @@ fn selector_mut() { let result = selector_mut .str_path(r#"$.store..price"#) .unwrap() - .value(read_json("./benches/example.json")) + .value(read_json("./benchmark/example.json")) .replace_with(&mut |v| { if let Value::Number(n) = v { nums.push(n.as_f64().unwrap()); @@ -53,3 +53,11 @@ fn selector_mut() { 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)); +}