Skip to content

Commit

Permalink
Update the bundled compliance tests fixing errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorrowe committed Apr 4, 2018
1 parent 8db134d commit 65fa47d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased Changes
------------------

* Update the bundled compliance tests. Fix the 6 failing test cases that result
from updating the test suite. Test failures included parsing errors and
returning nil when comparing non-nil values.

* Fix typo of a license name in gemspec.

[See related GitHub pull request #41](https://github.com/jmespath/jmespath.rb/pull/41)
Expand Down
24 changes: 20 additions & 4 deletions lib/jmespath/nodes/comparator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,41 @@ def check(left_value, right_value)

class Gt < Comparator
def check(left_value, right_value)
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value > right_value
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
left_value > right_value
else
nil
end
end
end

class Gte < Comparator
def check(left_value, right_value)
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value >= right_value
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
left_value >= right_value
else
nil
end
end
end

class Lt < Comparator
def check(left_value, right_value)
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value < right_value
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
left_value < right_value
else
nil
end
end
end

class Lte < Comparator
def check(left_value, right_value)
left_value.is_a?(Numeric) && right_value.is_a?(Numeric) && left_value <= right_value
if left_value.is_a?(Numeric) && right_value.is_a?(Numeric)
left_value <= right_value
else
nil
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/jmespath/nodes/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def call(args)
return maybe_raise Errors::InvalidArityError, "function avg() expects one argument"
end
if Array === values
return nil if values.empty?
values.inject(0) do |total,n|
if Numeric === n
total + n
Expand Down
2 changes: 1 addition & 1 deletion lib/jmespath/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def method_missing(method_name, *args)
# @param [Integer] rbp Right binding power
def expr(stream, rbp = 0)
left = send("nud_#{stream.token.type}", stream)
while rbp < stream.token.binding_power
while rbp < (stream.token.binding_power || 0)
left = send("led_#{stream.token.type}", stream, left)
end
left
Expand Down
26 changes: 20 additions & 6 deletions spec/compliance/benchmarks.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,36 @@
}
}
}
},
"b": true,
"c": {
"d": true
}
},
"cases": [
{
"comment": "simple field",
"expression": "a",
"expression": "b",
"bench": "full"
},
{
"comment": "simple subexpression",
"expression": "a.b",
"expression": "c.d",
"bench": "full"
},
{
"comment": "deep field selection",
"comment": "deep field selection no match",
"expression": "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s",
"bench": "full"
},
{
"comment": "deep field selection",
"expression": "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p",
"bench": "full"
},
{
"comment": "simple or",
"expression": "not_there || a",
"expression": "not_there || b",
"bench": "full"
}
]
Expand All @@ -76,7 +85,12 @@
},
{
"comment": "lots of summing",
"expression": "sum(z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a)",
"expression": "sum([z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a])",
"bench": "full"
},
{
"comment": "lots of function application",
"expression": "sum([z, sum([y, sum([x, sum([w, sum([v, sum([u, sum([t, sum([s, sum([r, sum([q, sum([p, sum([o, sum([n, sum([m, sum([l, sum([k, sum([j, sum([i, sum([h, sum([g, sum([f, sum([e, sum([d, sum([c, sum([b, a])])])])])])])])])])])])])])])])])])])])])])])])])",
"bench": "full"
},
{
Expand Down Expand Up @@ -116,7 +130,7 @@
},
{
"comment": "filter projection",
"expression": "foo[bar > baz][qux > baz]",
"expression": "foo[?bar > baz][?qux > baz]",
"bench": "parse"
}
]
Expand Down
40 changes: 14 additions & 26 deletions spec/compliance/boolean.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
"one": 1,
"two": 2,
"three": 3,
"one_dot_zero": 1.0,
"two_dot_zero": 2.0
"emptylist": [],
"boolvalue": false
},
"cases": [
{
Expand Down Expand Up @@ -239,48 +239,36 @@
"result": true
},
{
"expression": "one < two && three > one",
"result": true
},
{
"expression": "one < two || three > one",
"result": true
"expression": "emptylist < one",
"result": null
},
{
"expression": "one < two || three < one",
"result": true
"expression": "emptylist < nullvalue",
"result": null
},
{
"expression": "two < one || three < one",
"result": false
"expression": "emptylist < boolvalue",
"result": null
},
{
"expression": "one_dot_zero < two_dot_zero",
"result": true
"expression": "one < boolvalue",
"result": null
},
{
"expression": "two_dot_zero > one_dot_zero",
"expression": "one < two && three > one",
"result": true
},
{
"expression": "one_dot_zero <= one_dot_zero",
"expression": "one < two || three > one",
"result": true
},
{
"expression": "one_dot_zero >= one_dot_zero",
"expression": "one < two || three < one",
"result": true
},
{
"expression": "one_dot_zero == two_dot_zero",
"expression": "two < one || three < one",
"result": false
},
{
"expression": "one_dot_zero != two_dot_zero",
"result": true
},
{
"expression": "one_dot_zero == one_dot_zero",
"result": true
}
]
}
Expand Down
18 changes: 11 additions & 7 deletions spec/compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"expression": "avg(strings)",
"error": "invalid-type"
},
{
"expression": "avg(empty_list)",
"result": null
},
{
"expression": "ceil(`1.2`)",
"result": 2
Expand Down Expand Up @@ -572,12 +576,12 @@
"error": "invalid-arity"
},
{
"description": "function projection on single arg function",
"comment": "function projection on single arg function",
"expression": "numbers[].to_string(@)",
"result": ["-1", "3", "4", "5"]
},
{
"description": "function projection on single arg function",
"comment": "function projection on single arg function",
"expression": "array[].to_number(@)",
"result": [-1, 3, 4, 5, 100]
}
Expand All @@ -595,7 +599,7 @@
},
"cases": [
{
"description": "function projection on variadic function",
"comment": "function projection on variadic function",
"expression": "foo[].not_null(f, e, d, c, b, a)",
"result": ["b", "c", "d", "e", "f"]
}
Expand All @@ -613,7 +617,7 @@
},
"cases": [
{
"description": "sort by field expression",
"comment": "sort by field expression",
"expression": "sort_by(people, &age)",
"result": [
{"age": 10, "age_str": "10", "bool": true, "name": 3},
Expand All @@ -634,7 +638,7 @@
]
},
{
"description": "sort by function expression",
"comment": "sort by function expression",
"expression": "sort_by(people, &to_number(age_str))",
"result": [
{"age": 10, "age_str": "10", "bool": true, "name": 3},
Expand All @@ -645,7 +649,7 @@
]
},
{
"description": "function projection on sort_by function",
"comment": "function projection on sort_by function",
"expression": "sort_by(people, &age)[].name",
"result": [3, "a", "c", "b", "d"]
},
Expand Down Expand Up @@ -733,7 +737,7 @@
},
"cases": [
{
"description": "stable sort order",
"comment": "stable sort order",
"expression": "sort_by(people, &age)",
"result": [
{"age": 10, "order": "1"},
Expand Down
10 changes: 10 additions & 0 deletions spec/compliance/literal.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@
"comment": "Can escape the single quote",
"expression": "'foo\\'bar'",
"result": "foo'bar"
},
{
"comment": "Backslash not followed by single quote is treated as any other character",
"expression": "'\\z'",
"result": "\\z"
},
{
"comment": "Backslash not followed by single quote is treated as any other character",
"expression": "'\\\\'",
"result": "\\\\"
}
]
}
Expand Down
8 changes: 8 additions & 0 deletions spec/compliance/syntax.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@
{
"expression": "!",
"error": "syntax"
},
{
"expression": "@=",
"error": "syntax"
},
{
"expression": "@``",
"error": "syntax"
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions tasks/test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ $RSPEC_OPTS = ENV['VERBOSE'] ? ' --format doc --color' : ''
desc "Run unit tests"
RSpec::Core::RakeTask.new('test:unit') do |t|
t.rspec_opts = "-I lib#{$RSPEC_OPTS}"
t.exclude_pattern = 'spec/compliance_spec.rb'
t.exclude_pattern = 'spec/compliance_*.rb'
end

desc "Run compliance tests"
RSpec::Core::RakeTask.new('test:compliance') do |t|
t.rspec_opts = "-I lib#{$RSPEC_OPTS}"
t.pattern = 'compliance_spec.rb'
t.pattern = 'spec/compliance_*.rb'
end

task 'test' => ['test:unit', 'test:compliance']

0 comments on commit 65fa47d

Please sign in to comment.