ruby-path 1.0.0

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