kagemusha 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.ja.txt CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ == 0.0.6 2008-07-17
3
+
4
+ * Kagemusha::Compositeクラスを追加
5
+ * Kagemusha#+を追加
6
+ * private/protectedなインスタンスメソッドの置換に対応
7
+
2
8
  == 0.0.5 2008-07-16
3
9
 
4
10
  * Kernelモジュールのメソッドの復元に失敗する不具合を修正
data/History.txt CHANGED
@@ -1,4 +1,8 @@
1
1
 
2
+ == 0.0.6 2008-07-17
3
+
4
+ ...
5
+
2
6
  == 0.0.5 2008-07-16
3
7
 
4
8
  ...
@@ -1,6 +1,10 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: core.rb 67 2008-07-16 04:15:10Z yuyakato $
3
+ # $Id: core.rb 97 2008-07-17 13:40:55Z yuyakato $
4
+ #==============================================================================#
5
+
6
+ require File.join(File.dirname(__FILE__), "composite")
7
+
4
8
  #==============================================================================#
5
9
 
6
10
  class Kagemusha #:nodoc:
@@ -40,80 +44,111 @@ class Kagemusha #:nodoc:
40
44
  alias :undef :undefine_instance_method
41
45
 
42
46
  def swap #:nodoc:
43
- original_class_methods = {}
44
- original_instance_methods = {}
45
-
46
- @class_methods.each { |name, proc|
47
+ recover_class_methods = @class_methods.map { |name, proc|
47
48
  if proc
48
49
  begin
49
50
  # replace method
51
+ attr = get_accessibility(@meta, name.to_s)
52
+
53
+ methods = @klass.singleton_methods(false)
54
+
50
55
  method = @meta.instance_method(name)
51
- method = :remove unless @klass.singleton_methods(false).include?(name.to_s)
56
+
52
57
  @meta.instance_eval { define_method(name, proc) }
53
- original_class_methods[name] = method
58
+ @meta.instance_eval { private(name) } if attr == :private
59
+
60
+ (methods.include?(name.to_s) ? [name, :define, nil, method] : [name, :remove])
54
61
  rescue NameError
55
62
  # insert method
56
63
  @meta.instance_eval { define_method(name, proc) }
57
- original_class_methods[name] = :undef
64
+ [name, :undef]
58
65
  end
59
66
  else
60
67
  begin
61
68
  # remove method
62
69
  method = @meta.instance_method(name)
63
70
  @meta.instance_eval { undef_method(name) }
64
- original_class_methods[name] = method
71
+ [name, :define, nil, method] # FIXME: attr
65
72
  rescue NameError
66
- # nop
73
+ [name, :nop]
67
74
  end
68
75
  end
69
76
  }
70
77
 
71
- @instance_methods.each { |name, proc|
78
+ recover_instance_methods = @instance_methods.map { |name, proc|
72
79
  if proc
73
80
  begin
74
81
  # replace method
82
+ attr = get_accessibility(@klass, name.to_s)
83
+
84
+ methods = @klass.public_instance_methods(false)
85
+ methods += @klass.protected_instance_methods(false)
86
+ methods += @klass.private_instance_methods(false)
87
+
75
88
  method = @klass.instance_method(name)
76
- if @klass == Kernel
77
- method = :remove unless @klass.methods(false).include?(name.to_s)
78
- else
79
- method = :remove unless @klass.instance_methods(false).include?(name.to_s)
80
- end
89
+
81
90
  @klass.instance_eval { define_method(name, proc) }
82
- original_instance_methods[name] = method
91
+ @klass.instance_eval { protected(name) } if attr == :protected
92
+ @klass.instance_eval { private(name) } if attr == :private
93
+
94
+ (methods.include?(name.to_s) ? [name, :define, attr, method] : [name, :remove])
83
95
  rescue NameError
84
96
  # insert method
85
97
  @klass.instance_eval { define_method(name, proc) }
86
- original_instance_methods[name] = :undef
98
+ [name, :undef]
87
99
  end
88
100
  else
89
101
  begin
90
102
  # remove method
91
103
  method = @klass.instance_method(name)
92
104
  @klass.instance_eval { undef_method(name) }
93
- original_instance_methods[name] = method
105
+ [name, :define, nil, method] # FIXME: attr
94
106
  rescue NameError
95
- # nop
107
+ [name, :nop]
96
108
  end
97
109
  end
98
110
  }
99
111
 
100
112
  return yield
101
113
  ensure
102
- original_class_methods.each { |name, method|
103
- case method
114
+ recover_class_methods.each { |name, type, attr, method|
115
+ case type
116
+ when :nop then # nop
104
117
  when :remove then @meta.instance_eval { remove_method(name) }
105
118
  when :undef then @meta.instance_eval { undef_method(name) }
106
- else @meta.instance_eval { define_method(name, method) }
119
+ when :define
120
+ @meta.instance_eval { define_method(name, method) }
121
+ @meta.instance_eval { private(name) } if attr == :private
122
+ else raise("BUG")
107
123
  end
108
124
  }
109
- original_instance_methods.each { |name, method|
110
- case method
125
+ recover_instance_methods.each { |name, type, attr, method|
126
+ case type
127
+ when :nop then # nop
111
128
  when :remove then @klass.instance_eval { remove_method(name) }
112
129
  when :undef then @klass.instance_eval { undef_method(name) }
113
- else @klass.instance_eval { define_method(name, method) }
130
+ when :define
131
+ @klass.instance_eval { define_method(name, method) }
132
+ @klass.instance_eval { protected(name) } if attr == :protected
133
+ @klass.instance_eval { private(name) } if attr == :private
134
+ else raise("BUG")
114
135
  end
115
136
  }
116
137
  end
138
+
139
+ def concat(mock)
140
+ return Kagemusha::Composite.new(self, mock)
141
+ end
142
+ alias + concat
143
+
144
+ private
145
+
146
+ def get_accessibility(target, name)
147
+ return :public if target.public_instance_methods(true).include?(name)
148
+ return :protected if target.protected_instance_methods(true).include?(name)
149
+ return :private if target.private_instance_methods(true).include?(name)
150
+ return nil
151
+ end
117
152
  end
118
153
 
119
154
  #==============================================================================#
@@ -1,6 +1,6 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: datetime.rb 44 2007-09-07 02:32:58Z yuyakato $
3
+ # $Id: datetime.rb 99 2008-07-17 13:56:26Z yuyakato $
4
4
  #==============================================================================#
5
5
 
6
6
  require File.join(File.dirname(__FILE__), "date")
@@ -9,11 +9,7 @@ require File.join(File.dirname(__FILE__), "time")
9
9
  #==============================================================================#
10
10
 
11
11
  class Kagemusha #:nodoc:
12
- class DateTime #:nodoc:
13
- def initialize(mock1, mock2)
14
- @mock1, @mock2 = mock1, mock2
15
- end
16
-
12
+ module DateTime #:nodoc:
17
13
  def self.at(time_or_year, mon = 1, mday = 1, hour = 0, min = 0, sec = 0, &block)
18
14
  if block_given?
19
15
  return self.at(time_or_year, mon, mday, hour, min, sec).swap(&block)
@@ -35,15 +31,7 @@ class Kagemusha #:nodoc:
35
31
  datemusha = Kagemusha::Date.on(date)
36
32
  timemusha = Kagemusha::Time.at(time)
37
33
 
38
- return self.new(datemusha, timemusha)
39
- end
40
-
41
- def swap
42
- @mock1.swap {
43
- @mock2.swap {
44
- return yield
45
- }
46
- }
34
+ return datemusha + timemusha
47
35
  end
48
36
  end
49
37
  end
@@ -1,13 +1,13 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: version.rb 69 2008-07-16 04:18:55Z yuyakato $
3
+ # $Id: version.rb 83 2008-07-17 05:22:48Z yuyakato $
4
4
  #==============================================================================#
5
5
 
6
6
  class Kagemusha #:nodoc:
7
7
  module VERSION #:nodoc:
8
8
  MAJOR = 0
9
9
  MINOR = 0
10
- TINY = 5
10
+ TINY = 6
11
11
 
12
12
  STRING = [MAJOR, MINOR, TINY].join('.')
13
13
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: test_complex_cases.rb 60 2008-07-09 08:15:02Z yuyakato $
3
+ # $Id: test_complex_cases.rb 84 2008-07-17 05:37:11Z yuyakato $
4
4
  #==============================================================================#
5
5
 
6
6
  require File.dirname(__FILE__) + "/test_helper.rb"
@@ -12,12 +12,27 @@ class TestComplexCases < Test::Unit::TestCase
12
12
  def m1_instance
13
13
  return :m1_instance
14
14
  end
15
+
16
+ def m1_private_instance
17
+ return :m1_private_instance
18
+ end
19
+ private :m1_private_instance
20
+
21
+ def m1_protected_instance
22
+ return :m1_protected_instance
23
+ end
24
+ protected :m1_protected_instance
15
25
  end
16
26
 
17
27
  module M2
18
28
  def m2_class
19
29
  return :m2_class
20
30
  end
31
+
32
+ def m2_private_class
33
+ return :m2_private_class
34
+ end
35
+ private :m2_private_class
21
36
  end
22
37
 
23
38
  class C1
@@ -28,9 +43,24 @@ class TestComplexCases < Test::Unit::TestCase
28
43
  return :c1_class
29
44
  end
30
45
 
46
+ def self.c1_private_class
47
+ return :c1_private_class
48
+ end
49
+ private_class_method :c1_private_class
50
+
31
51
  def c1_instance
32
52
  return :c1_instance
33
53
  end
54
+
55
+ def c1_private_instance
56
+ return :c1_private_instance
57
+ end
58
+ private :c1_private_instance
59
+
60
+ def c1_protected_instance
61
+ return :c1_protected_instance
62
+ end
63
+ protected :c1_protected_instance
34
64
  end
35
65
 
36
66
  class C2 < C1
@@ -53,16 +83,25 @@ class TestComplexCases < Test::Unit::TestCase
53
83
  assert_equal(old_value, c1.c1_instance)
54
84
  assert_equal(old_value, c2.c1_instance)
55
85
  assert_equal(old_value, c3.c1_instance)
86
+ c1_method_set = get_method_set(C1)
87
+ c2_method_set = get_method_set(C2)
88
+ c3_method_set = get_method_set(C3)
56
89
 
57
90
  musha.swap {
58
91
  assert_equal(new_value, c1.c1_instance)
59
92
  assert_equal(new_value, c2.c1_instance)
60
93
  assert_equal(new_value, c3.c1_instance)
94
+ assert_equal(c1_method_set, get_method_set(C1))
95
+ assert_equal(c2_method_set, get_method_set(C2))
96
+ assert_equal(c3_method_set, get_method_set(C3))
61
97
  }
62
98
 
63
99
  assert_equal(old_value, c1.c1_instance)
64
100
  assert_equal(old_value, c2.c1_instance)
65
101
  assert_equal(old_value, c3.c1_instance)
102
+ assert_equal(c1_method_set, get_method_set(C1))
103
+ assert_equal(c2_method_set, get_method_set(C2))
104
+ assert_equal(c3_method_set, get_method_set(C3))
66
105
 
67
106
  def c1.c1_instance
68
107
  return :singleton
@@ -83,6 +122,76 @@ class TestComplexCases < Test::Unit::TestCase
83
122
  assert_equal(old_value, c3.c1_instance)
84
123
  end
85
124
 
125
+ def test_replace_private_instance_method_defined_on_self_class
126
+ c1 = C1.new
127
+ c2 = C2.new
128
+ c3 = C3.new
129
+
130
+ old_value = :c1_private_instance
131
+ new_value = :c1_private_instance_replaced1
132
+
133
+ musha = Kagemusha.new(C1)
134
+ musha.def(:c1_private_instance) { new_value }
135
+
136
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
137
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
138
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
139
+ assert_raise(NoMethodError) { c1.c1_private_instance }
140
+ assert_raise(NoMethodError) { c2.c1_private_instance }
141
+ assert_raise(NoMethodError) { c3.c1_private_instance }
142
+
143
+ musha.swap {
144
+ assert_equal(new_value, c1.instance_eval { c1_private_instance })
145
+ assert_equal(new_value, c2.instance_eval { c1_private_instance })
146
+ assert_equal(new_value, c3.instance_eval { c1_private_instance })
147
+ assert_raise(NoMethodError) { c1.c1_private_instance }
148
+ assert_raise(NoMethodError) { c2.c1_private_instance }
149
+ assert_raise(NoMethodError) { c3.c1_private_instance }
150
+ }
151
+
152
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
153
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
154
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
155
+ assert_raise(NoMethodError) { c1.c1_private_instance }
156
+ assert_raise(NoMethodError) { c2.c1_private_instance }
157
+ assert_raise(NoMethodError) { c3.c1_private_instance }
158
+ end
159
+
160
+ def test_replace_protected_instance_method_defined_on_self_class
161
+ c1 = C1.new
162
+ c2 = C2.new
163
+ c3 = C3.new
164
+
165
+ old_value = :c1_protected_instance
166
+ new_value = :c1_protected_instance_replaced1
167
+
168
+ musha = Kagemusha.new(C1)
169
+ musha.def(:c1_protected_instance) { new_value }
170
+
171
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
172
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
173
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
174
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
175
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
176
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
177
+
178
+ musha.swap {
179
+ assert_equal(new_value, c1.instance_eval { c1_protected_instance })
180
+ assert_equal(new_value, c2.instance_eval { c1_protected_instance })
181
+ assert_equal(new_value, c3.instance_eval { c1_protected_instance })
182
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
183
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
184
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
185
+ }
186
+
187
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
188
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
189
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
190
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
191
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
192
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
193
+ end
194
+
86
195
  def test_replace_class_method_defined_on_self_class
87
196
  old_value = :c1_class
88
197
  new_value = :c1_class_replaced1
@@ -105,6 +214,37 @@ class TestComplexCases < Test::Unit::TestCase
105
214
  assert_equal(old_value, C3.c1_class) # FIXME: TypeError: singleton method bound for a different object
106
215
  end
107
216
 
217
+ def test_replace_private_class_method_defined_on_self_class
218
+ old_value = :c1_private_class
219
+ new_value = :c1_private_class_replaced1
220
+
221
+ musha = Kagemusha.new(C1)
222
+ musha.defs(:c1_private_class) { new_value }
223
+
224
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
225
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
226
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
227
+ assert_raise(NoMethodError) { C1.c1_private_class }
228
+ assert_raise(NoMethodError) { C2.c1_private_class }
229
+ assert_raise(NoMethodError) { C3.c1_private_class }
230
+
231
+ musha.swap {
232
+ assert_equal(new_value, C1.instance_eval { c1_private_class })
233
+ assert_equal(new_value, C2.instance_eval { c1_private_class })
234
+ assert_equal(new_value, C3.instance_eval { c1_private_class })
235
+ assert_raise(NoMethodError) { C1.c1_private_class }
236
+ assert_raise(NoMethodError) { C2.c1_private_class }
237
+ assert_raise(NoMethodError) { C3.c1_private_class }
238
+ }
239
+
240
+ assert_equal(old_value, C1.instance_eval { c1_private_class }) # FIXME: NameError: undefined local variable or method `c1_private_class' for TestComplexCases::C1:Class
241
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
242
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
243
+ assert_raise(NoMethodError) { C1.c1_private_class }
244
+ assert_raise(NoMethodError) { C2.c1_private_class }
245
+ assert_raise(NoMethodError) { C3.c1_private_class }
246
+ end
247
+
108
248
  def test_replace_instance_method_defined_on_parent_class
109
249
  c1 = C1.new
110
250
  c2 = C2.new
@@ -149,6 +289,76 @@ class TestComplexCases < Test::Unit::TestCase
149
289
  assert_equal(old_value, c3.c1_instance)
150
290
  end
151
291
 
292
+ def test_replace_private_instance_method_defined_on_parent_class
293
+ c1 = C1.new
294
+ c2 = C2.new
295
+ c3 = C3.new
296
+
297
+ old_value = :c1_private_instance
298
+ new_value = :c1_private_instance_replaced2
299
+
300
+ musha = Kagemusha.new(C2)
301
+ musha.def(:c1_private_instance) { new_value }
302
+
303
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
304
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
305
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
306
+ assert_raise(NoMethodError) { c1.c1_private_instance }
307
+ assert_raise(NoMethodError) { c2.c1_private_instance }
308
+ assert_raise(NoMethodError) { c3.c1_private_instance }
309
+
310
+ musha.swap {
311
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
312
+ assert_equal(new_value, c2.instance_eval { c1_private_instance })
313
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
314
+ assert_raise(NoMethodError) { c1.c1_private_instance }
315
+ assert_raise(NoMethodError) { c2.c1_private_instance }
316
+ assert_raise(NoMethodError) { c3.c1_private_instance }
317
+ }
318
+
319
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
320
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
321
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
322
+ assert_raise(NoMethodError) { c1.c1_private_instance }
323
+ assert_raise(NoMethodError) { c2.c1_private_instance }
324
+ assert_raise(NoMethodError) { c3.c1_private_instance }
325
+ end
326
+
327
+ def test_replace_protected_instance_method_defined_on_parent_class
328
+ c1 = C1.new
329
+ c2 = C2.new
330
+ c3 = C3.new
331
+
332
+ old_value = :c1_protected_instance
333
+ new_value = :c1_protected_instance_replaced2
334
+
335
+ musha = Kagemusha.new(C2)
336
+ musha.def(:c1_protected_instance) { new_value }
337
+
338
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
339
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
340
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
341
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
342
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
343
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
344
+
345
+ musha.swap {
346
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
347
+ assert_equal(new_value, c2.instance_eval { c1_protected_instance })
348
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
349
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
350
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
351
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
352
+ }
353
+
354
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
355
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
356
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
357
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
358
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
359
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
360
+ end
361
+
152
362
  def test_replace_class_method_defined_on_parent_class
153
363
  old_value = :c1_class
154
364
  new_value = :c1_class_replaced2
@@ -171,6 +381,37 @@ class TestComplexCases < Test::Unit::TestCase
171
381
  assert_equal(old_value, C3.c1_class)
172
382
  end
173
383
 
384
+ def test_replace_private_class_method_defined_on_parent_class
385
+ old_value = :c1_private_class
386
+ new_value = :c1_private_class_replaced2
387
+
388
+ musha = Kagemusha.new(C2)
389
+ musha.defs(:c1_private_class) { new_value }
390
+
391
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
392
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
393
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
394
+ assert_raise(NoMethodError) { C1.c1_private_class }
395
+ assert_raise(NoMethodError) { C2.c1_private_class }
396
+ assert_raise(NoMethodError) { C3.c1_private_class }
397
+
398
+ musha.swap {
399
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
400
+ assert_equal(new_value, C2.instance_eval { c1_private_class })
401
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
402
+ assert_raise(NoMethodError) { C1.c1_private_class }
403
+ assert_raise(NoMethodError) { C2.c1_private_class }
404
+ assert_raise(NoMethodError) { C3.c1_private_class }
405
+ }
406
+
407
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
408
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
409
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
410
+ assert_raise(NoMethodError) { C1.c1_private_class }
411
+ assert_raise(NoMethodError) { C2.c1_private_class }
412
+ assert_raise(NoMethodError) { C3.c1_private_class }
413
+ end
414
+
174
415
  def test_replace_instance_method_defined_on_self_class_including_module
175
416
  c1 = C1.new
176
417
  c2 = C2.new
@@ -215,6 +456,76 @@ class TestComplexCases < Test::Unit::TestCase
215
456
  assert_equal(old_value, c3.m1_instance)
216
457
  end
217
458
 
459
+ def test_replace_private_instance_method_defined_on_self_class_including_module
460
+ c1 = C1.new
461
+ c2 = C2.new
462
+ c3 = C3.new
463
+
464
+ old_value = :m1_private_instance
465
+ new_value = :m1_private_instance_replaced1
466
+
467
+ musha = Kagemusha.new(C1)
468
+ musha.def(:m1_private_instance) { new_value }
469
+
470
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
471
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
472
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
473
+ assert_raise(NoMethodError) { c1.m1_private_instance }
474
+ assert_raise(NoMethodError) { c2.m1_private_instance }
475
+ assert_raise(NoMethodError) { c3.m1_private_instance }
476
+
477
+ musha.swap {
478
+ assert_equal(new_value, c1.instance_eval { m1_private_instance })
479
+ assert_equal(new_value, c2.instance_eval { m1_private_instance })
480
+ assert_equal(new_value, c3.instance_eval { m1_private_instance })
481
+ assert_raise(NoMethodError) { c1.m1_private_instance }
482
+ assert_raise(NoMethodError) { c2.m1_private_instance }
483
+ assert_raise(NoMethodError) { c3.m1_private_instance }
484
+ }
485
+
486
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
487
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
488
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
489
+ assert_raise(NoMethodError) { c1.m1_private_instance }
490
+ assert_raise(NoMethodError) { c2.m1_private_instance }
491
+ assert_raise(NoMethodError) { c3.m1_private_instance }
492
+ end
493
+
494
+ def test_replace_protected_instance_method_defined_on_self_class_including_module
495
+ c1 = C1.new
496
+ c2 = C2.new
497
+ c3 = C3.new
498
+
499
+ old_value = :m1_protected_instance
500
+ new_value = :m1_protected_instance_replaced1
501
+
502
+ musha = Kagemusha.new(C1)
503
+ musha.def(:m1_protected_instance) { new_value }
504
+
505
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
506
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
507
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
508
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
509
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
510
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
511
+
512
+ musha.swap {
513
+ assert_equal(new_value, c1.instance_eval { m1_protected_instance })
514
+ assert_equal(new_value, c2.instance_eval { m1_protected_instance })
515
+ assert_equal(new_value, c3.instance_eval { m1_protected_instance })
516
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
517
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
518
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
519
+ }
520
+
521
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
522
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
523
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
524
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
525
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
526
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
527
+ end
528
+
218
529
  def test_replace_class_method_defined_on_self_class_extending_module
219
530
  old_value = :m2_class
220
531
  new_value = :m2_class_replaced1
@@ -237,6 +548,37 @@ class TestComplexCases < Test::Unit::TestCase
237
548
  assert_equal(old_value, C3.m2_class)
238
549
  end
239
550
 
551
+ def test_replace_private_class_method_defined_on_self_class_extending_module
552
+ old_value = :m2_private_class
553
+ new_value = :m2_private_class_replaced1
554
+
555
+ musha = Kagemusha.new(C1)
556
+ musha.defs(:m2_private_class) { new_value }
557
+
558
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
559
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
560
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
561
+ assert_raise(NoMethodError) { C1.m2_private_class }
562
+ assert_raise(NoMethodError) { C2.m2_private_class }
563
+ assert_raise(NoMethodError) { C3.m2_private_class }
564
+
565
+ musha.swap {
566
+ assert_equal(new_value, C1.instance_eval { m2_private_class })
567
+ assert_equal(new_value, C2.instance_eval { m2_private_class })
568
+ assert_equal(new_value, C3.instance_eval { m2_private_class })
569
+ assert_raise(NoMethodError) { C1.m2_private_class }
570
+ assert_raise(NoMethodError) { C2.m2_private_class }
571
+ assert_raise(NoMethodError) { C3.m2_private_class }
572
+ }
573
+
574
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
575
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
576
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
577
+ assert_raise(NoMethodError) { C1.m2_private_class }
578
+ assert_raise(NoMethodError) { C2.m2_private_class }
579
+ assert_raise(NoMethodError) { C3.m2_private_class }
580
+ end
581
+
240
582
  def test_replace_instance_method_defined_on_parent_class_including_module
241
583
  c1 = C1.new
242
584
  c2 = C2.new
@@ -281,6 +623,76 @@ class TestComplexCases < Test::Unit::TestCase
281
623
  assert_equal(old_value, c3.m1_instance)
282
624
  end
283
625
 
626
+ def test_replace_private_instance_method_defined_on_parent_class_including_module
627
+ c1 = C1.new
628
+ c2 = C2.new
629
+ c3 = C3.new
630
+
631
+ old_value = :m1_private_instance
632
+ new_value = :m1_private_instance_replaced2
633
+
634
+ musha = Kagemusha.new(C2)
635
+ musha.def(:m1_private_instance) { new_value }
636
+
637
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
638
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
639
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
640
+ assert_raise(NoMethodError) { c1.m1_private_instance }
641
+ assert_raise(NoMethodError) { c2.m1_private_instance }
642
+ assert_raise(NoMethodError) { c3.m1_private_instance }
643
+
644
+ musha.swap {
645
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
646
+ assert_equal(new_value, c2.instance_eval { m1_private_instance })
647
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
648
+ assert_raise(NoMethodError) { c1.m1_private_instance }
649
+ assert_raise(NoMethodError) { c2.m1_private_instance }
650
+ assert_raise(NoMethodError) { c3.m1_private_instance }
651
+ }
652
+
653
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
654
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
655
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
656
+ assert_raise(NoMethodError) { c1.m1_private_instance }
657
+ assert_raise(NoMethodError) { c2.m1_private_instance }
658
+ assert_raise(NoMethodError) { c3.m1_private_instance }
659
+ end
660
+
661
+ def test_replace_protected_instance_method_defined_on_parent_class_including_module
662
+ c1 = C1.new
663
+ c2 = C2.new
664
+ c3 = C3.new
665
+
666
+ old_value = :m1_protected_instance
667
+ new_value = :m1_protected_instance_replaced2
668
+
669
+ musha = Kagemusha.new(C2)
670
+ musha.def(:m1_protected_instance) { new_value }
671
+
672
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
673
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
674
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
675
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
676
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
677
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
678
+
679
+ musha.swap {
680
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
681
+ assert_equal(new_value, c2.instance_eval { m1_protected_instance })
682
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
683
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
684
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
685
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
686
+ }
687
+
688
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
689
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
690
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
691
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
692
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
693
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
694
+ end
695
+
284
696
  def test_replace_class_method_defined_on_parent_class_extending_module
285
697
  old_value = :m2_class
286
698
  new_value = :m2_class_replaced2
@@ -302,6 +714,60 @@ class TestComplexCases < Test::Unit::TestCase
302
714
  assert_equal(old_value, C2.m2_class)
303
715
  assert_equal(old_value, C3.m2_class)
304
716
  end
717
+
718
+ def test_replace_private_class_method_defined_on_parent_class_extending_module
719
+ old_value = :m2_private_class
720
+ new_value = :m2_private_class_replaced2
721
+
722
+ musha = Kagemusha.new(C2)
723
+ musha.defs(:m2_private_class) { new_value }
724
+
725
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
726
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
727
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
728
+ assert_raise(NoMethodError) { C1.m2_private_class }
729
+ assert_raise(NoMethodError) { C2.m2_private_class }
730
+ assert_raise(NoMethodError) { C3.m2_private_class }
731
+
732
+ musha.swap {
733
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
734
+ assert_equal(new_value, C2.instance_eval { m2_private_class })
735
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
736
+ assert_raise(NoMethodError) { C1.m2_private_class }
737
+ assert_raise(NoMethodError) { C2.m2_private_class }
738
+ assert_raise(NoMethodError) { C3.m2_private_class }
739
+ }
740
+
741
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
742
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
743
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
744
+ assert_raise(NoMethodError) { C1.m2_private_class }
745
+ assert_raise(NoMethodError) { C2.m2_private_class }
746
+ assert_raise(NoMethodError) { C3.m2_private_class }
747
+ end
748
+
749
+ private
750
+
751
+ def get_method_set(obj)
752
+ return {
753
+ :self => {
754
+ :public => obj.public_methods(false),
755
+ :public_with_inherited => obj.public_methods(true),
756
+ :protected => obj.protected_methods(false),
757
+ :protected_with_inherited => obj.protected_methods(true),
758
+ :private => obj.private_methods(false),
759
+ :private_with_inherited => obj.private_methods(true),
760
+ },
761
+ :instance => (obj.kind_of?(Module) ? {
762
+ :public => obj.public_instance_methods(false),
763
+ :public_with_inherited => obj.public_instance_methods(true),
764
+ :protected => obj.protected_instance_methods(false),
765
+ :protected_with_inherited => obj.protected_instance_methods(true),
766
+ :private => obj.private_instance_methods(false),
767
+ :private_with_inherited => obj.private_instance_methods(true),
768
+ } : nil),
769
+ }
770
+ end
305
771
  end
306
772
 
307
773
  #==============================================================================#
@@ -0,0 +1,106 @@
1
+
2
+ #==============================================================================#
3
+ # $Id: test_composite.rb 99 2008-07-17 13:56:26Z yuyakato $
4
+ #==============================================================================#
5
+
6
+ require File.dirname(__FILE__) + "/test_helper.rb"
7
+ require "date"
8
+
9
+ #==============================================================================#
10
+
11
+ class TestComposite < Test::Unit::TestCase
12
+ def setup
13
+ @com = Kagemusha::Composite.new
14
+ @musha1 = Kagemusha.new(Time)
15
+ @musha2 = Kagemusha.new(Date)
16
+ @musha3 = Kagemusha.new(Regexp)
17
+ end
18
+
19
+ def test_initialize
20
+ com1 = Kagemusha::Composite.new
21
+ assert_equal([], com1.mocks)
22
+
23
+ com2 = Kagemusha::Composite.new(@musha1)
24
+ assert_equal([@musha1], com2.mocks)
25
+
26
+ com3 = Kagemusha::Composite.new(@musha1, @musha2)
27
+ assert_equal([@musha1, @musha2], com3.mocks)
28
+ end
29
+
30
+ def test_size
31
+ assert_equal(0, @com.size)
32
+
33
+ @com << @musha1
34
+ assert_equal(1, @com.size)
35
+
36
+ @com << @musha2
37
+ assert_equal(2, @com.size)
38
+ end
39
+
40
+ def test_add
41
+ assert_equal([], @com.mocks)
42
+
43
+ assert_equal(@com, @com.add(@musha1))
44
+ assert_equal([@musha1], @com.mocks)
45
+
46
+ assert_equal(@com, @com << @musha2)
47
+ assert_equal([@musha1, @musha2], @com.mocks)
48
+
49
+ assert_raise(ArgumentError) {
50
+ @com << 1
51
+ }
52
+ end
53
+
54
+ def test_concat
55
+ com1 = @com
56
+ assert_equal([], com1.mocks)
57
+
58
+ com2 = com1.concat(@musha1)
59
+ assert_equal([], com1.mocks)
60
+ assert_equal([@musha1], com2.mocks)
61
+
62
+ com3 = com2 + @musha2
63
+ assert_equal([], com1.mocks)
64
+ assert_equal([@musha1], com2.mocks)
65
+ assert_equal([@musha1, @musha2], com3.mocks)
66
+
67
+ com4 = Kagemusha::Composite.new(@musha3)
68
+
69
+ com5 = com3 + com4
70
+ assert_equal([], com1.mocks)
71
+ assert_equal([@musha1], com2.mocks)
72
+ assert_equal([@musha1, @musha2], com3.mocks)
73
+ assert_equal([@musha1, @musha2, com4], com5.mocks)
74
+ end
75
+
76
+ def test_swap
77
+ @musha1.defs(:now) { :now }
78
+ @musha2.defs(:today) { :today }
79
+
80
+ assert_not_equal(:now, Time.now)
81
+ assert_not_equal(:today, Date.today)
82
+
83
+ @musha1.swap {
84
+ @musha2.swap {
85
+ assert_equal(:now, Time.now)
86
+ assert_equal(:today, Date.today)
87
+ }
88
+ }
89
+
90
+ assert_not_equal(:now, Time.now)
91
+ assert_not_equal(:today, Date.today)
92
+
93
+ ret = (@musha1 + @musha2).swap {
94
+ assert_equal(:now, Time.now)
95
+ assert_equal(:today, Date.today)
96
+ 1
97
+ }
98
+ assert_equal(1, ret)
99
+
100
+ assert_not_equal(:now, Time.now)
101
+ assert_not_equal(:today, Date.today)
102
+ end
103
+ end
104
+
105
+ #==============================================================================#
106
+ #==============================================================================#
data/test/test_helper.rb CHANGED
@@ -1,10 +1,13 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: test_helper.rb 27 2007-08-15 14:35:31Z yuyakato $
3
+ # $Id: test_helper.rb 90 2008-07-17 12:31:43Z yuyakato $
4
4
  #==============================================================================#
5
5
 
6
6
  require "test/unit"
7
7
  require File.dirname(__FILE__) + "/../lib/kagemusha"
8
8
 
9
+ require "rubygems"
10
+ require "redgreen"
11
+
9
12
  #==============================================================================#
10
13
  #==============================================================================#
@@ -1,6 +1,6 @@
1
1
 
2
2
  #==============================================================================#
3
- # $Id: test_kagemusha.rb 45 2007-09-07 04:49:51Z yuyakato $
3
+ # $Id: test_kagemusha.rb 97 2008-07-17 13:40:55Z yuyakato $
4
4
  #==============================================================================#
5
5
 
6
6
  require File.dirname(__FILE__) + "/test_helper.rb"
@@ -239,6 +239,17 @@ class TestKagemusha < Test::Unit::TestCase
239
239
  "".clone
240
240
  }
241
241
  end
242
+
243
+ def test_concat
244
+ musha2 = Kagemusha.new(Time)
245
+ musha3 = Kagemusha.new(Regexp)
246
+
247
+ com1 = @musha.concat(musha2)
248
+ assert_equal([@musha, musha2], com1.mocks)
249
+
250
+ com2 = musha3 + com1
251
+ assert_equal([musha3, com1], com2.mocks)
252
+ end
242
253
  end
243
254
 
244
255
  #==============================================================================#
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kagemusha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya Kato
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-16 00:00:00 +09:00
12
+ date: 2008-07-17 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -115,6 +115,7 @@ summary: Kagemusha is a library of helper functions for testing Ruby scripts.
115
115
  test_files:
116
116
  - test/test_bugs.rb
117
117
  - test/test_complex_cases.rb
118
+ - test/test_composite.rb
118
119
  - test/test_date.rb
119
120
  - test/test_datetime.rb
120
121
  - test/test_helper.rb