Skip to content

Commit

Permalink
Working tests, refactor needed
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Dec 27, 2023
1 parent 7261719 commit 826bb97
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
37 changes: 31 additions & 6 deletions lib/src/fun/fun_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,37 @@ class FunFactory {

Expression<T> _any1<T extends Object>(String name, Expression a0) {
final f = _getFun1<T>(name);
final cast0 = cast(value: f is Fun1<T, Maybe>, logical: f is Fun1<T, bool>);
return a0.map(cast0).map(f.call);
if (f is Fun1<T, Maybe>) {
if (a0 is Expression<Maybe>) {
return a0.map(f.call);
}
if (a0 is Expression<SingularNodeList>) {
return a0.map((v) => v.asValue).map(f.call);
}
}
if (f is Fun1<T, bool>) {
if (a0 is Expression<bool>) {
return a0.map(f.call);
}
if (a0 is Expression<SingularNodeList>) {
return a0.map((v) => v.asLogical).map(f.call);
}
}
if (f is Fun1<T, NodeList>) {
if (a0 is Expression<NodeList>) {
return a0.map(f.call);
}
}
throw Exception('Fun arg mismatch');
}

Expression<T> _any2<T extends Object>(
String name, Expression a0, Expression a1) {
final f = _getFun2<T>(name);
final cast0 = cast(
final cast0 = cast(a0,
value: f is Fun2<T, Maybe, Object>,
logical: f is Fun2<T, bool, Object>);
final cast1 = cast(
final cast1 = cast(a1,
value: f is Fun2<T, Object, Maybe>,
logical: f is Fun2<T, Object, bool>);
return a0.map(cast0).merge(a1.map(cast1), f.call);
Expand All @@ -76,9 +96,14 @@ class FunFactory {
throw StateError('Function "$name" of 2 arguments is not found');
}

static Object Function(Object) cast(
static Object Function(Object) cast(Expression arg,
{required bool value, required bool logical}) {
if (value) return _value;
if (value) {
if (arg is! Expression<Maybe> && arg is! Expression<SingularNodeList>) {
throw Exception('Arg type mismatch');
}
return _value;
}
if (logical) return _logical;
return _nodes;
}
Expand Down
10 changes: 6 additions & 4 deletions lib/src/grammar/json_path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class JsonPathGrammarDefinition

Parser<Expression> _funArgument() => [
literal,
_filterPath(),
ref0(_singularFilterPath),
ref0(_filterPath),
ref0(_valueFunExpr),
ref0(_logicalFunExpr),
ref0(_nodesFunExpr),
Expand Down Expand Up @@ -113,7 +114,7 @@ class JsonPathGrammarDefinition
ref0(_absPath),
].toChoiceParser();

Parser<Expression<NodeList>> _singularFilterPath() => [
Parser<Expression<SingularNodeList>> _singularFilterPath() => [
ref0(_singularRelPath),
ref0(_singularAbsPath),
].toChoiceParser();
Expand Down Expand Up @@ -156,12 +157,13 @@ class JsonPathGrammarDefinition

Parser<Expression<SingularNodeList>> _singularAbsPath() =>
_singularSegmentSequence()
.skip(before: char(r'$'))
.skip(before: char(r'$'), after: _segment().not())
.map((expr) => Expression((node) => expr.call(node.root)));

Parser<Expression<NodeList>> _relPath() =>
_segmentSequence().skip(before: char('@'));

Parser<Expression<SingularNodeList>> _singularRelPath() =>
_singularSegmentSequence().skip(before: char('@'));
_singularSegmentSequence()
.skip(before: char('@'), after: _segment().not());
}
78 changes: 73 additions & 5 deletions test/cases/extra/cases.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{
"tests": [
{
"name": "reverse()",
"name": "reverse(@)",
"selector" : "$[?reverse(@)=='cba']",
"document" : ["abc", "cba"],
"result": ["abc"]
},
{
"name": "reverse(reverse())",
"name": "reverse(reverse(@))",
"selector" : "$[?reverse(reverse(@))=='cba']",
"document" : ["abc", "cba"],
"result": ["cba"]
},
{
"name": "reverse(reverse())",
"name": "count(siblings(@))",
"selector" : "$..[?count(siblings(@)) == 1]",
"document" : {"a": {"b": "x", "d": "x"}},
"result": ["x", "x"]
},
{
"name": "is_object",
"name": "is_object(@)",
"selector" : "$[?is_object(@)]",
"document" : [1, true, {}, [42], "foo", {"a": "b"}],
"result": [{}, {"a": "b"}]
},
{
"name": "is_array",
"name": "is_array(@)",
"selector" : "$[?is_array(@)]",
"document" : [1, true, {}, [42], "foo", {"a": "b"}],
"result": [[42]]
Expand Down Expand Up @@ -53,6 +53,74 @@
"selector" : "$[?xor((@.b), (@.a))]",
"document" : [{"a": 0}, {"a": 0, "b": 0}, {"b": 0}, {}],
"result": [{"a": 0}, {"b": 0}]
},
{
"name": "functions, length, non-singular query arg",
"selector": "$[?length(@.*)<3]",
"invalid_selector": true
},
{
"name": "functions, length, arg is a function expression",
"selector": "$.values[?length(@.a)==length(value($..c))]",
"document": {
"c": "cd",
"values": [
{
"a": "ab"
},
{
"a": "d"
}
]
},
"result": [
{
"a": "ab"
}
]
}, {
"name": "functions, match, arg is a function expression",
"selector": "$.values[?match(@.a, value($..['regex']))]",
"document": {
"regex": "a.*",
"values": [
{
"a": "ab"
},
{
"a": "ba"
}
]
},
"result": [
{
"a": "ab"
}
]
},
{
"name": "functions, search, arg is a function expression",
"selector": "$.values[?search(@, value($..['regex']))]",
"document": {
"regex": "b.?b",
"values": [
"abc",
"bcd",
"bab",
"bba",
"bbab",
"b",
true,
[],
{}
]
},
"result": [
"bab",
"bba",
"bbab"
]
}

]
}
2 changes: 2 additions & 0 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ void main() {
});
});
}


0 comments on commit 826bb97

Please sign in to comment.