Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: handle non-determinism tests #85

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions serde_json_path/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **testing**: support tests for non-determinism in compliance test suite ([#85])
- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#85]: https://github.com/hiltontj/serde_json_path/pull/85
[#84]: https://github.com/hiltontj/serde_json_path/pull/84

# 0.6.6 (23 February 2024)
Expand Down
73 changes: 49 additions & 24 deletions serde_json_path/tests/compliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,39 @@ struct TestCase {
name: String,
selector: String,
#[serde(default)]
invalid_selector: bool,
#[serde(default)]
document: Value,
#[serde(default)]
result: Vec<Value>,
#[serde(flatten)]
result: TestResult,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum TestResult {
Deterministic { result: Vec<Value> },
NonDeterministic { results: Vec<Vec<Value>> },
InvalidSelector { invalid_selector: bool },
}

impl TestResult {
fn verify(&self, name: &str, actual: Vec<&Value>) {
match self {
TestResult::Deterministic { result } => assert_eq!(
result.iter().collect::<Vec<&Value>>(),
actual,
"{name}: incorrect result, expected {result:?}, got {actual:?}"
),
TestResult::NonDeterministic { results } => {
assert!(results
.iter()
.any(|r| r.iter().collect::<Vec<&Value>>().eq(&actual)))
}
TestResult::InvalidSelector { .. } => unreachable!(),
}
}

fn is_invalid_selector(&self) -> bool {
matches!(self, Self::InvalidSelector { invalid_selector } if *invalid_selector)
}
}

#[test]
Expand All @@ -36,44 +64,36 @@ fn compliance_test_suite() {
TestCase {
name,
selector,
invalid_selector,
document,
result,
},
) in test_cases.tests.iter().enumerate()
{
println!("Test ({i}): {name}");
let path = JsonPath::parse(selector);
if *invalid_selector {
if result.is_invalid_selector() {
assert!(
path.is_err(),
"{name}: parsing {selector:?} should have failed",
);
} else {
let path = path.expect("valid JSON Path string");
let expected = result.iter().collect::<Vec<&Value>>();
{
// Query using JsonPath::query
let actual = path.query(document).all();
assert_eq!(
expected, actual,
"{name}: incorrect result, expected {expected:?}, got {actual:?}"
);
result.verify(name, actual);
}
{
// Query using JsonPath::query_located
let q = path.query_located(document);
let actual = q.nodes().collect::<Vec<&Value>>();
assert_eq!(
expected, actual,
"(located) {name}: incorrect result, expected {expected:?}, got {actual:?}"
);
result.verify(name, actual);
}
}
}
}

const TEST_CASE_N: usize = 388;
const TEST_CASE_N: usize = 10;

#[test]
#[ignore = "this is only for testing individual CTS test cases as needed"]
Expand All @@ -87,24 +107,29 @@ fn compliance_single() {
let TestCase {
name,
selector,
invalid_selector,
document,
result,
} = &test_cases.tests[TEST_CASE_N];
println!("Test Case: {name}");
let path = JsonPath::parse(selector);
if *invalid_selector {
if result.is_invalid_selector() {
println!("...this test should fail");
assert!(
path.is_err(),
"{name}: parsing {selector:?} should have failed",
);
} else {
let actual = path.expect("valid JSON Path string").query(document).all();
let expected = result.iter().collect::<Vec<&Value>>();
assert_eq!(
expected, actual,
"{name}: incorrect result, expected {expected:?}, got {actual:?}"
);
let path = path.expect("valid JSON Path string");
{
// Query using JsonPath::query
let actual = path.query(document).all();
result.verify(name, actual);
}
{
// Query using JsonPath::query_located
let q = path.query_located(document);
let actual = q.nodes().collect::<Vec<&Value>>();
result.verify(name, actual);
}
}
}
2 changes: 2 additions & 0 deletions serde_json_path_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **testing**: support tests for non-determinism in compliance test suite ([#85])
- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#85]: https://github.com/hiltontj/serde_json_path/pull/85
[#84]: https://github.com/hiltontj/serde_json_path/pull/84

# 0.1.5 (23 February 2024)
Expand Down
2 changes: 2 additions & 0 deletions serde_json_path_macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **testing**: support tests for non-determinism in compliance test suite ([#85])
- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#85]: https://github.com/hiltontj/serde_json_path/pull/85
[#84]: https://github.com/hiltontj/serde_json_path/pull/84

# 0.1.3 (23 February 2024)
Expand Down
Loading