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,371 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestPathMatcher < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @matcher = 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(Path::Matcher::ANY_VALUE, "foo_key")
83
+ assert !@matcher.match_node("foo_key", 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
+ 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 = 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 = 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 = 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 = 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 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 = 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 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 = 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 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 = 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 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 Path::Matcher::ANY_VALUE, @matcher.parse_node("*")
335
+ assert_equal Path::Matcher::ANY_VALUE, @matcher.parse_node("")
336
+ assert_equal Path::Matcher::ANY_VALUE, @matcher.parse_node("**?*?*?")
337
+ assert_equal 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 = 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 Path::PARENT,
367
+ @matcher.parse_node(Path::PARENT)
368
+
369
+ assert_equal :thing, @matcher.parse_node(:thing)
370
+ end
371
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-path
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremie Castagna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.10'
30
+ - !ruby/object:Gem::Dependency
31
+ name: hoe
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ description: Simple path-based search and lookup for Ruby.
47
+ email:
48
+ - yaksnrainbows@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - History.rdoc
53
+ - Manifest.txt
54
+ - README.rdoc
55
+ files:
56
+ - .autotest
57
+ - History.rdoc
58
+ - Manifest.txt
59
+ - README.rdoc
60
+ - Rakefile
61
+ - lib/path.rb
62
+ - lib/path/core_ext.rb
63
+ - lib/path/match.rb
64
+ - lib/path/matcher.rb
65
+ - lib/path/transaction.rb
66
+ - test/test_path.rb
67
+ - test/test_core_ext.rb
68
+ - test/test_path_match.rb
69
+ - test/test_path_matcher.rb
70
+ - test/test_helper.rb
71
+ - .gemtest
72
+ homepage: https://github.com/yaksnrainbows/ruby-path
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --main
77
+ - README.rdoc
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project: ruby-path
94
+ rubygems_version: 1.8.18
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Simple path-based search and lookup for Ruby.
98
+ test_files:
99
+ - test/test_core_ext.rb
100
+ - test/test_helper.rb
101
+ - test/test_path.rb
102
+ - test/test_path_match.rb
103
+ - test/test_path_matcher.rb