Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / test / t / module.rb
1 ##
2 # Module ISO Test
3
4 def labeled_module(name, &block)
5   Module.new do
6     (class <<self; self end).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
12 end
13
14 def labeled_class(name, supklass = Object, &block)
15   Class.new(supklass) do
16     (class <<self; self end).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
22 end
23
24 assert('Module', '15.2.2') do
25   assert_equal Class, Module.class
26 end
27
28 # TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do
29
30 assert('Module#ancestors', '15.2.2.4.9') do
31   class Test4ModuleAncestors
32   end
33   r = String.ancestors
34
35   assert_equal Array, r.class
36   assert_true r.include?(String)
37   assert_true r.include?(Object)
38 end
39
40 assert('Module#append_features', '15.2.2.4.10') do
41   module Test4AppendFeatures
42     def self.append_features(mod)
43       Test4AppendFeatures2.const_set(:Const4AppendFeatures2, mod)
44     end
45   end
46   module Test4AppendFeatures2
47     include Test4AppendFeatures
48   end
49
50   assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2)
51 end
52
53 assert('Module#attr NameError') do
54   %w[
55     foo?
56     @foo
57     @@foo
58     $foo
59   ].each do |name|
60     module NameTest; end
61
62     assert_raise(NameError) do
63       NameTest.module_eval { attr_reader name.to_sym }
64     end
65
66     assert_raise(NameError) do
67       NameTest.module_eval { attr_writer name.to_sym }
68     end
69
70     assert_raise(NameError) do
71       NameTest.module_eval { attr name.to_sym }
72     end
73
74     assert_raise(NameError) do
75       NameTest.module_eval { attr_accessor name.to_sym }
76     end
77   end
78
79 end
80
81 assert('Module#attr', '15.2.2.4.11') do
82   class AttrTest
83     class << self
84       attr :cattr
85       def cattr_val=(val)
86         @cattr = val
87       end
88     end
89     attr :iattr
90     def iattr_val=(val)
91       @iattr = val
92     end
93   end
94
95   test = AttrTest.new
96   assert_true AttrTest.respond_to?(:cattr)
97   assert_true test.respond_to?(:iattr)
98
99   assert_false AttrTest.respond_to?(:cattr=)
100   assert_false test.respond_to?(:iattr=)
101
102   test.iattr_val = 'test'
103   assert_equal 'test', test.iattr
104
105   AttrTest.cattr_val = 'test'
106   assert_equal 'test', AttrTest.cattr
107 end
108
109 assert('Module#attr_accessor', '15.2.2.4.12') do
110   class AttrTestAccessor
111     class << self
112       attr_accessor :cattr
113     end
114     attr_accessor :iattr, 'iattr2'
115   end
116
117   attr_instance = AttrTestAccessor.new
118   assert_true AttrTestAccessor.respond_to?(:cattr=)
119   assert_true attr_instance.respond_to?(:iattr=)
120   assert_true attr_instance.respond_to?(:iattr2=)
121   assert_true AttrTestAccessor.respond_to?(:cattr)
122   assert_true attr_instance.respond_to?(:iattr)
123   assert_true attr_instance.respond_to?(:iattr2)
124
125   attr_instance.iattr = 'test'
126   assert_equal 'test', attr_instance.iattr
127
128   AttrTestAccessor.cattr = 'test'
129   assert_equal 'test', AttrTestAccessor.cattr
130 end
131
132 assert('Module#attr_reader', '15.2.2.4.13') do
133   class AttrTestReader
134     class << self
135       attr_reader :cattr
136       def cattr_val=(val)
137         @cattr = val
138       end
139     end
140     attr_reader :iattr, 'iattr2'
141     def iattr_val=(val)
142       @iattr = val
143     end
144   end
145
146   attr_instance = AttrTestReader.new
147   assert_true AttrTestReader.respond_to?(:cattr)
148   assert_true attr_instance.respond_to?(:iattr)
149   assert_true attr_instance.respond_to?(:iattr2)
150
151   assert_false AttrTestReader.respond_to?(:cattr=)
152   assert_false attr_instance.respond_to?(:iattr=)
153   assert_false attr_instance.respond_to?(:iattr2=)
154
155   attr_instance.iattr_val = 'test'
156   assert_equal 'test', attr_instance.iattr
157
158   AttrTestReader.cattr_val = 'test'
159   assert_equal 'test', AttrTestReader.cattr
160 end
161
162 assert('Module#attr_writer', '15.2.2.4.14') do
163   class AttrTestWriter
164     class << self
165       attr_writer :cattr
166       def cattr_val
167         @cattr
168       end
169     end
170     attr_writer :iattr, 'iattr2'
171     def iattr_val
172       @iattr
173     end
174   end
175
176   attr_instance = AttrTestWriter.new
177   assert_true AttrTestWriter.respond_to?(:cattr=)
178   assert_true attr_instance.respond_to?(:iattr=)
179   assert_true attr_instance.respond_to?(:iattr2=)
180
181   assert_false AttrTestWriter.respond_to?(:cattr)
182   assert_false attr_instance.respond_to?(:iattr)
183   assert_false attr_instance.respond_to?(:iattr2)
184
185   attr_instance.iattr = 'test'
186   assert_equal 'test', attr_instance.iattr_val
187
188   AttrTestWriter.cattr = 'test'
189   assert_equal 'test', AttrTestWriter.cattr_val
190 end
191
192 assert('Module#class_eval', '15.2.2.4.15') do
193   class Test4ClassEval
194     @a = 11
195     @b = 12
196   end
197   Test4ClassEval.class_eval do
198     def method1
199     end
200   end
201   assert_equal 11, Test4ClassEval.class_eval{ @a }
202   assert_equal 12, Test4ClassEval.class_eval{ @b }
203   assert_equal true, Test4ClassEval.new.respond_to?(:method1)
204 end
205
206 assert('Module#const_defined?', '15.2.2.4.20') do
207   module Test4ConstDefined
208     Const4Test4ConstDefined = true
209   end
210
211   assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined)
212   assert_false Test4ConstDefined.const_defined?(:NotExisting)
213   assert_raise(NameError){ Test4ConstDefined.const_defined?(:wrong_name) }
214 end
215
216 assert('Module#const_get', '15.2.2.4.21') do
217   module Test4ConstGet
218     Const4Test4ConstGet = 42
219   end
220
221   assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet)
222   assert_equal 42, Test4ConstGet.const_get("Const4Test4ConstGet")
223   assert_equal 42, Object.const_get("Test4ConstGet::Const4Test4ConstGet")
224
225   assert_raise(TypeError){ Test4ConstGet.const_get(123) }
226   assert_raise(NameError){ Test4ConstGet.const_get(:I_DO_NOT_EXIST) }
227   assert_raise(NameError){ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") }
228   assert_raise(NameError){ Test4ConstGet.const_get(:wrong_name) }
229 end
230
231 assert('Module#const_set', '15.2.2.4.23') do
232   module Test4ConstSet
233     Const4Test4ConstSet = 42
234   end
235
236   assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23)
237   assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet)
238   ["", "wrongNAME", "Wrong-Name"].each do |n|
239     assert_raise(NameError) { Test4ConstSet.const_set(n, 1) }
240   end
241 end
242
243 assert('Module#remove_const', '15.2.2.4.40') do
244   module Test4RemoveConst
245     ExistingConst = 23
246   end
247
248   assert_equal 23, Test4RemoveConst.remove_const(:ExistingConst)
249   assert_false Test4RemoveConst.const_defined?(:ExistingConst)
250   assert_raise(NameError) { Test4RemoveConst.remove_const(:NonExistingConst) }
251   %i[x X!].each do |n|
252     assert_raise(NameError) { Test4RemoveConst.remove_const(n) }
253   end
254 end
255
256 assert('Module#const_missing', '15.2.2.4.22') do
257   module Test4ConstMissing
258     def self.const_missing(sym)
259       42 # the answer to everything
260     end
261   end
262
263   assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist)
264 end
265
266 assert('Module#include', '15.2.2.4.27') do
267   module Test4Include
268     Const4Include = 42
269   end
270   module Test4Include2
271     @include_result = include Test4Include
272     class << self
273       attr_reader :include_result
274     end
275   end
276
277   assert_equal 42, Test4Include2.const_get(:Const4Include)
278   assert_equal Test4Include2, Test4Include2.include_result
279 end
280
281 assert('Module#include?', '15.2.2.4.28') do
282   module Test4IncludeP
283   end
284   class Test4IncludeP2
285     include Test4IncludeP
286   end
287   class Test4IncludeP3 < Test4IncludeP2
288   end
289
290   assert_true Test4IncludeP2.include?(Test4IncludeP)
291   assert_true Test4IncludeP3.include?(Test4IncludeP)
292   assert_false Test4IncludeP.include?(Test4IncludeP)
293 end
294
295 assert('Module#included', '15.2.2.4.29') do
296   module Test4Included
297     Const4Included = 42
298     def self.included mod
299       Test4Included.const_set(:Const4Included2, mod)
300     end
301   end
302   module Test4Included2
303     include Test4Included
304   end
305
306   assert_equal 42, Test4Included2.const_get(:Const4Included)
307   assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2)
308 end
309
310 assert('Module#initialize', '15.2.2.4.31') do
311   assert_kind_of Module, Module.new
312   mod = Module.new { def hello; "hello"; end }
313   cls = Class.new{include mod}
314   assert_true cls.new.respond_to?(:hello)
315   a = nil
316   mod = Module.new { |m| a = m }
317   assert_equal mod, a
318 end
319
320 assert('Module#method_defined?', '15.2.2.4.34') do
321   module Test4MethodDefined
322     module A
323       def method1()  end
324     end
325
326     class B
327       def method2()  end
328     end
329
330     class C < B
331       include A
332       def method3()  end
333     end
334   end
335
336   assert_true Test4MethodDefined::A.method_defined? :method1
337   assert_true Test4MethodDefined::C.method_defined? :method1
338   assert_true Test4MethodDefined::C.method_defined? "method2"
339   assert_true Test4MethodDefined::C.method_defined? "method3"
340   assert_false Test4MethodDefined::C.method_defined? "method4"
341 end
342
343 assert('Module#module_eval', '15.2.2.4.35') do
344   module Test4ModuleEval
345     @a = 11
346     @b = 12
347   end
348
349   assert_equal 11, Test4ModuleEval.module_eval{ @a }
350   assert_equal 12, Test4ModuleEval.module_eval{ @b }
351 end
352
353 assert('Module#undef_method', '15.2.2.4.42') do
354   module Test4UndefMethod
355     class Parent
356       def hello
357       end
358      end
359
360      class Child < Parent
361       def hello
362       end
363      end
364
365      class GrandChild < Child
366      end
367   end
368   Test4UndefMethod::Child.class_eval{ undef_method :hello }
369
370   assert_true Test4UndefMethod::Parent.new.respond_to?(:hello)
371   assert_false Test4UndefMethod::Child.new.respond_to?(:hello)
372   assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello)
373 end
374
375 # Not ISO specified
376
377 assert('Module#define_method') do
378   c = Class.new {
379     define_method(:m1) { :ok }
380     define_method(:m2, Proc.new { :ok })
381   }
382   assert_equal c.new.m1, :ok
383   assert_equal c.new.m2, :ok
384   assert_raise(TypeError) do
385     Class.new { define_method(:n1, nil) }
386   end
387 end
388
389 # @!group prepend
390   assert('Module#prepend') do
391     module M0
392       def m1; [:M0] end
393     end
394     module M1
395       def m1; [:M1, super, :M1] end
396     end
397     module M2
398       def m1; [:M2, super, :M2] end
399     end
400     M3 = Module.new do
401       def m1; [:M3, super, :M3] end
402     end
403     module M4
404       def m1; [:M4, super, :M4] end
405     end
406
407     class P0
408       include M0
409       prepend M1
410       def m1; [:C0, super, :C0] end
411     end
412     class P1 < P0
413       prepend M2, M3
414       include M4
415       def m1; [:C1, super, :C1] end
416     end
417
418     obj = P1.new
419     expected = [:M2,[:M3,[:C1,[:M4,[:M1,[:C0,[:M0],:C0],:M1],:M4],:C1],:M3],:M2]
420     assert_equal(expected, obj.m1)
421   end
422
423   assert('Module#prepend result') do
424     module TestPrepended; end
425     module TestPrependResult
426       @prepend_result = prepend TestPrepended
427       class << self
428         attr_reader :prepend_result
429       end
430     end
431
432     assert_equal TestPrependResult, TestPrependResult.prepend_result
433   end
434
435   # mruby shouldn't be affected by this since there is
436   # no visibility control (yet)
437   assert('Module#prepend public') do
438     assert_nothing_raised('ruby/ruby #8846') do
439       Class.new.prepend(Module.new)
440     end
441   end
442
443   assert('Module#prepend inheritance') do
444     bug6654 = '[ruby-core:45914]'
445     a = labeled_module('a')
446     b = labeled_module('b') { include a }
447     c = labeled_module('c') { prepend b }
448
449     #assert bug6654 do
450       # the Module#< operator should be used here instead, but we don't have it
451       assert_include(c.ancestors, a)
452       assert_include(c.ancestors, b)
453     #end
454
455     bug8357 = '[ruby-core:54736] [Bug #8357]'
456     b = labeled_module('b') { prepend a }
457     c = labeled_class('c') { include b }
458
459     #assert bug8357 do
460       # the Module#< operator should be used here instead, but we don't have it
461       assert_include(c.ancestors, a)
462       assert_include(c.ancestors, b)
463     #end
464
465     bug8357 = '[ruby-core:54742] [Bug #8357]'
466     assert_kind_of(b, c.new, bug8357)
467   end
468
469   assert 'Module#prepend + Class#ancestors' do
470     bug6658 = '[ruby-core:45919]'
471     m = labeled_module("m")
472     c = labeled_class("c") {prepend m}
473     assert_equal([m, c], c.ancestors[0, 2], bug6658)
474
475     bug6662 = '[ruby-dev:45868]'
476     c2 = labeled_class("c2", c)
477     anc = c2.ancestors
478     assert_equal([c2, m, c, Object], anc[0..anc.index(Object)], bug6662)
479   end
480
481   assert 'Module#prepend + Module#ancestors' do
482     bug6659 = '[ruby-dev:45861]'
483     m0 = labeled_module("m0") { def x; [:m0, *super] end }
484     m1 = labeled_module("m1") { def x; [:m1, *super] end; prepend m0 }
485     m2 = labeled_module("m2") { def x; [:m2, *super] end; prepend m1 }
486     c0 = labeled_class("c0") { def x; [:c0] end }
487     c1 = labeled_class("c1") { def x; [:c1] end; prepend m2 }
488     c2 = labeled_class("c2", c0) { def x; [:c2, *super] end; include m2 }
489     #
490     assert_equal([m0, m1], m1.ancestors, bug6659)
491     #
492     bug6662 = '[ruby-dev:45868]'
493     assert_equal([m0, m1, m2], m2.ancestors, bug6662)
494     assert_equal([m0, m1, m2, c1], c1.ancestors[0, 4], bug6662)
495     assert_equal([:m0, :m1, :m2, :c1], c1.new.x)
496     assert_equal([c2, m0, m1, m2, c0], c2.ancestors[0, 5], bug6662)
497     assert_equal([:c2, :m0, :m1, :m2, :c0], c2.new.x)
498     #
499     m3 = labeled_module("m3") { include m1; prepend m1 }
500     assert_equal([m3, m0, m1], m3.ancestors)
501     m3 = labeled_module("m3") { prepend m1; include m1 }
502     assert_equal([m0, m1, m3], m3.ancestors)
503     m3 = labeled_module("m3") { prepend m1; prepend m1 }
504     assert_equal([m0, m1, m3], m3.ancestors)
505     m3 = labeled_module("m3") { include m1; include m1 }
506     assert_equal([m3, m0, m1], m3.ancestors)
507   end
508
509   assert 'cyclic Module#prepend' do
510     bug7841 = '[ruby-core:52205] [Bug #7841]'
511     m1 = Module.new
512     m2 = Module.new
513     m1.instance_eval { prepend(m2) }
514     assert_raise(ArgumentError, bug7841) do
515       m2.instance_eval { prepend(m1) }
516     end
517   end
518
519   # these assertions will not run without a #assert_separately method
520   #assert 'test_prepend_optmethod' do
521   #  bug7983 = '[ruby-dev:47124] [Bug #7983]'
522   #  assert_separately [], %{
523   #    module M
524   #      def /(other)
525   #        to_f / other
526   #      end
527   #    end
528   #    Fixnum.send(:prepend, M)
529   #    assert_equal(0.5, 1 / 2, "#{bug7983}")
530   #  }
531   #  assert_equal(0, 1 / 2)
532   #end
533
534   # mruby has no visibility control
535   # assert 'Module#prepend visibility' do
536   #   bug8005 = '[ruby-core:53106] [Bug #8005]'
537   #   c = Class.new do
538   #     prepend Module.new {}
539   #     def foo() end
540   #     protected :foo
541   #   end
542   #   a = c.new
543   #   assert_true a.respond_to?(:foo), bug8005
544   #   assert_nothing_raised(bug8005) {a.send :foo}
545   # end
546
547   # mruby has no visibility control
548   # assert 'Module#prepend inherited visibility' do
549   #   bug8238 = '[ruby-core:54105] [Bug #8238]'
550   #   module Test4PrependVisibilityInherited
551   #     class A
552   #       def foo() A; end
553   #       private :foo
554   #     end
555   #     class B < A
556   #       public :foo
557   #       prepend Module.new
558   #     end
559   #   end
560   #   assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}")
561   # end
562
563   # assert 'Module#prepend super in alias' do
564   #   skip "super does not currently work in aliased methods"
565   #   bug7842 = '[Bug #7842]'
566
567   #   p = labeled_module("P") do
568   #     def m; "P"+super; end
569   #   end
570
571   #   a = labeled_class("A") do
572   #     def m; "A"; end
573   #   end
574
575   #   b = labeled_class("B", a) do
576   #     def m; "B"+super; end
577   #     alias m2 m
578   #     prepend p
579   #     alias m3 m
580   #   end
581
582   #   assert_nothing_raised do
583   #     assert_equal("BA", b.new.m2, bug7842)
584   #   end
585
586   #   assert_nothing_raised do
587   #     assert_equal("PBA", b.new.m3, bug7842)
588   #   end
589   # end
590
591   assert 'Module#prepend each class' do
592     m = labeled_module("M")
593     c1 = labeled_class("C1") {prepend m}
594     c2 = labeled_class("C2", c1) {prepend m}
595     assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should be able to prepend each class")
596   end
597
598   assert 'Module#prepend no duplication' do
599     m = labeled_module("M")
600     c = labeled_class("C") {prepend m; prepend m}
601     assert_equal([m, c], c.ancestors[0, 2], "should never duplicate")
602   end
603
604   assert 'Module#prepend in superclass' do
605     m = labeled_module("M")
606     c1 = labeled_class("C1")
607     c2 = labeled_class("C2", c1) {prepend m}
608     c1.class_eval {prepend m}
609     assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass")
610   end
611
612   # requires #assert_separately
613   #assert 'Module#prepend call super' do
614   #  assert_separately([], <<-'end;') #do
615   #    bug10847 = '[ruby-core:68093] [Bug #10847]'
616   #    module M; end
617   #    Float.prepend M
618   #    assert_nothing_raised(SystemStackError, bug10847) do
619   #      0.3.numerator
620   #    end
621   #  end;
622   #end
623 # @!endgroup prepend
624
625 assert('Module#to_s') do
626   module Outer
627     class Inner; end
628     const_set :SetInner, Class.new
629   end
630
631   assert_equal 'Outer', Outer.to_s
632   assert_equal 'Outer::Inner', Outer::Inner.to_s
633   assert_equal 'Outer::SetInner', Outer::SetInner.to_s
634
635   outer = Module.new do
636     const_set :SetInner, Class.new
637   end
638   Object.const_set :SetOuter, outer
639
640   assert_equal 'SetOuter', SetOuter.to_s
641   assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s
642
643   mod = Module.new
644   cls = Class.new
645
646   assert_equal "#<Module:0x", mod.to_s[0,11]
647   assert_equal "#<Class:0x", cls.to_s[0,10]
648 end
649
650 assert('Module#inspect') do
651   module Test4to_sModules
652   end
653
654   assert_equal 'Test4to_sModules', Test4to_sModules.inspect
655 end
656
657 assert('Issue 1467') do
658   module M1
659     def initialize()
660       super()
661     end
662   end
663
664   class C1
665     include M1
666      def initialize()
667        super()
668      end
669   end
670
671   class C2
672     include M1
673   end
674
675   C1.new
676   C2.new
677 end
678
679 assert('clone Module') do
680   module M1
681     def foo
682       true
683     end
684   end
685
686   class B
687     include M1.clone
688   end
689
690   B.new.foo
691 end
692
693 assert('Module#module_function') do
694   module M
695     def modfunc; end
696     module_function :modfunc
697   end
698
699   assert_true M.respond_to?(:modfunc)
700 end
701
702 assert('module with non-class/module outer raises TypeError') do
703   assert_raise(TypeError) { module 0::M1 end }
704   assert_raise(TypeError) { module []::M2 end }
705 end
706
707 assert('module to return the last value') do
708   m = module M; :m end
709   assert_equal(m, :m)
710 end
711
712 assert('module to return nil if body is empty') do
713   assert_nil(module M end)
714 end
715
716 assert('get constant of parent module in singleton class; issue #3568') do
717   actual = module GetConstantInSingletonTest
718     EXPECTED = "value"
719     class << self
720       EXPECTED
721     end
722   end
723
724   assert_equal("value", actual)
725 end