diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-08-30 16:07:59 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-08-30 22:30:36 +0900 |
| commit | e471d37ca5f1422860a1eaa81d4c9f1b3c8b6aed (patch) | |
| tree | 9f474e50d1dd921abe1e2611fd33e9e03825d191 /test/t/module.rb | |
| parent | 75a01af710309e4fc53db285c9018e233c61cb56 (diff) | |
| download | mruby-e471d37ca5f1422860a1eaa81d4c9f1b3c8b6aed.tar.gz mruby-e471d37ca5f1422860a1eaa81d4c9f1b3c8b6aed.zip | |
Separate meta-programming features to `mruby-metaprog` gem.
We assume meta-programming is less used in embedded environments.
We have moved following methods:
* Kernel module
global_variables, local_variables, singleton_class,
instance_variables, instance_variables_defined?, instance_variable_get,
instance_variable_set, methods, private_methods, public_methods,
protected_methods, singleton_methods, define_singleton_methods
* Module class
class_variables, class_variables_defined?, class_variable_get,
class_variable_set, remove_class_variable, included_modules,
instance_methods, remove_method, method_removed, constants
* Module class methods
constants, nesting
Note:
Following meta-programming methods are kept in the core:
* Module class
alias_method, undef_method, ancestors, const_defined?, const_get,
const_set, remove_const, method_defined?, define_method
* Toplevel object
define_method
`mruby-metaprog` gem is linked by default (specified in default.gembox).
When it is removed, it will save 40KB (stripped:8KB) on x86-64
environment last time I measured.
Diffstat (limited to 'test/t/module.rb')
| -rw-r--r-- | test/t/module.rb | 258 |
1 files changed, 32 insertions, 226 deletions
diff --git a/test/t/module.rb b/test/t/module.rb index fb82fc934..fda375721 100644 --- a/test/t/module.rb +++ b/test/t/module.rb @@ -3,7 +3,7 @@ def labeled_module(name, &block) Module.new do - singleton_class.class_eval do + (class <<self; self end).class_eval do define_method(:to_s) { name } alias_method :inspect, :to_s end @@ -13,7 +13,7 @@ end def labeled_class(name, supklass = Object, &block) Class.new(supklass) do - singleton_class.class_eval do + (class <<self; self end).class_eval do define_method(:to_s) { name } alias_method :inspect, :to_s end @@ -27,24 +27,9 @@ end # TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do -# TODO not implemented ATM assert('Module.nesting', '15.2.2.3.2') do - -assert('Module.nesting', '15.2.2.2.2') do - module Test4ModuleNesting - module Test4ModuleNesting2 - assert_equal [Test4ModuleNesting2, Test4ModuleNesting], - Module.nesting - end - end - module Test4ModuleNesting::Test4ModuleNesting2 - assert_equal [Test4ModuleNesting::Test4ModuleNesting2], Module.nesting - end -end - assert('Module#ancestors', '15.2.2.4.9') do class Test4ModuleAncestors end - sc = Test4ModuleAncestors.singleton_class r = String.ancestors assert_equal Array, r.class @@ -213,56 +198,9 @@ assert('Module#class_eval', '15.2.2.4.15') do def method1 end end - r = Test4ClassEval.instance_methods - assert_equal 11, Test4ClassEval.class_eval{ @a } assert_equal 12, Test4ClassEval.class_eval{ @b } - assert_equal Array, r.class - assert_true r.include?(:method1) -end - -assert('Module#class_variable_defined?', '15.2.2.4.16') do - class Test4ClassVariableDefined - @@cv = 99 - end - - assert_true Test4ClassVariableDefined.class_variable_defined?(:@@cv) - assert_false Test4ClassVariableDefined.class_variable_defined?(:@@noexisting) -end - -assert('Module#class_variable_get', '15.2.2.4.17') do - class Test4ClassVariableGet - @@cv = 99 - end - - assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv) -end - -assert('Module#class_variable_set', '15.2.2.4.18') do - class Test4ClassVariableSet - @@foo = 100 - def foo - @@foo - end - end - - assert_true Test4ClassVariableSet.class_variable_set(:@@cv, 99) - assert_true Test4ClassVariableSet.class_variable_set(:@@foo, 101) - assert_true Test4ClassVariableSet.class_variables.include? :@@cv - assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv) - assert_equal 101, Test4ClassVariableSet.new.foo -end - -assert('Module#class_variables', '15.2.2.4.19') do - class Test4ClassVariables1 - @@var1 = 1 - end - class Test4ClassVariables2 < Test4ClassVariables1 - @@var2 = 2 - end - - assert_equal [:@@var1], Test4ClassVariables1.class_variables - assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables + assert_equal true, Test4ClassEval.new.respond_to?(:method1) end assert('Module#const_defined?', '15.2.2.4.20') do @@ -288,16 +226,6 @@ assert('Module#const_get', '15.2.2.4.21') do assert_raise(NameError){ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") } end -assert('Module#const_missing', '15.2.2.4.22') do - module Test4ConstMissing - def self.const_missing(sym) - 42 # the answer to everything - end - end - - assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist) -end - assert('Module#const_set', '15.2.2.4.23') do module Test4ConstSet Const4Test4ConstSet = 42 @@ -307,19 +235,36 @@ assert('Module#const_set', '15.2.2.4.23') do assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) end -assert('Module#constants', '15.2.2.4.24') do - $n = [] - module TestA - C = 1 +assert('Module#remove_const', '15.2.2.4.40') do + module Test4RemoveConst + ExistingConst = 23 end - class TestB - include TestA - C2 = 1 - $n = constants.sort + + result = Test4RemoveConst.module_eval { remove_const :ExistingConst } + + name_error = false + begin + Test4RemoveConst.module_eval { remove_const :NonExistingConst } + rescue NameError + name_error = true + end + + # Constant removed from Module + assert_false Test4RemoveConst.const_defined? :ExistingConst + # Return value of binding + assert_equal 23, result + # Name Error raised when Constant doesn't exist + assert_true name_error +end + +assert('Module#const_missing', '15.2.2.4.22') do + module Test4ConstMissing + def self.const_missing(sym) + 42 # the answer to everything + end end - assert_equal [ :C ], TestA.constants - assert_equal [ :C, :C2 ], $n + assert_equal 42, Test4ConstMissing.const_get(:ConstDoesntExist) end assert('Module#include', '15.2.2.4.27') do @@ -366,48 +311,16 @@ assert('Module#included', '15.2.2.4.29') do assert_equal Test4Included2, Test4Included2.const_get(:Const4Included2) end -assert('Module#included_modules', '15.2.2.4.30') do - module Test4includedModules - end - module Test4includedModules2 - include Test4includedModules - end - r = Test4includedModules2.included_modules - - assert_equal Array, r.class - assert_true r.include?(Test4includedModules) -end - assert('Module#initialize', '15.2.2.4.31') do assert_kind_of Module, Module.new mod = Module.new { def hello; "hello"; end } - assert_equal [:hello], mod.instance_methods + cls = Class.new{include mod} + assert_true cls.new.respond_to?(:hello) a = nil mod = Module.new { |m| a = m } assert_equal mod, a end -assert('Module#instance_methods', '15.2.2.4.33') do - module Test4InstanceMethodsA - def method1() end - end - class Test4InstanceMethodsB - def method2() end - end - class Test4InstanceMethodsC < Test4InstanceMethodsB - def method3() end - end - - r = Test4InstanceMethodsC.instance_methods(true) - - assert_equal [:method1], Test4InstanceMethodsA.instance_methods - assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false) - assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false) - assert_equal Array, r.class - assert_true r.include?(:method3) - assert_true r.include?(:method2) -end - assert('Module#method_defined?', '15.2.2.4.34') do module Test4MethodDefined module A @@ -431,7 +344,6 @@ assert('Module#method_defined?', '15.2.2.4.34') do assert_false Test4MethodDefined::C.method_defined? "method4" end - assert('Module#module_eval', '15.2.2.4.35') do module Test4ModuleEval @a = 11 @@ -442,55 +354,6 @@ assert('Module#module_eval', '15.2.2.4.35') do assert_equal 12, Test4ModuleEval.module_eval{ @b } end -assert('Module#remove_class_variable', '15.2.2.4.39') do - class Test4RemoveClassVariable - @@cv = 99 - end - - assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv) - assert_false Test4RemoveClassVariable.class_variables.include? :@@cv -end - -assert('Module#remove_const', '15.2.2.4.40') do - module Test4RemoveConst - ExistingConst = 23 - end - - result = Test4RemoveConst.module_eval { remove_const :ExistingConst } - - name_error = false - begin - Test4RemoveConst.module_eval { remove_const :NonExistingConst } - rescue NameError - name_error = true - end - - # Constant removed from Module - assert_false Test4RemoveConst.const_defined? :ExistingConst - # Return value of binding - assert_equal 23, result - # Name Error raised when Constant doesn't exist - assert_true name_error -end - -assert('Module#remove_method', '15.2.2.4.41') do - module Test4RemoveMethod - class Parent - def hello - end - end - - class Child < Parent - def hello - end - end - end - - assert_true Test4RemoveMethod::Child.class_eval{ remove_method :hello } - assert_true Test4RemoveMethod::Child.instance_methods.include? :hello - assert_false Test4RemoveMethod::Child.instance_methods(false).include? :hello -end - assert('Module#undef_method', '15.2.2.4.42') do module Test4UndefMethod class Parent @@ -511,7 +374,6 @@ assert('Module#undef_method', '15.2.2.4.42') do assert_true Test4UndefMethod::Parent.new.respond_to?(:hello) assert_false Test4UndefMethod::Child.new.respond_to?(:hello) assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello) - assert_false Test4UndefMethod::Child.instance_methods(false).include? :hello end # Not ISO specified @@ -608,41 +470,6 @@ end assert_kind_of(b, c.new, bug8357) end - assert('Moduler#prepend + #instance_methods') do - bug6655 = '[ruby-core:45915]' - assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655) - end - - assert 'Module#prepend + #singleton_methods' do - o = Object.new - o.singleton_class.class_eval {prepend Module.new} - assert_equal([], o.singleton_methods) - end - - assert 'Module#prepend + #remove_method' do - c = Class.new do - prepend Module.new { def foo; end } - end - assert_raise(NameError) do - c.class_eval do - remove_method(:foo) - end - end - c.class_eval do - def foo; end - end - removed = nil - c.singleton_class.class_eval do - define_method(:method_removed) {|id| removed = id} - end - assert_nothing_raised('[Bug #7843]') do - c.class_eval do - remove_method(:foo) - end - end - assert_equal(:foo, removed) - end - assert 'Module#prepend + Class#ancestors' do bug6658 = '[ruby-core:45919]' m = labeled_module("m") @@ -683,12 +510,6 @@ end assert_equal([m3, m0, m1], m3.ancestors) end - assert 'Module#prepend #instance_methods(false)' do - bug6660 = '[ruby-dev:45863]' - assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660) - assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660) - end - assert 'cyclic Module#prepend' do bug7841 = '[ruby-core:52205] [Bug #7841]' m1 = Module.new @@ -743,21 +564,6 @@ end assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}") end - assert 'Module#prepend + #included_modules' do - bug8025 = '[ruby-core:53158] [Bug #8025]' - mixin = labeled_module("mixin") - c = labeled_module("c") {prepend mixin} - im = c.included_modules - assert_not_include(im, c, bug8025) - assert_include(im, mixin, bug8025) - c1 = labeled_class("c1") {prepend mixin} - c2 = labeled_class("c2", c1) - im = c2.included_modules - assert_not_include(im, c1, bug8025) - assert_not_include(im, c2, bug8025) - assert_include(im, mixin, bug8025) - end - assert 'Module#prepend super in alias' do skip "super does not currently work in aliased methods" bug7842 = '[Bug #7842]' |
