Skip to content

Commit 80c80eb

Browse files
authored
Merge pull request jsonpath-standard#12 from glyn/json-cts
Represent CTS in JSON
2 parents 748884c + 7d22b8c commit 80c80eb

File tree

4 files changed

+52
-128
lines changed

4 files changed

+52
-128
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
**WORK IN PROGRESS**
44

5-
See [cts.yaml](tests/cts.yaml) for the Compliance Test Suite and [grammar.pest](src/grammar.pest) for the Parsing Expression Grammar of the Reference Implementation.
5+
See [cts.json](tests/cts.json) for the Compliance Test Suite and [grammar.pest](src/grammar.pest) for the Parsing Expression Grammar of the Reference Implementation.

tests/cts.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{ "tests": [
2+
{
3+
"name": "root",
4+
"selector": "$",
5+
"document": ["first", "second"],
6+
"result": [["first", "second"]]
7+
}, {
8+
"name": "dot child",
9+
"selector": "$.a",
10+
"document": {"a" : "A", "b" : "B"},
11+
"result": ["A"]
12+
}, {
13+
"name": "dot child absent",
14+
"selector": "$.c",
15+
"document": {"a" : "A", "b" : "B"},
16+
"result": []
17+
}, {
18+
"name": "dot child of array",
19+
"selector": "$.a",
20+
"document": ["first", "second"],
21+
"result": []
22+
}, {
23+
"name": "wildcarded child",
24+
"selector": "$.*",
25+
"document": {"a" : "A", "b" : "B"},
26+
"result": ["A", "B"]
27+
}, {
28+
"name": "wildcarded child of array",
29+
"selector": "$.*",
30+
"document": ["first", "second"],
31+
"result": []
32+
}, {
33+
"name": "wildcarded child dot child",
34+
"selector": "$.*.a",
35+
"document": {"x": {"a" : "Ax", "b" : "Bx"}, "y": {"a" : "Ay", "b" : "By"}},
36+
"result": ["Ax", "Ay"]
37+
}
38+
]}

tests/cts.rs

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ mod tests {
2222
struct Testcase {
2323
name: String,
2424
selector: String,
25-
document: serde_yaml::Value, // JSON deserialised as YAML
26-
result: serde_yaml::Value, // JSON deserialised as YAML
25+
document: serde_json::Value,
26+
result: serde_json::Value,
2727
#[serde(default)]
2828
focus: bool, // if true, run only tests with focus set to true
2929
}
3030

3131
#[test]
3232
fn compliance_test_suite() {
33-
let y = fs::read_to_string("tests/cts.yaml").expect("failed to read cts.yaml");
33+
let cts_json = fs::read_to_string("tests/cts.json").expect("failed to read cts.json");
3434

35-
let suite: TestSuite = serde_yaml::from_str(&y).expect("failed to deserialize cts.yaml");
35+
let suite: TestSuite =
36+
serde_json::from_str(&cts_json).expect("failed to deserialize cts.json");
3637

3738
let focussed = (&suite.tests).iter().find(|t| t.focus).is_some();
3839

@@ -44,11 +45,8 @@ mod tests {
4445
let result = panic::catch_unwind(|| {
4546
if VERBOSE {
4647
println!(
47-
"name = {}, selector = {}, document = {}, result = {}",
48-
t.name,
49-
t.selector,
50-
as_json(&t.document).expect("invalid document"),
51-
as_json(&t.result).expect("invalid result")
48+
"name = {}, selector = {}, document = {:?}, result = {:?}",
49+
t.name, t.selector, t.document, t.result
5250
);
5351
}
5452
let path = jsonpath::parse(&t.selector);
@@ -60,18 +58,13 @@ mod tests {
6058
);
6159

6260
if let Ok(p) = path {
63-
if let Ok(result) =
64-
p.find(&as_json_value(t.document).expect("invalid document"))
65-
{
66-
if !equal(
67-
&result,
68-
as_json_value_array(&t.result).expect("invalid result"),
69-
) {
61+
if let Ok(result) = p.find(&t.document) {
62+
if !equal(&result, as_array(&t.result).expect("invalid result")) {
7063
assert!(
7164
false,
7265
"incorrect result for {}, expected: {:?}, got: {:?}",
7366
t.name,
74-
as_json_value_array(&t.result).unwrap(),
67+
as_array(&t.result).unwrap(),
7568
result
7669
)
7770
}
@@ -100,91 +93,13 @@ mod tests {
10093
}
10194
}
10295

103-
fn as_json(v: &serde_yaml::Value) -> Result<String, String> {
96+
fn as_array(v: &serde_json::Value) -> Result<Vec<serde_json::Value>, String> {
10497
match v {
105-
serde_yaml::Value::Null => Ok("null".to_string()),
106-
107-
serde_yaml::Value::Bool(b) => Ok(b.to_string()),
108-
109-
serde_yaml::Value::Number(num) => Ok(num.to_string()),
110-
111-
serde_yaml::Value::String(s) => Ok(json::stringify(s.to_string())),
112-
113-
serde_yaml::Value::Sequence(seq) => {
114-
let array_elements = seq
115-
.into_iter()
116-
.map(|v| as_json(v).expect("invalid sequence element"));
117-
Ok(format!("[{}]", itertools::join(array_elements, ",")))
118-
}
119-
120-
serde_yaml::Value::Mapping(map) => {
121-
let object_members = map.iter().map(|(k, v)| {
122-
format!(
123-
"{}:{}",
124-
as_json(k).expect("invalid object key"),
125-
as_json(v).expect("invalid object value")
126-
)
127-
});
128-
Ok(format!("{{{}}}", itertools::join(object_members, ",")))
129-
}
130-
}
131-
}
132-
133-
fn as_json_value(v: serde_yaml::Value) -> Result<serde_json::Value, String> {
134-
match v {
135-
serde_yaml::Value::Null => Ok(serde_json::Value::Null),
136-
137-
serde_yaml::Value::Bool(b) => Ok(serde_json::Value::Bool(b)),
138-
139-
serde_yaml::Value::Number(num) => {
140-
Ok(serde_json::Value::Number(yaml_number_as_json(num.clone())))
141-
}
142-
143-
serde_yaml::Value::String(s) => Ok(serde_json::Value::String(s.clone())),
144-
145-
serde_yaml::Value::Sequence(seq) => {
146-
let array_elements = seq
147-
.into_iter()
148-
.map(|v| as_json_value(v).expect("invalid sequence element"));
149-
Ok(serde_json::Value::Array(array_elements.collect()))
150-
}
151-
152-
serde_yaml::Value::Mapping(map) => {
153-
let object_members = map.iter().map(|(k, v)| {
154-
if let serde_yaml::Value::String(k_string) = k {
155-
(
156-
k_string.clone(), //serde_json::to_string(k).expect("non-string mapping key"),
157-
as_json_value(v.clone()).expect("invalid map value"),
158-
)
159-
} else {
160-
panic!("non-string mapping key")
161-
}
162-
});
163-
Ok(serde_json::Value::Object(object_members.collect()))
164-
}
165-
}
166-
}
167-
168-
fn as_json_value_array(v: &serde_yaml::Value) -> Result<Vec<serde_json::Value>, String> {
169-
match v {
170-
serde_yaml::Value::Sequence(seq) => {
171-
let array_elements = seq
172-
.into_iter()
173-
.map(|v| as_json_value(v.clone()).expect("invalid sequence element"));
98+
serde_json::Value::Array(seq) => {
99+
let array_elements = seq.into_iter().map(|v| v.clone());
174100
Ok(array_elements.collect())
175101
}
176102
_ => Err("not a sequence".to_string()),
177103
}
178104
}
179-
180-
fn yaml_number_as_json(n: serde_yaml::Number) -> serde_json::Number {
181-
if n.is_i64() {
182-
serde_json::Number::from(n.as_i64().expect("invalid i64 in YAML"))
183-
} else if n.is_u64() {
184-
serde_json::Number::from(n.as_u64().expect("invalid u64 in YAML"))
185-
} else {
186-
serde_json::Number::from_f64(n.as_f64().expect("invalid f64 in YAML"))
187-
.expect("invalid f64 for JSON")
188-
}
189-
}
190105
}

tests/cts.yaml

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)