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,74 +0,0 @@
1
- require 'test/test_helper'
2
- require 'lib/kronk/core_ext'
3
-
4
- class TestCoreExt < Test::Unit::TestCase
5
-
6
- def setup
7
- @array = [:a, :b, {:foo => "bar", :bar => [:a, :b, {:foo => "other"}]}, :c]
8
- @hash = {:foo => "bar", :bar => [:a, :b, {:foo => "other"}], :a => [1,2,3]}
9
- end
10
-
11
-
12
- def test_has_path_array
13
- assert @array.has_path?("**/foo")
14
- assert @array.has_path?("**/foo=other")
15
- assert !@array.has_path?("**/foobar")
16
- end
17
-
18
-
19
- def test_has_path_hash
20
- assert @hash.has_path?("**/foo")
21
- assert @hash.has_path?("**/foo=other")
22
- assert !@hash.has_path?("**/foobar")
23
- end
24
-
25
-
26
- def test_find_data_array
27
- out = @array.find_data "**/foo"
28
- assert_equal({[2, :foo] => "bar", [2, :bar, 2, :foo] => "other"}, out)
29
- end
30
-
31
-
32
- def test_find_data_array_empty
33
- out = @array.find_data "**/foobar"
34
- assert_equal({}, out)
35
- end
36
-
37
-
38
- def test_find_data_array_block
39
- collected = []
40
-
41
- @array.find_data "**/foo" do |data, key, path|
42
- collected << [data, key, path]
43
- end
44
-
45
- assert_equal 2, collected.length
46
- assert collected.include?([@array[2], :foo, [2, :foo]])
47
- assert collected.include?([@array[2][:bar][2], :foo, [2, :bar, 2, :foo]])
48
- end
49
-
50
-
51
- def test_find_data_hash
52
- out = @hash.find_data "**/foo"
53
- assert_equal({[:foo] => "bar", [:bar, 2, :foo] => "other"}, out)
54
- end
55
-
56
-
57
- def test_find_data_hash_empty
58
- out = @hash.find_data "**/foobar"
59
- assert_equal({}, out)
60
- end
61
-
62
-
63
- def test_find_data_hash_block
64
- collected = []
65
-
66
- @hash.find_data "**/foo" do |data, key, path|
67
- collected << [data, key, path]
68
- end
69
-
70
- assert_equal 2, collected.length
71
- assert collected.include?([@hash, :foo, [:foo]])
72
- assert collected.include?([@hash[:bar][2], :foo, [:bar, 2, :foo]])
73
- end
74
- end
data/test/test_path.rb DELETED
@@ -1,317 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestPath < 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
- end
27
-
28
-
29
- def test_find_wildcard
30
- keys = []
31
- data_points = []
32
-
33
- Kronk::Path.find "*/key1?", @data do |data, key, path|
34
- keys << key.to_s
35
- data_points << data
36
- end
37
-
38
- assert_equal ['key1a', 'key1b'], keys.sort
39
- assert_equal [@data[:key1], @data[:key1]], data_points
40
- end
41
-
42
-
43
- def test_find_recursive
44
- keys = []
45
- paths = []
46
- data_points = []
47
-
48
- Kronk::Path.find "**/findme", @data do |data, key, path|
49
- keys << key.to_s
50
- data_points << data
51
- paths << path
52
- end
53
-
54
- expected_paths =
55
- [[:key1,:key1a,3,:findme], ["findme"], ["findme",2,:findme]]
56
-
57
- assert_equal 3, keys.length
58
- assert_equal 1, keys.uniq.length
59
- assert_equal "findme", keys.first
60
-
61
- assert data_points.include?(@data)
62
- assert data_points.include?(@data[:key1][:key1a].last)
63
- assert data_points.include?(@data['findme'].last)
64
-
65
- assert_equal expected_paths, (expected_paths | paths)
66
-
67
- splat_path = paths.find{|pa| pa[0] == :key1}
68
- assert_equal [:key1, :key1a, 3], splat_path.splat[0][1]
69
- end
70
-
71
-
72
- def test_find_recursive_many_splat
73
- path_match = nil
74
-
75
- Kronk::Path.find "**/key1a/**=thing", @data do |data, key, path|
76
- path_match = path
77
- end
78
-
79
- assert path_match
80
- assert_equal [:key1], path_match.splat[0][1]
81
- assert_equal [3], path_match.splat[1][1]
82
- end
83
-
84
-
85
- def test_find_recursive_many_splat_end_match
86
- path_match = nil
87
-
88
- Kronk::Path.find "**/key1a/**/*=thing", @data do |data, key, path|
89
- path_match = path
90
- end
91
-
92
- assert path_match
93
- assert_equal [:key1], path_match.splat[0][1]
94
- assert_equal [3], path_match.splat[1][1]
95
- end
96
-
97
-
98
- def test_find_index
99
- keys = []
100
- data_points = []
101
-
102
- Kronk::Path.find "*/*/0|1", @data do |data, key, path|
103
- keys << key
104
- data_points << data
105
- end
106
-
107
- assert_equal [1,0,1,0], keys
108
- assert_equal 2, data_points.count(@data[:key1][:key1a])
109
- assert_equal 2, data_points.count(@data[:key3][:key3a])
110
- end
111
-
112
-
113
- def test_find_data_recursive_wildcard_value
114
- keys = []
115
- paths = []
116
- data_points = []
117
-
118
- Kronk::Path.find "**=foo*", @data do |data, key, path|
119
- keys << key
120
- data_points << data
121
- paths << path
122
- end
123
-
124
- expected_paths = [[:key1,:key1a,0], [:key1,:key1a,2], [:key2]]
125
-
126
- assert_equal [0,2,:key2], ([0,2,:key2] | keys)
127
- assert_equal expected_paths, (expected_paths | paths)
128
- end
129
-
130
-
131
- def test_pathed
132
- expected = {
133
- "/findme/0" => 123,
134
- "/findme/1" => 456,
135
- "/findme/2/findme" => 123456,
136
- "/key1/key1a/0" => "foo",
137
- "/key1/key1a/1" => "bar",
138
- "/key1/key1a/2" => "foobar",
139
- "/key1/key1a/3/findme" => "thing",
140
- "/key1/key1b" => "findme",
141
- "/key2" => "foobar",
142
- "/key3/key3a/0" => "val1",
143
- "/key3/key3a/1" => "val2",
144
- "/key3/key3a/2" => "val3"
145
- }
146
- assert_equal expected, Kronk::Path.pathed(@data)
147
- end
148
-
149
-
150
- def test_pathed_esc
151
- @data['findme'][2]['*thing?'] = {
152
- ".." => "parent",
153
- "**" => "recurse",
154
- "(hi|hey)" => "paren",
155
- "more/here" => "path"
156
- }
157
-
158
- expected = {
159
- "/findme/0" => 123,
160
- "/findme/1" => 456,
161
- "/findme/2/findme" => 123456,
162
-
163
- "/findme/2/\\*thing\\?/\\.\\." => "parent",
164
- "/findme/2/\\*thing\\?/\\*\\*" => "recurse",
165
- "/findme/2/\\*thing\\?/\\(hi\\|hey\\)" => "paren",
166
- "/findme/2/\\*thing\\?/more\\/here" => "path",
167
-
168
- "/key1/key1a/0" => "foo",
169
- "/key1/key1a/1" => "bar",
170
- "/key1/key1a/2" => "foobar",
171
- "/key1/key1a/3/findme" => "thing",
172
- "/key1/key1b" => "findme",
173
- "/key2" => "foobar",
174
- "/key3/key3a/0" => "val1",
175
- "/key3/key3a/1" => "val2",
176
- "/key3/key3a/2" => "val3"
177
- }
178
- assert_equal expected, Kronk::Path.pathed(@data)
179
- end
180
-
181
-
182
- def test_pathed_not_esc
183
- @data['findme'][2]['*thing?'] = {
184
- ".." => "parent",
185
- "**" => "recurse",
186
- "(hi|hey)" => "paren",
187
- "more/here" => "path"
188
- }
189
-
190
- expected = {
191
- "/findme/0" => 123,
192
- "/findme/1" => 456,
193
- "/findme/2/findme" => 123456,
194
-
195
- "/findme/2/*thing?/.." => "parent",
196
- "/findme/2/*thing?/**" => "recurse",
197
- "/findme/2/*thing?/(hi|hey)" => "paren",
198
- "/findme/2/*thing?/more/here" => "path",
199
-
200
- "/key1/key1a/0" => "foo",
201
- "/key1/key1a/1" => "bar",
202
- "/key1/key1a/2" => "foobar",
203
- "/key1/key1a/3/findme" => "thing",
204
- "/key1/key1b" => "findme",
205
- "/key2" => "foobar",
206
- "/key3/key3a/0" => "val1",
207
- "/key3/key3a/1" => "val2",
208
- "/key3/key3a/2" => "val3"
209
- }
210
- assert_equal expected, Kronk::Path.pathed(@data, false)
211
- end
212
-
213
-
214
- def test_parse_path_str_yield
215
- all_args = []
216
-
217
- Kronk::Path.parse_path_str "path/**/to=foo/item" do |*args|
218
- all_args << args
219
- end
220
-
221
- expected = [
222
- [Kronk::Path::Matcher.new(:key => "path"), false],
223
- [Kronk::Path::Matcher.new(:key => "to", :value => "foo",
224
- :recursive => true), false],
225
- [Kronk::Path::Matcher.new(:key => "item"), true],
226
- ]
227
-
228
- assert_equal expected, all_args
229
- end
230
-
231
-
232
- def test_parse_path_str_simple
233
- assert_path %w{path to item}, "path/to/item"
234
- assert_path %w{path to item}, "///path//to/././item///"
235
- assert_path %w{path to item}, "///path//to/./item///"
236
- assert_path %w{path/to item}, "path\\/to/item"
237
-
238
- assert_path %w{path/to item/ i}, "path\\/to/item\\//i"
239
-
240
- assert_path [/\A(?:path\/\.to)\Z/i, /\A(?:item)\Z/i],
241
- "path\\/.to/item", Regexp::IGNORECASE
242
-
243
- assert_path ['path', /\A(?:to|for)\Z/, 'item'], "path/to|for/item"
244
- end
245
-
246
-
247
- def test_parse_path_str_value
248
- assert_path ['path', ['to', 'foo'], 'item'], "path/to=foo/item"
249
- assert_path ['path', ["*", 'foo'], 'item'], "path/*=foo/item"
250
- assert_path ['path', [nil, 'foo'], 'item'], "path/=foo/item"
251
-
252
- assert_path ['path', ['to', /\A(?:foo|bar)\Z/], 'item'],
253
- "path/to=foo|bar/item"
254
-
255
- assert_path \
256
- [/\A(?:path)\Z/i, [/\A(?:to)\Z/i, /\A(?:foo)\Z/i], /\A(?:item)\Z/i],
257
- "path/to=foo/item", Regexp::IGNORECASE
258
- end
259
-
260
-
261
- def test_parse_path_str_recur
262
- assert_path ['path', ['to', 'foo', true], 'item'], "path/**/to=foo/item"
263
- assert_path [['path', nil, true], 'to', 'item'], "**/**/path/to/item"
264
- assert_path ['path', 'to', 'item', ["*", nil, true]], "path/to/item/**/**"
265
- assert_path ['path', ["*", 'foo', true], 'item'], "path/**=foo/item"
266
- end
267
-
268
-
269
- def test_parse_path_str_parent
270
- assert_path ['path', PARENT, 'item'], "path/../item"
271
- assert_path ['path', [PARENT, 'foo'], 'item'], "path/..=foo/item"
272
- assert_path ['path', ["*", 'foo', true], 'item'], "path/**/..=foo/item"
273
- assert_path ['path', [nil, 'foo', true], 'item'], "path/**/=foo/item"
274
- assert_path ['path', ['item', nil, true]], "path/**/../item"
275
- assert_path ['path', PARENT, ['item', nil, true]], "path/../**/item"
276
- end
277
-
278
-
279
- def test_parse_regex_opts
280
- path = "path/to/item///mix"
281
- opts = Kronk::Path.parse_regex_opts! path
282
-
283
- assert_equal "path/to/item/", path
284
-
285
- expected_opts = Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE
286
- assert_equal expected_opts, opts
287
- end
288
-
289
-
290
- def test_parse_regex_opts_mix
291
- opts = Kronk::Path.parse_regex_opts! "path/to/item//m", Regexp::EXTENDED
292
- assert_equal Regexp::EXTENDED | Regexp::MULTILINE, opts
293
- end
294
-
295
-
296
- def test_parse_regex_opts_none
297
- assert_nil Kronk::Path.parse_regex_opts!("path/to/item//")
298
- assert_equal Regexp::EXTENDED,
299
- Kronk::Path.parse_regex_opts!("path/to/item//", Regexp::EXTENDED)
300
- end
301
-
302
-
303
- private
304
-
305
- PARENT = Kronk::Path::PARENT
306
- ANY_VALUE = Kronk::Path::Matcher::ANY_VALUE
307
-
308
- def assert_path match, path, regexp_opt=nil
309
- match.map! do |i|
310
- i = [i] unless Array === i
311
- Kronk::Path::Matcher.new :key => i[0], :value => i[1],
312
- :recursive => i[2], :regex_opts => regexp_opt
313
- end
314
-
315
- assert_equal match, Kronk::Path.parse_path_str(path, regexp_opt)
316
- end
317
- end
@@ -1,105 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestPathMatch < Test::Unit::TestCase
4
-
5
- def setup
6
- @pmatch = Kronk::Path::Match.new %w{path to resource}
7
- @pmatch.matches = %w{this is 4 foo}
8
-
9
- @splat = @pmatch.dup
10
- @splat.append_splat "first", "path"
11
- @splat.append_splat "first", "to"
12
- @splat.append_splat "second", "resource"
13
- @splat.append_splat "second", "bar"
14
- end
15
-
16
-
17
- def test_new
18
- assert_equal %w{this is 4 foo}, @pmatch.matches
19
- assert_equal %w{path to resource}, @pmatch
20
- end
21
-
22
-
23
- def test_dup
24
- new_match = @pmatch.dup
25
- assert_equal new_match, @pmatch
26
- assert_not_equal @pmatch.matches.object_id, new_match.matches.object_id
27
- assert_equal %w{this is 4 foo}, @pmatch.matches
28
- assert_equal %w{this is 4 foo}, new_match.matches
29
- end
30
-
31
-
32
- def test_make_path
33
- path = @pmatch.make_path "/%3/2/path/%4_%1"
34
- assert_equal %w{4 2 path foo_this}, path
35
- end
36
-
37
-
38
- def test_make_path_consecutive
39
- path = @pmatch.make_path "/%3%4/2/path/%1"
40
- assert_equal %w{4foo 2 path this}, path
41
- end
42
-
43
-
44
- def test_make_path_no_splat
45
- path = @pmatch.make_path "/%%3/2/path/%4_%1"
46
- assert_equal %w{3 2 path foo_this}, path
47
- end
48
-
49
-
50
- def test_make_path_consecutive_no_splat
51
- path = @pmatch.make_path "/%%%4/2/path/%1"
52
- assert_equal %w{foo 2 path this}, path
53
-
54
- path = @pmatch.make_path "/%4%%/2/path/%1"
55
- assert_equal %w{foo 2 path this}, path
56
- end
57
-
58
-
59
- def test_make_path_escape
60
- path = @pmatch.make_path "/\\%3/2\\/path/%4_%1"
61
- assert_equal %w{%3 2/path foo_this}, path
62
-
63
- path = @pmatch.make_path "/\\\\%3/2/path/%4_%1/bar"
64
- assert_equal %w{\4 2 path foo_this bar}, path
65
- end
66
-
67
-
68
- def test_make_path_escape_token_num
69
- path = @pmatch.make_path "/%3\\1/2/path/%4_%1"
70
- assert_equal %w{41 2 path foo_this}, path
71
-
72
- path = @pmatch.make_path "/%\\31/2/path/%4_%1"
73
- assert_equal %w{31 2 path foo_this}, path
74
- end
75
-
76
-
77
- def test_make_path_bad_token_num
78
- path = @pmatch.make_path "/%31/2/path/%4_%1"
79
- assert_equal ["", "2", "path", "foo_this"], path
80
- end
81
-
82
-
83
- def test_make_path_splat
84
- path = @splat.make_path "/%4%%/2/thing"
85
- assert_equal ["foopath", "to", "2", "thing"], path
86
-
87
- path = @splat.make_path "/%4/%%/2/thing"
88
- assert_equal ["foo", "path", "to", "2", "thing"], path
89
-
90
- path = @splat.make_path "/%4/bah%%2/thing"
91
- assert_equal ["foo", "bahpath", "to2", "thing"], path
92
- end
93
-
94
-
95
- def test_make_path_splat_multiple
96
- path = @splat.make_path "/%4%%%%/2/thing"
97
- assert_equal ["foopath", "toresource", "bar", "2", "thing"], path
98
-
99
- path = @splat.make_path "/%4/%%/2/thing/%%"
100
- assert_equal ["foo", "path", "to", "2", "thing", "resource", "bar"], path
101
-
102
- path = @splat.make_path "%%/%4/bah%%2/thing/%%"
103
- assert_equal %w{path to foo bahresource bar2 thing}, path
104
- end
105
- end