ruby_ex 0.1.2 → 0.2.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.
- data/ChangeLog +693 -0
- data/NEWS +74 -0
- data/SPEC.dyn.yml +6 -6
- data/SPEC.gemspec +14 -0
- data/SPEC.yml +4 -4
- data/lib/abstract.rb +2 -4
- data/lib/abstract_node.rb +1 -2
- data/lib/algorithms/simulated_annealing.rb +50 -29
- data/lib/attributed_class.rb +50 -21
- data/lib/auto_object.rb +102 -0
- data/lib/blank_slate.rb +102 -0
- data/lib/cache.rb +1 -2
- data/lib/choose.rb +165 -163
- data/lib/commands.rb +2 -3
- data/lib/commands/command.rb +47 -20
- data/lib/commands/datas.rb +1 -1
- data/lib/commands/datas/composite.rb +5 -1
- data/lib/commands/datas/data.rb +102 -5
- data/lib/commands/datas/factory.rb +13 -6
- data/lib/commands/datas/temp.rb +3 -5
- data/lib/commands/factory.rb +1 -1
- data/lib/commands/helpers.rb +1 -1
- data/lib/commands/pipe.rb +10 -1
- data/lib/commands/runners.rb +1 -1
- data/lib/commands/runners/exec.rb +1 -1
- data/lib/commands/runners/fork.rb +3 -16
- data/lib/commands/runners/mock.rb +67 -0
- data/lib/commands/runners/runner.rb +5 -3
- data/lib/commands/runners/system.rb +1 -1
- data/lib/commands/seq.rb +2 -1
- data/lib/config_file.rb +10 -2
- data/lib/const_regexp.rb +1 -2
- data/lib/{dlogger.rb → d_logger.rb} +1 -2
- data/lib/daemon.rb +1 -2
- data/lib/diff.rb +1 -2
- data/lib/drb/drb_observable.rb +1 -2
- data/lib/drb/drb_observable_pool.rb +2 -2
- data/lib/drb/drb_service.rb +1 -2
- data/lib/drb/drb_undumped_attributes.rb +1 -2
- data/lib/drb/drb_undumped_indexed_object.rb +1 -2
- data/lib/drb/insecure_protected_methods.rb +1 -2
- data/lib/drb_ex.rb +2 -2
- data/lib/file_type.rb +466 -0
- data/lib/generate_id.rb +12 -6
- data/lib/genpasswd.rb +22 -0
- data/lib/hash_eval.rb +83 -0
- data/lib/histogram.rb +1 -2
- data/lib/hookable.rb +3 -4
- data/lib/hooker.rb +1 -3
- data/lib/html_encode.rb +191 -0
- data/lib/indexed_node.rb +0 -1
- data/lib/io_marshal.rb +4 -2
- data/lib/ioo.rb +3 -2
- data/lib/kill_all.rb +46 -0
- data/lib/labeled_node.rb +0 -1
- data/lib/logger_observer.rb +8 -4
- data/lib/md5sum.rb +3 -3
- data/lib/meta_factory.rb +99 -0
- data/lib/method_call.rb +87 -0
- data/lib/mocks.rb +12 -0
- data/lib/mocks/assertions.rb +50 -0
- data/lib/mocks/method_logger.rb +40 -0
- data/lib/mocks/mock.rb +64 -0
- data/lib/mocks/object.rb +47 -0
- data/lib/mocks/observer.rb +38 -0
- data/lib/module/autoload_tree.rb +30 -29
- data/lib/module/hierarchy.rb +176 -171
- data/lib/module/instance_method_visibility.rb +39 -38
- data/lib/node.rb +0 -1
- data/lib/object_monitor.rb +1 -2
- data/lib/object_monitor_activity.rb +1 -2
- data/lib/observable.rb +1 -2
- data/lib/observable_pool.rb +1 -2
- data/lib/{orderedhash.rb → ordered_hash.rb} +41 -5
- data/lib/pp_hierarchy.rb +7 -2
- data/lib/r_path.rb +307 -0
- data/lib/random_generators.rb +7 -20
- data/lib/random_generators/random_generator.rb +2 -4
- data/lib/random_generators/ruby.rb +4 -2
- data/lib/regex_path.rb +124 -0
- data/lib/ruby_ex.rb +28 -98
- data/lib/safe_eval.rb +1 -2
- data/lib/sendmail.rb +14 -17
- data/lib/service_manager.rb +1 -2
- data/lib/shuffle.rb +6 -2
- data/lib/spring.rb +1 -2
- data/lib/spring_set.rb +1 -2
- data/lib/{symtbl.rb → sym_tbl.rb} +90 -5
- data/lib/sym_tbl_gsub.rb +227 -0
- data/lib/{synflow.rb → syn_flow.rb} +1 -2
- data/lib/text.rb +218 -0
- data/lib/timeout_ex.rb +1 -2
- data/lib/trace.rb +9 -8
- data/lib/uri/druby.rb +1 -2
- data/lib/uri/file.rb +1 -1
- data/lib/uri/ftp_ex.rb +1 -1
- data/lib/uri/http_ex.rb +1 -1
- data/lib/uri/mysql.rb +121 -0
- data/lib/uri/pgsql.rb +19 -38
- data/lib/uri/svn.rb +1 -2
- data/lib/uri_ex.rb +45 -3
- data/lib/verbose_object.rb +181 -38
- data/lib/yaml/chop_header.rb +19 -11
- data/lib/yaml/transform.rb +17 -11
- data/lib/yaml/yregexpath.rb +11 -5
- data/test/algorithms/simulated_annealing_test.rb +2 -2
- data/test/resources/foo.tar.gz +0 -0
- data/test/resources/tar.gz.log +49 -0
- data/test/sanity-suite.yml +5 -7
- data/test/sanity/multiple-requires.yml +17 -7
- data/test/sanity/single-requires.yml +38 -20
- data/test/stress-tests/threads_and_exceptions.yml +13 -0
- data/test/test-unit-setup.rb +3 -1
- data/test/unit-suite.yml +7 -8
- metadata +42 -31
- data/lib/algorithms.rb +0 -12
- data/lib/ask.rb +0 -100
- data/lib/checkout.rb +0 -12
- data/lib/dumpable_proc.rb +0 -57
- data/lib/filetype.rb +0 -229
- data/lib/thread_mutex.rb +0 -11
- data/lib/yaml/basenode_ext.rb +0 -63
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
# Copyright
|
|
2
|
-
# Author
|
|
3
|
-
# License
|
|
1
|
+
# Copyright:: Copyright (c) 2004 Nicolas Despres. All rights reserved.
|
|
2
|
+
# Author:: Nicolas Despres <polrop@lrde.epita.fr>.
|
|
3
|
+
# License:: Gnu General Public License.
|
|
4
|
+
# Revision:: $Id: instance_method_visibility.rb 339 2005-09-06 23:27:27Z ertai $
|
|
4
5
|
|
|
5
|
-
# $LastChangedBy: polrop $
|
|
6
|
-
# $Id: instance_method_visibility.rb 279 2005-06-05 17:46:53Z ertai $
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class Module
|
|
7
|
+
module InstanceMethodVisibility
|
|
11
8
|
|
|
12
9
|
def instance_method_visibility(name)
|
|
13
10
|
if private_method_defined?(name)
|
|
@@ -22,50 +19,54 @@ class Module
|
|
|
22
19
|
end
|
|
23
20
|
end
|
|
24
21
|
|
|
22
|
+
end # module InstanceMethodVisibility
|
|
23
|
+
|
|
24
|
+
class Module
|
|
25
|
+
include InstanceMethodVisibility
|
|
25
26
|
end # class Module
|
|
26
27
|
|
|
28
|
+
module InstanceMethodVisibility
|
|
27
29
|
|
|
28
|
-
test_section __FILE__ do
|
|
30
|
+
test_section __FILE__ do
|
|
29
31
|
|
|
32
|
+
class InstanceMethodVisibilityTest < Test::Unit::TestCase
|
|
30
33
|
|
|
34
|
+
class A
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
def pub; end
|
|
37
|
+
def pri; end
|
|
38
|
+
def pro; end
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
public :pub
|
|
41
|
+
private :pri
|
|
42
|
+
protected :pro
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
def pri; end
|
|
38
|
-
def pro; end
|
|
44
|
+
end # class A
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
def test_simple
|
|
47
|
+
assert_equal(:public, A.instance_method_visibility('pub'))
|
|
48
|
+
assert_equal(:private, A.instance_method_visibility('pri'))
|
|
49
|
+
assert_equal(:protected, A.instance_method_visibility('pro'))
|
|
50
|
+
assert_raises(NoMethodError) { A.instance_method_visibility('new') }
|
|
51
|
+
end
|
|
43
52
|
|
|
44
|
-
|
|
53
|
+
class AA
|
|
54
|
+
def f; end
|
|
55
|
+
end
|
|
56
|
+
class BB < AA
|
|
57
|
+
private :f
|
|
58
|
+
end
|
|
45
59
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
end
|
|
60
|
+
def test_inheritance
|
|
61
|
+
assert_equal(:public, AA.instance_method_visibility('f'))
|
|
62
|
+
assert(! BB.public_method_defined?('f'))
|
|
63
|
+
assert_equal(:private, BB.instance_method_visibility('f'))
|
|
64
|
+
end
|
|
52
65
|
|
|
53
|
-
|
|
54
|
-
def f; end
|
|
55
|
-
end
|
|
56
|
-
class BB < AA
|
|
57
|
-
private :f
|
|
58
|
-
end
|
|
66
|
+
end # class InstanceMethodVisibilityTest
|
|
59
67
|
|
|
60
|
-
def test_inheritance
|
|
61
|
-
assert_equal(:public, AA.instance_method_visibility('f'))
|
|
62
|
-
assert(! BB.public_method_defined?('f'))
|
|
63
|
-
assert_equal(:private, BB.instance_method_visibility('f'))
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
end #
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
end
|
|
70
|
+
end # module InstanceMethodVisibility
|
|
70
71
|
|
|
71
72
|
|
data/lib/node.rb
CHANGED
data/lib/object_monitor.rb
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
# License: Gnu General Public License.
|
|
4
4
|
|
|
5
5
|
# $LastChangedBy: ertai $
|
|
6
|
-
# $Id: object_monitor_activity.rb
|
|
6
|
+
# $Id: object_monitor_activity.rb 339 2005-09-06 23:27:27Z ertai $
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
require 'ruby_ex'
|
|
10
9
|
require 'observable'
|
|
11
10
|
|
|
12
11
|
|
data/lib/observable.rb
CHANGED
data/lib/observable_pool.rb
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
# License: Gnu General Public License.
|
|
4
4
|
|
|
5
5
|
# $LastChangedBy: ertai $
|
|
6
|
-
# $Id: observable_pool.rb
|
|
6
|
+
# $Id: observable_pool.rb 339 2005-09-06 23:27:27Z ertai $
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
require 'ruby_ex'
|
|
10
9
|
require 'observable'
|
|
11
10
|
require 'thread'
|
|
12
11
|
|
|
@@ -18,11 +18,10 @@
|
|
|
18
18
|
#
|
|
19
19
|
|
|
20
20
|
# $LastChangedBy: ertai $
|
|
21
|
-
# $Id:
|
|
21
|
+
# $Id: ordered_hash.rb 339 2005-09-06 23:27:27Z ertai $
|
|
22
22
|
|
|
23
|
-
require 'ruby_ex'
|
|
24
23
|
|
|
25
|
-
module
|
|
24
|
+
module OrderedHash
|
|
26
25
|
|
|
27
26
|
def store ( a, b )
|
|
28
27
|
@order.push a unless has_key? a
|
|
@@ -118,6 +117,30 @@ module OrderedHash_ext
|
|
|
118
117
|
to_a.to_s
|
|
119
118
|
end
|
|
120
119
|
|
|
120
|
+
def inspect
|
|
121
|
+
arr = to_a.map{ |k, v| "#{k.inspect}=>#{v.inspect}" }
|
|
122
|
+
'{(ordered) ' + arr.join(', ') + ' }'
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def pretty_print ( q )
|
|
126
|
+
q.group(1, '{(ordered) ', '}') {
|
|
127
|
+
q.seplist(self, nil, :each_pair) {|k, v|
|
|
128
|
+
q.group {
|
|
129
|
+
q.pp k
|
|
130
|
+
q.text '=>'
|
|
131
|
+
q.group(1) {
|
|
132
|
+
q.breakable ''
|
|
133
|
+
q.pp v
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def pretty_print_cycle(q)
|
|
141
|
+
q.text(empty? ? '{(ordered)}' : '{(ordered) ...}')
|
|
142
|
+
end
|
|
143
|
+
|
|
121
144
|
def update ( hsh2 )
|
|
122
145
|
hsh2.each { |k,v| self[k] = v }
|
|
123
146
|
self
|
|
@@ -142,7 +165,7 @@ module OrderedHash_ext
|
|
|
142
165
|
!@order.nil?
|
|
143
166
|
end
|
|
144
167
|
|
|
145
|
-
end # module
|
|
168
|
+
end # module OrderedHash
|
|
146
169
|
|
|
147
170
|
|
|
148
171
|
class OHash < Hash
|
|
@@ -151,7 +174,7 @@ class OHash < Hash
|
|
|
151
174
|
|
|
152
175
|
alias :unordered_keys :keys
|
|
153
176
|
|
|
154
|
-
include
|
|
177
|
+
include OrderedHash
|
|
155
178
|
|
|
156
179
|
def initialize ( *a )
|
|
157
180
|
@order = unordered_keys
|
|
@@ -227,6 +250,19 @@ test_section __FILE__ do
|
|
|
227
250
|
check_ordered({}, [[2, 3], [4, 3]])
|
|
228
251
|
end
|
|
229
252
|
|
|
253
|
+
def test_inspect
|
|
254
|
+
hsh = OHash['z', 1, 'a', 2, 'c', 3]
|
|
255
|
+
hsh2 = OHash['a', 2, 'z', 1, 'c', 3]
|
|
256
|
+
assert_equal('{(ordered) "z"=>1, "a"=>2, "c"=>3 }', hsh.inspect)
|
|
257
|
+
assert_equal('{(ordered) "a"=>2, "z"=>1, "c"=>3 }', hsh2.inspect)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def test_pretty_print
|
|
261
|
+
require 'pp'
|
|
262
|
+
hsh = OHash['z', 1, 'a', 2, 'c', 3]
|
|
263
|
+
assert_match(/^\{\(ordered\) /, PP.pp(hsh, ''))
|
|
264
|
+
end
|
|
265
|
+
|
|
230
266
|
def test_little_ordered
|
|
231
267
|
a = (0..11).to_a
|
|
232
268
|
h = OHash[*a]
|
data/lib/pp_hierarchy.rb
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
# $LastChangedBy: polrop $
|
|
6
6
|
# $Id: hierarchy.rb 145 2005-01-30 14:16:14Z polrop $
|
|
7
7
|
|
|
8
|
-
require 'ruby_ex'
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
module PpHierarchy
|
|
11
10
|
|
|
12
11
|
# Pretty print a hash symbolizing a class hierarchy.
|
|
13
12
|
def pp_hierarchy(prefix=//, abstract=false)
|
|
@@ -27,4 +26,10 @@ class Hash
|
|
|
27
26
|
end
|
|
28
27
|
end
|
|
29
28
|
|
|
29
|
+
end # module PpHierarchy
|
|
30
|
+
|
|
31
|
+
class Hash
|
|
32
|
+
|
|
33
|
+
include PpHierarchy
|
|
34
|
+
|
|
30
35
|
end # class Hash
|
data/lib/r_path.rb
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
# Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
|
|
2
|
+
# Author:: Nicolas Pouillard <ertai@lrde.epita.fr>.
|
|
3
|
+
# License:: Gnu General Public License.
|
|
4
|
+
# Revision:: $Id: r_path.rb 339 2005-09-06 23:27:27Z ertai $
|
|
5
|
+
|
|
6
|
+
require 'regex_path'
|
|
7
|
+
|
|
8
|
+
class Object
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# RPath is a sort of the well known XPath. XPath is XML and RPath is
|
|
12
|
+
# obviously built for Ruby.
|
|
13
|
+
#
|
|
14
|
+
# When you have a Ruby tree -- by tree I mean a complex data structure with
|
|
15
|
+
# no cycles -- you can search something into it:
|
|
16
|
+
#
|
|
17
|
+
# tree = {
|
|
18
|
+
# :A => {
|
|
19
|
+
# :AA => { :AAA => 'foo' },
|
|
20
|
+
# 'AB' => [ :AB1, 2, 'a b 3' ],
|
|
21
|
+
# },
|
|
22
|
+
# :B => {
|
|
23
|
+
# 'foo' => { :home => 'here', :email => 'foo@foo.com' },
|
|
24
|
+
# 'bar' => { :home => "bar's home", :email => 'bar@foo.bar' },
|
|
25
|
+
# 'baz' => { :home => 'nowhere', :email => 'baz@bar.com' }
|
|
26
|
+
# }
|
|
27
|
+
# }
|
|
28
|
+
#
|
|
29
|
+
# tree.rpath('/A/AA/AAA/') { |x| p x } # => 'foo'
|
|
30
|
+
#
|
|
31
|
+
# But we are dealing with regexp so if you want just this path you need:
|
|
32
|
+
# tree.rpath('/^A$/^AA$/^AAA$/') { |x| p x } # => 'foo'
|
|
33
|
+
#
|
|
34
|
+
# But if you are lazy you can type that:
|
|
35
|
+
# tree.rpath('/A/AA//') { |x| p x } # => 'foo'
|
|
36
|
+
# or
|
|
37
|
+
# tree.rpath('///AAA/') { |x| p x } # => 'foo'
|
|
38
|
+
#
|
|
39
|
+
# tree.rpath('/B//home/nowhere') { |x| p x } # => 'nowhere'
|
|
40
|
+
#
|
|
41
|
+
# But you would prefer get the complete baz record, so to do that you
|
|
42
|
+
# can mark your dezired levels with `*'.
|
|
43
|
+
#
|
|
44
|
+
# tree.rpath('/B//*home/nowhere') { |x| p x[:email] } # => 'baz@bar.com'
|
|
45
|
+
#
|
|
46
|
+
# tree.rpath('/B//*home/ere') { |x| p x[:email] }
|
|
47
|
+
# => 'foo@foo.com'
|
|
48
|
+
# => 'baz@bar.com'
|
|
49
|
+
#
|
|
50
|
+
# tree.rpath('/B//*home/(.*)') { |tree, home| p [home, tree[:email]] }
|
|
51
|
+
# => [ 'here', 'foo@foo.com' ]
|
|
52
|
+
# => [ "bar's home", 'bar@foo.bar' ]
|
|
53
|
+
# => [ 'nowhere', 'baz@bar.com' ]
|
|
54
|
+
#
|
|
55
|
+
def rpath ( re, &block )
|
|
56
|
+
raise ArgumentError, 'no block given' unless block_given?
|
|
57
|
+
re = RegexPath.new(re) unless re.is_a? RegexPath
|
|
58
|
+
regex_path_match(re, nil, [], &block)
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
#
|
|
63
|
+
# Act like rpath it does not take a block but
|
|
64
|
+
#
|
|
65
|
+
def rpath_select ( re )
|
|
66
|
+
results = []
|
|
67
|
+
re = RegexPath.new(re) unless re.is_a? RegexPath
|
|
68
|
+
regex_path_match(re, nil, []) do |x|
|
|
69
|
+
results << x
|
|
70
|
+
end
|
|
71
|
+
results
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
#
|
|
75
|
+
# Like rpath_select with match datas
|
|
76
|
+
#
|
|
77
|
+
def rpath_select_match_data ( re )
|
|
78
|
+
results = []
|
|
79
|
+
re = RegexPath.new(re) unless re.is_a? RegexPath
|
|
80
|
+
regex_path_match(re, nil, []) do |*a|
|
|
81
|
+
results << a
|
|
82
|
+
end
|
|
83
|
+
results
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
#
|
|
87
|
+
# mode = [:first, :all]
|
|
88
|
+
# rpath_find :all is like rpath_select
|
|
89
|
+
# rpath_find :first is the first element of rpath_select
|
|
90
|
+
#
|
|
91
|
+
def rpath_find ( mode, re )
|
|
92
|
+
results = rpath_select(re)
|
|
93
|
+
case mode
|
|
94
|
+
when :all then results
|
|
95
|
+
when :first then results.first
|
|
96
|
+
else raise "Bad mode `#{mode}' for rpath_find"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def regex_path_match ( re, trees, args, &block ) # :nodoc:
|
|
101
|
+
to_s.regex_path_match(re, trees, args, &block)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
end # class Object
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class Hash
|
|
109
|
+
|
|
110
|
+
def regex_path_match ( re, trees, args, &block ) # :nodoc:
|
|
111
|
+
trees = (trees || []) + [self] if re.starred?
|
|
112
|
+
|
|
113
|
+
if re.empty?
|
|
114
|
+
trees ||= [self]
|
|
115
|
+
trees.each { |tree| block[tree, *args] }
|
|
116
|
+
else
|
|
117
|
+
seg, tail_re = re.split
|
|
118
|
+
each do |key, val|
|
|
119
|
+
match_data = seg =~ key.to_s
|
|
120
|
+
if match_data
|
|
121
|
+
sub_args = args + match_data.to_a[1..-1]
|
|
122
|
+
val.regex_path_match(tail_re, trees, sub_args, &block)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
end # class Hash
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class Array
|
|
132
|
+
|
|
133
|
+
def regex_path_match ( re, trees, args, &block ) # :nodoc:
|
|
134
|
+
each do |x|
|
|
135
|
+
x.regex_path_match(re, trees, args, &block)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
end # class Array
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class String
|
|
143
|
+
|
|
144
|
+
def regex_path_match ( re, trees, args, &block ) # :nodoc:
|
|
145
|
+
seg, tail_re = re.split
|
|
146
|
+
|
|
147
|
+
trees = (trees || []) + [self] if re.starred?
|
|
148
|
+
|
|
149
|
+
if (seg.nil? or match_data = seg =~ self) and tail_re.empty?
|
|
150
|
+
trees ||= [self]
|
|
151
|
+
args = args + match_data.to_a[1..-1] if defined? match_data and match_data
|
|
152
|
+
trees.each { |tree| block[tree, *args] }
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
end # class String
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
module RPath
|
|
160
|
+
|
|
161
|
+
test_section __FILE__ do
|
|
162
|
+
|
|
163
|
+
class RPathTest < Test::Unit::TestCase
|
|
164
|
+
|
|
165
|
+
def assert_rpath_base ( re, ref, ref_match=[] )
|
|
166
|
+
select = @tree.rpath_select_match_data(re)
|
|
167
|
+
my = []
|
|
168
|
+
select.map { |x, *a| my << a unless a.empty? }
|
|
169
|
+
[ref.flatten, select.map { |a| a.first }, ref_match, my]
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def assert_rpath ( re, ref, ref_match=[] )
|
|
173
|
+
ref1, my1, ref2, my2 = assert_rpath_base(re, ref, ref_match)
|
|
174
|
+
assert_equal(ref1, my1, 'trees are not equal')
|
|
175
|
+
assert_equal(ref2, my2, 'match datas are not equal')
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def assert_rpath_set ( re, ref, ref_match=[] )
|
|
179
|
+
ref1, my1, ref2, my2 = assert_rpath_base(re, ref, ref_match)
|
|
180
|
+
assert_equal(ref1.to_set, my1.to_set, 'trees are not equal')
|
|
181
|
+
assert_equal(ref2.to_set, my2.to_set, 'match datas are not equal')
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def setup
|
|
185
|
+
@tree = {
|
|
186
|
+
'Name' => 'Foo',
|
|
187
|
+
'FirstName' => 'Bar',
|
|
188
|
+
'Emails' => {
|
|
189
|
+
'home' => 'foo312@coldmail.com',
|
|
190
|
+
'work' => 'bfoo@staff.com',
|
|
191
|
+
'edu' => 'bar.foo@school.edu'
|
|
192
|
+
},
|
|
193
|
+
'Phones' => ['101', '202', '203'],
|
|
194
|
+
'Deep' => { 'A' => { 'B' => ['1', 2, 'a', '3'], 'C' => '4' } },
|
|
195
|
+
'users' => [
|
|
196
|
+
{
|
|
197
|
+
'name' => 'Sea',
|
|
198
|
+
'tel' => '111'
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
'name' => 'Sex',
|
|
202
|
+
'tel' => '222'
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
'name' => 'Sun',
|
|
206
|
+
'tel' => '333'
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
'foo' => 'SeSe',
|
|
210
|
+
'tel' => '222'
|
|
211
|
+
},
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def test_simple
|
|
217
|
+
assert_rpath('Name', ['Foo', 'Bar'])
|
|
218
|
+
assert_rpath('^Name', ['Foo'])
|
|
219
|
+
assert_rpath('tName', ['Bar'])
|
|
220
|
+
assert_rpath('Emails/home', ["foo312@coldmail.com"])
|
|
221
|
+
assert_rpath('Emails/edu', ["bar.foo@school.edu"])
|
|
222
|
+
assert_rpath('Name/home', [])
|
|
223
|
+
assert_rpath('Phone/101', ['101'])
|
|
224
|
+
assert_rpath('Phone/\d0\d', ["101", "202", "203"])
|
|
225
|
+
assert_rpath('Phone/20\d', ["202", "203"])
|
|
226
|
+
assert_rpath('Deep/\d', [])
|
|
227
|
+
assert_rpath('Deep/A', [{"B"=>["1", 2, "a", "3"], "C"=>"4"}])
|
|
228
|
+
assert_rpath('Deep/.*/\d', [])
|
|
229
|
+
assert_rpath('Deep//\d', [])
|
|
230
|
+
assert_rpath('Deep///\d', ['1', '2', '3', '4'])
|
|
231
|
+
|
|
232
|
+
assert_rpath('Name/Foo', ['Foo'])
|
|
233
|
+
assert_rpath('Name/Foo/Bar', [])
|
|
234
|
+
assert_rpath('/E/o/com', ["foo312@coldmail.com", "bfoo@staff.com"])
|
|
235
|
+
assert_rpath('//o/com', ["foo312@coldmail.com", "bfoo@staff.com"])
|
|
236
|
+
|
|
237
|
+
assert_rpath('///Se', ["Sea", "Sex", "SeSe"])
|
|
238
|
+
|
|
239
|
+
assert_rpath('//a/Se', ["Sea", "Sex"])
|
|
240
|
+
assert_rpath('/users/name/Se[ax]', ["Sea", "Sex"])
|
|
241
|
+
assert_rpath('/users//S[eu].', ["Sea", "Sex", 'Sun', 'SeSe'])
|
|
242
|
+
assert_rpath('/users//S[eu].$', ["Sea", "Sex", 'Sun'])
|
|
243
|
+
|
|
244
|
+
assert_rpath('//*a/Sea', ['name' => 'Sea', 'tel' => '111'])
|
|
245
|
+
|
|
246
|
+
assert_rpath('//*a/Se', [{"name"=>"Sea", "tel"=>"111"},
|
|
247
|
+
{"name"=>"Sex", "tel"=>"222"}])
|
|
248
|
+
|
|
249
|
+
assert_rpath('//*a/*Se', [{"name"=>"Sea", "tel"=>"111"}, 'Sea',
|
|
250
|
+
{"name"=>"Sex", "tel"=>"222"}, 'Sex'])
|
|
251
|
+
|
|
252
|
+
assert_rpath('/*/*a/*Se', [@tree, {"name"=>"Sea", "tel"=>"111"}, 'Sea',
|
|
253
|
+
@tree, {"name"=>"Sex", "tel"=>"222"}, 'Sex'])
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def test_example
|
|
257
|
+
foo = { :home => 'here', :email => 'foo@foo.com' }
|
|
258
|
+
bar = { :home => "bar's home", :email => 'bar@foo.bar' }
|
|
259
|
+
baz = { :home => 'nowhere', :email => 'baz@bar.com' }
|
|
260
|
+
@tree = {
|
|
261
|
+
:A => {
|
|
262
|
+
:AA => { :AAA => 'foo' },
|
|
263
|
+
'AB' => [ :AB1, 2, 'a b 3' ],
|
|
264
|
+
},
|
|
265
|
+
:B => {
|
|
266
|
+
'foo' => foo,
|
|
267
|
+
'bar' => bar,
|
|
268
|
+
'baz' => baz
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
assert_rpath('/A/AA/AAA/', ['foo'])
|
|
273
|
+
|
|
274
|
+
assert_rpath('/^A$/^AA$/^AAA$/', ['foo'])
|
|
275
|
+
|
|
276
|
+
assert_rpath('/A/AA//', ['foo'])
|
|
277
|
+
assert_rpath('///AAA/', ['foo'])
|
|
278
|
+
|
|
279
|
+
assert_rpath('/B//home/nowhere', ['nowhere'])
|
|
280
|
+
|
|
281
|
+
assert_rpath('/B//*home/nowhere', [baz])
|
|
282
|
+
|
|
283
|
+
assert_rpath_set('/B//*home/ere', [foo, baz])
|
|
284
|
+
|
|
285
|
+
assert_rpath_set('/B//*home/(.*)', [foo, bar, baz],
|
|
286
|
+
[['here'], ["bar's home"], ['nowhere']])
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def test_match_data
|
|
290
|
+
assert_rpath('^N(a)me', ['Foo'], [['a']])
|
|
291
|
+
assert_rpath('N(a)m(e)', ['Foo', 'Bar'], [["a", "e"], ["a", "e"]])
|
|
292
|
+
assert_rpath('/(.*)/(.*o.*)/(.*com.*)',
|
|
293
|
+
["foo312@coldmail.com", "bfoo@staff.com"],
|
|
294
|
+
[["Emails", "home", "foo312@coldmail.com"],
|
|
295
|
+
["Emails", "work", "bfoo@staff.com"]])
|
|
296
|
+
assert_rpath('/users/name/Se([ax])', ["Sea", "Sex"], [["a"], ["x"]])
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def test_rpath
|
|
300
|
+
assert_nothing_raised { @tree.rpath('/A/AA/AAA/') { |x| } }
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
end # class RPathTest
|
|
304
|
+
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
end # module RPath
|