Skip to content

Commit

Permalink
test: added snapshot tests for the serde feature
Browse files Browse the repository at this point in the history
Added snapshot tests based on `insta` to both the rsonpath-syntax
and rsonpath-lib serialization features.

Ref: #624
  • Loading branch information
V0ldek committed Dec 30, 2024
1 parent 1a50ebb commit 1fddf02
Show file tree
Hide file tree
Showing 17 changed files with 616 additions and 6 deletions.
36 changes: 31 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ log = "0.4.22"
thiserror = "2.0.9"
# Dev-dependencies
ciborium = { version = "0.2.2", default-features = false }
insta = { version = "1.41.1" }
itertools = "0.13.0"
pretty_assertions = "1.4.1"
proptest = "1.5.0"
Expand Down
1 change: 1 addition & 0 deletions crates/rsonpath-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ vector-map = "1.0.1"

[dev-dependencies]
ciborium = { workspace = true, default-features = true }
insta = { workspace = true, features = ["ron"] }
itertools = { workspace = true }
pretty_assertions = { workspace = true }
proptest = { workspace = true }
Expand Down
43 changes: 43 additions & 0 deletions crates/rsonpath-lib/tests/engine_serialization_snapshots.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use insta::assert_ron_snapshot;
use rsonpath::engine::{Compiler, RsonpathEngine};

mod ron {
use super::*;
use std::error::Error;

fn engine(string: &str) -> Result<RsonpathEngine, Box<dyn Error>> {
let query = rsonpath_syntax::parse(string)?;
let engine = RsonpathEngine::compile_query(&query)?;
Ok(engine)
}

#[test]
fn empty_query() -> Result<(), Box<dyn Error>> {
assert_ron_snapshot!(&engine("$")?);
Ok(())
}

#[test]
fn readme_query() -> Result<(), Box<dyn Error>> {
assert_ron_snapshot!(&engine("$.jsonpath[*]")?);
Ok(())
}

#[test]
fn jsonpath_example_query() -> Result<(), Box<dyn Error>> {
assert_ron_snapshot!(&engine("$..phoneNumbers[*].number")?);
Ok(())
}

#[test]
fn real_life_query() -> Result<(), Box<dyn Error>> {
assert_ron_snapshot!(&engine("$.personal.details.contact.information.phones.home")?);
Ok(())
}

#[test]
fn slice() -> Result<(), Box<dyn Error>> {
assert_ron_snapshot!(&engine("$..entries[3:5:7]")?);
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/rsonpath-lib/tests/engine_serialization_snapshots.rs
expression: "&engine(\"$\")?"
snapshot_kind: text
---
(V1, Automaton(
states: [
StateTable(
attributes: StateAttributes(2),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(1),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
],
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
source: crates/rsonpath-lib/tests/engine_serialization_snapshots.rs
expression: "&engine(\"$..phoneNumbers[*].number\")?"
snapshot_kind: text
---
(V1, Automaton(
states: [
StateTable(
attributes: StateAttributes(2),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(0),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(2)),
],
array_transitions: [],
fallback_state: State(1),
),
StateTable(
attributes: StateAttributes(0),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(4)),
],
array_transitions: [],
fallback_state: State(3),
),
StateTable(
attributes: StateAttributes(8),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(2)),
(StringPattern(JsonString(
quoted: "\"number\"",
)), State(6)),
],
array_transitions: [],
fallback_state: State(1),
),
StateTable(
attributes: StateAttributes(8),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(4)),
(StringPattern(JsonString(
quoted: "\"number\"",
)), State(5)),
],
array_transitions: [],
fallback_state: State(3),
),
StateTable(
attributes: StateAttributes(9),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(2)),
(StringPattern(JsonString(
quoted: "\"number\"",
)), State(6)),
],
array_transitions: [],
fallback_state: State(1),
),
StateTable(
attributes: StateAttributes(1),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phoneNumbers\"",
)), State(2)),
],
array_transitions: [],
fallback_state: State(1),
),
],
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/rsonpath-lib/tests/engine_serialization_snapshots.rs
expression: "&engine(\"$.jsonpath[*]\")?"
snapshot_kind: text
---
(V1, Automaton(
states: [
StateTable(
attributes: StateAttributes(2),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"jsonpath\"",
)), State(2)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(8),
member_transitions: [],
array_transitions: [],
fallback_state: State(3),
),
StateTable(
attributes: StateAttributes(1),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
],
))
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
source: crates/rsonpath-lib/tests/engine_serialization_snapshots.rs
expression: "&engine(\"$.personal.details.contact.information.phones.home\")?"
snapshot_kind: text
---
(V1, Automaton(
states: [
StateTable(
attributes: StateAttributes(2),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"personal\"",
)), State(2)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"details\"",
)), State(3)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"contact\"",
)), State(4)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"information\"",
)), State(5)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(4),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"phones\"",
)), State(6)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(12),
member_transitions: [
(StringPattern(JsonString(
quoted: "\"home\"",
)), State(7)),
],
array_transitions: [],
fallback_state: State(0),
),
StateTable(
attributes: StateAttributes(1),
member_transitions: [],
array_transitions: [],
fallback_state: State(0),
),
],
))
Loading

0 comments on commit 1fddf02

Please sign in to comment.