File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ require 'spec_helper'
2+
3+ module Wrapper
4+ def self . wrap ( o )
5+ o . respond_to? ( :to_ary ) ? Arrayish . new ( o ) : o . respond_to? ( :to_hash ) ? Hashish . new ( o ) : o
6+ end
7+ end
8+
9+ class Arrayish
10+ def initialize ( ary )
11+ @ary = ary . to_ary
12+ end
13+
14+ attr_reader :ary
15+
16+ def to_ary
17+ @ary . map { |e | Wrapper . wrap ( e ) }
18+ end
19+ end
20+
21+ class Hashish
22+ def initialize ( hash )
23+ @hash = hash . to_hash
24+ end
25+
26+ attr_reader :hash
27+
28+ def to_hash
29+ to_hash = { }
30+ @hash . each_pair { |k , v | to_hash [ k ] = Wrapper . wrap ( v ) }
31+ to_hash
32+ end
33+ end
34+
35+ module JMESPath
36+ describe '.search' do
37+ describe 'implicit conversion' do
38+
39+ it 'searches hash/array structures' do
40+ data = Hashish . new ( { 'foo' => { 'bar' => [ 'value' ] } } )
41+ result = JMESPath . search ( 'foo.bar' , data )
42+ expect ( result ) . to be_instance_of ( Arrayish )
43+ expect ( result . ary ) . to eq ( [ 'value' ] )
44+ end
45+
46+ it 'searches with flatten' do
47+ data = Hashish . new ( { 'foo' => [ [ { 'bar' => 0 } ] , [ { 'baz' => 0 } ] ] } )
48+ result = JMESPath . search ( 'foo[]' , data )
49+ expect ( result . size ) . to eq ( 2 )
50+ expect ( result [ 0 ] ) . to be_instance_of ( Hashish )
51+ expect ( result [ 0 ] . hash ) . to eq ( { 'bar' => 0 } )
52+ expect ( result [ 1 ] ) . to be_instance_of ( Hashish )
53+ expect ( result [ 1 ] . hash ) . to eq ( { 'baz' => 0 } )
54+ end
55+
56+ end
57+ end
58+ end
You can’t perform that action at this time.
0 commit comments