Skip to content

Commit

Permalink
Add compliance specs to the implicit conversion. Fix some errors in f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
alextwoods committed Jan 5, 2022
1 parent fc025ab commit 761f3e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/jmespath/nodes/function.rb
Expand Up @@ -476,7 +476,7 @@ class SortByFunction < Function
def call(args)
if args.count == 2
if get_type(args[0]) == ARRAY_TYPE && get_type(args[1]) == EXPRESSION_TYPE
values = args[0]
values = args[0].to_ary
expression = args[1]
array_type = get_type(expression.eval(values[0]))
if array_type == STRING_TYPE || array_type == NUMBER_TYPE || values.size == 0
Expand Down Expand Up @@ -509,7 +509,7 @@ module CompareBy

def compare_by(mode, *args)
if args.count == 2
values = args[0]
values = args[0].to_ary
expression = args[1]
if get_type(values) == ARRAY_TYPE && get_type(expression) == EXPRESSION_TYPE
type = get_type(expression.eval(values.first))
Expand Down
39 changes: 39 additions & 0 deletions spec/implicit_conversion_spec.rb
Expand Up @@ -4,6 +4,18 @@ module Wrapper
def self.wrap(o)
o.respond_to?(:to_ary) ? Arrayish.new(o) : o.respond_to?(:to_hash) ? Hashish.new(o) : o
end

def self.unwrap(o)
if o.respond_to?(:to_ary)
o.to_ary.map { |i| Wrapper.unwrap(i) }
elsif o.respond_to?(:to_hash)
to_hash = {}
o.to_hash.each_pair { |k, v| to_hash[k] = Wrapper.unwrap(v) }
to_hash
else
o
end
end
end

class Arrayish
Expand Down Expand Up @@ -52,7 +64,34 @@ module JMESPath
expect(result[1]).to be_instance_of(Hashish)
expect(result[1].hash).to eq({'baz' => 0})
end
end

describe 'Compliance' do
Dir.glob('spec/{compliance,legacy}/*.json').each do |path|

test_file = File.basename(path).split('.').first
next if test_file == 'benchmarks'
next if ENV['TEST_FILE'] && ENV['TEST_FILE'] != test_file

describe(test_file) do
JMESPath.load_json(path).each do |scenario|
describe("Given #{scenario['given'].to_json}") do
scenario['cases'].each do |test_case|

if !test_case['error']
it "searching #{test_case['expression'].inspect} returns #{test_case['result'].to_json}" do
result = JMESPath.search(test_case['expression'], Wrapper.wrap(scenario['given']))

expect(Wrapper.unwrap(result)).to eq(test_case['result'])
end

end
end
end
end
end
end
end

end
end

0 comments on commit 761f3e5

Please sign in to comment.