-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
searcher_spec.rb
208 lines (174 loc) · 5.18 KB
/
searcher_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require_relative "support/helper"
class Collection
def initialize(ary, rspec)
@ary = ary
@rspec = rspec
end
def member_by(key, value)
@rspec.expect(key).to @rspec.eql("id")
@rspec.expect(value).to @rspec.eql(1)
@ary.last
end
end
RSpec.describe "Searcher" do
it "searching for a child node returns the proc, and refined found options" do
json = Props::Searcher.new(nil, ["outer", "inner", "hit"])
target_proc = proc {}
target_opts = {some_options: 1}
json.set!("outer") do
json.set!("inner") do
json.set!("hit", target_opts, &target_proc)
end
end
found_block, found_options = json.found!
expect(found_block).to eql(target_proc)
expect(found_options).to eql({
some_options: 1,
path_suffix: ["inner", "hit"]
})
end
it "searching with an empty path means you found nothing" do
json = Props::Searcher.new(nil, [])
target_proc = proc do
json.set!("inner") do
json.set!("hit", {}, &target_proc)
end
end
json.set!("outer", {}, &target_proc)
found_block, _found_options = json.found!
expect(found_block).to be_nil
end
it "searching for a child node with siblings in back" do
json = Props::Searcher.new(nil, ["outer", "inner"])
target_proc = proc do
json.set!("foo", 32)
end
json.set!("outer") do
json.set!("inner", {}, &target_proc)
json.set!("bad") do
json.set!("foo", "should not touch")
end
end
found_block, _found_options = json.found!
expect(found_block).to eql(target_proc)
end
it "searching for a child node with siblings in front" do
json = Props::Searcher.new(nil, ["outer", "inner"])
target_proc = proc do
json.foo 32
end
json.set!("outer") do
json.set!("bad") do
json.set!("foo", "should not touch")
end
json.set!("inner", {}, &target_proc)
end
found_block, _found_options = json.found!
expect(found_block).to eql(target_proc)
end
it "searching for a subtree" do
json = Props::Searcher.new(nil, ["outer", "inner", "deep"])
target_proc = proc do
json.set!("deeper") do
json.set!("foo", 32)
end
end
json.set!("outer") do
json.set!("inner") do
json.set!("deep", {}, &target_proc)
end
end
found_block, _found_options = json.found!
expect(found_block).to eql(target_proc)
end
it "searching for a leaf node is unsupported" do
json = Props::Searcher.new(nil, ["outer", "inner", "foo"])
json.set!("outer") do
json.set!("inner") do
json.set!("foo", 32)
end
end
found_block, _found_options = json.found!
expect(found_block).to be_nil
end
it "searching for a node beyond whats available is equivalent to not finding anything" do
json = Props::Searcher.new(nil, ["outer", "inner", "a", "b"])
json.set!("outer") do
json.set!("inner") do
json.set!("foo", 32)
end
end
found_block, _found_options = json.found!
expect(found_block).to be_nil
end
it "searching for an item inside an array, includes refined found options" do
json = Props::Searcher.new(nil, ["outer", 1])
target_opts = {some_options: 1}
json.set!("outer") do
json.array!(["hello", "world"], target_opts) do |item|
item
end
end
found_block, found_options = json.found!
expect(found_options).to eql({some_options: 1, path_suffix: [1]})
expect(found_block.call).to eql("world")
end
it "searching for an item inside an array using an id=val keypath" do
json = Props::Searcher.new(nil, ["outer", "id=1"])
collection = Collection.new(
[{id: 0}, {id: 1}],
self
)
json.set!("outer") do
json.array!(collection) do |item|
item
end
end
found_block, _found_options = json.found!
expect(found_block.call).to eql({id: 1})
end
it "searching for an node belonging an array" do
json = Props::Searcher.new(nil, ["outer", "inner", 1, "foo"])
json.set!("outer") do
json.set!("inner") do
json.array! ["hello", "world"] do |item|
json.set!("foo") do
item
end
end
end
end
found_block, _found_options = json.found!
expect(found_block.call).to eql("world")
end
it "searching for an node outside the length of the array, is equivalent to not finding anything" do
json = Props::Searcher.new(nil, ["outer", "inner", 10, "foo"])
json.set!("outer") do
json.set!("inner") do
json.array! [1, 2] do |item|
json.set!("foo") do
json.set!("bar", item)
end
end
end
end
found_block, _found_options = json.found!
expect(found_block).to be_nil
end
it "searching for object inside a nested array" do
json = Props::Searcher.new(nil, ["outer", "inner", 1, "foo", 0])
json.set!("outer") do
json.set!("inner") do
json.array! [0, 1] do |item|
json.set!("foo") do
json.array! ["hello", "world"] do |inner_item|
inner_item
end
end
end
end
end
found_block, _found_options = json.found!
expect(found_block.call).to eql("hello")
end
end