nayutaya-kagemusha 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,783 @@
1
+ # coding: utf-8
2
+
3
+ #==============================================================================#
4
+ # $Id: test_complex_cases.rb 115 2009-02-09 07:58:44Z yuyakato $
5
+ #==============================================================================#
6
+
7
+ require File.dirname(__FILE__) + "/test_helper.rb"
8
+
9
+ #==============================================================================#
10
+
11
+ class TestComplexCases < Test::Unit::TestCase
12
+ module M1
13
+ def m1_instance
14
+ return :m1_instance
15
+ end
16
+
17
+ def m1_private_instance
18
+ return :m1_private_instance
19
+ end
20
+ private :m1_private_instance
21
+
22
+ def m1_protected_instance
23
+ return :m1_protected_instance
24
+ end
25
+ protected :m1_protected_instance
26
+ end
27
+
28
+ module M2
29
+ def m2_class
30
+ return :m2_class
31
+ end
32
+
33
+ def m2_private_class
34
+ return :m2_private_class
35
+ end
36
+ private :m2_private_class
37
+ end
38
+
39
+ class C1
40
+ include M1
41
+ extend M2
42
+
43
+ def self.c1_class
44
+ return :c1_class
45
+ end
46
+
47
+ def self.c1_private_class
48
+ return :c1_private_class
49
+ end
50
+ private_class_method :c1_private_class
51
+
52
+ def c1_instance
53
+ return :c1_instance
54
+ end
55
+
56
+ def c1_private_instance
57
+ return :c1_private_instance
58
+ end
59
+ private :c1_private_instance
60
+
61
+ def c1_protected_instance
62
+ return :c1_protected_instance
63
+ end
64
+ protected :c1_protected_instance
65
+ end
66
+
67
+ class C2 < C1
68
+ end
69
+
70
+ class C3 < C1
71
+ end
72
+
73
+ def test_replace_instance_method_defined_on_self_class
74
+ c1 = C1.new
75
+ c2 = C2.new
76
+ c3 = C3.new
77
+
78
+ old_value = :c1_instance
79
+ new_value = :c1_instance_replaced1
80
+
81
+ musha = Kagemusha.new(C1)
82
+ musha.def(:c1_instance) { new_value }
83
+
84
+ assert_equal(old_value, c1.c1_instance)
85
+ assert_equal(old_value, c2.c1_instance)
86
+ assert_equal(old_value, c3.c1_instance)
87
+ c1_method_set = get_method_set(C1)
88
+ c2_method_set = get_method_set(C2)
89
+ c3_method_set = get_method_set(C3)
90
+
91
+ musha.swap {
92
+ assert_equal(new_value, c1.c1_instance)
93
+ assert_equal(new_value, c2.c1_instance)
94
+ assert_equal(new_value, c3.c1_instance)
95
+ assert_equal(c1_method_set, get_method_set(C1))
96
+ assert_equal(c2_method_set, get_method_set(C2))
97
+ assert_equal(c3_method_set, get_method_set(C3))
98
+ }
99
+
100
+ assert_equal(old_value, c1.c1_instance)
101
+ assert_equal(old_value, c2.c1_instance)
102
+ assert_equal(old_value, c3.c1_instance)
103
+ assert_equal(c1_method_set, get_method_set(C1))
104
+ assert_equal(c2_method_set, get_method_set(C2))
105
+ assert_equal(c3_method_set, get_method_set(C3))
106
+
107
+ def c1.c1_instance
108
+ return :singleton
109
+ end
110
+
111
+ assert_equal(:singleton, c1.c1_instance)
112
+ assert_equal(old_value, c2.c1_instance)
113
+ assert_equal(old_value, c3.c1_instance)
114
+
115
+ musha.swap {
116
+ assert_equal(:singleton, c1.c1_instance)
117
+ assert_equal(new_value, c2.c1_instance)
118
+ assert_equal(new_value, c3.c1_instance)
119
+ }
120
+
121
+ assert_equal(:singleton, c1.c1_instance)
122
+ assert_equal(old_value, c2.c1_instance)
123
+ assert_equal(old_value, c3.c1_instance)
124
+ end
125
+
126
+ def test_replace_private_instance_method_defined_on_self_class
127
+ c1 = C1.new
128
+ c2 = C2.new
129
+ c3 = C3.new
130
+
131
+ old_value = :c1_private_instance
132
+ new_value = :c1_private_instance_replaced1
133
+
134
+ musha = Kagemusha.new(C1)
135
+ musha.def(:c1_private_instance) { new_value }
136
+
137
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
138
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
139
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
140
+ assert_raise(NoMethodError) { c1.c1_private_instance }
141
+ assert_raise(NoMethodError) { c2.c1_private_instance }
142
+ assert_raise(NoMethodError) { c3.c1_private_instance }
143
+
144
+ musha.swap {
145
+ assert_equal(new_value, c1.instance_eval { c1_private_instance })
146
+ assert_equal(new_value, c2.instance_eval { c1_private_instance })
147
+ assert_equal(new_value, c3.instance_eval { c1_private_instance })
148
+ assert_raise(NoMethodError) { c1.c1_private_instance }
149
+ assert_raise(NoMethodError) { c2.c1_private_instance }
150
+ assert_raise(NoMethodError) { c3.c1_private_instance }
151
+ }
152
+
153
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
154
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
155
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
156
+ assert_raise(NoMethodError) { c1.c1_private_instance }
157
+ assert_raise(NoMethodError) { c2.c1_private_instance }
158
+ assert_raise(NoMethodError) { c3.c1_private_instance }
159
+ end
160
+
161
+ def test_replace_protected_instance_method_defined_on_self_class
162
+ c1 = C1.new
163
+ c2 = C2.new
164
+ c3 = C3.new
165
+
166
+ old_value = :c1_protected_instance
167
+ new_value = :c1_protected_instance_replaced1
168
+
169
+ musha = Kagemusha.new(C1)
170
+ musha.def(:c1_protected_instance) { new_value }
171
+
172
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
173
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
174
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
175
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
176
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
177
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
178
+
179
+ musha.swap {
180
+ assert_equal(new_value, c1.instance_eval { c1_protected_instance })
181
+ assert_equal(new_value, c2.instance_eval { c1_protected_instance })
182
+ assert_equal(new_value, c3.instance_eval { c1_protected_instance })
183
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
184
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
185
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
186
+ }
187
+
188
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
189
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
190
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
191
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
192
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
193
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
194
+ end
195
+
196
+ def test_replace_class_method_defined_on_self_class
197
+ old_value = :c1_class
198
+ new_value = :c1_class_replaced1
199
+
200
+ musha = Kagemusha.new(C1)
201
+ musha.defs(:c1_class) { new_value }
202
+
203
+ assert_equal(old_value, C1.c1_class)
204
+ assert_equal(old_value, C2.c1_class)
205
+ assert_equal(old_value, C3.c1_class)
206
+
207
+ musha.swap {
208
+ assert_equal(new_value, C1.c1_class)
209
+ assert_equal(new_value, C2.c1_class)
210
+ assert_equal(new_value, C3.c1_class)
211
+ }
212
+
213
+ assert_equal(old_value, C1.c1_class)
214
+
215
+ if RUBY_VERSION < "1.9.0"
216
+ # @1.8.6 TypeError: singleton method bound for a different object
217
+ assert_raise(TypeError) { C2.c1_class }
218
+ assert_raise(TypeError) { C3.c1_class }
219
+ else
220
+ # @1.9.1 No Error
221
+ assert_equal(old_value, C2.c1_class)
222
+ assert_equal(old_value, C3.c1_class)
223
+ end
224
+ end
225
+
226
+ def test_replace_private_class_method_defined_on_self_class
227
+ old_value = :c1_private_class
228
+ new_value = :c1_private_class_replaced1
229
+
230
+ musha = Kagemusha.new(C1)
231
+ musha.defs(:c1_private_class) { new_value }
232
+
233
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
234
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
235
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
236
+ assert_raise(NoMethodError) { C1.c1_private_class }
237
+ assert_raise(NoMethodError) { C2.c1_private_class }
238
+ assert_raise(NoMethodError) { C3.c1_private_class }
239
+
240
+ musha.swap {
241
+ assert_equal(new_value, C1.instance_eval { c1_private_class })
242
+ assert_equal(new_value, C2.instance_eval { c1_private_class })
243
+ assert_equal(new_value, C3.instance_eval { c1_private_class })
244
+ assert_raise(NoMethodError) { C1.c1_private_class }
245
+ assert_raise(NoMethodError) { C2.c1_private_class }
246
+ assert_raise(NoMethodError) { C3.c1_private_class }
247
+ }
248
+
249
+ assert_equal(old_value, C1.instance_eval { c1_private_class }) # FIXME: NameError: undefined local variable or method `c1_private_class' for TestComplexCases::C1:Class
250
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
251
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
252
+ assert_raise(NoMethodError) { C1.c1_private_class }
253
+ assert_raise(NoMethodError) { C2.c1_private_class }
254
+ assert_raise(NoMethodError) { C3.c1_private_class }
255
+ end
256
+
257
+ def test_replace_instance_method_defined_on_parent_class
258
+ c1 = C1.new
259
+ c2 = C2.new
260
+ c3 = C3.new
261
+
262
+ old_value = :c1_instance
263
+ new_value = :c1_instance_replaced2
264
+
265
+ musha = Kagemusha.new(C2)
266
+ musha.def(:c1_instance) { new_value }
267
+
268
+ assert_equal(old_value, c1.c1_instance)
269
+ assert_equal(old_value, c2.c1_instance)
270
+ assert_equal(old_value, c3.c1_instance)
271
+
272
+ musha.swap {
273
+ assert_equal(old_value, c1.c1_instance)
274
+ assert_equal(new_value, c2.c1_instance)
275
+ assert_equal(old_value, c3.c1_instance)
276
+ }
277
+
278
+ assert_equal(old_value, c1.c1_instance)
279
+ assert_equal(old_value, c2.c1_instance)
280
+ assert_equal(old_value, c3.c1_instance)
281
+
282
+ def c2.c1_instance
283
+ return :singleton
284
+ end
285
+
286
+ assert_equal(old_value, c1.c1_instance)
287
+ assert_equal(:singleton, c2.c1_instance)
288
+ assert_equal(old_value, c3.c1_instance)
289
+
290
+ musha.swap {
291
+ assert_equal(old_value, c1.c1_instance)
292
+ assert_equal(:singleton, c2.c1_instance)
293
+ assert_equal(old_value, c3.c1_instance)
294
+ }
295
+
296
+ assert_equal(old_value, c1.c1_instance)
297
+ assert_equal(:singleton, c2.c1_instance)
298
+ assert_equal(old_value, c3.c1_instance)
299
+ end
300
+
301
+ def test_replace_private_instance_method_defined_on_parent_class
302
+ c1 = C1.new
303
+ c2 = C2.new
304
+ c3 = C3.new
305
+
306
+ old_value = :c1_private_instance
307
+ new_value = :c1_private_instance_replaced2
308
+
309
+ musha = Kagemusha.new(C2)
310
+ musha.def(:c1_private_instance) { new_value }
311
+
312
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
313
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
314
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
315
+ assert_raise(NoMethodError) { c1.c1_private_instance }
316
+ assert_raise(NoMethodError) { c2.c1_private_instance }
317
+ assert_raise(NoMethodError) { c3.c1_private_instance }
318
+
319
+ musha.swap {
320
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
321
+ assert_equal(new_value, c2.instance_eval { c1_private_instance })
322
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
323
+ assert_raise(NoMethodError) { c1.c1_private_instance }
324
+ assert_raise(NoMethodError) { c2.c1_private_instance }
325
+ assert_raise(NoMethodError) { c3.c1_private_instance }
326
+ }
327
+
328
+ assert_equal(old_value, c1.instance_eval { c1_private_instance })
329
+ assert_equal(old_value, c2.instance_eval { c1_private_instance })
330
+ assert_equal(old_value, c3.instance_eval { c1_private_instance })
331
+ assert_raise(NoMethodError) { c1.c1_private_instance }
332
+ assert_raise(NoMethodError) { c2.c1_private_instance }
333
+ assert_raise(NoMethodError) { c3.c1_private_instance }
334
+ end
335
+
336
+ def test_replace_protected_instance_method_defined_on_parent_class
337
+ c1 = C1.new
338
+ c2 = C2.new
339
+ c3 = C3.new
340
+
341
+ old_value = :c1_protected_instance
342
+ new_value = :c1_protected_instance_replaced2
343
+
344
+ musha = Kagemusha.new(C2)
345
+ musha.def(:c1_protected_instance) { new_value }
346
+
347
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
348
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
349
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
350
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
351
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
352
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
353
+
354
+ musha.swap {
355
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
356
+ assert_equal(new_value, c2.instance_eval { c1_protected_instance })
357
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
358
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
359
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
360
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
361
+ }
362
+
363
+ assert_equal(old_value, c1.instance_eval { c1_protected_instance })
364
+ assert_equal(old_value, c2.instance_eval { c1_protected_instance })
365
+ assert_equal(old_value, c3.instance_eval { c1_protected_instance })
366
+ assert_raise(NoMethodError) { c1.c1_protected_instance }
367
+ assert_raise(NoMethodError) { c2.c1_protected_instance }
368
+ assert_raise(NoMethodError) { c3.c1_protected_instance }
369
+ end
370
+
371
+ def test_replace_class_method_defined_on_parent_class
372
+ old_value = :c1_class
373
+ new_value = :c1_class_replaced2
374
+
375
+ musha = Kagemusha.new(C2)
376
+ musha.defs(:c1_class) { new_value }
377
+
378
+ assert_equal(old_value, C1.c1_class)
379
+ assert_equal(old_value, C2.c1_class)
380
+ assert_equal(old_value, C3.c1_class)
381
+
382
+ musha.swap {
383
+ assert_equal(old_value, C1.c1_class)
384
+ assert_equal(new_value, C2.c1_class)
385
+ assert_equal(old_value, C3.c1_class)
386
+ }
387
+
388
+ assert_equal(old_value, C1.c1_class)
389
+ assert_equal(old_value, C2.c1_class)
390
+ assert_equal(old_value, C3.c1_class)
391
+ end
392
+
393
+ def test_replace_private_class_method_defined_on_parent_class
394
+ old_value = :c1_private_class
395
+ new_value = :c1_private_class_replaced2
396
+
397
+ musha = Kagemusha.new(C2)
398
+ musha.defs(:c1_private_class) { new_value }
399
+
400
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
401
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
402
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
403
+ assert_raise(NoMethodError) { C1.c1_private_class }
404
+ assert_raise(NoMethodError) { C2.c1_private_class }
405
+ assert_raise(NoMethodError) { C3.c1_private_class }
406
+
407
+ musha.swap {
408
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
409
+ assert_equal(new_value, C2.instance_eval { c1_private_class })
410
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
411
+ assert_raise(NoMethodError) { C1.c1_private_class }
412
+ assert_raise(NoMethodError) { C2.c1_private_class }
413
+ assert_raise(NoMethodError) { C3.c1_private_class }
414
+ }
415
+
416
+ assert_equal(old_value, C1.instance_eval { c1_private_class })
417
+ assert_equal(old_value, C2.instance_eval { c1_private_class })
418
+ assert_equal(old_value, C3.instance_eval { c1_private_class })
419
+ assert_raise(NoMethodError) { C1.c1_private_class }
420
+ assert_raise(NoMethodError) { C2.c1_private_class }
421
+ assert_raise(NoMethodError) { C3.c1_private_class }
422
+ end
423
+
424
+ def test_replace_instance_method_defined_on_self_class_including_module
425
+ c1 = C1.new
426
+ c2 = C2.new
427
+ c3 = C3.new
428
+
429
+ old_value = :m1_instance
430
+ new_value = :m1_instance_replaced1
431
+
432
+ musha = Kagemusha.new(C1)
433
+ musha.def(:m1_instance) { new_value }
434
+
435
+ assert_equal(old_value, c1.m1_instance)
436
+ assert_equal(old_value, c2.m1_instance)
437
+ assert_equal(old_value, c3.m1_instance)
438
+
439
+ musha.swap {
440
+ assert_equal(new_value, c1.m1_instance)
441
+ assert_equal(new_value, c2.m1_instance)
442
+ assert_equal(new_value, c3.m1_instance)
443
+ }
444
+
445
+ assert_equal(old_value, c1.m1_instance)
446
+ assert_equal(old_value, c2.m1_instance)
447
+ assert_equal(old_value, c3.m1_instance)
448
+
449
+ def c1.m1_instance
450
+ return :singleton
451
+ end
452
+
453
+ assert_equal(:singleton, c1.m1_instance)
454
+ assert_equal(old_value, c2.m1_instance)
455
+ assert_equal(old_value, c3.m1_instance)
456
+
457
+ musha.swap {
458
+ assert_equal(:singleton, c1.m1_instance)
459
+ assert_equal(new_value, c2.m1_instance)
460
+ assert_equal(new_value, c3.m1_instance)
461
+ }
462
+
463
+ assert_equal(:singleton, c1.m1_instance)
464
+ assert_equal(old_value, c2.m1_instance)
465
+ assert_equal(old_value, c3.m1_instance)
466
+ end
467
+
468
+ def test_replace_private_instance_method_defined_on_self_class_including_module
469
+ c1 = C1.new
470
+ c2 = C2.new
471
+ c3 = C3.new
472
+
473
+ old_value = :m1_private_instance
474
+ new_value = :m1_private_instance_replaced1
475
+
476
+ musha = Kagemusha.new(C1)
477
+ musha.def(:m1_private_instance) { new_value }
478
+
479
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
480
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
481
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
482
+ assert_raise(NoMethodError) { c1.m1_private_instance }
483
+ assert_raise(NoMethodError) { c2.m1_private_instance }
484
+ assert_raise(NoMethodError) { c3.m1_private_instance }
485
+
486
+ musha.swap {
487
+ assert_equal(new_value, c1.instance_eval { m1_private_instance })
488
+ assert_equal(new_value, c2.instance_eval { m1_private_instance })
489
+ assert_equal(new_value, c3.instance_eval { m1_private_instance })
490
+ assert_raise(NoMethodError) { c1.m1_private_instance }
491
+ assert_raise(NoMethodError) { c2.m1_private_instance }
492
+ assert_raise(NoMethodError) { c3.m1_private_instance }
493
+ }
494
+
495
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
496
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
497
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
498
+ assert_raise(NoMethodError) { c1.m1_private_instance }
499
+ assert_raise(NoMethodError) { c2.m1_private_instance }
500
+ assert_raise(NoMethodError) { c3.m1_private_instance }
501
+ end
502
+
503
+ def test_replace_protected_instance_method_defined_on_self_class_including_module
504
+ c1 = C1.new
505
+ c2 = C2.new
506
+ c3 = C3.new
507
+
508
+ old_value = :m1_protected_instance
509
+ new_value = :m1_protected_instance_replaced1
510
+
511
+ musha = Kagemusha.new(C1)
512
+ musha.def(:m1_protected_instance) { new_value }
513
+
514
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
515
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
516
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
517
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
518
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
519
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
520
+
521
+ musha.swap {
522
+ assert_equal(new_value, c1.instance_eval { m1_protected_instance })
523
+ assert_equal(new_value, c2.instance_eval { m1_protected_instance })
524
+ assert_equal(new_value, c3.instance_eval { m1_protected_instance })
525
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
526
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
527
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
528
+ }
529
+
530
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
531
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
532
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
533
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
534
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
535
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
536
+ end
537
+
538
+ def test_replace_class_method_defined_on_self_class_extending_module
539
+ old_value = :m2_class
540
+ new_value = :m2_class_replaced1
541
+
542
+ musha = Kagemusha.new(C1)
543
+ musha.defs(:m2_class) { new_value }
544
+
545
+ assert_equal(old_value, C1.m2_class)
546
+ assert_equal(old_value, C2.m2_class)
547
+ assert_equal(old_value, C3.m2_class)
548
+
549
+ musha.swap {
550
+ assert_equal(new_value, C1.m2_class)
551
+ assert_equal(new_value, C2.m2_class)
552
+ assert_equal(new_value, C3.m2_class)
553
+ }
554
+
555
+ assert_equal(old_value, C1.m2_class)
556
+ assert_equal(old_value, C2.m2_class)
557
+ assert_equal(old_value, C3.m2_class)
558
+ end
559
+
560
+ def test_replace_private_class_method_defined_on_self_class_extending_module
561
+ old_value = :m2_private_class
562
+ new_value = :m2_private_class_replaced1
563
+
564
+ musha = Kagemusha.new(C1)
565
+ musha.defs(:m2_private_class) { new_value }
566
+
567
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
568
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
569
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
570
+ assert_raise(NoMethodError) { C1.m2_private_class }
571
+ assert_raise(NoMethodError) { C2.m2_private_class }
572
+ assert_raise(NoMethodError) { C3.m2_private_class }
573
+
574
+ musha.swap {
575
+ assert_equal(new_value, C1.instance_eval { m2_private_class })
576
+ assert_equal(new_value, C2.instance_eval { m2_private_class })
577
+ assert_equal(new_value, C3.instance_eval { m2_private_class })
578
+ assert_raise(NoMethodError) { C1.m2_private_class }
579
+ assert_raise(NoMethodError) { C2.m2_private_class }
580
+ assert_raise(NoMethodError) { C3.m2_private_class }
581
+ }
582
+
583
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
584
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
585
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
586
+ assert_raise(NoMethodError) { C1.m2_private_class }
587
+ assert_raise(NoMethodError) { C2.m2_private_class }
588
+ assert_raise(NoMethodError) { C3.m2_private_class }
589
+ end
590
+
591
+ def test_replace_instance_method_defined_on_parent_class_including_module
592
+ c1 = C1.new
593
+ c2 = C2.new
594
+ c3 = C3.new
595
+
596
+ old_value = :m1_instance
597
+ new_value = :m1_instance_replaced2
598
+
599
+ musha = Kagemusha.new(C2)
600
+ musha.def(:m1_instance) { new_value }
601
+
602
+ assert_equal(old_value, c1.m1_instance)
603
+ assert_equal(old_value, c2.m1_instance)
604
+ assert_equal(old_value, c3.m1_instance)
605
+
606
+ musha.swap {
607
+ assert_equal(old_value, c1.m1_instance)
608
+ assert_equal(new_value, c2.m1_instance)
609
+ assert_equal(old_value, c3.m1_instance)
610
+ }
611
+
612
+ assert_equal(old_value, c1.m1_instance)
613
+ assert_equal(old_value, c2.m1_instance)
614
+ assert_equal(old_value, c3.m1_instance)
615
+
616
+ def c2.m1_instance
617
+ return :singleton
618
+ end
619
+
620
+ assert_equal(old_value, c1.m1_instance)
621
+ assert_equal(:singleton, c2.m1_instance)
622
+ assert_equal(old_value, c3.m1_instance)
623
+
624
+ musha.swap {
625
+ assert_equal(old_value, c1.m1_instance)
626
+ assert_equal(:singleton, c2.m1_instance)
627
+ assert_equal(old_value, c3.m1_instance)
628
+ }
629
+
630
+ assert_equal(old_value, c1.m1_instance)
631
+ assert_equal(:singleton, c2.m1_instance)
632
+ assert_equal(old_value, c3.m1_instance)
633
+ end
634
+
635
+ def test_replace_private_instance_method_defined_on_parent_class_including_module
636
+ c1 = C1.new
637
+ c2 = C2.new
638
+ c3 = C3.new
639
+
640
+ old_value = :m1_private_instance
641
+ new_value = :m1_private_instance_replaced2
642
+
643
+ musha = Kagemusha.new(C2)
644
+ musha.def(:m1_private_instance) { new_value }
645
+
646
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
647
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
648
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
649
+ assert_raise(NoMethodError) { c1.m1_private_instance }
650
+ assert_raise(NoMethodError) { c2.m1_private_instance }
651
+ assert_raise(NoMethodError) { c3.m1_private_instance }
652
+
653
+ musha.swap {
654
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
655
+ assert_equal(new_value, c2.instance_eval { m1_private_instance })
656
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
657
+ assert_raise(NoMethodError) { c1.m1_private_instance }
658
+ assert_raise(NoMethodError) { c2.m1_private_instance }
659
+ assert_raise(NoMethodError) { c3.m1_private_instance }
660
+ }
661
+
662
+ assert_equal(old_value, c1.instance_eval { m1_private_instance })
663
+ assert_equal(old_value, c2.instance_eval { m1_private_instance })
664
+ assert_equal(old_value, c3.instance_eval { m1_private_instance })
665
+ assert_raise(NoMethodError) { c1.m1_private_instance }
666
+ assert_raise(NoMethodError) { c2.m1_private_instance }
667
+ assert_raise(NoMethodError) { c3.m1_private_instance }
668
+ end
669
+
670
+ def test_replace_protected_instance_method_defined_on_parent_class_including_module
671
+ c1 = C1.new
672
+ c2 = C2.new
673
+ c3 = C3.new
674
+
675
+ old_value = :m1_protected_instance
676
+ new_value = :m1_protected_instance_replaced2
677
+
678
+ musha = Kagemusha.new(C2)
679
+ musha.def(:m1_protected_instance) { new_value }
680
+
681
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
682
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
683
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
684
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
685
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
686
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
687
+
688
+ musha.swap {
689
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
690
+ assert_equal(new_value, c2.instance_eval { m1_protected_instance })
691
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
692
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
693
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
694
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
695
+ }
696
+
697
+ assert_equal(old_value, c1.instance_eval { m1_protected_instance })
698
+ assert_equal(old_value, c2.instance_eval { m1_protected_instance })
699
+ assert_equal(old_value, c3.instance_eval { m1_protected_instance })
700
+ assert_raise(NoMethodError) { c1.m1_protected_instance }
701
+ assert_raise(NoMethodError) { c2.m1_protected_instance }
702
+ assert_raise(NoMethodError) { c3.m1_protected_instance }
703
+ end
704
+
705
+ def test_replace_class_method_defined_on_parent_class_extending_module
706
+ old_value = :m2_class
707
+ new_value = :m2_class_replaced2
708
+
709
+ musha = Kagemusha.new(C2)
710
+ musha.defs(:m2_class) { new_value }
711
+
712
+ assert_equal(old_value, C1.m2_class)
713
+ assert_equal(old_value, C2.m2_class)
714
+ assert_equal(old_value, C3.m2_class)
715
+
716
+ musha.swap {
717
+ assert_equal(old_value, C1.m2_class)
718
+ assert_equal(new_value, C2.m2_class)
719
+ assert_equal(old_value, C3.m2_class)
720
+ }
721
+
722
+ assert_equal(old_value, C1.m2_class)
723
+ assert_equal(old_value, C2.m2_class)
724
+ assert_equal(old_value, C3.m2_class)
725
+ end
726
+
727
+ def test_replace_private_class_method_defined_on_parent_class_extending_module
728
+ old_value = :m2_private_class
729
+ new_value = :m2_private_class_replaced2
730
+
731
+ musha = Kagemusha.new(C2)
732
+ musha.defs(:m2_private_class) { new_value }
733
+
734
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
735
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
736
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
737
+ assert_raise(NoMethodError) { C1.m2_private_class }
738
+ assert_raise(NoMethodError) { C2.m2_private_class }
739
+ assert_raise(NoMethodError) { C3.m2_private_class }
740
+
741
+ musha.swap {
742
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
743
+ assert_equal(new_value, C2.instance_eval { m2_private_class })
744
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
745
+ assert_raise(NoMethodError) { C1.m2_private_class }
746
+ assert_raise(NoMethodError) { C2.m2_private_class }
747
+ assert_raise(NoMethodError) { C3.m2_private_class }
748
+ }
749
+
750
+ assert_equal(old_value, C1.instance_eval { m2_private_class })
751
+ assert_equal(old_value, C2.instance_eval { m2_private_class })
752
+ assert_equal(old_value, C3.instance_eval { m2_private_class })
753
+ assert_raise(NoMethodError) { C1.m2_private_class }
754
+ assert_raise(NoMethodError) { C2.m2_private_class }
755
+ assert_raise(NoMethodError) { C3.m2_private_class }
756
+ end
757
+
758
+ private
759
+
760
+ def get_method_set(obj)
761
+ return {
762
+ :self => {
763
+ :public => obj.public_methods(false),
764
+ :public_with_inherited => obj.public_methods(true),
765
+ :protected => obj.protected_methods(false),
766
+ :protected_with_inherited => obj.protected_methods(true),
767
+ :private => obj.private_methods(false),
768
+ :private_with_inherited => obj.private_methods(true),
769
+ },
770
+ :instance => (obj.kind_of?(Module) ? {
771
+ :public => obj.public_instance_methods(false),
772
+ :public_with_inherited => obj.public_instance_methods(true),
773
+ :protected => obj.protected_instance_methods(false),
774
+ :protected_with_inherited => obj.protected_instance_methods(true),
775
+ :private => obj.private_instance_methods(false),
776
+ :private_with_inherited => obj.private_instance_methods(true),
777
+ } : nil),
778
+ }
779
+ end
780
+ end
781
+
782
+ #==============================================================================#
783
+ #==============================================================================#