ruby_ex 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/AUTHORS +51 -0
  2. data/ChangeLog +1763 -0
  3. data/NEWS +3 -0
  4. data/README +1 -0
  5. data/Rakefile +8 -0
  6. data/SPEC.dyn.yml +10 -0
  7. data/SPEC.gem.yml +269 -0
  8. data/SPEC.yml +36 -0
  9. data/src/abstract.rb +253 -0
  10. data/src/abstract_node.rb +85 -0
  11. data/src/algorithms.rb +12 -0
  12. data/src/algorithms/simulated_annealing.rb +142 -0
  13. data/src/ask.rb +100 -0
  14. data/src/attributed_class.rb +303 -0
  15. data/src/cache.rb +350 -0
  16. data/src/checkout.rb +12 -0
  17. data/src/choose.rb +271 -0
  18. data/src/commands.rb +20 -0
  19. data/src/commands/command.rb +492 -0
  20. data/src/commands/datas.rb +16 -0
  21. data/src/commands/datas/composite.rb +31 -0
  22. data/src/commands/datas/data.rb +65 -0
  23. data/src/commands/datas/factory.rb +69 -0
  24. data/src/commands/datas/temp.rb +26 -0
  25. data/src/commands/factory.rb +67 -0
  26. data/src/commands/helpers.rb +81 -0
  27. data/src/commands/pipe.rb +66 -0
  28. data/src/commands/runners.rb +16 -0
  29. data/src/commands/runners/exec.rb +50 -0
  30. data/src/commands/runners/fork.rb +130 -0
  31. data/src/commands/runners/runner.rb +140 -0
  32. data/src/commands/runners/system.rb +57 -0
  33. data/src/commands/seq.rb +32 -0
  34. data/src/config_file.rb +95 -0
  35. data/src/const_regexp.rb +57 -0
  36. data/src/daemon.rb +135 -0
  37. data/src/diff.rb +665 -0
  38. data/src/dlogger.rb +62 -0
  39. data/src/drb/drb_observable.rb +95 -0
  40. data/src/drb/drb_observable_pool.rb +27 -0
  41. data/src/drb/drb_service.rb +44 -0
  42. data/src/drb/drb_undumped_attributes.rb +56 -0
  43. data/src/drb/drb_undumped_indexed_object.rb +55 -0
  44. data/src/drb/insecure_protected_methods.rb +101 -0
  45. data/src/drb_ex.rb +12 -0
  46. data/src/dumpable_proc.rb +57 -0
  47. data/src/filetype.rb +229 -0
  48. data/src/generate_id.rb +44 -0
  49. data/src/histogram.rb +222 -0
  50. data/src/hookable.rb +283 -0
  51. data/src/hooker.rb +54 -0
  52. data/src/indexed_node.rb +65 -0
  53. data/src/io_marshal.rb +99 -0
  54. data/src/ioo.rb +193 -0
  55. data/src/labeled_node.rb +62 -0
  56. data/src/logger_observer.rb +24 -0
  57. data/src/md5sum.rb +70 -0
  58. data/src/module/autoload_tree.rb +65 -0
  59. data/src/module/hierarchy.rb +334 -0
  60. data/src/module/instance_method_visibility.rb +71 -0
  61. data/src/node.rb +81 -0
  62. data/src/object_monitor.rb +143 -0
  63. data/src/object_monitor_activity.rb +34 -0
  64. data/src/observable.rb +138 -0
  65. data/src/observable_pool.rb +291 -0
  66. data/src/orderedhash.rb +252 -0
  67. data/src/pp_hierarchy.rb +30 -0
  68. data/src/random_generators.rb +29 -0
  69. data/src/random_generators/random_generator.rb +33 -0
  70. data/src/random_generators/ruby.rb +25 -0
  71. data/src/ruby_ex.rb +124 -0
  72. data/src/safe_eval.rb +346 -0
  73. data/src/sendmail.rb +214 -0
  74. data/src/service_manager.rb +122 -0
  75. data/src/shuffle.rb +30 -0
  76. data/src/spring.rb +134 -0
  77. data/src/spring_set.rb +134 -0
  78. data/src/symtbl.rb +108 -0
  79. data/src/synflow.rb +474 -0
  80. data/src/thread_mutex.rb +11 -0
  81. data/src/timeout_ex.rb +79 -0
  82. data/src/trace.rb +26 -0
  83. data/src/uri/druby.rb +78 -0
  84. data/src/uri/file.rb +63 -0
  85. data/src/uri/ftp_ex.rb +36 -0
  86. data/src/uri/http_ex.rb +41 -0
  87. data/src/uri/pgsql.rb +136 -0
  88. data/src/uri/ssh.rb +87 -0
  89. data/src/uri/svn.rb +113 -0
  90. data/src/uri_ex.rb +71 -0
  91. data/src/verbose_object.rb +70 -0
  92. data/src/yaml/basenode_ext.rb +63 -0
  93. data/src/yaml/chop_header.rb +24 -0
  94. data/src/yaml/transform.rb +450 -0
  95. data/src/yaml/yregexpath.rb +76 -0
  96. data/test/algorithms/simulated_annealing_test.rb +102 -0
  97. data/test/check-pkg-ruby_ex.yml +15 -0
  98. data/test/check-ruby_ex.yml +12 -0
  99. data/test/resources/autoload_tree/A.rb +11 -0
  100. data/test/resources/autoload_tree/B.rb +10 -0
  101. data/test/resources/autoload_tree/foo/C.rb +18 -0
  102. data/test/resources/foo.txt +6 -0
  103. data/test/sanity-suite.yml +12 -0
  104. data/test/sanity/multiple-requires.yml +20 -0
  105. data/test/sanity/single-requires.yml +24 -0
  106. data/test/test-unit-setup.rb +6 -0
  107. data/test/unit-suite.yml +14 -0
  108. metadata +269 -0
@@ -0,0 +1,57 @@
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
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: const_regexp.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+ require 'ruby_ex'
9
+
10
+ module ConstRegexp
11
+
12
+ RB_INST_VAR = /^@[A-Za-z_][A-Za-z0-9_]*$/
13
+ RB_CLASS_VAR = /^@@[A-Za-z_][A-Za-z0-9_]*$/ #FIXME: concat with RB_INST_VAR
14
+ RB_LOCAL_VAR = /^[a-z_][A-Za-z0-9_]*$/
15
+ RB_METHOD = /^[a-z_][A-Za-z0-9_]*[=?!]?$/
16
+ RB_REGEXP = /^\/([^\\]|\\.)*\/$/
17
+ RB_CONST = /^[A-Z][a-zA-Z0-9_]*$/
18
+
19
+ CRYPTED_STRING = /^[A-Za-z0-9.\/]{13}$/
20
+
21
+ DRB_URI = /^([^:]+):\/\/([^:]+):(\d+)$/
22
+
23
+
24
+
25
+ end # module ConstRegexp
26
+
27
+
28
+ #
29
+ # Unit test suite
30
+ #
31
+ test_section __FILE__ do
32
+
33
+
34
+ class ConstRegexpTest < Test::Unit::TestCase
35
+
36
+ include ConstRegexp
37
+
38
+ #
39
+ # Tests
40
+ #
41
+ def test_rb_regexp
42
+ assert_match(RB_REGEXP, '//')
43
+ assert_no_match(RB_REGEXP, '')
44
+ assert_match(RB_REGEXP, '/\//')
45
+ assert_match(RB_REGEXP, '/\/\//')
46
+ assert_match(RB_REGEXP, '/reg\/e/')
47
+ assert_no_match(RB_REGEXP, '/\\/')
48
+ assert_match(RB_REGEXP, '/\\\\/')
49
+ assert_match(RB_REGEXP, '/\e/')
50
+ assert_no_match(RB_REGEXP, '/\/')
51
+ end
52
+
53
+ end # class ConstRegexpTest
54
+
55
+
56
+ end
57
+
data/src/daemon.rb ADDED
@@ -0,0 +1,135 @@
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
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: daemon.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+
9
+ require 'pathname'
10
+ require 'singleton'
11
+
12
+ require 'ruby_ex'
13
+ require 'observable'
14
+ require 'observable_pool'
15
+ require 'service_manager'
16
+
17
+
18
+ class Daemon
19
+ include Singleton
20
+ include ServiceManager
21
+ include Observable
22
+ include ObservablePool
23
+
24
+ module Notification
25
+
26
+ START = :daemon_notification_start
27
+ STOP = :daemon_notification_stop
28
+ WORKDIR = :daemon_notification_workdir
29
+ CONFIG = :daemon_notification_config
30
+ CLEAN_WORKDIR = :daemon_notification_clean_workdir
31
+ DETACH = :daemon_notification_detach
32
+ SIGNAL_TRAPS = :daemon_notification_signal_traps
33
+ RELOAD_CONFIG = :daemon_notification_reload_config
34
+
35
+ end # module Notification
36
+
37
+ def initialize
38
+ @config = nil
39
+ @workdir = Pathname.new('')
40
+ @detached = false
41
+ add_observable(self)
42
+ @logger = nil
43
+ end
44
+
45
+ def config=(config)
46
+ unless config.nil? or config.respond_to?(:__load__)
47
+ raise(NoMethodError, 'configuration class need to respond to `load\'')
48
+ end
49
+ @config = config
50
+ changed
51
+ notify_observers(Notification::CONFIG, @config)
52
+ @config
53
+ end
54
+
55
+ attr_reader :config
56
+
57
+ def workdir=(workdir)
58
+ unless workdir.to_s.empty?
59
+ unless File.directory?(workdir)
60
+ raise(ArgumentError, "`#{workdir}' - not a directory")
61
+ end
62
+ Dir.chdir(workdir)
63
+ end
64
+ @workdir = Pathname.new(workdir)
65
+ changed
66
+ notify_observers(Notification::WORKDIR, @workdir)
67
+ @workdir
68
+ end
69
+
70
+ attr_reader :workdir
71
+
72
+ def clean_workdir
73
+ @workdir.each_entry do |p|
74
+ next if p.to_s =~ /^\./
75
+ (@workdir + p).rmtree
76
+ end
77
+ changed
78
+ notify_observers(Notification::CLEAN_WORKDIR)
79
+ nil
80
+ end
81
+
82
+ def detach
83
+ return if @detached
84
+ @detached = true
85
+ if pid = fork # father
86
+ exit
87
+ else # son
88
+ Dir.chdir(@workdir.to_s.empty? ? '/' : @workdir.to_s)
89
+ Process.setsid
90
+ STDIN.close
91
+ STDOUT.close
92
+ STDERR.close
93
+ end
94
+ changed
95
+ notify_observers(Notification::DETACH)
96
+ nil
97
+ end
98
+
99
+ def detached?
100
+ @detached
101
+ end
102
+
103
+ def logger=(logger)
104
+ delete_global_observer(@logger) unless @logger.nil?
105
+ @logger = logger
106
+ add_global_observer(@logger)
107
+ changed
108
+ notify_observers(Notification::START)
109
+ end
110
+
111
+ attr_reader :logger
112
+
113
+ def stop_daemon
114
+ stop_services
115
+ changed
116
+ notify_observers(Notification::STOP)
117
+ exit
118
+ end
119
+
120
+ def reload_config
121
+ unless @config.nil?
122
+ @config.__load__
123
+ changed
124
+ notify_observers(Notification::RELOAD_CONFIG)
125
+ @config
126
+ end
127
+ end
128
+
129
+ def start_daemon(*args)
130
+ end
131
+
132
+ end # class Daemon
133
+
134
+
135
+ #FIXME: test me somehow
data/src/diff.rb ADDED
@@ -0,0 +1,665 @@
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
+
5
+ # $LastChangedBy: ertai $
6
+ # $Id: diff.rb 266 2005-06-01 14:27:18Z ertai $
7
+
8
+ require 'ruby_ex'
9
+
10
+ module Diff
11
+
12
+ def diff(other, recursive=true)
13
+ gen_diff(other, recursive) {|a, b| a == b}
14
+ end
15
+
16
+ def gen_diff(other, recursive=true, &comp)
17
+ result = {
18
+ :similar => {},
19
+ :different => {},
20
+ :missing => {},
21
+ :additional => {}
22
+ }
23
+ if not other.is_a?(self.class)
24
+ result[:different] = {:self => self.class, :other => other.class}
25
+ return result
26
+ end
27
+ self.each_pair do |k, v|
28
+ if other.has_key?(k)
29
+ if comp[v, other[k]]
30
+ result[:similar][k] = v
31
+ else
32
+ if recursive and v.respond_to?(:gen_diff)
33
+ diff_result = v.gen_diff(other[k], recursive, &comp)
34
+ else
35
+ diff_result = {:self => v, :other => other[k]}
36
+ end
37
+ result[:different][k] = diff_result unless diff_result.no_diff?
38
+ end
39
+ else
40
+ result[:additional][k] = v
41
+ end
42
+ end
43
+ other.each_pair do |k, v|
44
+ if not self.has_key?(k)
45
+ result[:missing][k] = v
46
+ end
47
+ end
48
+ result
49
+ end
50
+
51
+ def no_diff?
52
+ self[:different] == {} and self[:missing] == {} and self[:additional] == {}
53
+ end
54
+
55
+ end # module Diff
56
+
57
+
58
+ class Array
59
+
60
+ include Diff
61
+
62
+ def each_pair
63
+ for i in 0...length do
64
+ yield(i, self[i])
65
+ end
66
+ end
67
+
68
+ def has_index?(i)
69
+ 0 <= i and i < length
70
+ end
71
+
72
+ alias has_key? has_index?
73
+
74
+ end # class Array
75
+
76
+
77
+ class Hash
78
+
79
+ include Diff
80
+
81
+ def no_diff?
82
+ self[:different] == {} and self[:missing] == {} and self[:additional] == {}
83
+ end
84
+
85
+ end # class Hash
86
+
87
+
88
+ #
89
+ # Unit test suite
90
+ #
91
+ test_section __FILE__ do
92
+
93
+
94
+ #FIXME: Tidy me because some of my test are duplicated.
95
+
96
+
97
+ class DiffTest < Test::Unit::TestCase
98
+
99
+ #
100
+ # Setup
101
+ #
102
+ class A < Array ; end
103
+ class H < Hash ; end
104
+
105
+ #
106
+ # Tests
107
+ #
108
+ def test_empty
109
+ h = []
110
+ other_h = []
111
+ result_h = h.diff(h)
112
+ assert_equal(true, result_h.no_diff?)
113
+ assert_equal({}, result_h[:similar])
114
+ end
115
+
116
+ def test_simple_similar
117
+ other_h = h = [ 'toto' => 'tata' ]
118
+ result_h = {
119
+ :additional => {},
120
+ :missing => {},
121
+ :similar => { 0 => { 'toto' => 'tata' } },
122
+ :different => {}
123
+ }
124
+ assert_equal(result_h, h.diff(other_h))
125
+ end
126
+
127
+ def test_simple_additional
128
+ h = [ 'toto' => 'tata' ]
129
+ other_h = []
130
+ result_h = {
131
+ :additional => { 0 => { 'toto' => 'tata' } },
132
+ :missing => {},
133
+ :similar => {},
134
+ :different => {}
135
+ }
136
+ assert_equal(result_h, h.diff(other_h))
137
+ end
138
+
139
+ def test_simple_missing
140
+ h = []
141
+ other_h = [ 'toto' => 'tata' ]
142
+ result_h = {
143
+ :additional => {},
144
+ :missing => { 0 => { 'toto' => 'tata' } },
145
+ :similar => {},
146
+ :different => {}
147
+ }
148
+ assert_equal(result_h, h.diff(other_h))
149
+ end
150
+
151
+ def test_simple_different
152
+ h = [ 'tutu' ]
153
+ other_h = [ 'tata' ]
154
+ result_h = {
155
+ :additional => {},
156
+ :missing => {},
157
+ :similar => {},
158
+ :different => {0 => {:self => 'tutu', :other => 'tata'}},
159
+ }
160
+ assert_equal(result_h, h.diff(other_h))
161
+ end
162
+
163
+ def test_several_similar
164
+ other_h = h = [ 'toto', 'tata']
165
+ result_h = {
166
+ :additional => {},
167
+ :missing => {},
168
+ :similar => {0 => 'toto', 1 => 'tata'},
169
+ :different => {}
170
+ }
171
+ assert_equal(result_h, h.diff(other_h))
172
+ end
173
+
174
+ def test_several_additional
175
+ h = [ 'toto', 'tata' ]
176
+ other_h = []
177
+ result_h = {
178
+ :additional => {0 => 'toto', 1 => 'tata'},
179
+ :missing => {},
180
+ :similar => {},
181
+ :different => {}
182
+ }
183
+ assert_equal(result_h, h.diff(other_h))
184
+ end
185
+
186
+ def test_several_missing
187
+ h = []
188
+ other_h = [ 'toto', 'tata' ]
189
+ result_h = {
190
+ :additional => {},
191
+ :missing => {0 => 'toto', 1 => 'tata'},
192
+ :similar => {},
193
+ :different => {}
194
+ }
195
+ assert_equal(result_h, h.diff(other_h))
196
+ end
197
+
198
+ def test_several_different
199
+ h = [ 'foo', 'tutu' ]
200
+ other_h = [ 'bar', 'tata' ]
201
+ result_h = {
202
+ :additional => {},
203
+ :missing => {},
204
+ :similar => {},
205
+ :different => {
206
+ 0 => {:self => 'foo', :other => 'bar'},
207
+ 1 => {:self => 'tutu', :other => 'tata'},
208
+ }
209
+ }
210
+ assert_equal(result_h, h.diff(other_h))
211
+ end
212
+
213
+ def test_simple_missing_missing
214
+ h = []
215
+ other_h = []
216
+ h[0] = 'toto'
217
+ other_h[1] = 'foo'
218
+ result_h = {
219
+ :additional => {},
220
+ :missing => {1 => 'foo'},
221
+ :similar => {},
222
+ :different => {0 => {:self => 'toto', :other => nil}}
223
+ }
224
+ assert_equal(result_h, h.diff(other_h))
225
+ end
226
+
227
+ def test_simple_additional_similar
228
+ h = [ 'foo', 'toto' ]
229
+ other_h = [ 'foo' ]
230
+ result_h = {
231
+ :additional => {1 => 'toto'},
232
+ :missing => {},
233
+ :similar => {0 => 'foo'},
234
+ :different => {}
235
+ }
236
+ assert_equal(result_h, h.diff(other_h))
237
+ end
238
+
239
+ def test_simple_additional_different
240
+ h = [ 'toto', 'tata' ]
241
+ other_h = [ 'foo' ]
242
+ result_h = {
243
+ :additional => {1 => 'tata'},
244
+ :missing => {},
245
+ :similar => {},
246
+ :different => {0 => { :self => 'toto', :other => 'foo' } }
247
+ }
248
+ assert_equal(result_h, h.diff(other_h))
249
+ end
250
+
251
+ def test_simple_missing_similar
252
+ h = [ 'foo' ]
253
+ other_h = [ 'foo', 'toto' ]
254
+ result_h = {
255
+ :additional => {},
256
+ :missing => {1 => 'toto'},
257
+ :similar => {0 => 'foo'},
258
+ :different => {}
259
+ }
260
+ assert_equal(result_h, h.diff(other_h))
261
+ end
262
+
263
+ def test_simple_missing_different
264
+ h = ['foo']
265
+ other_h = ['bor', 'toto']
266
+ result_h = {
267
+ :additional => {},
268
+ :missing => {1 => 'toto'},
269
+ :similar => {},
270
+ :different => {0 => {:self => 'foo', :other => 'bor'}}
271
+ }
272
+ assert_equal(result_h, h.diff(other_h))
273
+ end
274
+
275
+ def test_simple_similar_different
276
+ h = ['bar', 'toto']
277
+ other_h = ['bor', 'toto']
278
+ result_h = {
279
+ :additional => {},
280
+ :missing => {},
281
+ :similar => {1 => 'toto'},
282
+ :different => {0 => {:self => 'bar', :other => 'bor'}}
283
+ }
284
+ assert_equal(result_h, h.diff(other_h))
285
+ end
286
+
287
+ def test_nested_similar_different
288
+ h = [['toto', 'bar'], 'tata']
289
+ other_h = [['toto', 'bor'], 'tata']
290
+ result_h = {
291
+ :additional => {},
292
+ :missing => {},
293
+ :similar => {1 => 'tata'},
294
+ :different => {0 => {
295
+ :additional => {},
296
+ :missing => {},
297
+ :similar => {0 => 'toto'},
298
+ :different => {1 => {:self => 'bar', :other => 'bor'}}
299
+ }
300
+ }
301
+ }
302
+ assert_equal(result_h, h.diff(other_h))
303
+ end
304
+
305
+ def test_nested_different_different
306
+ h = [['toto', 'bar'], 'tata']
307
+ other_h = [['toto', 'bor'], 'tutu']
308
+ result_h = {
309
+ :additional => {},
310
+ :missing => {},
311
+ :similar => {},
312
+ :different => {
313
+ 0 => {
314
+ :additional => {},
315
+ :missing => {},
316
+ :similar => {0 => 'toto'},
317
+ :different => {1 => {:self => 'bar', :other => 'bor'}}
318
+ },
319
+ 1 => {:self => 'tata', :other => 'tutu'}
320
+ }
321
+ }
322
+ assert_equal(result_h, h.diff(other_h))
323
+ end
324
+
325
+ def test_similar_different_additional
326
+ h = ['goo', 'wrong', 'add']
327
+ other_h = ['goo', 'bar']
328
+ result_h = {
329
+ :additional => {2 => 'add'},
330
+ :missing => {},
331
+ :similar => {0 => 'goo'},
332
+ :different => {1 => {:self => 'wrong', :other => 'bar'}}
333
+ }
334
+ assert_equal(result_h, h.diff(other_h))
335
+ end
336
+
337
+ def test_diff_class
338
+ h = ['bar', 'tata']
339
+ other_h = {0 => 'bar', 1 => 'tata'}
340
+ result_h = {
341
+ :additional => {},
342
+ :missing => {},
343
+ :similar => {},
344
+ :different => {:self => Array, :other => Hash}
345
+ }
346
+ assert_equal(result_h, h.diff(other_h))
347
+ end
348
+
349
+ def test_diff_inherited_class
350
+ h = A.new(['bar', 'tata'])
351
+ other_h = {0 => 'bar', 1 => 'tata'}
352
+ result_h = {
353
+ :additional => {},
354
+ :missing => {},
355
+ :similar => {},
356
+ :different => {:self => A, :other => Hash}
357
+ }
358
+ assert_equal(result_h, h.diff(other_h))
359
+ end
360
+
361
+ def test_array_in_hash
362
+ h = {'toto' => 'tata', 'foo' => ['goo', 'wrong', 'add']}
363
+ other_h = {'toto' => 'tata', 'foo' => ['goo', 'bar']}
364
+ result_h = {
365
+ :additional => {},
366
+ :missing => {},
367
+ :similar => {'toto' => 'tata'},
368
+ :different => {'foo' => {
369
+ :additional => {2 => 'add'},
370
+ :missing => {},
371
+ :similar => {0 => 'goo'},
372
+ :different => {1 => {:self => 'wrong', :other => 'bar'}}
373
+ }
374
+ }
375
+ }
376
+ assert_equal(result_h, h.diff(other_h))
377
+ end
378
+
379
+ def test_no_recursion
380
+ h = [['toto', 'bar'], 'tata']
381
+ other_h = [['toto', 'bor'], 'tutu']
382
+ result_h = {
383
+ :additional => {},
384
+ :missing => {},
385
+ :similar => {},
386
+ :different => {0 => {
387
+ :self => ['toto', 'bar'],
388
+ :other => ['toto', 'bor']
389
+ },
390
+ 1 => {
391
+ :self => 'tata',
392
+ :other => 'tutu'
393
+ }
394
+ }
395
+ }
396
+ assert_equal(result_h, h.diff(other_h,false))
397
+ end
398
+
399
+ def test_other_comp_fun
400
+ h = [['false']]
401
+ other_h = [[false]]
402
+ result_h = {
403
+ :additional => {},
404
+ :missing => {},
405
+ :similar => { 0 => ['false']},
406
+ :different => {}
407
+ }
408
+ assert_equal(result_h, h.gen_diff(other_h, true) {|a, b| a.to_s == b.to_s})
409
+ end
410
+
411
+ def test_empty2
412
+ other_h = h = {}
413
+ diff_h = h.diff(h)
414
+ assert_equal(true, diff_h.no_diff?)
415
+ assert_equal({}, diff_h[:similar])
416
+ end
417
+
418
+ def test_simple_similar2
419
+ other_h = h = { 'toto' => 'tata' }
420
+ result_h = {
421
+ :additional => {},
422
+ :missing => {},
423
+ :similar => { 'toto' => 'tata' },
424
+ :different => {}
425
+ }
426
+ assert_equal(result_h, h.diff(other_h))
427
+ end
428
+
429
+ def test_simple_additional2
430
+ h = { 'toto' => 'tata' }
431
+ other_h = {}
432
+ result_h = {
433
+ :additional => { 'toto' => 'tata' },
434
+ :missing => {},
435
+ :similar => {},
436
+ :different => {}
437
+ }
438
+ assert_equal(result_h, h.diff(other_h))
439
+ end
440
+
441
+ def test_simple_missing2
442
+ h = {}
443
+ other_h = { 'toto' => 'tata' }
444
+ result_h = {
445
+ :additional => {},
446
+ :missing => { 'toto' => 'tata' },
447
+ :similar => {},
448
+ :different => {}
449
+ }
450
+ assert_equal(result_h, h.diff(other_h))
451
+ end
452
+
453
+ def test_simple_different2
454
+ h = { 'toto' => 'tutu' }
455
+ other_h = { 'toto' => 'tata' }
456
+ result_h = {
457
+ :additional => {},
458
+ :missing => {},
459
+ :similar => {},
460
+ :different => { 'toto' => { :self => 'tutu', :other => 'tata' } }
461
+ }
462
+ assert_equal(result_h, h.diff(other_h))
463
+ end
464
+
465
+ def test_several_similar2
466
+ other_h = h = { 'toto' => 'tata', 'foo' => 'bar' }
467
+ result_h = {
468
+ :additional => {},
469
+ :missing => {},
470
+ :similar => { 'toto' => 'tata', 'foo' => 'bar' },
471
+ :different => {}
472
+ }
473
+ diff = h.diff(other_h)
474
+ assert_equal(result_h, diff)
475
+ assert_equal(true, diff.no_diff?)
476
+ end
477
+
478
+ def test_several_additional2
479
+ h = { 'toto' => 'tata', 'foo' => 'bar' }
480
+ other_h = {}
481
+ result_h = {
482
+ :additional => { 'toto' => 'tata', 'foo' => 'bar' },
483
+ :missing => {},
484
+ :similar => {},
485
+ :different => {}
486
+ }
487
+ assert_equal(result_h, h.diff(other_h))
488
+ end
489
+
490
+ def test_several_missing2
491
+ h = {}
492
+ other_h = { 'toto' => 'tata', 'foo' => 'bar' }
493
+ result_h = {
494
+ :additional => {},
495
+ :missing => { 'toto' => 'tata', 'foo' => 'bar' },
496
+ :similar => {},
497
+ :different => {}
498
+ }
499
+ assert_equal(result_h, h.diff(other_h))
500
+ end
501
+
502
+ def test_several_different2
503
+ h = { 'toto' => 'tutu', 'foo' => 'bor' }
504
+ other_h = { 'toto' => 'tata', 'foo' => 'bar' }
505
+ result_h = {
506
+ :additional => {},
507
+ :missing => {},
508
+ :similar => {},
509
+ :different => {
510
+ 'toto' => { :self => 'tutu', :other => 'tata' },
511
+ 'foo' => { :self => 'bor', :other => 'bar' },
512
+ }
513
+ }
514
+ assert_equal(result_h, h.diff(other_h))
515
+ end
516
+
517
+ def test_simple_missing_missing2
518
+ h = { 'toto' => 'tata' }
519
+ other_h = { 'foo' => 'bar' }
520
+ result_h = {
521
+ :additional => { 'toto' => 'tata' },
522
+ :missing => { 'foo' => 'bar' },
523
+ :similar => {},
524
+ :different => {}
525
+ }
526
+ assert_equal(result_h, h.diff(other_h))
527
+ end
528
+
529
+ def test_simple_additional_similar2
530
+ h = { 'toto' => 'tata', 'foo' => 'bar' }
531
+ other_h = { 'foo' => 'bar' }
532
+ result_h = {
533
+ :additional => { 'toto' => 'tata' },
534
+ :missing => {},
535
+ :similar => { 'foo' => 'bar' },
536
+ :different => {}
537
+ }
538
+ diff = h.diff(other_h)
539
+ assert_equal(result_h, diff)
540
+ assert_equal(false, diff.no_diff?)
541
+ end
542
+
543
+ def test_simple_additional_different2
544
+ h = { 'toto' => 'tata', 'foo' => 'bar' }
545
+ other_h = { 'foo' => 'bor' }
546
+ result_h = {
547
+ :additional => { 'toto' => 'tata' },
548
+ :missing => {},
549
+ :similar => {},
550
+ :different => { 'foo' => { :self => 'bar', :other => 'bor' } }
551
+ }
552
+ assert_equal(result_h, h.diff(other_h))
553
+ end
554
+
555
+ def test_simple_missing_similar2
556
+ h = { 'foo' => 'bar' }
557
+ other_h = { 'foo' => 'bar', 'toto' => 'tata' }
558
+ result_h = {
559
+ :additional => {},
560
+ :missing => { 'toto' => 'tata' },
561
+ :similar => { 'foo' => 'bar' },
562
+ :different => {}
563
+ }
564
+ assert_equal(result_h, h.diff(other_h))
565
+ end
566
+
567
+ def test_simple_missing_different2
568
+ h = { 'foo' => 'bar' }
569
+ other_h = { 'foo' => 'bor', 'toto' => 'tata' }
570
+ result_h = {
571
+ :additional => {},
572
+ :missing => { 'toto' => 'tata' },
573
+ :similar => {},
574
+ :different => { 'foo' => { :self => 'bar', :other => 'bor' } }
575
+ }
576
+ assert_equal(result_h, h.diff(other_h))
577
+ end
578
+
579
+ def test_simple_similar_different2
580
+ h = { 'foo' => 'bar', 'toto' => 'tata' }
581
+ other_h = { 'foo' => 'bor', 'toto' => 'tata' }
582
+ result_h = {
583
+ :additional => {},
584
+ :missing => {},
585
+ :similar => { 'toto' => 'tata' },
586
+ :different => { 'foo' => { :self => 'bar', :other => 'bor' } }
587
+ }
588
+ assert_equal(result_h, h.diff(other_h))
589
+ end
590
+
591
+ def test_nested_similar_different2
592
+ h = {'foo' => {'toto' => 'tata', 'foo' => 'bar'}, 'toto' => 'tata'}
593
+ other_h = {'foo' => {'toto' => 'tata', 'foo' => 'bor'}, 'toto' => 'tata'}
594
+ result_h = {
595
+ :additional => {},
596
+ :missing => {},
597
+ :similar => {'toto' => 'tata'},
598
+ :different => {'foo' => {
599
+ :additional => {},
600
+ :missing => {},
601
+ :similar => {'toto' => 'tata'},
602
+ :different => {'foo' => {:self => 'bar', :other => 'bor'}}
603
+ }
604
+ }
605
+ }
606
+ assert_equal(result_h, h.diff(other_h))
607
+ end
608
+
609
+ def test_diff_class2
610
+ h = {0 => 'bar', 1 => 'tata'}
611
+ other_h = ['bar', 'tata']
612
+ result_h = {
613
+ :additional => {},
614
+ :missing => {},
615
+ :similar => {},
616
+ :different => {:self => Hash, :other => Array}
617
+ }
618
+ assert_equal(result_h, h.diff(other_h))
619
+ end
620
+
621
+ def test_diff_inherited_class2
622
+ h = H.new(0 => 'bar', 1 => 'tata')
623
+ other_h = ['bar', 'tata']
624
+ result_h = {
625
+ :additional => {},
626
+ :missing => {},
627
+ :similar => {},
628
+ :different => {:self => H, :other => Array}
629
+ }
630
+ assert_equal(result_h, h.diff(other_h))
631
+ end
632
+
633
+ def test_no_recursion2
634
+ h = {'foo' => {'toto' => 'tata', 'foo' => 'bar'}, 'toto' => 'tata'}
635
+ other_h = {'foo' => {'toto' => 'tata', 'foo' => 'bor'}, 'toto' => 'tata'}
636
+ result_h = {
637
+ :additional => {},
638
+ :missing => {},
639
+ :similar => {'toto' => 'tata'},
640
+ :different => {'foo' => {
641
+ :self => {'toto' => 'tata', 'foo' => 'bar'},
642
+ :other => {'toto' => 'tata', 'foo' => 'bor'}
643
+ }
644
+ }
645
+ }
646
+ assert_equal(result_h, h.diff(other_h, false))
647
+ end
648
+
649
+ def test_other_comp_fun2
650
+ h = {'foo' => {'toto' => 'false', 'foo' => '0'}}
651
+ other_h = {'foo' => {'toto' => false, 'foo' => 0}}
652
+ result_h = {
653
+ :additional => {},
654
+ :missing => {},
655
+ :similar => {'foo' => {'toto' => 'false', 'foo' => '0'}},
656
+ :different => {}
657
+ }
658
+ assert_equal(result_h, h.gen_diff(other_h, true) {|a, b| a.to_s == b.to_s})
659
+ end
660
+
661
+ end # class DiffTest
662
+
663
+
664
+ end
665
+