1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
use std::fs;
use serde::Deserialize;
use serde_json::Value;
use serde_json_path::JsonPath;
#[cfg(feature = "trace")]
use test_log::test;
#[derive(Deserialize)]
struct TestSuite {
tests: Vec<TestCase>,
}
#[derive(Deserialize)]
struct TestCase {
name: String,
selector: String,
#[serde(default)]
document: 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)
}
}
//disabled due to failures #[test]
fn compliance_test_suite() {
let cts_json_str = fs::read_to_string("jsonpath-compliance-test-suite/cts.json")
.expect("read cts.json file");
let test_cases: TestSuite =
serde_json::from_str(cts_json_str.as_str()).expect("parse cts_json_str");
for (
i,
TestCase {
name,
selector,
document,
result,
},
) in test_cases.tests.iter().enumerate()
{
println!("Test ({i}): {name}");
let path = JsonPath::parse(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");
{
// 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);
}
}
}
}
const TEST_CASE_N: usize = 10;
#[test]
#[ignore = "this is only for testing individual CTS test cases as needed"]
fn compliance_single() {
let cts_json_str = fs::read_to_string("../jsonpath-compliance-test-suite/cts.json")
.expect("read cts.json file");
let test_cases: TestSuite =
serde_json::from_str(cts_json_str.as_str()).expect("parse cts_json_str");
let TestCase {
name,
selector,
document,
result,
} = &test_cases.tests[TEST_CASE_N];
println!("Test Case: {name}");
let path = JsonPath::parse(selector);
if result.is_invalid_selector() {
println!("...this test should fail");
assert!(
path.is_err(),
"{name}: parsing {selector:?} should have failed",
);
} else {
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);
}
}
}
|