kronk 1.8.5 → 1.8.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,371 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestPathMatcher < Test::Unit::TestCase
4
-
5
- def setup
6
- @matcher = Kronk::Path::Matcher.new :key => "foo*", :value => "*bar*"
7
- @data = {
8
- :key1 => {
9
- :key1a => [
10
- "foo",
11
- "bar",
12
- "foobar",
13
- {:findme => "thing"}
14
- ],
15
- 'key1b' => "findme"
16
- },
17
- 'findme' => [
18
- 123,
19
- 456,
20
- {:findme => 123456}
21
- ],
22
- :key2 => "foobar",
23
- :key3 => {
24
- :key3a => ["val1", "val2", "val3"]
25
- }
26
- }
27
- end
28
-
29
-
30
- def test_new
31
- assert_equal %r{\A(?:foo(.*))\Z}, @matcher.key
32
- assert_equal %r{\A(?:(.*)bar(.*))\Z}, @matcher.value
33
- assert !@matcher.recursive
34
- end
35
-
36
-
37
-
38
- def test_each_data_item_hash
39
- hash = {
40
- :a => 1,
41
- :b => 2,
42
- :c => 3
43
- }
44
-
45
- keys = []
46
- values = []
47
-
48
- @matcher.each_data_item hash do |key, val|
49
- keys << key
50
- values << val
51
- end
52
-
53
- assert_equal keys, (keys | hash.keys)
54
- assert_equal values, (values | hash.values)
55
- end
56
-
57
-
58
- def test_each_data_item_array
59
- ary = [:a, :b, :c]
60
-
61
- keys = []
62
- values = []
63
-
64
- @matcher.each_data_item ary do |key, val|
65
- keys << key
66
- values << val
67
- end
68
-
69
- assert_equal [2,1,0], keys
70
- assert_equal ary.reverse, values
71
- end
72
-
73
-
74
- def test_match_node
75
- assert @matcher.match_node(:key, "key")
76
- assert @matcher.match_node("key", :key)
77
-
78
- assert @matcher.match_node(/key/, "foo_key")
79
- assert !@matcher.match_node("foo_key", /key/)
80
- assert @matcher.match_node(/key/, /key/)
81
-
82
- assert @matcher.match_node(Kronk::Path::Matcher::ANY_VALUE, "foo_key")
83
- assert !@matcher.match_node("foo_key", Kronk::Path::Matcher::ANY_VALUE)
84
-
85
- assert @matcher.match_node(1..3, 1)
86
- assert !@matcher.match_node(1, 1..3)
87
- assert @matcher.match_node(1..3, 1..3)
88
- end
89
-
90
-
91
- def test_find_in
92
- keys = []
93
-
94
- Kronk::Path::Matcher.new(:key => /key/).find_in @data do |data, key|
95
- keys << key.to_s
96
- assert_equal @data, data
97
- end
98
-
99
- assert_equal ['key1', 'key2', 'key3'], keys.sort
100
- end
101
-
102
-
103
- def test_find_in_recursive
104
- keys = []
105
- data_points = []
106
-
107
- matcher = Kronk::Path::Matcher.new :key => :findme,
108
- :recursive => true
109
-
110
- path_matches = matcher.find_in @data do |data, key|
111
- keys << key.to_s
112
- data_points << data
113
- end
114
-
115
- assert path_matches.find{|pm| pm.splat == [[matcher, [:key1, :key1a, 3]]]}
116
- assert_equal 3, keys.length
117
- assert_equal 1, keys.uniq.length
118
- assert_equal "findme", keys.first
119
-
120
- assert_equal 3, data_points.length
121
- assert data_points.include?(@data)
122
- assert data_points.include?({:findme => "thing"})
123
- assert data_points.include?({:findme => 123456})
124
- end
125
-
126
-
127
- def test_find_in_value
128
- keys = []
129
- data_points = []
130
-
131
- matcher = Kronk::Path::Matcher.new :key => "*", :value => "findme"
132
- matcher.find_in @data do |data, key|
133
- keys << key.to_s
134
- data_points << data
135
- end
136
-
137
- assert keys.empty?
138
- assert data_points.empty?
139
-
140
- matcher = Kronk::Path::Matcher.new :key => "*",
141
- :value => "findme",
142
- :recursive => true
143
-
144
- paths = matcher.find_in @data do |data, key|
145
- keys << key.to_s
146
- data_points << data
147
- end
148
-
149
- assert_equal ['key1b'], keys
150
- assert_equal [@data[:key1]], data_points
151
- assert_equal ['key1b'], paths.first.matches
152
- end
153
-
154
-
155
- def test_find_in_match
156
- matcher = Kronk::Path::Matcher.new :key => "find*",
157
- :value => "th*g",
158
- :recursive => true
159
- paths = matcher.find_in @data
160
- assert_equal [[:key1, :key1a, 3, :findme]], paths
161
- assert_equal Kronk::Path::Match, paths.first.class
162
-
163
- assert_equal ["me", "in"], paths.first.matches
164
- end
165
-
166
-
167
- def test_find_in_match_one
168
- matcher = Kronk::Path::Matcher.new :key => "findme|foo",
169
- :recursive => true
170
- paths = matcher.find_in @data
171
-
172
- expected_paths = [
173
- ["findme"],
174
- ["findme", 2, :findme],
175
- [:key1, :key1a, 3, :findme]
176
- ]
177
-
178
- assert_equal expected_paths, (expected_paths | paths)
179
- assert_equal Kronk::Path::Match, paths.first.class
180
-
181
- assert_equal ["findme"], paths.first.matches
182
- end
183
-
184
-
185
- def test_find_in_match_one_value
186
- matcher = Kronk::Path::Matcher.new :key => "findme|foo",
187
- :value => "th*g",
188
- :recursive => true
189
- paths = matcher.find_in @data
190
- assert_equal [[:key1, :key1a, 3, :findme]], paths
191
- assert_equal Kronk::Path::Match, paths.first.class
192
-
193
- assert_equal ["findme", "in"], paths.first.matches
194
- end
195
-
196
-
197
- def test_find_in_match_any
198
- matcher = Kronk::Path::Matcher.new :key => "*"
199
- paths = matcher.find_in @data
200
-
201
- expected_paths = [
202
- ["findme"],
203
- [:key1],
204
- [:key2],
205
- [:key3]
206
- ]
207
-
208
- paths.each do |path|
209
- assert path.splat.empty?,
210
- "Expected empty splat for #{path.inspect} but got #{path.splat.inspect}"
211
- end
212
-
213
- assert_equal expected_paths, (expected_paths | paths)
214
- assert_equal Kronk::Path::Match, paths.first.class
215
- assert_equal expected_paths, (expected_paths | paths.map{|p| p.matches})
216
- end
217
-
218
-
219
- def test_find_in_match_value_only
220
- matcher = Kronk::Path::Matcher.new :value => "th*g",
221
- :recursive => true
222
-
223
- paths = matcher.find_in @data
224
-
225
- assert_equal [[:key1, :key1a, 3, :findme]], paths
226
- assert_equal ["in"], paths.first.matches
227
- end
228
-
229
-
230
- def test_find_in_match_value_and_nil_key
231
- matcher = Kronk::Path::Matcher.new :key => nil,
232
- :value => "th*g",
233
- :recursive => true
234
-
235
- paths = matcher.find_in @data
236
-
237
- assert_equal [[:key1, :key1a, 3, :findme]], paths
238
- assert_equal ["in"], paths.first.matches
239
- end
240
-
241
-
242
- def test_find_in_match_value_and_empty_key
243
- matcher = Kronk::Path::Matcher.new :key => "",
244
- :value => "th*g",
245
- :recursive => true
246
-
247
- paths = matcher.find_in @data
248
-
249
- assert_equal [[:key1, :key1a, 3, :findme]], paths
250
- assert_equal ["in"], paths.first.matches
251
- end
252
-
253
-
254
- def test_find_in_match_value_and_nil_value
255
- matcher = Kronk::Path::Matcher.new :key => "*3a",
256
- :value => nil,
257
- :recursive => true
258
-
259
- paths = matcher.find_in @data
260
-
261
- assert_equal [[:key3, :key3a]], paths
262
- assert_equal ["key"], paths.first.matches
263
- end
264
-
265
-
266
- def test_find_in_match_value_and_empty_value
267
- matcher = Kronk::Path::Matcher.new :key => "*3a",
268
- :value => "",
269
- :recursive => true
270
-
271
- paths = matcher.find_in @data
272
-
273
- assert_equal [[:key3, :key3a]], paths
274
- assert_equal ["key"], paths.first.matches
275
- end
276
-
277
-
278
- def test_find_in_match_splat
279
- matcher = Kronk::Path::Matcher.new :key => "findme",
280
- :recursive => true
281
-
282
- matches = matcher.find_in @data
283
-
284
- splat_i = matches.index [:key1, :key1a, 3, :findme]
285
- assert_equal [:key1, :key1a, 3], matches[splat_i].splat[0][1]
286
-
287
- splat_i = matches.index ["findme"]
288
- assert_equal [], matches[splat_i].splat[0][1]
289
-
290
- splat_i = matches.index ["findme", 2, :findme]
291
- assert_equal ["findme", 2], matches[splat_i].splat[0][1]
292
- end
293
-
294
-
295
- def test_find_in_match_splat_value
296
- matcher = Kronk::Path::Matcher.new :value => "foobar",
297
- :recursive => true
298
-
299
- matches = matcher.find_in @data
300
-
301
- assert(matches.any?{|m|
302
- m == [:key1, :key1a, 2] && assert_equal([:key1, :key1a, 2], m.splat[0][1])
303
- true
304
- })
305
-
306
- assert(matches.any?{|m|
307
- m == [:key2] && assert_equal([:key2], m.splat[0][1])
308
- true
309
- })
310
- end
311
-
312
-
313
- def test_parse_node_range
314
- assert_equal 1..4, @matcher.parse_node("1..4")
315
- assert_equal 1...4, @matcher.parse_node("1...4")
316
- assert_equal "1..4", @matcher.parse_node("\\1..4")
317
- assert_equal "1..4", @matcher.parse_node("1\\..4")
318
- assert_equal "1..4", @matcher.parse_node("1.\\.4")
319
- assert_equal "1..4", @matcher.parse_node("1..\\4")
320
- assert_equal "1..4", @matcher.parse_node("1..4\\")
321
- end
322
-
323
-
324
- def test_parse_node_index_length
325
- assert_equal 2...6, @matcher.parse_node("2,4")
326
- assert_equal "2,4", @matcher.parse_node("\\2,4")
327
- assert_equal "2,4", @matcher.parse_node("2\\,4")
328
- assert_equal "2,4", @matcher.parse_node("2,\\4")
329
- assert_equal "2,4", @matcher.parse_node("2,4\\")
330
- end
331
-
332
-
333
- def test_parse_node_anyval
334
- assert_equal Kronk::Path::Matcher::ANY_VALUE, @matcher.parse_node("*")
335
- assert_equal Kronk::Path::Matcher::ANY_VALUE, @matcher.parse_node("")
336
- assert_equal Kronk::Path::Matcher::ANY_VALUE, @matcher.parse_node("**?*?*?")
337
- assert_equal Kronk::Path::Matcher::ANY_VALUE, @matcher.parse_node(nil)
338
- end
339
-
340
-
341
- def test_parse_node_regex
342
- assert_equal(/\A(?:test(.*))\Z/, @matcher.parse_node("test*"))
343
- assert_equal(/\A(?:(.?)test(.*))\Z/, @matcher.parse_node("?test*"))
344
- assert_equal(/\A(?:\?test(.*))\Z/, @matcher.parse_node("\\?test*"))
345
- assert_equal(/\A(?:(.?)test\*(.*))\Z/, @matcher.parse_node("?test\\**"))
346
- assert_equal(/\A(?:(.?)test(.*))\Z/, @matcher.parse_node("?test*?**??"))
347
- assert_equal(/\A(?:(.?)test(.?)(.?)(.*))\Z/,
348
- @matcher.parse_node("?test??**??"))
349
- assert_equal(/\A(?:a|b)\Z/, @matcher.parse_node("a|b"))
350
- assert_equal(/\A(?:a|b(c|d))\Z/, @matcher.parse_node("a|b(c|d)"))
351
-
352
- matcher = Kronk::Path::Matcher.new :regex_opts => Regexp::IGNORECASE
353
- assert_equal(/\A(?:a|b(c|d))\Z/i, matcher.parse_node("a|b(c|d)"))
354
- end
355
-
356
-
357
- def test_parse_node_string
358
- assert_equal "a|b", @matcher.parse_node("a\\|b")
359
- assert_equal "a(b", @matcher.parse_node("a\\(b")
360
- assert_equal "a?b", @matcher.parse_node("a\\?b")
361
- assert_equal "a*b", @matcher.parse_node("a\\*b")
362
- end
363
-
364
-
365
- def test_parse_node_passthru
366
- assert_equal Kronk::Path::PARENT,
367
- @matcher.parse_node(Kronk::Path::PARENT)
368
-
369
- assert_equal :thing, @matcher.parse_node(:thing)
370
- end
371
- end
@@ -1,520 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestTransaction < Test::Unit::TestCase
4
-
5
- def setup
6
- @data = {
7
- :key1 => {
8
- :key1a => [
9
- "foo",
10
- "bar",
11
- "foobar",
12
- {:findme => "thing"}
13
- ],
14
- 'key1b' => "findme"
15
- },
16
- 'findme' => [
17
- 123,
18
- 456,
19
- {:findme => 123456}
20
- ],
21
- :key2 => "foobar",
22
- :key3 => {
23
- :key3a => ["val1", "val2", "val3"]
24
- }
25
- }
26
-
27
- @trans = Kronk::Path::Transaction.new @data
28
- end
29
-
30
-
31
- def test_many_transactions
32
- data = @trans.run do |t|
33
- t.select "findme/0..1", "key(1|3)"
34
- t.move [["key3/*/0", "key_value"], ["key?", "thing%1"]]
35
- t.delete "findme/0"
36
- t.move "findme/1" => "last_thing"
37
- end
38
-
39
- expected = {
40
- "last_thing"=>456 , "key_value"=>"val1", "findme" => [],
41
- "thing1"=>{:key1a=>["foo", "bar", "foobar", {:findme=>"thing"}],
42
- "key1b"=>"findme"},
43
- "thing3"=>{:key3a=>["val2", "val3"]}}
44
-
45
- assert_equal expected, data
46
- end
47
-
48
-
49
- def test_many_transactions_with_map
50
- data = @trans.run do |t|
51
- t.select "findme/0..1", "key(1|3)"
52
- t.map [["key3/*/0", "key_value"], ["key?", "thing%1"]]
53
- t.delete "findme/0"
54
- t.move "findme/1" => "last_thing"
55
- end
56
-
57
- expected = {
58
- "key_value"=>"val1",
59
- "thing1"=>{:key1a=>["foo", "bar", "foobar", {:findme=>"thing"}],
60
- "key1b"=>"findme"},
61
- "thing3"=>{:key3a=>["val2", "val3"]}}
62
-
63
- assert_equal expected, data
64
- end
65
-
66
-
67
- def test_class_run
68
- block = lambda do |t|
69
- t.delete "key3/key*/2", "**=thing"
70
- t.select "**=foo*", "**/findme"
71
- end
72
-
73
- assert_equal @trans.run(&block),
74
- Kronk::Path::Transaction.run(@data, &block)
75
-
76
- assert_equal @trans.run(:keep_indicies => true, &block),
77
- Kronk::Path::Transaction.run(@data, :keep_indicies => true, &block)
78
- end
79
-
80
-
81
- def test_run
82
- expected = {
83
- :key1=>{:key1a=>["foo", "foobar"]},
84
- :key2=>"foobar",
85
- "findme"=>[123, 456, {:findme=>123456}]
86
- }
87
-
88
- result = @trans.run do |t|
89
- t.delete "key3/key*/2", "**=thing"
90
- t.select "**=foo*", "**/findme"
91
- end
92
-
93
- assert_equal expected, result
94
- assert_equal @data, @trans.run
95
-
96
- result2 = @trans.run do |t|
97
- t.delete "key3/key*/2", "**=thing"
98
- t.select "**=foo*", "**/findme"
99
- end
100
-
101
- assert_equal expected, result2
102
- end
103
-
104
-
105
- def test_results
106
- @trans.clear
107
- @trans.delete "key3/key*/2", "**=thing"
108
- @trans.select "**=foo*", "**/findme"
109
- result = @trans.results
110
-
111
- expected = {
112
- :key1=>{:key1a=>["foo", "foobar"]},
113
- :key2=>"foobar",
114
- "findme"=>[123, 456, {:findme=>123456}]
115
- }
116
-
117
- assert_equal expected, result
118
- end
119
-
120
-
121
- def test_results_keep_indicies
122
- @trans.clear
123
- @trans.delete "key3/key*/2", "**=thing"
124
- @trans.select "**=foo*", "**/findme"
125
- result = @trans.results :keep_indicies => true
126
-
127
- expected = {
128
- :key1=>{:key1a=>{2=>"foobar", 0=>"foo"}},
129
- :key2=>"foobar",
130
- "findme"=>[123, 456, {:findme=>123456}]
131
- }
132
-
133
- assert_equal expected, result
134
- end
135
-
136
-
137
- def test_remake_arrays_select
138
- result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
139
- result = @trans.remake_arrays result
140
-
141
- expected = {:key1=>{:key1a=>["foo"]}, :key3=>{:key3a=>["val3"]}}
142
-
143
- assert_equal expected, result
144
- end
145
-
146
-
147
- def test_remake_arrays_select_except_modified
148
- result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
149
- result = @trans.remake_arrays result, true
150
-
151
- expected = {:key1=>{:key1a=>{0=>"foo"}}, :key3=>{:key3a=>{2=>"val3"}}}
152
-
153
- assert_equal expected, result
154
- end
155
-
156
-
157
- def test_remake_arrays_select_root
158
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
159
-
160
- @trans = Kronk::Path::Transaction.new data_arr
161
- result = @trans.transaction_select data_arr, "**=foo", "3/key*/2"
162
- result = @trans.remake_arrays result
163
-
164
- expected = [{:key1a=>["foo"]}, {:key3a=>["val3"]}]
165
-
166
- assert_equal expected, result
167
- end
168
-
169
-
170
- def test_remake_arrays_select_root_except_modified
171
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
172
-
173
- @trans = Kronk::Path::Transaction.new data_arr
174
- result = @trans.transaction_select data_arr, "**=foo", "3/key*"
175
- result = @trans.remake_arrays result, true
176
-
177
- expected = {1=>{:key1a=>{0=>"foo"}}, 3=>{:key3a=>["val1", "val2", "val3"]}}
178
-
179
- assert_equal expected, result
180
- end
181
-
182
-
183
- def test_remake_arrays_delete
184
- result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
185
- result = @trans.remake_arrays result
186
-
187
- expected = {
188
- :key1 => {
189
- :key1a => ["bar", "foobar", {:findme => "thing"}],
190
- 'key1b' => "findme"
191
- },
192
- 'findme' => [123, 456, {:findme => 123456}],
193
- :key2 => "foobar",
194
- :key3 => {
195
- :key3a => ["val1", "val2"]
196
- }
197
- }
198
-
199
- assert_equal expected, result
200
- end
201
-
202
-
203
- def test_remake_arrays_delete_except_modified
204
- result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
205
- result = @trans.remake_arrays result, true
206
-
207
- expected = {
208
- :key1 => {
209
- :key1a => {1=>"bar", 2=>"foobar", 3=>{:findme=>"thing"}},
210
- 'key1b' => "findme"
211
- },
212
- 'findme' => [123, 456, {:findme => 123456}],
213
- :key2 => "foobar",
214
- :key3 => {
215
- :key3a => {0=>"val1", 1=>"val2"}
216
- }
217
- }
218
-
219
- assert_equal expected, result
220
- end
221
-
222
-
223
- def test_remake_arrays_delete_root
224
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
225
-
226
- @trans = Kronk::Path::Transaction.new data_arr
227
- result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
228
- result = @trans.remake_arrays result
229
-
230
- expected = [
231
- [123, 456, {:findme=>123456}],
232
- {:key1a=>["bar", "foobar", {:findme=>"thing"}], "key1b"=>"findme"},
233
- "foobar",
234
- {:key3a=>["val1", "val2"]}
235
- ]
236
-
237
- assert_equal expected, result
238
- end
239
-
240
-
241
- def test_remake_arrays_delete_root_except_modified
242
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
243
-
244
- @trans = Kronk::Path::Transaction.new data_arr
245
- result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
246
- result = @trans.remake_arrays result, true
247
-
248
- expected = [
249
- [123, 456, {:findme=>123456}],
250
- {:key1a=>{1=>"bar", 2=>"foobar", 3=>{:findme=>"thing"}},
251
- "key1b"=>"findme"},
252
- "foobar",
253
- {:key3a=>{0=>"val1", 1=>"val2"}}
254
- ]
255
-
256
- assert_equal expected, result
257
- end
258
-
259
-
260
- def test_transaction_select
261
- result = @trans.transaction_select @data, "**=foo", "key3/key*/2"
262
- expected = {:key1=>{:key1a=>{0=>"foo"}}, :key3=>{:key3a=>{2=>"val3"}}}
263
-
264
- assert_equal expected, result
265
- end
266
-
267
-
268
- def test_transaction_select_array
269
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
270
-
271
- result = @trans.transaction_select data_arr, "**=foo", "3/key*/2"
272
- expected = {1=>{:key1a=>{0=>"foo"}}, 3=>{:key3a=>{2=>"val3"}}}
273
-
274
- assert_equal expected, result
275
- end
276
-
277
-
278
- def test_transaction_select_empty
279
- assert_equal @data, @trans.transaction_select(@data)
280
- end
281
-
282
-
283
- def test_transaction_delete
284
- result = @trans.transaction_delete @data, "**=foo", "key3/key*/2"
285
- expected = {
286
- :key1 => {
287
- :key1a => {1 => "bar", 2 => "foobar", 3 => {:findme => "thing"}},
288
- 'key1b' => "findme"
289
- },
290
- 'findme' => [123, 456, {:findme => 123456}],
291
- :key2 => "foobar",
292
- :key3 => {
293
- :key3a => {0 => "val1", 1 => "val2"}
294
- }
295
- }
296
-
297
- assert_equal expected, result
298
- end
299
-
300
-
301
- def test_transaction_delete_array
302
- data_arr = @data.keys.sort{|x,y| x.to_s <=> y.to_s}.map{|k| @data[k]}
303
-
304
- result = @trans.transaction_delete data_arr, "**=foo", "3/key*/2"
305
- expected = {
306
- 0 => [123, 456, {:findme => 123456}],
307
- 1 => {
308
- :key1a => {1 => "bar", 2 => "foobar", 3 => {:findme => "thing"}},
309
- 'key1b' => "findme"
310
- },
311
- 2 => "foobar",
312
- 3 => {:key3a => {0 => "val1", 1 => "val2"}}
313
- }
314
-
315
- assert_equal expected, result
316
- end
317
-
318
-
319
- def test_transaction_delete_many_from_embedded_data
320
- result = @trans.transaction_delete @data, "key1/key1a/1", "key1/key1a/0"
321
- expected = {
322
- :key1a => {2 => "foobar", 3 => {:findme => "thing"}},
323
- 'key1b' => "findme"
324
- }
325
-
326
- assert_equal expected, result[:key1]
327
- end
328
-
329
-
330
- def test_transaction_delete_empty
331
- assert_equal @data, @trans.transaction_delete(@data)
332
- end
333
-
334
-
335
- def test_transaction_move
336
- expected = {:key1=>{}, :key2=>"foobar",
337
- "mapped"=>{
338
- "1-a"=>["foo", "bar", "foobar", {}],
339
- "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
340
- :key3=>{}, "findme"=>[123, 456, {}],
341
- "more"=>{"one-findme"=>"thing", "two-findme"=>123456}}
342
-
343
- data = @trans.transaction_move @data,
344
- ["**=thing", "more/one-%1"],
345
- ["key*/key??", "mapped/%1-%3"],
346
- ["mapped", "remapped"],
347
- ["**=123456", "more/two-%1"]
348
- data = @trans.remake_arrays data
349
-
350
- assert_equal expected, data
351
- assert_not_equal @data, data
352
- end
353
-
354
-
355
- def test_transaction_map
356
- expected = {
357
- "mapped"=>{
358
- "1-a"=>["foo", "bar", "foobar", {}],
359
- "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
360
- "more"=>[{:findme=>"thing"}]
361
- }
362
-
363
- data = @trans.transaction_map @data,
364
- ["**=thing", "more/12/%1"],
365
- ["key*/key??", "mapped/%1-%3"],
366
- ["mapped", "remapped"]
367
-
368
- data = @trans.remake_arrays data
369
-
370
- assert_equal expected, data
371
- assert_not_equal @data, data
372
- end
373
-
374
-
375
- def test_transaction_map_no_target
376
- expected = {
377
- "mapped"=>{
378
- "1-a"=>["foo", "bar", "foobar", {}],
379
- "1-b"=>"findme", "3-a"=>["val1", "val2", "val3"]},
380
- :key1=>{:key1a => [{:findme=>"thing"}]}
381
- }
382
-
383
- data = @trans.transaction_map @data,
384
- "**=thing",
385
- ["key*/key??", "mapped/%1-%3"],
386
- ["mapped", "remapped"]
387
-
388
- data = @trans.remake_arrays data
389
-
390
- assert_equal expected, data
391
- assert_not_equal @data, data
392
- end
393
-
394
-
395
- def test_transaction_move_array_conflicting
396
- expected = {:key1=>{:key1a=>[], "key1b"=>"findme"},:key2=>"foobar",
397
- :key3=>{:key3a=>[]}, "findme"=>[123, 456, {:findme=>123456}]}
398
-
399
- data = @trans.transaction_move @data, ["key*/key??/*", "mapped/%4"]
400
- data = @trans.remake_arrays data
401
-
402
- mapped = data.delete "mapped"
403
-
404
- assert_equal expected, data
405
- assert_not_equal @data, data
406
-
407
- assert_equal({:findme=>"thing"}, mapped.last)
408
-
409
- # Due to unordered hashes, this could be
410
- # %w{val1 val2 val3} OR %w{foo bar foobar}
411
- assert_equal [String], mapped[0..2].map{|v| v.class}.uniq
412
- end
413
-
414
-
415
- def test_force_assign_paths
416
- data = {'foo' => 'bar'}
417
-
418
- new_data = @trans.force_assign_paths data,
419
- %w{sub thing one} => 'val1',
420
- %w{sub thing two} => 'val2',
421
- ['sub', 'other', 3] => 'val3',
422
- ['sub', 'other', 1] => 'val4',
423
- ['sub', 'other', 5, 6] => 'val5'
424
-
425
- assert_equal({'foo' => 'bar'}, data)
426
-
427
- expected = {
428
- 'foo' => 'bar',
429
- 'sub' => {
430
- 'thing' => {'one' => 'val1', 'two' => 'val2'},
431
- 'other' => {3 => 'val3', 1 => 'val4', 5 => {6 => 'val5'}}
432
- }
433
- }
434
- assert_equal expected, new_data
435
-
436
- expected['sub']['other'] = ['val4', 'val3', ['val5']]
437
- new_data = @trans.remake_arrays new_data
438
- assert_equal expected, new_data
439
- end
440
-
441
-
442
- def test_force_assign_paths_root_array
443
- data = ['foo', 'bar']
444
-
445
- new_data = @trans.force_assign_paths data,
446
- [1, 'thing', 'one'] => 'val1',
447
- [1, 'thing', 'two'] => 'val2',
448
- [3, 'other', 3] => 'val3',
449
- [3, 'other', 1] => 'val4',
450
- [3, 'other', 5, 6] => 'val5'
451
-
452
- assert_equal(['foo', 'bar'], data)
453
-
454
- expected = {
455
- 0 => 'foo',
456
- 1 => {'thing' => {'one' => 'val1', 'two' => 'val2'}},
457
- 3 => {'other' => {3 => 'val3', 1 => 'val4', 5 => {6 => 'val5'}}}
458
- }
459
- assert_equal expected, new_data
460
-
461
- expected[3]['other'] = ['val4', 'val3', ['val5']]
462
- expected = @trans.hash_to_ary expected
463
-
464
- new_data = @trans.remake_arrays new_data
465
- assert_equal expected, new_data
466
- end
467
-
468
-
469
- def test_is_integer
470
- assert @trans.is_integer?("123")
471
- assert @trans.is_integer?(:"123")
472
- assert !@trans.is_integer?("foo123")
473
- assert !@trans.is_integer?(:foo123)
474
- end
475
-
476
-
477
- def test_ary_to_hash
478
- expected = {1 => :a, 0 => :foo, 2 => :b}
479
- assert_equal expected, @trans.ary_to_hash([:foo, :a, :b])
480
- end
481
-
482
-
483
- def test_hash_to_ary
484
- assert_equal [:foo, :a, :b], @trans.hash_to_ary(1 => :a, 0 => :foo, 2 => :b)
485
- end
486
-
487
-
488
- def test_clear
489
- @trans.delete "foo"
490
- @trans.select "bar"
491
-
492
- @trans.clear
493
-
494
- assert @trans.instance_variable_get(:@actions).empty?
495
- assert @trans.instance_variable_get(:@make_array).empty?
496
- end
497
-
498
-
499
- def test_select
500
- @trans.select "path1", "path2", "path3"
501
- assert_equal [[:select, ["path1", "path2", "path3"]]],
502
- @trans.instance_variable_get(:@actions)
503
-
504
- @trans.select "path4", "path5"
505
- assert_equal [[:select, ["path1", "path2", "path3", "path4", "path5"]]],
506
- @trans.instance_variable_get(:@actions)
507
- end
508
-
509
-
510
- def test_delete
511
- @trans.delete "path1", "path2", "path3"
512
- assert_equal [[:delete, ["path1", "path2", "path3"]]],
513
- @trans.instance_variable_get(:@actions)
514
-
515
- @trans.delete "path4", "path5"
516
- assert_equal [[:delete, ["path1", "path2", "path3"]],
517
- [:delete, ["path4", "path5"]]],
518
- @trans.instance_variable_get(:@actions)
519
- end
520
- end