source: EcnlProtoTool/trunk/mruby-1.3.0/test/t/module.rb@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 21.0 KB
Line 
1##
2# Module ISO Test
3
4def labeled_module(name, &block)
5 Module.new do
6 singleton_class.class_eval do
7 define_method(:to_s) { name }
8 alias_method :inspect, :to_s
9 end
10 class_eval(&block) if block
11 end
12end
13
14def labeled_class(name, supklass = Object, &block)
15 Class.new(supklass) do
16 singleton_class.class_eval do
17 define_method(:to_s) { name }
18 alias_method :inspect, :to_s
19 end
20 class_eval(&block) if block
21 end
22end
23
24assert('Module', '15.2.2') do
25 assert_equal Class, Module.class
26end
27
28# TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do
29
30# TODO not implemented ATM assert('Module.nesting', '15.2.2.3.2') do
31
32assert('Module#ancestors', '15.2.2.4.9') do
33 class Test4ModuleAncestors
34 end
35 sc = Test4ModuleAncestors.singleton_class
36 r = String.ancestors
37
38 assert_equal Array, r.class
39 assert_true r.include?(String)
40 assert_true r.include?(Object)
41end
42
43assert('Module#append_features', '15.2.2.4.10') do
44 module Test4AppendFeatures
45 def self.append_features(mod)
46 Test4AppendFeatures2.const_set(:Const4AppendFeatures2, mod)
47 end
48 end
49 module Test4AppendFeatures2
50 include Test4AppendFeatures
51 end
52
53 assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2)
54end
55
56assert('Module#attr NameError') do
57 %w[
58 foo?
59 @foo
60 @@foo
61 $foo
62 ].each do |name|
63 module NameTest; end
64
65 assert_raise(NameError) do
66 NameTest.module_eval { attr_reader name.to_sym }
67 end
68
69 assert_raise(NameError) do
70 NameTest.module_eval { attr_writer name.to_sym }
71 end
72
73 assert_raise(NameError) do
74 NameTest.module_eval { attr name.to_sym }
75 end
76
77 assert_raise(NameError) do
78 NameTest.module_eval { attr_accessor name.to_sym }
79 end
80 end
81
82end
83
84assert('Module#attr', '15.2.2.4.11') do
85 class AttrTest
86 class << self
87 attr :cattr
88 def cattr_val=(val)
89 @cattr = val
90 end
91 end
92 attr :iattr
93 def iattr_val=(val)
94 @iattr = val
95 end
96 end
97
98 test = AttrTest.new
99 assert_true AttrTest.respond_to?(:cattr)
100 assert_true test.respond_to?(:iattr)
101
102 assert_false AttrTest.respond_to?(:cattr=)
103 assert_false test.respond_to?(:iattr=)
104
105 test.iattr_val = 'test'
106 assert_equal 'test', test.iattr
107
108 AttrTest.cattr_val = 'test'
109 assert_equal 'test', AttrTest.cattr
110end
111
112assert('Module#attr_accessor', '15.2.2.4.12') do
113 class AttrTestAccessor
114 class << self
115 attr_accessor :cattr
116 end
117 attr_accessor :iattr, 'iattr2'
118 end
119
120 attr_instance = AttrTestAccessor.new
121 assert_true AttrTestAccessor.respond_to?(:cattr=)
122 assert_true attr_instance.respond_to?(:iattr=)
123 assert_true attr_instance.respond_to?(:iattr2=)
124 assert_true AttrTestAccessor.respond_to?(:cattr)
125 assert_true attr_instance.respond_to?(:iattr)
126 assert_true attr_instance.respond_to?(:iattr2)
127
128 attr_instance.iattr = 'test'
129 assert_equal 'test', attr_instance.iattr
130
131 AttrTestAccessor.cattr = 'test'
132 assert_equal 'test', AttrTestAccessor.cattr
133end
134
135assert('Module#attr_reader', '15.2.2.4.13') do
136 class AttrTestReader
137 class << self
138 attr_reader :cattr
139 def cattr_val=(val)
140 @cattr = val
141 end
142 end
143 attr_reader :iattr, 'iattr2'
144 def iattr_val=(val)
145 @iattr = val
146 end
147 end
148
149 attr_instance = AttrTestReader.new
150 assert_true AttrTestReader.respond_to?(:cattr)
151 assert_true attr_instance.respond_to?(:iattr)
152 assert_true attr_instance.respond_to?(:iattr2)
153
154 assert_false AttrTestReader.respond_to?(:cattr=)
155 assert_false attr_instance.respond_to?(:iattr=)
156 assert_false attr_instance.respond_to?(:iattr2=)
157
158 attr_instance.iattr_val = 'test'
159 assert_equal 'test', attr_instance.iattr
160
161 AttrTestReader.cattr_val = 'test'
162 assert_equal 'test', AttrTestReader.cattr
163end
164
165assert('Module#attr_writer', '15.2.2.4.14') do
166 class AttrTestWriter
167 class << self
168 attr_writer :cattr
169 def cattr_val
170 @cattr
171 end
172 end
173 attr_writer :iattr, 'iattr2'
174 def iattr_val
175 @iattr
176 end
177 end
178
179 attr_instance = AttrTestWriter.new
180 assert_true AttrTestWriter.respond_to?(:cattr=)
181 assert_true attr_instance.respond_to?(:iattr=)
182 assert_true attr_instance.respond_to?(:iattr2=)
183
184 assert_false AttrTestWriter.respond_to?(:cattr)
185 assert_false attr_instance.respond_to?(:iattr)
186 assert_false attr_instance.respond_to?(:iattr2)
187
188 attr_instance.iattr = 'test'
189 assert_equal 'test', attr_instance.iattr_val
190
191 AttrTestWriter.cattr = 'test'
192 assert_equal 'test', AttrTestWriter.cattr_val
193end
194
195assert('Module#class_eval', '15.2.2.4.15') do
196 class Test4ClassEval
197 @a = 11
198 @b = 12
199 end
200 Test4ClassEval.class_eval do
201 def method1
202 end
203 end
204 r = Test4ClassEval.instance_methods
205
206 assert_equal 11, Test4ClassEval.class_eval{ @a }
207 assert_equal 12, Test4ClassEval.class_eval{ @b }
208 assert_equal Array, r.class
209 assert_true r.include?(:method1)
210end
211
212assert('Module#class_variable_defined?', '15.2.2.4.16') do
213 class Test4ClassVariableDefined
214 @@cv = 99
215 end
216
217 assert_true Test4ClassVariableDefined.class_variable_defined?(:@@cv)
218 assert_false Test4ClassVariableDefined.class_variable_defined?(:@@noexisting)
219end
220
221assert('Module#class_variable_get', '15.2.2.4.17') do
222 class Test4ClassVariableGet
223 @@cv = 99
224 end
225
226 assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv)
227end
228
229assert('Module#class_variable_set', '15.2.2.4.18') do
230 class Test4ClassVariableSet
231 @@foo = 100
232 def foo
233 @@foo
234 end
235 end
236
237 assert_true Test4ClassVariableSet.class_variable_set(:@@cv, 99)
238 assert_true Test4ClassVariableSet.class_variable_set(:@@foo, 101)
239 assert_true Test4ClassVariableSet.class_variables.include? :@@cv
240 assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv)
241 assert_equal 101, Test4ClassVariableSet.new.foo
242end
243
244assert('Module#class_variables', '15.2.2.4.19') do
245 class Test4ClassVariables1
246 @@var1 = 1
247 end
248 class Test4ClassVariables2 < Test4ClassVariables1
249 @@var2 = 2
250 end
251
252 assert_equal [:@@var1], Test4ClassVariables1.class_variables
253 assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables
254end
255
256assert('Module#const_defined?', '15.2.2.4.20') do
257 module Test4ConstDefined
258 Const4Test4ConstDefined = true
259 end
260
261 assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined)
262 assert_false Test4ConstDefined.const_defined?(:NotExisting)
263end
264
265assert('Module#const_get', '15.2.2.4.21') do
266 module Test4ConstGet
267 Const4Test4ConstGet = 42
268 end
269
270 assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet)
271end
272
273assert('Module#const_missing', '15.2.2.4.22') do
274 module Test4ConstMissing
275 def self.const_missing(sym)
276 42 # the answer to everything
277 end
278 end
279
280 assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist)
281end
282
283assert('Module#const_get', '15.2.2.4.23') do
284 module Test4ConstSet
285 Const4Test4ConstSet = 42
286 end
287
288 assert_true Test4ConstSet.const_set(:Const4Test4ConstSet, 23)
289 assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet)
290end
291
292assert('Module#constants', '15.2.2.4.24') do
293 $n = []
294 module TestA
295 C = 1
296 end
297 class TestB
298 include TestA
299 C2 = 1
300 $n = constants.sort
301 end
302
303 assert_equal [ :C ], TestA.constants
304 assert_equal [ :C, :C2 ], $n
305end
306
307assert('Module#include', '15.2.2.4.27') do
308 module Test4Include
309 Const4Include = 42
310 end
311 module Test4Include2
312 include Test4Include
313 end
314
315 assert_equal 42, Test4Include2.const_get(:Const4Include)
316end
317
318assert('Module#include?', '15.2.2.4.28') do
319 module Test4IncludeP
320 end
321 class Test4IncludeP2
322 include Test4IncludeP
323 end
324 class Test4IncludeP3 < Test4IncludeP2
325 end
326
327 assert_true Test4IncludeP2.include?(Test4IncludeP)
328 assert_true Test4IncludeP3.include?(Test4IncludeP)
329 assert_false Test4IncludeP.include?(Test4IncludeP)
330end
331
332assert('Module#included', '15.2.2.4.29') do
333 module Test4Included
334 Const4Included = 42
335 def self.included mod
336 Test4Included.const_set(:Const4Included2, mod)
337 end
338 end
339 module Test4Included2
340 include Test4Included
341 end
342
343 assert_equal 42, Test4Included2.const_get(:Const4Included)
344 assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2)
345end
346
347assert('Module#included_modules', '15.2.2.4.30') do
348 module Test4includedModules
349 end
350 module Test4includedModules2
351 include Test4includedModules
352 end
353 r = Test4includedModules2.included_modules
354
355 assert_equal Array, r.class
356 assert_true r.include?(Test4includedModules)
357end
358
359assert('Module#initialize', '15.2.2.4.31') do
360 assert_kind_of Module, Module.new
361 mod = Module.new { def hello; "hello"; end }
362 assert_equal [:hello], mod.instance_methods
363 a = nil
364 mod = Module.new { |m| a = m }
365 assert_equal mod, a
366end
367
368assert('Module#instance_methods', '15.2.2.4.33') do
369 module Test4InstanceMethodsA
370 def method1() end
371 end
372 class Test4InstanceMethodsB
373 def method2() end
374 end
375 class Test4InstanceMethodsC < Test4InstanceMethodsB
376 def method3() end
377 end
378
379 r = Test4InstanceMethodsC.instance_methods(true)
380
381 assert_equal [:method1], Test4InstanceMethodsA.instance_methods
382 assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false)
383 assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false)
384 assert_equal Array, r.class
385 assert_true r.include?(:method3)
386 assert_true r.include?(:method2)
387end
388
389assert('Module#method_defined?', '15.2.2.4.34') do
390 module Test4MethodDefined
391 module A
392 def method1() end
393 end
394
395 class B
396 def method2() end
397 end
398
399 class C < B
400 include A
401 def method3() end
402 end
403 end
404
405 assert_true Test4MethodDefined::A.method_defined? :method1
406 assert_true Test4MethodDefined::C.method_defined? :method1
407 assert_true Test4MethodDefined::C.method_defined? "method2"
408 assert_true Test4MethodDefined::C.method_defined? "method3"
409 assert_false Test4MethodDefined::C.method_defined? "method4"
410end
411
412
413assert('Module#module_eval', '15.2.2.4.35') do
414 module Test4ModuleEval
415 @a = 11
416 @b = 12
417 end
418
419 assert_equal 11, Test4ModuleEval.module_eval{ @a }
420 assert_equal 12, Test4ModuleEval.module_eval{ @b }
421end
422
423assert('Module#remove_class_variable', '15.2.2.4.39') do
424 class Test4RemoveClassVariable
425 @@cv = 99
426 end
427
428 assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv)
429 assert_false Test4RemoveClassVariable.class_variables.include? :@@cv
430end
431
432assert('Module#remove_const', '15.2.2.4.40') do
433 module Test4RemoveConst
434 ExistingConst = 23
435 end
436
437 result = Test4RemoveConst.module_eval { remove_const :ExistingConst }
438
439 name_error = false
440 begin
441 Test4RemoveConst.module_eval { remove_const :NonExistingConst }
442 rescue NameError
443 name_error = true
444 end
445
446 # Constant removed from Module
447 assert_false Test4RemoveConst.const_defined? :ExistingConst
448 # Return value of binding
449 assert_equal 23, result
450 # Name Error raised when Constant doesn't exist
451 assert_true name_error
452end
453
454assert('Module#remove_method', '15.2.2.4.41') do
455 module Test4RemoveMethod
456 class Parent
457 def hello
458 end
459 end
460
461 class Child < Parent
462 def hello
463 end
464 end
465 end
466
467 assert_true Test4RemoveMethod::Child.class_eval{ remove_method :hello }
468 assert_true Test4RemoveMethod::Child.instance_methods.include? :hello
469 assert_false Test4RemoveMethod::Child.instance_methods(false).include? :hello
470end
471
472assert('Module#undef_method', '15.2.2.4.42') do
473 module Test4UndefMethod
474 class Parent
475 def hello
476 end
477 end
478
479 class Child < Parent
480 def hello
481 end
482 end
483
484 class GrandChild < Child
485 end
486 end
487 Test4UndefMethod::Child.class_eval{ undef_method :hello }
488
489 assert_true Test4UndefMethod::Parent.new.respond_to?(:hello)
490 assert_false Test4UndefMethod::Child.new.respond_to?(:hello)
491 assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello)
492 assert_false Test4UndefMethod::Child.instance_methods(false).include? :hello
493end
494
495# Not ISO specified
496
497assert('Module#define_method') do
498 c = Class.new {
499 define_method(:m1) { :ok }
500 define_method(:m2, Proc.new { :ok })
501 }
502 assert_equal c.new.m1, :ok
503 assert_equal c.new.m2, :ok
504 assert_raise(TypeError) do
505 Class.new { define_method(:n1, nil) }
506 end
507end
508
509# @!group prepend
510 assert('Module#prepend') do
511 module M0
512 def m1; [:M0] end
513 end
514 module M1
515 def m1; [:M1, super, :M1] end
516 end
517 module M2
518 def m1; [:M2, super, :M2] end
519 end
520 M3 = Module.new do
521 def m1; [:M3, super, :M3] end
522 end
523 module M4
524 def m1; [:M4, super, :M4] end
525 end
526
527 class P0
528 include M0
529 prepend M1
530 def m1; [:C0, super, :C0] end
531 end
532 class P1 < P0
533 prepend M2, M3
534 include M4
535 def m1; [:C1, super, :C1] end
536 end
537
538 obj = P1.new
539 expected = [:M2,[:M3,[:C1,[:M4,[:M1,[:C0,[:M0],:C0],:M1],:M4],:C1],:M3],:M2]
540 assert_equal(expected, obj.m1)
541 end
542
543 # mruby shouldn't be affected by this since there is
544 # no visibility control (yet)
545 assert('Module#prepend public') do
546 assert_nothing_raised('ruby/ruby #8846') do
547 Class.new.prepend(Module.new)
548 end
549 end
550
551 assert('Module#prepend inheritance') do
552 bug6654 = '[ruby-core:45914]'
553 a = labeled_module('a')
554 b = labeled_module('b') { include a }
555 c = labeled_module('c') { prepend b }
556
557 #assert bug6654 do
558 # the Module#< operator should be used here instead, but we don't have it
559 assert_include(c.ancestors, a)
560 assert_include(c.ancestors, b)
561 #end
562
563 bug8357 = '[ruby-core:54736] [Bug #8357]'
564 b = labeled_module('b') { prepend a }
565 c = labeled_class('c') { include b }
566
567 #assert bug8357 do
568 # the Module#< operator should be used here instead, but we don't have it
569 assert_include(c.ancestors, a)
570 assert_include(c.ancestors, b)
571 #end
572
573 bug8357 = '[ruby-core:54742] [Bug #8357]'
574 assert_kind_of(b, c.new, bug8357)
575 end
576
577 assert('Moduler#prepend + #instance_methods') do
578 bug6655 = '[ruby-core:45915]'
579 assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655)
580 end
581
582 assert 'Module#prepend + #singleton_methods' do
583 o = Object.new
584 o.singleton_class.class_eval {prepend Module.new}
585 assert_equal([], o.singleton_methods)
586 end
587
588 assert 'Module#prepend + #remove_method' do
589 c = Class.new do
590 prepend Module.new { def foo; end }
591 end
592 assert_raise(NameError) do
593 c.class_eval do
594 remove_method(:foo)
595 end
596 end
597 c.class_eval do
598 def foo; end
599 end
600 removed = nil
601 c.singleton_class.class_eval do
602 define_method(:method_removed) {|id| removed = id}
603 end
604 assert_nothing_raised(NoMethodError, NameError, '[Bug #7843]') do
605 c.class_eval do
606 remove_method(:foo)
607 end
608 end
609 assert_equal(:foo, removed)
610 end
611
612 assert 'Module#prepend + Class#ancestors' do
613 bug6658 = '[ruby-core:45919]'
614 m = labeled_module("m")
615 c = labeled_class("c") {prepend m}
616 assert_equal([m, c], c.ancestors[0, 2], bug6658)
617
618 bug6662 = '[ruby-dev:45868]'
619 c2 = labeled_class("c2", c)
620 anc = c2.ancestors
621 assert_equal([c2, m, c, Object], anc[0..anc.index(Object)], bug6662)
622 end
623
624 assert 'Module#prepend + Module#ancestors' do
625 bug6659 = '[ruby-dev:45861]'
626 m0 = labeled_module("m0") { def x; [:m0, *super] end }
627 m1 = labeled_module("m1") { def x; [:m1, *super] end; prepend m0 }
628 m2 = labeled_module("m2") { def x; [:m2, *super] end; prepend m1 }
629 c0 = labeled_class("c0") { def x; [:c0] end }
630 c1 = labeled_class("c1") { def x; [:c1] end; prepend m2 }
631 c2 = labeled_class("c2", c0) { def x; [:c2, *super] end; include m2 }
632 #
633 assert_equal([m0, m1], m1.ancestors, bug6659)
634 #
635 bug6662 = '[ruby-dev:45868]'
636 assert_equal([m0, m1, m2], m2.ancestors, bug6662)
637 assert_equal([m0, m1, m2, c1], c1.ancestors[0, 4], bug6662)
638 assert_equal([:m0, :m1, :m2, :c1], c1.new.x)
639 assert_equal([c2, m0, m1, m2, c0], c2.ancestors[0, 5], bug6662)
640 assert_equal([:c2, :m0, :m1, :m2, :c0], c2.new.x)
641 #
642 m3 = labeled_module("m3") { include m1; prepend m1 }
643 assert_equal([m3, m0, m1], m3.ancestors)
644 m3 = labeled_module("m3") { prepend m1; include m1 }
645 assert_equal([m0, m1, m3], m3.ancestors)
646 m3 = labeled_module("m3") { prepend m1; prepend m1 }
647 assert_equal([m0, m1, m3], m3.ancestors)
648 m3 = labeled_module("m3") { include m1; include m1 }
649 assert_equal([m3, m0, m1], m3.ancestors)
650 end
651
652 assert 'Module#prepend #instance_methods(false)' do
653 bug6660 = '[ruby-dev:45863]'
654 assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
655 assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
656 end
657
658 assert 'cyclic Module#prepend' do
659 bug7841 = '[ruby-core:52205] [Bug #7841]'
660 m1 = Module.new
661 m2 = Module.new
662 m1.instance_eval { prepend(m2) }
663 assert_raise(ArgumentError, bug7841) do
664 m2.instance_eval { prepend(m1) }
665 end
666 end
667
668 # these assertions will not run without a #assert_seperately method
669 #assert 'test_prepend_optmethod' do
670 # bug7983 = '[ruby-dev:47124] [Bug #7983]'
671 # assert_separately [], %{
672 # module M
673 # def /(other)
674 # to_f / other
675 # end
676 # end
677 # Fixnum.send(:prepend, M)
678 # assert_equal(0.5, 1 / 2, "#{bug7983}")
679 # }
680 # assert_equal(0, 1 / 2)
681 #end
682
683 # mruby has no visibility control
684 assert 'Module#prepend visibility' do
685 bug8005 = '[ruby-core:53106] [Bug #8005]'
686 c = Class.new do
687 prepend Module.new {}
688 def foo() end
689 protected :foo
690 end
691 a = c.new
692 assert_true a.respond_to?(:foo), bug8005
693 assert_nothing_raised(NoMethodError, bug8005) {a.send :foo}
694 end
695
696 # mruby has no visibility control
697 assert 'Module#prepend inherited visibility' do
698 bug8238 = '[ruby-core:54105] [Bug #8238]'
699 module Test4PrependVisibilityInherited
700 class A
701 def foo() A; end
702 private :foo
703 end
704 class B < A
705 public :foo
706 prepend Module.new
707 end
708 end
709 assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}")
710 end
711
712 assert 'Module#prepend + #included_modules' do
713 bug8025 = '[ruby-core:53158] [Bug #8025]'
714 mixin = labeled_module("mixin")
715 c = labeled_module("c") {prepend mixin}
716 im = c.included_modules
717 assert_not_include(im, c, bug8025)
718 assert_include(im, mixin, bug8025)
719 c1 = labeled_class("c1") {prepend mixin}
720 c2 = labeled_class("c2", c1)
721 im = c2.included_modules
722 assert_not_include(im, c1, bug8025)
723 assert_not_include(im, c2, bug8025)
724 assert_include(im, mixin, bug8025)
725 end
726
727 assert 'Module#prepend super in alias' do
728 skip "super does not currently work in aliased methods"
729 bug7842 = '[Bug #7842]'
730
731 p = labeled_module("P") do
732 def m; "P"+super; end
733 end
734
735 a = labeled_class("A") do
736 def m; "A"; end
737 end
738
739 b = labeled_class("B", a) do
740 def m; "B"+super; end
741 alias m2 m
742 prepend p
743 alias m3 m
744 end
745
746 assert_nothing_raised do
747 assert_equal("BA", b.new.m2, bug7842)
748 end
749
750 assert_nothing_raised do
751 assert_equal("PBA", b.new.m3, bug7842)
752 end
753 end
754
755 assert 'Module#prepend each class' do
756 m = labeled_module("M")
757 c1 = labeled_class("C1") {prepend m}
758 c2 = labeled_class("C2", c1) {prepend m}
759 assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should be able to prepend each class")
760 end
761
762 assert 'Module#prepend no duplication' do
763 m = labeled_module("M")
764 c = labeled_class("C") {prepend m; prepend m}
765 assert_equal([m, c], c.ancestors[0, 2], "should never duplicate")
766 end
767
768 assert 'Module#prepend in superclass' do
769 m = labeled_module("M")
770 c1 = labeled_class("C1")
771 c2 = labeled_class("C2", c1) {prepend m}
772 c1.class_eval {prepend m}
773 assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass")
774 end
775
776 # requires #assert_seperately
777 #assert 'Module#prepend call super' do
778 # assert_separately([], <<-'end;') #do
779 # bug10847 = '[ruby-core:68093] [Bug #10847]'
780 # module M; end
781 # Float.prepend M
782 # assert_nothing_raised(SystemStackError, bug10847) do
783 # 0.3.numerator
784 # end
785 # end;
786 #end
787# @!endgroup prepend
788
789assert('Module#to_s') do
790 module Test4to_sModules
791 end
792
793 assert_equal 'Test4to_sModules', Test4to_sModules.to_s
794end
795
796assert('Module#inspect') do
797 module Test4to_sModules
798 end
799
800 assert_equal 'Test4to_sModules', Test4to_sModules.inspect
801end
802
803assert('Issue 1467') do
804 module M1
805 def initialize()
806 super()
807 end
808 end
809
810 class C1
811 include M1
812 def initialize()
813 super()
814 end
815 end
816
817 class C2
818 include M1
819 end
820
821 C1.new
822 C2.new
823end
824
825assert('clone Module') do
826 module M1
827 def foo
828 true
829 end
830 end
831
832 class B
833 include M1.clone
834 end
835
836 B.new.foo
837end
838
839assert('Module#module_function') do
840 module M
841 def modfunc; end
842 module_function :modfunc
843 end
844
845 assert_true M.respond_to?(:modfunc)
846end
847
848assert('module with non-class/module outer raises TypeError') do
849 assert_raise(TypeError) { module 0::M1 end }
850 assert_raise(TypeError) { module []::M2 end }
851end
852
853assert('get constant of parent module in singleton class; issue #3568') do
854 actual = module GetConstantInSingletonTest
855 EXPECTED = "value"
856 class << self
857 EXPECTED
858 end
859 end
860
861 assert_equal("value", actual)
862end
Note: See TracBrowser for help on using the repository browser.