Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Array slices #22

Merged
merged 8 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pest = "2.1.3"
pest_derive = "2.1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.57"
slyce = "0.2.1"
18 changes: 17 additions & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,25 @@ test to `true`, for example:
</pre>

When one or more tests are focussed in this way, the test suite will fail with the message
"testcase(s) still focussed" even if all the tests pass.
"testcase(s) still focussed" if and only if all the focussed tests pass.
This prevents pull requests being merged in which tests are accidentally left focussed.

To skip one or more tests, edit [cts.json](tests/cts.json) and set the `skip` property of the relevant
test to `true`, for example:
<pre>
}, {
"name": "wildcarded child",
<b>"skip": true,</b>
"selector": "$.*",
"document": {"a" : "A", "b" : "B"},
"result": ["A", "B"]
}, {
</pre>

When one or more tests are skipped in this way, the test suite will fail with the message
"testcase(s) still skipped" if and only if all the tests pass and none are focussed.
This prevents pull requests being merged in which tests are accidentally left skipped.

To see details of which tests run, use:
```
cargo test -- --show-output
Expand Down
10 changes: 10 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

use serde_json::Value;
use slyce::Slice;
use std::iter;

/// A path is a tree of selector nodes.
///
Expand Down Expand Up @@ -57,6 +59,7 @@ pub enum Selector {
#[derive(Debug)]
pub enum UnionElement {
Name(String),
Slice(Slice),
Index(i64),
}

Expand Down Expand Up @@ -89,6 +92,13 @@ impl UnionElement {
pub fn get<'a>(&self, v: &'a Value) -> Iter<'a> {
match self {
UnionElement::Name(name) => Box::new(v.get(name).into_iter()),
UnionElement::Slice(slice) => {
if let Value::Array(arr) = v {
Box::new(slice.apply(arr))
} else {
Box::new(iter::empty())
}
}
UnionElement::Index(num) => Box::new(v.get(abs_index(*num, v)).into_iter()),
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ selector = _{ SOI ~ rootSelector ~ matchers ~ EOI }
matchers = ${ matcher* }
rootSelector = { "$" }

matcher = { dotChild | union }
matcher = !{ dotChild | union }

dotChild = _{ wildcardedDotChild | namedDotChild }
wildcardedDotChild = { ".*" }
Expand All @@ -18,9 +18,14 @@ char = {
}

union = { "[" ~ unionElement ~ ("," ~ unionElement)* ~ "]" }
unionElement = _{ unionChild | unionArrayIndex } // TODO: add unionArraySlice
unionChild = { doubleQuotedString | singleQuotedString }
unionArrayIndex = { "-" ? ~ ( "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* ) }
unionElement = _{ unionChild | unionArraySlice | unionArrayIndex }
unionChild = ${ doubleQuotedString | singleQuotedString }
unionArrayIndex = @{ integer }
integer = _{ "-" ? ~ ( "0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT* ) }
unionArraySlice = { sliceStart ? ~ ":" ~ sliceEnd ? ~ ( ":" ~ sliceStep ? ) ? }
sliceStart = @{ integer }
sliceEnd = @{ integer }
sliceStep = @{ integer }

doubleQuotedString = _{ "\"" ~ doubleInner ~ "\"" }
doubleInner = @{ doubleChar* }
Expand Down
31 changes: 31 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

pub use crate::ast::*;
use crate::pest::Parser;
use slyce::{Index, Slice};

#[derive(Parser)]
#[grammar = "grammar.pest"]
Expand Down Expand Up @@ -50,6 +51,7 @@ fn parse_union_indices(matcher_rule: pest::iterators::Pair<Rule>) -> Vec<UnionEl
.into_inner()
.map(|r| match r.as_rule() {
Rule::unionChild => parse_union_child(r),
Rule::unionArraySlice => parse_union_array_slice(r),
Rule::unionArrayIndex => parse_union_array_index(r),
_ => panic!("invalid parse tree {:?}", r),
})
Expand All @@ -71,6 +73,35 @@ fn parse_union_array_index(matcher_rule: pest::iterators::Pair<Rule>) -> UnionEl
UnionElement::Index(i)
}

fn parse_union_array_slice(matcher_rule: pest::iterators::Pair<Rule>) -> UnionElement {
let mut start: Option<isize> = None;
let mut end: Option<isize> = None;
let mut step: Option<isize> = None;
for r in matcher_rule.into_inner() {
match r.as_rule() {
Rule::sliceStart => {
start = Some(r.as_str().parse().unwrap());
}

Rule::sliceEnd => {
end = Some(r.as_str().parse().unwrap());
}

Rule::sliceStep => {
step = Some(r.as_str().parse().unwrap());
}

_ => panic!("invalid parse tree {:?}", r),
}
}

UnionElement::Slice(Slice {
start: start.map(Index::from).unwrap_or_default(),
end: end.map(Index::from).unwrap_or_default(),
step,
})
}

fn unescape(contents: &str) -> String {
let s = format!(r#""{}""#, contents);
serde_json::from_str(&s).unwrap()
Expand Down
194 changes: 193 additions & 1 deletion tests/cts.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,20 @@
"name": "union child, single quotes, incomplete escape",
"selector": "$['\\']",
"invalid_selector": true
}, {
"name": "union",
"selector": "$[0,2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 2]
}, {
"name": "union with whitespace",
"selector": "$[ 0 , 1 ]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 1]
}, {
"name": "empty union",
"selector": "$[]",
"invalid_selector": true
}, {
"name": "union array access",
"selector": "$[0]",
Expand Down Expand Up @@ -524,5 +538,183 @@
"name": "union array access, leading -0",
"selector": "$[-01]",
"invalid_selector": true
}
}, {
"name": "union array slice",
"selector": "$[1:3]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [1, 2]
}, {
"name": "union array slice with step",
"selector": "$[1:6:2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [1, 3, 5]
}, {
"name": "union array slice with everything omitted, short form",
"selector": "$[:]",
"document": [0, 1, 2, 3],
"result": [0, 1, 2, 3]
}, {
"name": "union array slice with everything omitted, long form",
"selector": "$[::]",
"document": [0, 1, 2, 3],
"result": [0, 1, 2, 3]
}, {
"name": "union array slice with start omitted",
"selector": "$[:2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 1]
}, {
"name": "union array slice with start and end omitted",
"selector": "$[::2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 2, 4, 6, 8]
}, {
"name": "union array slice, last index",
"selector": "$[-1]",
"document": [0, 1, 2, 3],
"result": [3]
}, {
"name": "union array slice, overflowed index",
"selector": "$[4]",
"document": [0, 1, 2, 3],
"result": []
}, {
"name": "union array slice, underflowed index",
"selector": "$[-5]",
"document": [0, 1, 2, 3],
"result": []
}, {
"name": "union array slice, negative step with default start and end",
"selector": "$[::-1]",
"document": [0, 1, 2, 3],
"result": [3, 2, 1, 0]
}, {
"name": "union array slice, negative step with default start",
"selector": "$[:0:-1]",
"document": [0, 1, 2, 3],
"result": [3, 2, 1]
}, {
"name": "union array slice, negative step with default end",
"selector": "$[2::-1]",
"document": [0, 1, 2, 3],
"result": [2, 1, 0]
}, {
"name": "union array slice, larger negative step",
"selector": "$[::-2]",
"document": [0, 1, 2, 3],
"result": [3, 1]
}, {
"name": "union array slice, negative range with default step",
"selector": "$[-1:-3]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": []
}, {
"name": "union array slice, negative range with negative step",
"selector": "$[-1:-3:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 8]
}, {
"name": "union array slice, negative range with larger negative step",
"selector": "$[-1:-6:-2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 7, 5]
}, {
"name": "union array slice, larger negative range with larger negative step",
"selector": "$[-1:-7:-2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 7, 5]
}, {
"name": "union array slice, negative from, positive to",
"selector": "$[-5:7]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [5, 6]
}, {
"name": "union array slice, negative from",
"selector": "$[-2:]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [8, 9]
}, {
"name": "union array slice, positive from, negative to",
"selector": "$[1:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [1, 2, 3, 4, 5, 6, 7, 8]
}, {
"name": "union array slice, negative from, positive to, negative step",
"selector": "$[-1:1:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 8, 7, 6, 5, 4, 3, 2]
}, {
"name": "union array slice, positive from, negative to, negative step",
"selector": "$[7:-5:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [7, 6]
}, {
"name": "union array slice, too many colons",
"selector": "$[1:2:3:4]",
"invalid_selector": true
}, {
"name": "union array slice, non-integer array index",
"selector": "$[1:2:a]",
"invalid_selector": true
}, {
"name": "union array slice, zero step",
"selector": "$[1:2:0]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": []
}, {
"name": "union array slice, empty range",
"selector": "$[2:2]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": []
}, {
"name": "union array slice, default indices with empty array",
"selector": "$[:]",
"document": [],
"result": []
}, {
"name": "union array slice, negative step with empty array",
"selector": "$[::-1]",
"document": [],
"result": []
}, {
"name": "union array slice, maximal range with positive step",
"selector": "$[0:10]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}, {
"name": "union array slice, maximal range with negative step",
"selector": "$[9:0:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 8, 7, 6, 5, 4, 3, 2, 1]
}, {
"name": "union array slice, excessively large to value",
"selector": "$[2:113667776004]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [2, 3, 4, 5, 6, 7, 8, 9]
}, {
"name": "union array slice, excessively small from value",
"selector": "$[-113667776004:1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [0]
}, {
"name": "union array slice, excessively large from value with negative step",
"selector": "$[113667776004:0:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9, 8, 7, 6, 5, 4, 3, 2, 1]
}, {
"name": "union array slice, excessively small to value with negative step",
"selector": "$[3:-113667776004:-1]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [3, 2, 1, 0]
}, {
"name": "union array slice, excessively large step",
"selector": "$[1:10:113667776004]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [1]
}, {
"name": "union array slice, excessively small step",
"selector": "$[-1:-10:-113667776004]",
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"result": [9]
}
]}
11 changes: 9 additions & 2 deletions tests/cts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mod tests {

#[serde(default)]
focus: bool, // if true, run only tests with focus set to true

#[serde(default)]
skip: bool, // if true, do not run this test
}

#[test]
Expand All @@ -42,10 +45,11 @@ mod tests {
serde_json::from_str(&cts_json).expect("failed to deserialize cts.json");

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

let mut errors: Vec<String> = Vec::new();
for t in suite.tests {
if focussed && !t.focus {
if t.skip || (focussed && !t.focus) {
continue;
}
let result = panic::catch_unwind(|| {
Expand Down Expand Up @@ -100,10 +104,13 @@ mod tests {
errors.push(format!("{:?}", err));
}
}
assert!(errors.is_empty());
assert!(errors.is_empty(), "testcase(s) failed, see above");
if focussed {
assert!(false, "testcase(s) still focussed")
}
if skipped {
assert!(false, "testcase(s) still skipped")
}
}

fn equal(actual: &Vec<&serde_json::Value>, expected: Vec<serde_json::Value>) -> bool {
Expand Down