"Fossies" - the Fresh Open Source Software Archive

Member "ruby-2.7.4/test/ruby/test_method.rb" (7 Jul 2021, 44324 Bytes) of package /linux/misc/ruby-2.7.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_method.rb": 2.7.3_vs_2.7.4.

    1 # -*- coding: us-ascii -*-
    2 # frozen_string_literal: false
    3 require 'test/unit'
    4 
    5 class TestMethod < Test::Unit::TestCase
    6   def setup
    7     @verbose = $VERBOSE
    8     $VERBOSE = nil
    9   end
   10 
   11   def teardown
   12     $VERBOSE = @verbose
   13   end
   14 
   15   def m0() end
   16   def m1(a) end
   17   def m2(a, b) end
   18   def mo1(a = nil, &b) end
   19   def mo2(a, b = nil) end
   20   def mo3(*a) end
   21   def mo4(a, *b, &c) end
   22   def mo5(a, *b, c) end
   23   def mo6(a, *b, c, &d) end
   24   def mo7(a, b = nil, *c, d, &e) end
   25   def mo8(a, b = nil, *, d, &e) end
   26   def ma1((a), &b) nil && a end
   27   def mk1(**) end
   28   def mk2(**o) nil && o end
   29   def mk3(a, **o) nil && o end
   30   def mk4(a = nil, **o) nil && o end
   31   def mk5(a, b = nil, **o) nil && o end
   32   def mk6(a, b = nil, c, **o) nil && o end
   33   def mk7(a, b = nil, *c, d, **o) nil && o end
   34   def mk8(a, b = nil, *c, d, e:, f: nil, **o) nil && o end
   35   def mnk(**nil) end
   36   def mf(...) end
   37 
   38   class Base
   39     def foo() :base end
   40   end
   41   class Derived < Base
   42     def foo() :derived end
   43   end
   44   class T
   45     def initialize; end
   46     def initialize_copy(*) super end
   47     def initialize_clone(*) super end
   48     def initialize_dup(*) super end
   49     def respond_to_missing?(*) super end
   50     def normal_method; end
   51   end
   52   module M
   53     def func; end
   54     module_function :func
   55     def meth; :meth end
   56   end
   57 
   58   def mv1() end
   59   def mv2() end
   60   private :mv2
   61   def mv3() end
   62   protected :mv3
   63 
   64   class Visibility
   65     def mv1() end
   66     def mv2() end
   67     private :mv2
   68     def mv3() end
   69     protected :mv3
   70   end
   71 
   72   def test_arity
   73     assert_equal(0, method(:m0).arity)
   74     assert_equal(1, method(:m1).arity)
   75     assert_equal(2, method(:m2).arity)
   76     assert_equal(-1, method(:mo1).arity)
   77     assert_equal(-2, method(:mo2).arity)
   78     assert_equal(-1, method(:mo3).arity)
   79     assert_equal(-2, method(:mo4).arity)
   80     assert_equal(-3, method(:mo5).arity)
   81     assert_equal(-3, method(:mo6).arity)
   82     assert_equal(-1, method(:mk1).arity)
   83     assert_equal(-1, method(:mk2).arity)
   84     assert_equal(-2, method(:mk3).arity)
   85     assert_equal(-1, method(:mk4).arity)
   86     assert_equal(-2, method(:mk5).arity)
   87     assert_equal(-3, method(:mk6).arity)
   88     assert_equal(-3, method(:mk7).arity)
   89   end
   90 
   91   def test_arity_special
   92     assert_equal(-1, method(:__send__).arity)
   93   end
   94 
   95   def test_unbind
   96     assert_equal(:derived, Derived.new.foo)
   97     um = Derived.new.method(:foo).unbind
   98     assert_instance_of(UnboundMethod, um)
   99     Derived.class_eval do
  100       def foo() :changed end
  101     end
  102     assert_equal(:changed, Derived.new.foo)
  103     assert_equal(:derived, um.bind(Derived.new).call)
  104     assert_raise(TypeError) do
  105       um.bind(Base.new)
  106     end
  107   end
  108 
  109   def test_callee
  110     assert_equal(:test_callee, __method__)
  111     assert_equal(:m, Class.new {def m; __method__; end}.new.m)
  112     assert_equal(:m, Class.new {def m; tap{return __method__}; end}.new.m)
  113     assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
  114     assert_equal(:m, Class.new {define_method(:m) {tap{return __method__}}}.new.m)
  115     assert_nil(eval("class TestCallee; __method__; end"))
  116 
  117     assert_equal(:test_callee, __callee__)
  118     [
  119       ["method",              Class.new {def m; __callee__; end},],
  120       ["block",               Class.new {def m; tap{return __callee__}; end},],
  121       ["define_method",       Class.new {define_method(:m) {__callee__}}],
  122       ["define_method block", Class.new {define_method(:m) {tap{return __callee__}}}],
  123     ].each do |mesg, c|
  124       c.class_eval {alias m2 m}
  125       o = c.new
  126       assert_equal(:m, o.m, mesg)
  127       assert_equal(:m2, o.m2, mesg)
  128     end
  129     assert_nil(eval("class TestCallee; __callee__; end"))
  130   end
  131 
  132   def test_orphan_callee
  133     c = Class.new{def foo; proc{__callee__}; end; alias alias_foo foo}
  134     assert_equal(:alias_foo, c.new.alias_foo.call, '[Bug #11046]')
  135   end
  136 
  137   def test_method_in_define_method_block
  138     bug4606 = '[ruby-core:35386]'
  139     c = Class.new do
  140       [:m1, :m2].each do |m|
  141         define_method(m) do
  142           __method__
  143         end
  144       end
  145     end
  146     assert_equal(:m1, c.new.m1, bug4606)
  147     assert_equal(:m2, c.new.m2, bug4606)
  148   end
  149 
  150   def test_method_in_block_in_define_method_block
  151     bug4606 = '[ruby-core:35386]'
  152     c = Class.new do
  153       [:m1, :m2].each do |m|
  154         define_method(m) do
  155           tap { return __method__ }
  156         end
  157       end
  158     end
  159     assert_equal(:m1, c.new.m1, bug4606)
  160     assert_equal(:m2, c.new.m2, bug4606)
  161   end
  162 
  163   def test_body
  164     o = Object.new
  165     def o.foo; end
  166     assert_nothing_raised { RubyVM::InstructionSequence.disasm(o.method(:foo)) }
  167     assert_nothing_raised { RubyVM::InstructionSequence.disasm("x".method(:upcase)) }
  168     assert_nothing_raised { RubyVM::InstructionSequence.disasm(method(:to_s).to_proc) }
  169   end
  170 
  171   def test_new
  172     c1 = Class.new
  173     c1.class_eval { def foo; :foo; end }
  174     c2 = Class.new(c1)
  175     c2.class_eval { private :foo }
  176     o = c2.new
  177     o.extend(Module.new)
  178     assert_raise(NameError) { o.method(:bar) }
  179     assert_raise(NameError) { o.public_method(:foo) }
  180     assert_equal(:foo, o.method(:foo).call)
  181   end
  182 
  183   def test_eq
  184     o = Object.new
  185     class << o
  186       def foo; end
  187       alias bar foo
  188       def baz; end
  189     end
  190     assert_not_equal(o.method(:foo), nil)
  191     m = o.method(:foo)
  192     def m.foo; end
  193     assert_not_equal(o.method(:foo), m)
  194     assert_equal(o.method(:foo), o.method(:foo))
  195     assert_equal(o.method(:foo), o.method(:bar))
  196     assert_not_equal(o.method(:foo), o.method(:baz))
  197   end
  198 
  199   def test_hash
  200     o = Object.new
  201     def o.foo; end
  202     assert_kind_of(Integer, o.method(:foo).hash)
  203     assert_equal(Array.instance_method(:map).hash, Array.instance_method(:collect).hash)
  204     assert_kind_of(String, o.method(:foo).hash.to_s)
  205   end
  206 
  207   def test_owner
  208     c = Class.new do
  209       def foo; end
  210     end
  211     assert_equal(c, c.instance_method(:foo).owner)
  212     c2 = Class.new(c)
  213     assert_equal(c, c2.instance_method(:foo).owner)
  214   end
  215 
  216   def test_owner_missing
  217     c = Class.new do
  218       def respond_to_missing?(name, bool)
  219         name == :foo
  220       end
  221     end
  222     c2 = Class.new(c)
  223     assert_equal(c, c.new.method(:foo).owner)
  224     assert_equal(c2, c2.new.method(:foo).owner)
  225   end
  226 
  227   def test_receiver_name_owner
  228     o = Object.new
  229     def o.foo; end
  230     m = o.method(:foo)
  231     assert_equal(o, m.receiver)
  232     assert_equal(:foo, m.name)
  233     assert_equal(class << o; self; end, m.owner)
  234     assert_equal(:foo, m.unbind.name)
  235     assert_equal(class << o; self; end, m.unbind.owner)
  236     class << o
  237       alias bar foo
  238     end
  239     m = o.method(:bar)
  240     assert_equal(:bar, m.name)
  241     assert_equal(:foo, m.original_name)
  242   end
  243 
  244   def test_instance_method
  245     c = Class.new
  246     c.class_eval do
  247       def foo; :foo; end
  248       private :foo
  249     end
  250     o = c.new
  251     o.method(:foo).unbind
  252     assert_raise(NoMethodError) { o.foo }
  253     c.instance_method(:foo).bind(o)
  254     assert_equal(:foo, o.instance_eval { foo })
  255     assert_raise(NameError) { c.public_instance_method(:foo) }
  256     def o.bar; end
  257     m = o.method(:bar).unbind
  258     assert_raise(TypeError) { m.bind(Object.new) }
  259 
  260     cx = EnvUtil.labeled_class("X\u{1f431}")
  261     assert_raise_with_message(TypeError, /X\u{1f431}/) do
  262       o.method(cx)
  263     end
  264   end
  265 
  266   def test_bind_module_instance_method
  267     feature4254 = '[ruby-core:34267]'
  268     m = M.instance_method(:meth)
  269     assert_equal(:meth, m.bind(Object.new).call, feature4254)
  270   end
  271 
  272   def test_define_method
  273     c = Class.new
  274     c.class_eval { def foo; :foo; end }
  275     o = c.new
  276     def o.bar; :bar; end
  277     assert_raise(TypeError) do
  278       c.class_eval { define_method(:foo, :foo) }
  279     end
  280     assert_raise(ArgumentError) do
  281       c.class_eval { define_method }
  282     end
  283     c2 = Class.new(c)
  284     c2.class_eval { define_method(:baz, o.method(:foo)) }
  285     assert_equal(:foo, c2.new.baz)
  286     assert_raise(TypeError) do
  287       Class.new.class_eval { define_method(:foo, o.method(:foo)) }
  288     end
  289     assert_raise(TypeError) do
  290       Class.new.class_eval { define_method(:bar, o.method(:bar)) }
  291     end
  292     cx = EnvUtil.labeled_class("X\u{1f431}")
  293     assert_raise_with_message(TypeError, /X\u{1F431}/) do
  294       Class.new {define_method(cx) {}}
  295     end
  296   end
  297 
  298   def test_define_method_no_proc
  299     o = Object.new
  300     def o.foo(c)
  301       c.class_eval { define_method(:foo) }
  302     end
  303     c = Class.new
  304     assert_raise(ArgumentError) {o.foo(c)}
  305 
  306     bug11283 = '[ruby-core:69655] [Bug #11283]'
  307     assert_raise(ArgumentError, bug11283) {o.foo(c) {:foo}}
  308   end
  309 
  310   def test_define_singleton_method
  311     o = Object.new
  312     o.instance_eval { define_singleton_method(:foo) { :foo } }
  313     assert_equal(:foo, o.foo)
  314   end
  315 
  316   def test_define_singleton_method_no_proc
  317     o = Object.new
  318     assert_raise(ArgumentError) {
  319       o.instance_eval { define_singleton_method(:bar) }
  320     }
  321 
  322     bug11283 = '[ruby-core:69655] [Bug #11283]'
  323     def o.define(n)
  324       define_singleton_method(n)
  325     end
  326     assert_raise(ArgumentError, bug11283) {o.define(:bar) {:bar}}
  327   end
  328 
  329   def test_define_method_invalid_arg
  330     assert_raise(TypeError) do
  331       Class.new.class_eval { define_method(:foo, Object.new) }
  332     end
  333 
  334     assert_raise(TypeError) do
  335       Module.new.module_eval {define_method(:foo, Base.instance_method(:foo))}
  336     end
  337   end
  338 
  339   def test_define_singleton_method_with_extended_method
  340     bug8686 = "[ruby-core:56174]"
  341 
  342     m = Module.new do
  343       extend self
  344 
  345       def a
  346         "a"
  347       end
  348     end
  349 
  350     assert_nothing_raised(bug8686) do
  351       m.define_singleton_method(:a, m.method(:a))
  352     end
  353   end
  354 
  355   def test_define_method_transplating
  356     feature4254 = '[ruby-core:34267]'
  357     m = Module.new {define_method(:meth, M.instance_method(:meth))}
  358     assert_equal(:meth, Object.new.extend(m).meth, feature4254)
  359     c = Class.new {define_method(:meth, M.instance_method(:meth))}
  360     assert_equal(:meth, c.new.meth, feature4254)
  361   end
  362 
  363   def test_define_method_visibility
  364     c = Class.new do
  365       public
  366       define_method(:foo) {:foo}
  367       protected
  368       define_method(:bar) {:bar}
  369       private
  370       define_method(:baz) {:baz}
  371     end
  372 
  373     assert_equal(true, c.public_method_defined?(:foo))
  374     assert_equal(false, c.public_method_defined?(:bar))
  375     assert_equal(false, c.public_method_defined?(:baz))
  376 
  377     assert_equal(false, c.protected_method_defined?(:foo))
  378     assert_equal(true, c.protected_method_defined?(:bar))
  379     assert_equal(false, c.protected_method_defined?(:baz))
  380 
  381     assert_equal(false, c.private_method_defined?(:foo))
  382     assert_equal(false, c.private_method_defined?(:bar))
  383     assert_equal(true, c.private_method_defined?(:baz))
  384 
  385     m = Module.new do
  386       module_function
  387       define_method(:foo) {:foo}
  388     end
  389     assert_equal(true, m.respond_to?(:foo))
  390     assert_equal(false, m.public_method_defined?(:foo))
  391     assert_equal(false, m.protected_method_defined?(:foo))
  392     assert_equal(true, m.private_method_defined?(:foo))
  393   end
  394 
  395   def test_define_method_in_private_scope
  396     bug9005 = '[ruby-core:57747] [Bug #9005]'
  397     c = Class.new
  398     class << c
  399       public :define_method
  400     end
  401     TOPLEVEL_BINDING.eval("proc{|c|c.define_method(:x) {|x|throw x}}").call(c)
  402     o = c.new
  403     assert_throw(bug9005) {o.x(bug9005)}
  404   end
  405 
  406   def test_singleton_define_method_in_private_scope
  407     bug9141 = '[ruby-core:58497] [Bug #9141]'
  408     o = Object.new
  409     class << o
  410       public :define_singleton_method
  411     end
  412     TOPLEVEL_BINDING.eval("proc{|o|o.define_singleton_method(:x) {|x|throw x}}").call(o)
  413     assert_throw(bug9141) do
  414       o.x(bug9141)
  415     end
  416   end
  417 
  418   def test_super_in_proc_from_define_method
  419     c1 = Class.new {
  420       def m
  421         :m1
  422       end
  423     }
  424     c2 = Class.new(c1) { define_method(:m) { Proc.new { super() } } }
  425     assert_equal(:m1, c2.new.m.call, 'see [Bug #4881] and [Bug #3136]')
  426   end
  427 
  428   def test_clone
  429     o = Object.new
  430     def o.foo; :foo; end
  431     m = o.method(:foo)
  432     def m.bar; :bar; end
  433     assert_equal(:foo, m.clone.call)
  434     assert_equal(:bar, m.clone.bar)
  435   end
  436 
  437   def test_inspect
  438     o = Object.new
  439     def o.foo; end; line_no = __LINE__
  440     m = o.method(:foo)
  441     assert_equal("#<Method: #{ o.inspect }.foo() #{__FILE__}:#{line_no}>", m.inspect)
  442     m = o.method(:foo)
  443     assert_match("#<UnboundMethod: #{ class << o; self; end.inspect }#foo() #{__FILE__}:#{line_no}", m.unbind.inspect)
  444 
  445     c = Class.new
  446     c.class_eval { def foo; end; }; line_no = __LINE__
  447     m = c.new.method(:foo)
  448     assert_equal("#<Method: #{ c.inspect }#foo() #{__FILE__}:#{line_no}>", m.inspect)
  449     m = c.instance_method(:foo)
  450     assert_equal("#<UnboundMethod: #{ c.inspect }#foo() #{__FILE__}:#{line_no}>", m.inspect)
  451 
  452     c2 = Class.new(c)
  453     c2.class_eval { private :foo }
  454     m2 = c2.new.method(:foo)
  455     assert_equal("#<Method: #{ c2.inspect }(#{ c.inspect })#foo() #{__FILE__}:#{line_no}>", m2.inspect)
  456 
  457     bug7806 = '[ruby-core:52048] [Bug #7806]'
  458     c3 = Class.new(c)
  459     c3.class_eval { alias bar foo }
  460     m3 = c3.new.method(:bar)
  461     assert_equal("#<Method: #{c3.inspect}(#{c.inspect})#bar(foo)() #{__FILE__}:#{line_no}>", m3.inspect, bug7806)
  462   end
  463 
  464   def test_callee_top_level
  465     assert_in_out_err([], "p __callee__", %w(nil), [])
  466   end
  467 
  468   def test_caller_top_level
  469     assert_in_out_err([], "p caller", %w([]), [])
  470   end
  471 
  472   def test_caller_negative_level
  473     assert_raise(ArgumentError) { caller(-1) }
  474   end
  475 
  476   def test_attrset_ivar
  477     c = Class.new
  478     c.class_eval { attr_accessor :foo }
  479     o = c.new
  480     o.method(:foo=).call(42)
  481     assert_equal(42, o.foo)
  482     assert_raise(ArgumentError) { o.method(:foo=).call(1, 2, 3) }
  483     assert_raise(ArgumentError) { o.method(:foo).call(1) }
  484   end
  485 
  486   def test_default_accessibility
  487     tmethods = T.public_instance_methods
  488     assert_include tmethods, :normal_method, 'normal methods are public by default'
  489     assert_not_include tmethods, :initialize, '#initialize is private'
  490     assert_not_include tmethods, :initialize_copy, '#initialize_copy is private'
  491     assert_not_include tmethods, :initialize_clone, '#initialize_clone is private'
  492     assert_not_include tmethods, :initialize_dup, '#initialize_dup is private'
  493     assert_not_include tmethods, :respond_to_missing?, '#respond_to_missing? is private'
  494     mmethods = M.public_instance_methods
  495     assert_not_include mmethods, :func, 'module methods are private by default'
  496     assert_include mmethods, :meth, 'normal methods are public by default'
  497   end
  498 
  499   def test_respond_to_missing_argument
  500     obj = Struct.new(:mid).new
  501     def obj.respond_to_missing?(id, *)
  502       self.mid = id
  503       true
  504     end
  505     assert_kind_of(Method, obj.method("bug15640"))
  506     assert_kind_of(Symbol, obj.mid)
  507     assert_equal("bug15640", obj.mid.to_s)
  508 
  509     arg = Struct.new(:to_str).new("bug15640_2")
  510     assert_kind_of(Method, obj.method(arg))
  511     assert_kind_of(Symbol, obj.mid)
  512     assert_equal("bug15640_2", obj.mid.to_s)
  513   end
  514 
  515   define_method(:pm0) {||}
  516   define_method(:pm1) {|a|}
  517   define_method(:pm2) {|a, b|}
  518   define_method(:pmo1) {|a = nil, &b|}
  519   define_method(:pmo2) {|a, b = nil|}
  520   define_method(:pmo3) {|*a|}
  521   define_method(:pmo4) {|a, *b, &c|}
  522   define_method(:pmo5) {|a, *b, c|}
  523   define_method(:pmo6) {|a, *b, c, &d|}
  524   define_method(:pmo7) {|a, b = nil, *c, d, &e|}
  525   define_method(:pma1) {|(a), &b| nil && a}
  526   define_method(:pmk1) {|**|}
  527   define_method(:pmk2) {|**o|}
  528   define_method(:pmk3) {|a, **o|}
  529   define_method(:pmk4) {|a = nil, **o|}
  530   define_method(:pmk5) {|a, b = nil, **o|}
  531   define_method(:pmk6) {|a, b = nil, c, **o|}
  532   define_method(:pmk7) {|a, b = nil, *c, d, **o|}
  533   define_method(:pmk8) {|a, b = nil, *c, d, e:, f: nil, **o|}
  534   define_method(:pmnk) {|**nil|}
  535 
  536   def test_bound_parameters
  537     assert_equal([], method(:m0).parameters)
  538     assert_equal([[:req, :a]], method(:m1).parameters)
  539     assert_equal([[:req, :a], [:req, :b]], method(:m2).parameters)
  540     assert_equal([[:opt, :a], [:block, :b]], method(:mo1).parameters)
  541     assert_equal([[:req, :a], [:opt, :b]], method(:mo2).parameters)
  542     assert_equal([[:rest, :a]], method(:mo3).parameters)
  543     assert_equal([[:req, :a], [:rest, :b], [:block, :c]], method(:mo4).parameters)
  544     assert_equal([[:req, :a], [:rest, :b], [:req, :c]], method(:mo5).parameters)
  545     assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:mo6).parameters)
  546     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:mo7).parameters)
  547     assert_equal([[:req, :a], [:opt, :b], [:rest], [:req, :d], [:block, :e]], method(:mo8).parameters)
  548     assert_equal([[:req], [:block, :b]], method(:ma1).parameters)
  549     assert_equal([[:keyrest]], method(:mk1).parameters)
  550     assert_equal([[:keyrest, :o]], method(:mk2).parameters)
  551     assert_equal([[:req, :a], [:keyrest, :o]], method(:mk3).parameters)
  552     assert_equal([[:opt, :a], [:keyrest, :o]], method(:mk4).parameters)
  553     assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], method(:mk5).parameters)
  554     assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], method(:mk6).parameters)
  555     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], method(:mk7).parameters)
  556     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], method(:mk8).parameters)
  557     assert_equal([[:nokey]], method(:mnk).parameters)
  558     # pending
  559     assert_equal([[:rest, :*], [:block, :&]], method(:mf).parameters)
  560   end
  561 
  562   def test_unbound_parameters
  563     assert_equal([], self.class.instance_method(:m0).parameters)
  564     assert_equal([[:req, :a]], self.class.instance_method(:m1).parameters)
  565     assert_equal([[:req, :a], [:req, :b]], self.class.instance_method(:m2).parameters)
  566     assert_equal([[:opt, :a], [:block, :b]], self.class.instance_method(:mo1).parameters)
  567     assert_equal([[:req, :a], [:opt, :b]], self.class.instance_method(:mo2).parameters)
  568     assert_equal([[:rest, :a]], self.class.instance_method(:mo3).parameters)
  569     assert_equal([[:req, :a], [:rest, :b], [:block, :c]], self.class.instance_method(:mo4).parameters)
  570     assert_equal([[:req, :a], [:rest, :b], [:req, :c]], self.class.instance_method(:mo5).parameters)
  571     assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:mo6).parameters)
  572     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:mo7).parameters)
  573     assert_equal([[:req, :a], [:opt, :b], [:rest], [:req, :d], [:block, :e]], self.class.instance_method(:mo8).parameters)
  574     assert_equal([[:req], [:block, :b]], self.class.instance_method(:ma1).parameters)
  575     assert_equal([[:keyrest]], self.class.instance_method(:mk1).parameters)
  576     assert_equal([[:keyrest, :o]], self.class.instance_method(:mk2).parameters)
  577     assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:mk3).parameters)
  578     assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:mk4).parameters)
  579     assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], self.class.instance_method(:mk5).parameters)
  580     assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], self.class.instance_method(:mk6).parameters)
  581     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:mk7).parameters)
  582     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], self.class.instance_method(:mk8).parameters)
  583     assert_equal([[:nokey]], self.class.instance_method(:mnk).parameters)
  584     # pending
  585     assert_equal([[:rest, :*], [:block, :&]], self.class.instance_method(:mf).parameters)
  586   end
  587 
  588   def test_bmethod_bound_parameters
  589     assert_equal([], method(:pm0).parameters)
  590     assert_equal([[:req, :a]], method(:pm1).parameters)
  591     assert_equal([[:req, :a], [:req, :b]], method(:pm2).parameters)
  592     assert_equal([[:opt, :a], [:block, :b]], method(:pmo1).parameters)
  593     assert_equal([[:req, :a], [:opt, :b]], method(:pmo2).parameters)
  594     assert_equal([[:rest, :a]], method(:pmo3).parameters)
  595     assert_equal([[:req, :a], [:rest, :b], [:block, :c]], method(:pmo4).parameters)
  596     assert_equal([[:req, :a], [:rest, :b], [:req, :c]], method(:pmo5).parameters)
  597     assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:pmo6).parameters)
  598     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:pmo7).parameters)
  599     assert_equal([[:req], [:block, :b]], method(:pma1).parameters)
  600     assert_equal([[:keyrest]], method(:pmk1).parameters)
  601     assert_equal([[:keyrest, :o]], method(:pmk2).parameters)
  602     assert_equal([[:req, :a], [:keyrest, :o]], method(:pmk3).parameters)
  603     assert_equal([[:opt, :a], [:keyrest, :o]], method(:pmk4).parameters)
  604     assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], method(:pmk5).parameters)
  605     assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], method(:pmk6).parameters)
  606     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], method(:pmk7).parameters)
  607     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], method(:pmk8).parameters)
  608     assert_equal([[:nokey]], method(:pmnk).parameters)
  609   end
  610 
  611   def test_bmethod_unbound_parameters
  612     assert_equal([], self.class.instance_method(:pm0).parameters)
  613     assert_equal([[:req, :a]], self.class.instance_method(:pm1).parameters)
  614     assert_equal([[:req, :a], [:req, :b]], self.class.instance_method(:pm2).parameters)
  615     assert_equal([[:opt, :a], [:block, :b]], self.class.instance_method(:pmo1).parameters)
  616     assert_equal([[:req, :a], [:opt, :b]], self.class.instance_method(:pmo2).parameters)
  617     assert_equal([[:rest, :a]], self.class.instance_method(:pmo3).parameters)
  618     assert_equal([[:req, :a], [:rest, :b], [:block, :c]], self.class.instance_method(:pmo4).parameters)
  619     assert_equal([[:req, :a], [:rest, :b], [:req, :c]], self.class.instance_method(:pmo5).parameters)
  620     assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:pmo6).parameters)
  621     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:pmo7).parameters)
  622     assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters)
  623     assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters)
  624     assert_equal([[:keyrest]], self.class.instance_method(:pmk1).parameters)
  625     assert_equal([[:keyrest, :o]], self.class.instance_method(:pmk2).parameters)
  626     assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:pmk3).parameters)
  627     assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:pmk4).parameters)
  628     assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], self.class.instance_method(:pmk5).parameters)
  629     assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], self.class.instance_method(:pmk6).parameters)
  630     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:pmk7).parameters)
  631     assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], self.class.instance_method(:pmk8).parameters)
  632     assert_equal([[:nokey]], self.class.instance_method(:pmnk).parameters)
  633   end
  634 
  635   def test_hidden_parameters
  636     instance_eval("def m((_)"+",(_)"*256+");end")
  637     assert_empty(method(:m).parameters.map{|_,n|n}.compact)
  638   end
  639 
  640   def test_method_parameters_inspect
  641     assert_include(method(:m0).inspect, "()")
  642     assert_include(method(:m1).inspect, "(a)")
  643     assert_include(method(:m2).inspect, "(a, b)")
  644     assert_include(method(:mo1).inspect, "(a=..., &b)")
  645     assert_include(method(:mo2).inspect, "(a, b=...)")
  646     assert_include(method(:mo3).inspect, "(*a)")
  647     assert_include(method(:mo4).inspect, "(a, *b, &c)")
  648     assert_include(method(:mo5).inspect, "(a, *b, c)")
  649     assert_include(method(:mo6).inspect, "(a, *b, c, &d)")
  650     assert_include(method(:mo7).inspect, "(a, b=..., *c, d, &e)")
  651     assert_include(method(:mo8).inspect, "(a, b=..., *, d, &e)")
  652     assert_include(method(:ma1).inspect, "(_, &b)")
  653     assert_include(method(:mk1).inspect, "(**)")
  654     assert_include(method(:mk2).inspect, "(**o)")
  655     assert_include(method(:mk3).inspect, "(a, **o)")
  656     assert_include(method(:mk4).inspect, "(a=..., **o)")
  657     assert_include(method(:mk5).inspect, "(a, b=..., **o)")
  658     assert_include(method(:mk6).inspect, "(a, b=..., c, **o)")
  659     assert_include(method(:mk7).inspect, "(a, b=..., *c, d, **o)")
  660     assert_include(method(:mk8).inspect, "(a, b=..., *c, d, e:, f: ..., **o)")
  661     assert_include(method(:mnk).inspect, "(**nil)")
  662     assert_include(method(:mf).inspect, "(...)")
  663   end
  664 
  665   def test_unbound_method_parameters_inspect
  666     assert_include(self.class.instance_method(:m0).inspect, "()")
  667     assert_include(self.class.instance_method(:m1).inspect, "(a)")
  668     assert_include(self.class.instance_method(:m2).inspect, "(a, b)")
  669     assert_include(self.class.instance_method(:mo1).inspect, "(a=..., &b)")
  670     assert_include(self.class.instance_method(:mo2).inspect, "(a, b=...)")
  671     assert_include(self.class.instance_method(:mo3).inspect, "(*a)")
  672     assert_include(self.class.instance_method(:mo4).inspect, "(a, *b, &c)")
  673     assert_include(self.class.instance_method(:mo5).inspect, "(a, *b, c)")
  674     assert_include(self.class.instance_method(:mo6).inspect, "(a, *b, c, &d)")
  675     assert_include(self.class.instance_method(:mo7).inspect, "(a, b=..., *c, d, &e)")
  676     assert_include(self.class.instance_method(:mo8).inspect, "(a, b=..., *, d, &e)")
  677     assert_include(self.class.instance_method(:ma1).inspect, "(_, &b)")
  678     assert_include(self.class.instance_method(:mk1).inspect, "(**)")
  679     assert_include(self.class.instance_method(:mk2).inspect, "(**o)")
  680     assert_include(self.class.instance_method(:mk3).inspect, "(a, **o)")
  681     assert_include(self.class.instance_method(:mk4).inspect, "(a=..., **o)")
  682     assert_include(self.class.instance_method(:mk5).inspect, "(a, b=..., **o)")
  683     assert_include(self.class.instance_method(:mk6).inspect, "(a, b=..., c, **o)")
  684     assert_include(self.class.instance_method(:mk7).inspect, "(a, b=..., *c, d, **o)")
  685     assert_include(self.class.instance_method(:mk8).inspect, "(a, b=..., *c, d, e:, f: ..., **o)")
  686     assert_include(self.class.instance_method(:mnk).inspect, "(**nil)")
  687     assert_include(self.class.instance_method(:mf).inspect, "(...)")
  688   end
  689 
  690   def test_public_method_with_zsuper_method
  691     c = Class.new
  692     c.class_eval do
  693       def foo
  694         :ok
  695       end
  696       private :foo
  697     end
  698     d = Class.new(c)
  699     d.class_eval do
  700       public :foo
  701     end
  702     assert_equal(:ok, d.new.public_method(:foo).call)
  703   end
  704 
  705   def test_public_methods_with_extended
  706     m = Module.new do def m1; end end
  707     a = Class.new do def a; end end
  708     bug = '[ruby-dev:41553]'
  709     obj = a.new
  710     assert_equal([:a], obj.public_methods(false), bug)
  711     obj.extend(m)
  712     assert_equal([:m1, :a], obj.public_methods(false), bug)
  713   end
  714 
  715   def test_visibility
  716     assert_equal('method', defined?(mv1))
  717     assert_equal('method', defined?(mv2))
  718     assert_equal('method', defined?(mv3))
  719 
  720     assert_equal('method', defined?(self.mv1))
  721     assert_equal(nil,      defined?(self.mv2))
  722     assert_equal('method', defined?(self.mv3))
  723 
  724     assert_equal(true,  respond_to?(:mv1))
  725     assert_equal(false, respond_to?(:mv2))
  726     assert_equal(false, respond_to?(:mv3))
  727 
  728     assert_equal(true,  respond_to?(:mv1, true))
  729     assert_equal(true,  respond_to?(:mv2, true))
  730     assert_equal(true,  respond_to?(:mv3, true))
  731 
  732     assert_nothing_raised { mv1 }
  733     assert_nothing_raised { mv2 }
  734     assert_nothing_raised { mv3 }
  735 
  736     assert_nothing_raised { self.mv1 }
  737     assert_nothing_raised { self.mv2 }
  738     assert_raise(NoMethodError) { (self).mv2 }
  739     assert_nothing_raised { self.mv3 }
  740 
  741     v = Visibility.new
  742 
  743     assert_equal('method', defined?(v.mv1))
  744     assert_equal(nil,      defined?(v.mv2))
  745     assert_equal(nil,      defined?(v.mv3))
  746 
  747     assert_equal(true,  v.respond_to?(:mv1))
  748     assert_equal(false, v.respond_to?(:mv2))
  749     assert_equal(false, v.respond_to?(:mv3))
  750 
  751     assert_equal(true,  v.respond_to?(:mv1, true))
  752     assert_equal(true,  v.respond_to?(:mv2, true))
  753     assert_equal(true,  v.respond_to?(:mv3, true))
  754 
  755     assert_nothing_raised { v.mv1 }
  756     assert_raise(NoMethodError) { v.mv2 }
  757     assert_raise(NoMethodError) { v.mv3 }
  758 
  759     assert_nothing_raised { v.__send__(:mv1) }
  760     assert_nothing_raised { v.__send__(:mv2) }
  761     assert_nothing_raised { v.__send__(:mv3) }
  762 
  763     assert_nothing_raised { v.instance_eval { mv1 } }
  764     assert_nothing_raised { v.instance_eval { mv2 } }
  765     assert_nothing_raised { v.instance_eval { mv3 } }
  766   end
  767 
  768   def test_bound_method_entry
  769     bug6171 = '[ruby-core:43383]'
  770     assert_ruby_status([], <<-EOC, bug6171)
  771       class Bug6171
  772         def initialize(target)
  773           define_singleton_method(:reverse, target.method(:reverse).to_proc)
  774         end
  775       end
  776       100.times {p = Bug6171.new('test'); 1000.times {p.reverse}}
  777       EOC
  778   end
  779 
  780   def test_unbound_method_proc_coerce
  781     # '&' coercion of an UnboundMethod raises TypeError
  782     assert_raise(TypeError) do
  783       Class.new do
  784         define_method('foo', &Object.instance_method(:to_s))
  785       end
  786     end
  787   end
  788 
  789   def test___dir__
  790     assert_instance_of String, __dir__
  791     assert_equal(File.dirname(File.realpath(__FILE__)), __dir__)
  792     bug8436 = '[ruby-core:55123] [Bug #8436]'
  793     assert_equal(__dir__, eval("__dir__", binding), bug8436)
  794     bug8662 = '[ruby-core:56099] [Bug #8662]'
  795     assert_equal("arbitrary", eval("__dir__", binding, "arbitrary/file.rb"), bug8662)
  796     assert_equal("arbitrary", Object.new.instance_eval("__dir__", "arbitrary/file.rb"), bug8662)
  797   end
  798 
  799   def test_alias_owner
  800     bug7613 = '[ruby-core:51105]'
  801     bug7993 = '[Bug #7993]'
  802     c = Class.new {
  803       def foo
  804       end
  805       prepend Module.new
  806       attr_reader :zot
  807     }
  808     x = c.new
  809     class << x
  810       alias bar foo
  811     end
  812     assert_equal(c, c.instance_method(:foo).owner)
  813     assert_equal(c, x.method(:foo).owner)
  814     assert_equal(x.singleton_class, x.method(:bar).owner)
  815     assert_not_equal(x.method(:foo), x.method(:bar), bug7613)
  816     assert_equal(c, x.method(:zot).owner, bug7993)
  817     assert_equal(c, c.instance_method(:zot).owner, bug7993)
  818   end
  819 
  820   def test_included
  821     m = Module.new {
  822       def foo
  823       end
  824     }
  825     c = Class.new {
  826       def foo
  827       end
  828       include m
  829     }
  830     assert_equal(c, c.instance_method(:foo).owner)
  831   end
  832 
  833   def test_prepended
  834     bug7836 = '[ruby-core:52160] [Bug #7836]'
  835     bug7988 = '[ruby-core:53038] [Bug #7988]'
  836     m = Module.new {
  837       def foo
  838       end
  839     }
  840     c = Class.new {
  841       def foo
  842       end
  843       prepend m
  844     }
  845     assert_raise(NameError, bug7988) {Module.new{prepend m}.instance_method(:bar)}
  846     true || c || bug7836
  847   end
  848 
  849   def test_gced_bmethod
  850     assert_normal_exit %q{
  851       require 'irb'
  852       IRB::Irb.module_eval do
  853         define_method(:eval_input) do
  854           IRB::Irb.module_eval { alias_method :eval_input, :to_s }
  855           GC.start
  856           Kernel
  857         end
  858       end
  859       IRB.start
  860     }, '[Bug #7825]'
  861   end
  862 
  863   def test_singleton_method
  864     feature8391 = '[ruby-core:54914] [Feature #8391]'
  865     c1 = Class.new
  866     c1.class_eval { def foo; :foo; end }
  867     o = c1.new
  868     def o.bar; :bar; end
  869     assert_nothing_raised(NameError) {o.method(:foo)}
  870     assert_raise(NameError, feature8391) {o.singleton_method(:foo)}
  871     m = assert_nothing_raised(NameError, feature8391) {break o.singleton_method(:bar)}
  872     assert_equal(:bar, m.call, feature8391)
  873   end
  874 
  875   def test_singleton_method_prepend
  876     bug14658 = '[Bug #14658]'
  877     c1 = Class.new
  878     o = c1.new
  879     def o.bar; :bar; end
  880     class << o; prepend Module.new; end
  881     m = assert_nothing_raised(NameError, bug14658) {o.singleton_method(:bar)}
  882     assert_equal(:bar, m.call, bug14658)
  883 
  884     o = Object.new
  885     assert_raise(NameError, bug14658) {o.singleton_method(:bar)}
  886   end
  887 
  888   Feature9783 = '[ruby-core:62212] [Feature #9783]'
  889 
  890   def assert_curry_three_args(m)
  891     curried = m.curry
  892     assert_equal(6, curried.(1).(2).(3), Feature9783)
  893 
  894     curried = m.curry(3)
  895     assert_equal(6, curried.(1).(2).(3), Feature9783)
  896 
  897     assert_raise_with_message(ArgumentError, /wrong number/) {m.curry(2)}
  898   end
  899 
  900   def test_curry_method
  901     c = Class.new {
  902       def three_args(a,b,c) a + b + c end
  903     }
  904     assert_curry_three_args(c.new.method(:three_args))
  905   end
  906 
  907   def test_curry_from_proc
  908     c = Class.new {
  909       define_method(:three_args) {|x,y,z| x + y + z}
  910     }
  911     assert_curry_three_args(c.new.method(:three_args))
  912   end
  913 
  914   def assert_curry_var_args(m)
  915     curried = m.curry(3)
  916     assert_equal([1, 2, 3], curried.(1).(2).(3), Feature9783)
  917 
  918     curried = m.curry(2)
  919     assert_equal([1, 2], curried.(1).(2), Feature9783)
  920 
  921     curried = m.curry(0)
  922     assert_equal([1], curried.(1), Feature9783)
  923   end
  924 
  925   def test_curry_var_args
  926     c = Class.new {
  927       def var_args(*args) args end
  928     }
  929     assert_curry_var_args(c.new.method(:var_args))
  930   end
  931 
  932   def test_curry_from_proc_var_args
  933     c = Class.new {
  934       define_method(:var_args) {|*args| args}
  935     }
  936     assert_curry_var_args(c.new.method(:var_args))
  937   end
  938 
  939   Feature9781 = '[ruby-core:62202] [Feature #9781]'
  940 
  941   def test_super_method
  942     o = Derived.new
  943     m = o.method(:foo).super_method
  944     assert_equal(Base, m.owner, Feature9781)
  945     assert_same(o, m.receiver, Feature9781)
  946     assert_equal(:foo, m.name, Feature9781)
  947     m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
  948     assert_nil(m, Feature9781)
  949   end
  950 
  951   def test_super_method_unbound
  952     m = Derived.instance_method(:foo)
  953     m = m.super_method
  954     assert_equal(Base.instance_method(:foo), m, Feature9781)
  955     m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
  956     assert_nil(m, Feature9781)
  957 
  958     bug11419 = '[ruby-core:70254]'
  959     m = Object.instance_method(:tap)
  960     m = assert_nothing_raised(NameError, bug11419) {break m.super_method}
  961     assert_nil(m, bug11419)
  962   end
  963 
  964   def test_super_method_module
  965     m1 = Module.new {def foo; end}
  966     c1 = Class.new(Derived) {include m1; def foo; end}
  967     m = c1.instance_method(:foo)
  968     assert_equal(c1, m.owner, Feature9781)
  969     m = m.super_method
  970     assert_equal(m1, m.owner, Feature9781)
  971     m = m.super_method
  972     assert_equal(Derived, m.owner, Feature9781)
  973     m = m.super_method
  974     assert_equal(Base, m.owner, Feature9781)
  975     m2 = Module.new {def foo; end}
  976     o = c1.new.extend(m2)
  977     m = o.method(:foo)
  978     assert_equal(m2, m.owner, Feature9781)
  979     m = m.super_method
  980     assert_equal(c1, m.owner, Feature9781)
  981     assert_same(o, m.receiver, Feature9781)
  982 
  983     c1 = Class.new {def foo; end}
  984     c2 = Class.new(c1) {include m1; include m2}
  985     m = c2.instance_method(:foo)
  986     assert_equal(m2, m.owner)
  987     m = m.super_method
  988     assert_equal(m1, m.owner)
  989     m = m.super_method
  990     assert_equal(c1, m.owner)
  991     assert_nil(m.super_method)
  992   end
  993 
  994   def test_super_method_bind_unbind_clone
  995     bug15629_m1 = Module.new do
  996       def foo; end
  997     end
  998 
  999     bug15629_m2 = Module.new do
 1000       def foo; end
 1001     end
 1002 
 1003     bug15629_c = Class.new do
 1004       include bug15629_m1
 1005       include bug15629_m2
 1006     end
 1007 
 1008     o  = bug15629_c.new
 1009     m = o.method(:foo)
 1010     sm = m.super_method
 1011     im = bug15629_c.instance_method(:foo)
 1012     sim = im.super_method
 1013 
 1014     assert_equal(sm, m.clone.super_method)
 1015     assert_equal(sim, m.unbind.super_method)
 1016     assert_equal(sim, m.unbind.clone.super_method)
 1017     assert_equal(sim, im.clone.super_method)
 1018     assert_equal(sm, m.unbind.bind(o).super_method)
 1019     assert_equal(sm, m.unbind.clone.bind(o).super_method)
 1020     assert_equal(sm, im.bind(o).super_method)
 1021     assert_equal(sm, im.clone.bind(o).super_method)
 1022   end
 1023 
 1024   def test_super_method_removed
 1025     c1 = Class.new {private def foo; end}
 1026     c2 = Class.new(c1) {public :foo}
 1027     c3 = Class.new(c2) {def foo; end}
 1028     c1.class_eval {undef foo}
 1029     m = c3.instance_method(:foo)
 1030     m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
 1031     assert_nil(m, Feature9781)
 1032   end
 1033 
 1034   def test_prepended_public_zsuper
 1035     mod = EnvUtil.labeled_module("Mod") {private def foo; :ok end}
 1036     mods = [mod]
 1037     obj = Object.new.extend(mod)
 1038     class << obj
 1039       public :foo
 1040     end
 1041     2.times do |i|
 1042       mods.unshift(mod = EnvUtil.labeled_module("Mod#{i}") {def foo; end})
 1043       obj.singleton_class.prepend(mod)
 1044     end
 1045     m = obj.method(:foo)
 1046     assert_equal(mods, mods.map {m.owner.tap {m = m.super_method}})
 1047     assert_nil(m)
 1048   end
 1049 
 1050   def test_super_method_with_prepended_module
 1051     bug = '[ruby-core:81666] [Bug #13656] should be the method of the parent'
 1052     c1 = EnvUtil.labeled_class("C1") {def m; end}
 1053     c2 = EnvUtil.labeled_class("C2", c1) {def m; end}
 1054     c2.prepend(EnvUtil.labeled_module("M"))
 1055     m1 = c1.instance_method(:m)
 1056     m2 = c2.instance_method(:m).super_method
 1057     assert_equal(m1, m2, bug)
 1058     assert_equal(c1, m2.owner, bug)
 1059     assert_equal(m1.source_location, m2.source_location, bug)
 1060   end
 1061 
 1062   def test_super_method_after_bind
 1063     assert_nil String.instance_method(:length).bind(String.new).super_method,
 1064       '[ruby-core:85231] [Bug #14421]'
 1065   end
 1066 
 1067   def test_super_method_alias
 1068     c0 = Class.new do
 1069       def m1
 1070         [:C0_m1]
 1071       end
 1072       def m2
 1073         [:C0_m2]
 1074       end
 1075     end
 1076 
 1077     c1 = Class.new(c0) do
 1078       def m1
 1079         [:C1_m1] + super
 1080       end
 1081       alias m2 m1
 1082     end
 1083 
 1084     c2 = Class.new(c1) do
 1085       def m2
 1086         [:C2_m2] + super
 1087       end
 1088     end
 1089     o1 = c2.new
 1090     assert_equal([:C2_m2, :C1_m1, :C0_m1], o1.m2)
 1091 
 1092     m = o1.method(:m2)
 1093     assert_equal([:C2_m2, :C1_m1, :C0_m1], m.call)
 1094 
 1095     m = m.super_method
 1096     assert_equal([:C1_m1, :C0_m1], m.call)
 1097 
 1098     m = m.super_method
 1099     assert_equal([:C0_m1], m.call)
 1100 
 1101     assert_nil(m.super_method)
 1102   end
 1103 
 1104   def test_super_method_alias_to_prepended_module
 1105     m = Module.new do
 1106       def m1
 1107         [:P_m1] + super
 1108       end
 1109 
 1110       def m2
 1111         [:P_m2] + super
 1112       end
 1113     end
 1114 
 1115     c0 = Class.new do
 1116       def m1
 1117         [:C0_m1]
 1118       end
 1119     end
 1120 
 1121     c1 = Class.new(c0) do
 1122       def m1
 1123         [:C1_m1] + super
 1124       end
 1125       prepend m
 1126       alias m2 m1
 1127     end
 1128 
 1129     o1 = c1.new
 1130     assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], o1.m2)
 1131 
 1132     m = o1.method(:m2)
 1133     assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], m.call)
 1134 
 1135     m = m.super_method
 1136     assert_equal([:P_m1, :C1_m1, :C0_m1], m.call)
 1137 
 1138     m = m.super_method
 1139     assert_equal([:C1_m1, :C0_m1], m.call)
 1140 
 1141     m = m.super_method
 1142     assert_equal([:C0_m1], m.call)
 1143 
 1144     assert_nil(m.super_method)
 1145   end
 1146 
 1147   # Bug 17780
 1148   def test_super_method_module_alias
 1149     m = Module.new do
 1150       def foo
 1151       end
 1152       alias :f :foo
 1153     end
 1154 
 1155     method = m.instance_method(:f)
 1156     super_method = method.super_method
 1157     assert_nil(super_method)
 1158   end
 1159 
 1160   def rest_parameter(*rest)
 1161     rest
 1162   end
 1163 
 1164   def test_splat_long_array
 1165     if File.exist?('/etc/os-release') && File.read('/etc/os-release').include?('openSUSE Leap')
 1166       # For RubyCI's openSUSE machine http://rubyci.s3.amazonaws.com/opensuseleap/ruby-trunk/recent.html, which tends to die with NoMemoryError here.
 1167       skip 'do not exhaust memory on RubyCI openSUSE Leap machine'
 1168     end
 1169     n = 10_000_000
 1170     assert_equal n  , rest_parameter(*(1..n)).size, '[Feature #10440]'
 1171   end
 1172 
 1173   class C
 1174     D = "Const_D"
 1175     def foo
 1176       a = b = c = a = b = c = 12345
 1177     end
 1178   end
 1179 
 1180   def test_to_proc_binding
 1181     bug11012 = '[ruby-core:68673] [Bug #11012]'
 1182 
 1183     b = C.new.method(:foo).to_proc.binding
 1184     assert_equal([], b.local_variables, bug11012)
 1185     assert_equal("Const_D", b.eval("D"), bug11012) # Check CREF
 1186 
 1187     assert_raise(NameError, bug11012){ b.local_variable_get(:foo) }
 1188     assert_equal(123, b.local_variable_set(:foo, 123), bug11012)
 1189     assert_equal(123, b.local_variable_get(:foo), bug11012)
 1190     assert_equal(456, b.local_variable_set(:bar, 456), bug11012)
 1191     assert_equal(123, b.local_variable_get(:foo), bug11012)
 1192     assert_equal(456, b.local_variable_get(:bar), bug11012)
 1193     assert_equal([:bar, :foo], b.local_variables.sort, bug11012)
 1194   end
 1195 
 1196   class MethodInMethodClass
 1197     def m1
 1198       def m2
 1199       end
 1200 
 1201       self.class.send(:define_method, :m3){} # [Bug #11754]
 1202     end
 1203     private
 1204   end
 1205 
 1206   def test_method_in_method_visibility_should_be_public
 1207     assert_equal([:m1].sort, MethodInMethodClass.public_instance_methods(false).sort)
 1208     assert_equal([].sort, MethodInMethodClass.private_instance_methods(false).sort)
 1209 
 1210     MethodInMethodClass.new.m1
 1211     assert_equal([:m1, :m2, :m3].sort, MethodInMethodClass.public_instance_methods(false).sort)
 1212     assert_equal([].sort, MethodInMethodClass.private_instance_methods(false).sort)
 1213   end
 1214 
 1215   def test_define_method_with_symbol
 1216     assert_normal_exit %q{
 1217       define_method(:foo, &:to_s)
 1218       define_method(:bar, :to_s.to_proc)
 1219     }, '[Bug #11850]'
 1220     c = Class.new{
 1221       define_method(:foo, &:to_s)
 1222       define_method(:bar, :to_s.to_proc)
 1223     }
 1224     obj = c.new
 1225     assert_equal('1', obj.foo(1))
 1226     assert_equal('1', obj.bar(1))
 1227   end
 1228 
 1229   def test_argument_error_location
 1230     body = <<-'END_OF_BODY'
 1231     eval <<-'EOS'
 1232     $line_lambda = __LINE__; $f = lambda do
 1233       _x = 1
 1234     end
 1235     $line_method = __LINE__; def foo
 1236       _x = 1
 1237     end
 1238     begin
 1239       $f.call(1)
 1240     rescue ArgumentError => e
 1241       assert_equal "(eval):#{$line_lambda.to_s}:in `block in <main>'", e.backtrace.first
 1242     end
 1243     begin
 1244       foo(1)
 1245     rescue ArgumentError => e
 1246       assert_equal "(eval):#{$line_method}:in `foo'", e.backtrace.first
 1247     end
 1248     EOS
 1249     END_OF_BODY
 1250 
 1251     assert_separately [], body
 1252     # without trace insn
 1253     assert_separately [], "RubyVM::InstructionSequence.compile_option = {trace_instruction: false}\n" + body
 1254   end
 1255 
 1256   def test_eqq
 1257     assert_operator(0.method(:<), :===, 5)
 1258     assert_not_operator(0.method(:<), :===, -5)
 1259   end
 1260 
 1261   def test_compose_with_method
 1262     c = Class.new {
 1263       def f(x) x * 2 end
 1264       def g(x) x + 1 end
 1265     }
 1266     f = c.new.method(:f)
 1267     g = c.new.method(:g)
 1268 
 1269     assert_equal(6, (f << g).call(2))
 1270     assert_equal(6, (g >> f).call(2))
 1271   end
 1272 
 1273   def test_compose_with_proc
 1274     c = Class.new {
 1275       def f(x) x * 2 end
 1276     }
 1277     f = c.new.method(:f)
 1278     g = proc {|x| x + 1}
 1279 
 1280     assert_equal(6, (f << g).call(2))
 1281     assert_equal(6, (g >> f).call(2))
 1282   end
 1283 
 1284   def test_compose_with_callable
 1285     c = Class.new {
 1286       def f(x) x * 2 end
 1287     }
 1288     c2 = Class.new {
 1289       def call(x) x + 1 end
 1290     }
 1291     f = c.new.method(:f)
 1292     g = c2.new
 1293 
 1294     assert_equal(6, (f << g).call(2))
 1295     assert_equal(5, (f >> g).call(2))
 1296   end
 1297 
 1298   def test_compose_with_noncallable
 1299     c = Class.new {
 1300       def f(x) x * 2 end
 1301     }
 1302     f = c.new.method(:f)
 1303 
 1304     assert_raise(TypeError) {
 1305       f << 5
 1306     }
 1307     assert_raise(TypeError) {
 1308       f >> 5
 1309     }
 1310   end
 1311 
 1312   def test_umethod_bind_call
 1313     foo = Base.instance_method(:foo)
 1314     assert_equal(:base, foo.bind_call(Base.new))
 1315     assert_equal(:base, foo.bind_call(Derived.new))
 1316 
 1317     plus = Integer.instance_method(:+)
 1318     assert_equal(3, plus.bind_call(1, 2))
 1319   end
 1320 
 1321   def test_method_list
 1322     # chkbuild lists all methods.
 1323     # The following code emulate this listing.
 1324 
 1325     # use_symbol = Object.instance_methods[0].is_a?(Symbol)
 1326     nummodule = nummethod = 0
 1327     mods = []
 1328     ObjectSpace.each_object(Module) {|m| mods << m if m.name }
 1329     mods = mods.sort_by {|m| m.name }
 1330     mods.each {|mod|
 1331       nummodule += 1
 1332       mc = mod.kind_of?(Class) ? "class" : "module"
 1333       puts_line = "#{mc} #{mod.name} #{(mod.ancestors - [mod]).inspect}"
 1334       puts_line = puts_line # prevent unused var warning
 1335       mod.singleton_methods(false).sort.each {|methname|
 1336         nummethod += 1
 1337         meth = mod.method(methname)
 1338         line = "#{mod.name}.#{methname} #{meth.arity}"
 1339         line << " not-implemented" if !mod.respond_to?(methname)
 1340         # puts line
 1341       }
 1342       ms = mod.instance_methods(false)
 1343       if true or use_symbol
 1344         ms << :initialize if mod.private_instance_methods(false).include? :initialize
 1345       else
 1346         ms << "initialize" if mod.private_instance_methods(false).include? "initialize"
 1347       end
 1348 
 1349       ms.sort.each {|methname|
 1350         nummethod += 1
 1351         meth = mod.instance_method(methname)
 1352         line = "#{mod.name}\##{methname} #{meth.arity}"
 1353         line << " not-implemented" if /\(not-implemented\)/ =~ meth.inspect
 1354         # puts line
 1355       }
 1356     }
 1357     # puts "#{nummodule} modules, #{nummethod} methods"
 1358 
 1359     assert_operator nummodule, :>, 0
 1360     assert_operator nummethod, :>, 0
 1361   end
 1362 end