Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/test/t/module.rb

    r331 r439  
    44def labeled_module(name, &block)
    55  Module.new do
    6     singleton_class.class_eval do
     6    (class <<self; self end).class_eval do
    77      define_method(:to_s) { name }
    88      alias_method :inspect, :to_s
     
    1414def labeled_class(name, supklass = Object, &block)
    1515  Class.new(supklass) do
    16     singleton_class.class_eval do
     16    (class <<self; self end).class_eval do
    1717      define_method(:to_s) { name }
    1818      alias_method :inspect, :to_s
     
    2222end
    2323
     24def assert_uninitialized_const(&block)
     25  assert_raise_with_message_pattern(NameError, "uninitialized constant *", &block)
     26end
     27
     28def assert_wrong_const_name(&block)
     29  assert_raise_with_message_pattern(NameError, "wrong constant name *", &block)
     30end
     31
    2432assert('Module', '15.2.2') do
    2533  assert_equal Class, Module.class
    2634end
    2735
     36assert('Module#alias_method', '15.2.2.4.8') do
     37  cls = Class.new do
     38    def foo
     39      "FOO"
     40    end
     41  end
     42
     43  assert_same(cls, cls.alias_method(:bar, :foo))
     44  assert_equal("FOO", cls.new.bar)
     45end
     46
    2847# 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
    3148
    3249assert('Module#ancestors', '15.2.2.4.9') do
    3350  class Test4ModuleAncestors
    3451  end
    35   sc = Test4ModuleAncestors.singleton_class
    3652  r = String.ancestors
    3753
     
    5268
    5369  assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2)
     70  assert_raise(FrozenError) { Module.new.append_features Class.new.freeze }
    5471end
    5572
     
    202219    end
    203220  end
    204   r = Test4ClassEval.instance_methods
    205 
    206221  assert_equal 11, Test4ClassEval.class_eval{ @a }
    207222  assert_equal 12, Test4ClassEval.class_eval{ @b }
    208   assert_equal Array, r.class
    209   assert_true r.include?(:method1)
    210 end
    211 
    212 assert('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)
    219 end
    220 
    221 assert('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)
    227 end
    228 
    229 assert('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
    242 end
    243 
    244 assert('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
     223  assert_equal true, Test4ClassEval.new.respond_to?(:method1)
    254224end
    255225
     
    261231  assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined)
    262232  assert_false Test4ConstDefined.const_defined?(:NotExisting)
     233  assert_wrong_const_name{ Test4ConstDefined.const_defined?(:wrong_name) }
    263234end
    264235
     
    269240
    270241  assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet)
     242  assert_equal 42, Test4ConstGet.const_get("Const4Test4ConstGet")
     243  assert_equal 42, Object.const_get("Test4ConstGet::Const4Test4ConstGet")
     244
     245  assert_raise(TypeError){ Test4ConstGet.const_get(123) }
     246  assert_uninitialized_const{ Test4ConstGet.const_get(:I_DO_NOT_EXIST) }
     247  assert_uninitialized_const{ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") }
     248  assert_wrong_const_name{ Test4ConstGet.const_get(:wrong_name) }
     249end
     250
     251assert('Module#const_set', '15.2.2.4.23') do
     252  module Test4ConstSet
     253    Const4Test4ConstSet = 42
     254  end
     255
     256  assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23)
     257  assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet)
     258  ["", "wrongNAME", "Wrong-Name"].each do |n|
     259    assert_wrong_const_name { Test4ConstSet.const_set(n, 1) }
     260  end
     261end
     262
     263assert('Module#remove_const', '15.2.2.4.40') do
     264  module Test4RemoveConst
     265    ExistingConst = 23
     266  end
     267
     268  assert_equal 23, Test4RemoveConst.remove_const(:ExistingConst)
     269  assert_false Test4RemoveConst.const_defined?(:ExistingConst)
     270  assert_raise_with_message_pattern(NameError, "constant * not defined") do
     271    Test4RemoveConst.remove_const(:NonExistingConst)
     272  end
     273  %i[x X!].each do |n|
     274    assert_wrong_const_name { Test4RemoveConst.remove_const(n) }
     275  end
     276  assert_raise(FrozenError) { Test4RemoveConst.freeze.remove_const(:A) }
    271277end
    272278
     
    281287end
    282288
    283 assert('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)
    290 end
    291 
    292 assert('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
     289assert('Module#extend_object', '15.2.2.4.25') do
     290  cls = Class.new
     291  mod = Module.new { def foo; end }
     292  a = cls.new
     293  b = cls.new
     294  mod.extend_object(b)
     295  assert_false a.respond_to?(:foo)
     296  assert_true b.respond_to?(:foo)
     297  assert_raise(FrozenError) { mod.extend_object(cls.new.freeze) }
     298  assert_raise(FrozenError, TypeError) { mod.extend_object(1) }
    305299end
    306300
     
    310304  end
    311305  module Test4Include2
    312     include Test4Include
     306    @include_result = include Test4Include
     307    class << self
     308      attr_reader :include_result
     309    end
    313310  end
    314311
    315312  assert_equal 42, Test4Include2.const_get(:Const4Include)
     313  assert_equal Test4Include2, Test4Include2.include_result
     314  assert_raise(FrozenError) { Module.new.freeze.include Test4Include }
    316315end
    317316
     
    345344end
    346345
    347 assert('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)
    357 end
    358 
    359346assert('Module#initialize', '15.2.2.4.31') do
    360347  assert_kind_of Module, Module.new
    361348  mod = Module.new { def hello; "hello"; end }
    362   assert_equal [:hello], mod.instance_methods
     349  cls = Class.new{include mod}
     350  assert_true cls.new.respond_to?(:hello)
    363351  a = nil
    364352  mod = Module.new { |m| a = m }
    365353  assert_equal mod, a
    366 end
    367 
    368 assert('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)
    387354end
    388355
     
    410377end
    411378
    412 
    413379assert('Module#module_eval', '15.2.2.4.35') do
    414380  module Test4ModuleEval
     
    419385  assert_equal 11, Test4ModuleEval.module_eval{ @a }
    420386  assert_equal 12, Test4ModuleEval.module_eval{ @b }
    421 end
    422 
    423 assert('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
    430 end
    431 
    432 assert('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
    452 end
    453 
    454 assert('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
    470387end
    471388
     
    490407  assert_false Test4UndefMethod::Child.new.respond_to?(:hello)
    491408  assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello)
    492   assert_false Test4UndefMethod::Child.instance_methods(false).include? :hello
    493409end
    494410
    495411# Not ISO specified
     412
     413assert('Module#dup') do
     414  module TestModuleDup
     415    @@cvar = :cvar
     416    class << self
     417      attr_accessor :cattr
     418      def cmeth; :cmeth end
     419    end
     420    def cvar; @@cvar end
     421    def imeth; :imeth end
     422    self.cattr = :cattr
     423  end
     424
     425  m = TestModuleDup.dup
     426  o = Object.include(m).new
     427  assert_equal(:cattr, m.cattr)
     428  assert_equal(:cmeth, m.cmeth)
     429  assert_equal(:cvar, o.cvar)
     430  assert_equal(:imeth, o.imeth)
     431  assert_match("#<Module:0x*>", m.to_s)
     432  assert_not_predicate(m, :frozen?)
     433  assert_not_predicate(TestModuleDup.freeze.dup, :frozen?)
     434end
    496435
    497436assert('Module#define_method') do
     
    507446end
    508447
     448assert 'Module#prepend_features' do
     449  mod = Module.new { def m; :mod end }
     450  cls = Class.new { def m; :cls end }
     451  assert_equal :cls, cls.new.m
     452  mod.prepend_features(cls)
     453  assert_equal :mod, cls.new.m
     454  assert_raise(FrozenError) { Module.new.prepend_features(Class.new.freeze) }
     455end
     456
    509457# @!group prepend
    510458  assert('Module#prepend') do
     
    539487    expected = [:M2,[:M3,[:C1,[:M4,[:M1,[:C0,[:M0],:C0],:M1],:M4],:C1],:M3],:M2]
    540488    assert_equal(expected, obj.m1)
     489  end
     490
     491  assert('Module#prepend result') do
     492    module TestPrepended; end
     493    module TestPrependResult
     494      @prepend_result = prepend TestPrepended
     495      class << self
     496        attr_reader :prepend_result
     497      end
     498    end
     499
     500    assert_equal TestPrependResult, TestPrependResult.prepend_result
    541501  end
    542502
     
    573533    bug8357 = '[ruby-core:54742] [Bug #8357]'
    574534    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)
    610535  end
    611536
     
    650575  end
    651576
    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 
    658577  assert 'cyclic Module#prepend' do
    659578    bug7841 = '[ruby-core:52205] [Bug #7841]'
     
    666585  end
    667586
    668   # these assertions will not run without a #assert_seperately method
     587  # these assertions will not run without a #assert_separately method
    669588  #assert 'test_prepend_optmethod' do
    670589  #  bug7983 = '[ruby-dev:47124] [Bug #7983]'
     
    682601
    683602  # 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
     603  # assert 'Module#prepend visibility' do
     604  #   bug8005 = '[ruby-core:53106] [Bug #8005]'
     605  #   c = Class.new do
     606  #     prepend Module.new {}
     607  #     def foo() end
     608  #     protected :foo
     609  #   end
     610  #   a = c.new
     611  #   assert_true a.respond_to?(:foo), bug8005
     612  #   assert_nothing_raised(bug8005) {a.send :foo}
     613  # end
    695614
    696615  # 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
     616  # assert 'Module#prepend inherited visibility' do
     617  #   bug8238 = '[ruby-core:54105] [Bug #8238]'
     618  #   module Test4PrependVisibilityInherited
     619  #     class A
     620  #       def foo() A; end
     621  #       private :foo
     622  #     end
     623  #     class B < A
     624  #       public :foo
     625  #       prepend Module.new
     626  #     end
     627  #   end
     628  #   assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}")
     629  # end
     630
     631  # assert 'Module#prepend super in alias' do
     632  #   skip "super does not currently work in aliased methods"
     633  #   bug7842 = '[Bug #7842]'
     634
     635  #   p = labeled_module("P") do
     636  #     def m; "P"+super; end
     637  #   end
     638
     639  #   a = labeled_class("A") do
     640  #     def m; "A"; end
     641  #   end
     642
     643  #   b = labeled_class("B", a) do
     644  #     def m; "B"+super; end
     645  #     alias m2 m
     646  #     prepend p
     647  #     alias m3 m
     648  #   end
     649
     650  #   assert_nothing_raised do
     651  #     assert_equal("BA", b.new.m2, bug7842)
     652  #   end
     653
     654  #   assert_nothing_raised do
     655  #     assert_equal("PBA", b.new.m3, bug7842)
     656  #   end
     657  # end
    754658
    755659  assert 'Module#prepend each class' do
     
    774678  end
    775679
    776   # requires #assert_seperately
     680  # requires #assert_separately
    777681  #assert 'Module#prepend call super' do
    778682  #  assert_separately([], <<-'end;') #do
     
    785689  #  end;
    786690  #end
     691
     692  assert 'Module#prepend to frozen class' do
     693    assert_raise(FrozenError) { Class.new.freeze.prepend Module.new }
     694  end
    787695# @!endgroup prepend
    788696
    789697assert('Module#to_s') do
    790   module Test4to_sModules
    791   end
    792 
    793   assert_equal 'Test4to_sModules', Test4to_sModules.to_s
     698  module Outer
     699    class Inner; end
     700    const_set :SetInner, Class.new
     701  end
     702
     703  assert_equal 'Outer', Outer.to_s
     704  assert_equal 'Outer::Inner', Outer::Inner.to_s
     705  assert_equal 'Outer::SetInner', Outer::SetInner.to_s
     706
     707  outer = Module.new do
     708    const_set :SetInner, Class.new
     709  end
     710  Object.const_set :SetOuter, outer
     711
     712  assert_equal 'SetOuter', SetOuter.to_s
     713  assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s
     714
     715  assert_match "#<Module:0x*>", Module.new.to_s
     716  assert_match "#<Class:0x*>", Class.new.to_s
     717
     718  assert_equal "FrozenClassToS", (FrozenClassToS = Class.new.freeze).to_s
     719  assert_equal "Outer::A", (Outer::A = Module.new.freeze).to_s
     720  assert_match "#<Module:0x*>::A", (Module.new::A = Class.new.freeze).to_s
    794721end
    795722
     
    819746  end
    820747
    821   C1.new
    822   C2.new
     748  assert_kind_of(M1, C1.new)
     749  assert_kind_of(M1, C2.new)
    823750end
    824751
     
    834761  end
    835762
    836   B.new.foo
     763  assert_true(B.new.foo)
    837764end
    838765
     
    849776  assert_raise(TypeError) { module 0::M1 end }
    850777  assert_raise(TypeError) { module []::M2 end }
     778end
     779
     780assert('module to return the last value') do
     781  m = module M; :m end
     782  assert_equal(m, :m)
     783end
     784
     785assert('module to return nil if body is empty') do
     786  assert_nil(module M end)
    851787end
    852788
Note: See TracChangeset for help on using the changeset viewer.