From a3dcb31033b28f9c38b7cd3d6732e80901a483b6 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 4 Jan 2022 23:55:16 -0800 Subject: [PATCH] test handling of objects which are implicitly convertible to hash or array --- spec/implicit_conversion_spec.rb | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 spec/implicit_conversion_spec.rb diff --git a/spec/implicit_conversion_spec.rb b/spec/implicit_conversion_spec.rb new file mode 100644 index 0000000..a08c993 --- /dev/null +++ b/spec/implicit_conversion_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +module Wrapper + def self.wrap(o) + o.respond_to?(:to_ary) ? Arrayish.new(o) : o.respond_to?(:to_hash) ? Hashish.new(o) : o + end +end + +class Arrayish + def initialize(ary) + @ary = ary.to_ary + end + + attr_reader :ary + + def to_ary + @ary.map { |e| Wrapper.wrap(e) } + end +end + +class Hashish + def initialize(hash) + @hash = hash.to_hash + end + + attr_reader :hash + + def to_hash + to_hash = {} + @hash.each_pair { |k, v| to_hash[k] = Wrapper.wrap(v) } + to_hash + end +end + +module JMESPath + describe '.search' do + describe 'implicit conversion' do + + it 'searches hash/array structures' do + data = Hashish.new({'foo' => {'bar' => ['value']}}) + result = JMESPath.search('foo.bar', data) + expect(result).to be_instance_of(Arrayish) + expect(result.ary).to eq(['value']) + end + + it 'searches with flatten' do + data = Hashish.new({'foo' => [[{'bar' => 0}], [{'baz' => 0}]]}) + result = JMESPath.search('foo[]', data) + expect(result.size).to eq(2) + expect(result[0]).to be_instance_of(Hashish) + expect(result[0].hash).to eq({'bar' => 0}) + expect(result[1]).to be_instance_of(Hashish) + expect(result[1].hash).to eq({'baz' => 0}) + end + + end + end +end