summaryrefslogtreecommitdiff
path: root/test/ruby/test_method.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_method.rb')
-rw-r--r--test/ruby/test_method.rb754
1 files changed, 729 insertions, 25 deletions
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb
index a1572fde5e..3bf25928c9 100644
--- a/test/ruby/test_method.rb
+++ b/test/ruby/test_method.rb
@@ -1,5 +1,6 @@
+# -*- coding: us-ascii -*-
+# frozen_string_literal: false
require 'test/unit'
-require_relative 'envutil'
class TestMethod < Test::Unit::TestCase
def setup
@@ -21,7 +22,14 @@ class TestMethod < Test::Unit::TestCase
def mo5(a, *b, c) end
def mo6(a, *b, c, &d) end
def mo7(a, b = nil, *c, d, &e) end
- def ma1((a), &b) end
+ def ma1((a), &b) nil && a end
+ def mk1(**) end
+ def mk2(**o) nil && o end
+ def mk3(a, **o) nil && o end
+ def mk4(a = nil, **o) nil && o end
+ def mk5(a, b = nil, **o) nil && o end
+ def mk6(a, b = nil, c, **o) nil && o end
+ def mk7(a, b = nil, *c, d, **o) nil && o end
class Base
def foo() :base end
@@ -31,6 +39,10 @@ class TestMethod < Test::Unit::TestCase
end
class T
def initialize; end
+ def initialize_copy(*) super end
+ def initialize_clone(*) super end
+ def initialize_dup(*) super end
+ def respond_to_missing?(*) super end
def normal_method; end
end
module M
@@ -63,6 +75,13 @@ class TestMethod < Test::Unit::TestCase
assert_equal(-2, method(:mo4).arity)
assert_equal(-3, method(:mo5).arity)
assert_equal(-3, method(:mo6).arity)
+ assert_equal(-1, method(:mk1).arity)
+ assert_equal(-1, method(:mk2).arity)
+ assert_equal(-2, method(:mk3).arity)
+ assert_equal(-1, method(:mk4).arity)
+ assert_equal(-2, method(:mk5).arity)
+ assert_equal(-3, method(:mk6).arity)
+ assert_equal(-3, method(:mk7).arity)
end
def test_arity_special
@@ -106,6 +125,11 @@ class TestMethod < Test::Unit::TestCase
assert_nil(eval("class TestCallee; __callee__; end"))
end
+ def test_orphan_callee
+ c = Class.new{def foo; proc{__callee__}; end; alias alias_foo foo}
+ assert_equal(:alias_foo, c.new.alias_foo.call, '[Bug #11046]')
+ end
+
def test_method_in_define_method_block
bug4606 = '[ruby-core:35386]'
c = Class.new do
@@ -173,6 +197,27 @@ class TestMethod < Test::Unit::TestCase
def o.foo; end
assert_kind_of(Integer, o.method(:foo).hash)
assert_equal(Array.instance_method(:map).hash, Array.instance_method(:collect).hash)
+ assert_kind_of(String, o.method(:foo).hash.to_s)
+ end
+
+ def test_owner
+ c = Class.new do
+ def foo; end
+ end
+ assert_equal(c, c.instance_method(:foo).owner)
+ c2 = Class.new(c)
+ assert_equal(c, c2.instance_method(:foo).owner)
+ end
+
+ def test_owner_missing
+ c = Class.new do
+ def respond_to_missing?(name, bool)
+ name == :foo
+ end
+ end
+ c2 = Class.new(c)
+ assert_equal(c, c.new.method(:foo).owner)
+ assert_equal(c2, c2.new.method(:foo).owner)
end
def test_receiver_name_owner
@@ -184,6 +229,12 @@ class TestMethod < Test::Unit::TestCase
assert_equal(class << o; self; end, m.owner)
assert_equal(:foo, m.unbind.name)
assert_equal(class << o; self; end, m.unbind.owner)
+ class << o
+ alias bar foo
+ end
+ m = o.method(:bar)
+ assert_equal(:bar, m.name)
+ assert_equal(:foo, m.original_name)
end
def test_instance_method
@@ -201,6 +252,17 @@ class TestMethod < Test::Unit::TestCase
def o.bar; end
m = o.method(:bar).unbind
assert_raise(TypeError) { m.bind(Object.new) }
+
+ cx = EnvUtil.labeled_class("X\u{1f431}")
+ assert_raise_with_message(TypeError, /X\u{1f431}/) do
+ o.method(cx)
+ end
+ end
+
+ def test_bind_module_instance_method
+ feature4254 = '[ruby-core:34267]'
+ m = M.instance_method(:meth)
+ assert_equal(:meth, m.bind(Object.new).call, feature4254)
end
def test_define_method
@@ -223,19 +285,44 @@ class TestMethod < Test::Unit::TestCase
assert_raise(TypeError) do
Class.new.class_eval { define_method(:bar, o.method(:bar)) }
end
+ cx = EnvUtil.labeled_class("X\u{1f431}")
+ assert_raise_with_message(TypeError, /X\u{1F431}/) do
+ Class.new {define_method(cx) {}}
+ end
+ end
+ def test_define_method_no_proc
o = Object.new
def o.foo(c)
c.class_eval { define_method(:foo) }
end
c = Class.new
- o.foo(c) { :foo }
- assert_equal(:foo, c.new.foo)
+ assert_raise(ArgumentError) {o.foo(c)}
+ bug11283 = '[ruby-core:69655] [Bug #11283]'
+ assert_raise(ArgumentError, bug11283) {o.foo(c) {:foo}}
+ end
+
+ def test_define_singleton_method
o = Object.new
o.instance_eval { define_singleton_method(:foo) { :foo } }
assert_equal(:foo, o.foo)
+ end
+
+ def test_define_singleton_method_no_proc
+ o = Object.new
+ assert_raise(ArgumentError) {
+ o.instance_eval { define_singleton_method(:bar) }
+ }
+ bug11283 = '[ruby-core:69655] [Bug #11283]'
+ def o.define(n)
+ define_singleton_method(n)
+ end
+ assert_raise(ArgumentError, bug11283) {o.define(:bar) {:bar}}
+ end
+
+ def test_define_method_invalid_arg
assert_raise(TypeError) do
Class.new.class_eval { define_method(:foo, Object.new) }
end
@@ -243,7 +330,25 @@ class TestMethod < Test::Unit::TestCase
assert_raise(TypeError) do
Module.new.module_eval {define_method(:foo, Base.instance_method(:foo))}
end
+ end
+
+ def test_define_singleton_method_with_extended_method
+ bug8686 = "[ruby-core:56174]"
+
+ m = Module.new do
+ extend self
+
+ def a
+ "a"
+ end
+ end
+
+ assert_nothing_raised(bug8686) do
+ m.define_singleton_method(:a, m.method(:a))
+ end
+ end
+ def test_define_method_transplating
feature4254 = '[ruby-core:34267]'
m = Module.new {define_method(:meth, M.instance_method(:meth))}
assert_equal(:meth, Object.new.extend(m).meth, feature4254)
@@ -251,6 +356,61 @@ class TestMethod < Test::Unit::TestCase
assert_equal(:meth, c.new.meth, feature4254)
end
+ def test_define_method_visibility
+ c = Class.new do
+ public
+ define_method(:foo) {:foo}
+ protected
+ define_method(:bar) {:bar}
+ private
+ define_method(:baz) {:baz}
+ end
+
+ assert_equal(true, c.public_method_defined?(:foo))
+ assert_equal(false, c.public_method_defined?(:bar))
+ assert_equal(false, c.public_method_defined?(:baz))
+
+ assert_equal(false, c.protected_method_defined?(:foo))
+ assert_equal(true, c.protected_method_defined?(:bar))
+ assert_equal(false, c.protected_method_defined?(:baz))
+
+ assert_equal(false, c.private_method_defined?(:foo))
+ assert_equal(false, c.private_method_defined?(:bar))
+ assert_equal(true, c.private_method_defined?(:baz))
+
+ m = Module.new do
+ module_function
+ define_method(:foo) {:foo}
+ end
+ assert_equal(true, m.respond_to?(:foo))
+ assert_equal(false, m.public_method_defined?(:foo))
+ assert_equal(false, m.protected_method_defined?(:foo))
+ assert_equal(true, m.private_method_defined?(:foo))
+ end
+
+ def test_define_method_in_private_scope
+ bug9005 = '[ruby-core:57747] [Bug #9005]'
+ c = Class.new
+ class << c
+ public :define_method
+ end
+ TOPLEVEL_BINDING.eval("proc{|c|c.define_method(:x) {|x|throw x}}").call(c)
+ o = c.new
+ assert_throw(bug9005) {o.x(bug9005)}
+ end
+
+ def test_singleton_define_method_in_private_scope
+ bug9141 = '[ruby-core:58497] [Bug #9141]'
+ o = Object.new
+ class << o
+ public :define_singleton_method
+ end
+ TOPLEVEL_BINDING.eval("proc{|o|o.define_singleton_method(:x) {|x|throw x}}").call(o)
+ assert_throw(bug9141) do
+ o.x(bug9141)
+ end
+ end
+
def test_super_in_proc_from_define_method
c1 = Class.new {
def m
@@ -258,11 +418,7 @@ class TestMethod < Test::Unit::TestCase
end
}
c2 = Class.new(c1) { define_method(:m) { Proc.new { super() } } }
- # c2.new.m.call should return :m1, but currently it raise NoMethodError.
- # see [Bug #4881] and [Bug #3136]
- assert_raise(NoMethodError) {
- c2.new.m.call
- }
+ assert_equal(:m1, c2.new.m.call, 'see [Bug #4881] and [Bug #3136]')
end
def test_clone
@@ -274,15 +430,6 @@ class TestMethod < Test::Unit::TestCase
assert_equal(:bar, m.clone.bar)
end
- def test_call
- o = Object.new
- def o.foo; p 1; end
- def o.bar(x); x; end
- m = o.method(:foo)
- m.taint
- assert_raise(SecurityError) { m.call }
- end
-
def test_inspect
o = Object.new
def o.foo; end
@@ -302,6 +449,23 @@ class TestMethod < Test::Unit::TestCase
c2.class_eval { private :foo }
m2 = c2.new.method(:foo)
assert_equal("#<Method: #{ c2.inspect }(#{ c.inspect })#foo>", m2.inspect)
+
+ bug7806 = '[ruby-core:52048] [Bug #7806]'
+ c3 = Class.new(c)
+ c3.class_eval { alias bar foo }
+ m3 = c3.new.method(:bar)
+ assert_equal("#<Method: #{c3.inspect}(#{c.inspect})#bar(foo)>", m3.inspect, bug7806)
+
+ m.taint
+ assert_predicate(m.inspect, :tainted?, "inspect result should be infected")
+
+ bug15608 = '[ruby-core:91570] [Bug #15608]'
+ c4 = Class.new(c)
+ c4.class_eval { alias bar foo }
+ o = c4.new
+ o.singleton_class
+ m4 = o.method(:bar)
+ assert_equal("#<Method: #{c4.inspect}(#{c.inspect})#bar(foo)>", m4.inspect, bug15608)
end
def test_callee_top_level
@@ -327,10 +491,16 @@ class TestMethod < Test::Unit::TestCase
end
def test_default_accessibility
- assert T.public_instance_methods.include?(:normal_method), 'normal methods are public by default'
- assert !T.public_instance_methods.include?(:initialize), '#initialize is private'
- assert !M.public_instance_methods.include?(:func), 'module methods are private by default'
- assert M.public_instance_methods.include?(:meth), 'normal methods are public by default'
+ tmethods = T.public_instance_methods
+ assert_include tmethods, :normal_method, 'normal methods are public by default'
+ assert_not_include tmethods, :initialize, '#initialize is private'
+ assert_not_include tmethods, :initialize_copy, '#initialize_copy is private'
+ assert_not_include tmethods, :initialize_clone, '#initialize_clone is private'
+ assert_not_include tmethods, :initialize_dup, '#initialize_dup is private'
+ assert_not_include tmethods, :respond_to_missing?, '#respond_to_missing? is private'
+ mmethods = M.public_instance_methods
+ assert_not_include mmethods, :func, 'module methods are private by default'
+ assert_include mmethods, :meth, 'normal methods are public by default'
end
define_method(:pm0) {||}
@@ -343,7 +513,14 @@ class TestMethod < Test::Unit::TestCase
define_method(:pmo5) {|a, *b, c|}
define_method(:pmo6) {|a, *b, c, &d|}
define_method(:pmo7) {|a, b = nil, *c, d, &e|}
- define_method(:pma1) {|(a), &b|}
+ define_method(:pma1) {|(a), &b| nil && a}
+ define_method(:pmk1) {|**|}
+ define_method(:pmk2) {|**o|}
+ define_method(:pmk3) {|a, **o|}
+ define_method(:pmk4) {|a = nil, **o|}
+ define_method(:pmk5) {|a, b = nil, **o|}
+ define_method(:pmk6) {|a, b = nil, c, **o|}
+ define_method(:pmk7) {|a, b = nil, *c, d, **o|}
def test_bound_parameters
assert_equal([], method(:m0).parameters)
@@ -357,6 +534,13 @@ class TestMethod < Test::Unit::TestCase
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:mo6).parameters)
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:mo7).parameters)
assert_equal([[:req], [:block, :b]], method(:ma1).parameters)
+ assert_equal([[:keyrest]], method(:mk1).parameters)
+ assert_equal([[:keyrest, :o]], method(:mk2).parameters)
+ assert_equal([[:req, :a], [:keyrest, :o]], method(:mk3).parameters)
+ assert_equal([[:opt, :a], [:keyrest, :o]], method(:mk4).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], method(:mk5).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], method(:mk6).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], method(:mk7).parameters)
end
def test_unbound_parameters
@@ -371,6 +555,13 @@ class TestMethod < Test::Unit::TestCase
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:mo6).parameters)
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:mo7).parameters)
assert_equal([[:req], [:block, :b]], self.class.instance_method(:ma1).parameters)
+ assert_equal([[:keyrest]], self.class.instance_method(:mk1).parameters)
+ assert_equal([[:keyrest, :o]], self.class.instance_method(:mk2).parameters)
+ assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:mk3).parameters)
+ assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:mk4).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], self.class.instance_method(:mk5).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], self.class.instance_method(:mk6).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:mk7).parameters)
end
def test_bmethod_bound_parameters
@@ -385,6 +576,13 @@ class TestMethod < Test::Unit::TestCase
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:pmo6).parameters)
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:pmo7).parameters)
assert_equal([[:req], [:block, :b]], method(:pma1).parameters)
+ assert_equal([[:keyrest]], method(:pmk1).parameters)
+ assert_equal([[:keyrest, :o]], method(:pmk2).parameters)
+ assert_equal([[:req, :a], [:keyrest, :o]], method(:pmk3).parameters)
+ assert_equal([[:opt, :a], [:keyrest, :o]], method(:pmk4).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], method(:pmk5).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], method(:pmk6).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], method(:pmk7).parameters)
end
def test_bmethod_unbound_parameters
@@ -399,6 +597,19 @@ class TestMethod < Test::Unit::TestCase
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:pmo6).parameters)
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:pmo7).parameters)
assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters)
+ assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters)
+ assert_equal([[:keyrest]], self.class.instance_method(:pmk1).parameters)
+ assert_equal([[:keyrest, :o]], self.class.instance_method(:pmk2).parameters)
+ assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:pmk3).parameters)
+ assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:pmk4).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:keyrest, :o]], self.class.instance_method(:pmk5).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:req, :c], [:keyrest, :o]], self.class.instance_method(:pmk6).parameters)
+ assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:pmk7).parameters)
+ end
+
+ def test_hidden_parameters
+ instance_eval("def m((_)"+",(_)"*256+");end")
+ assert_empty(method(:m).parameters.map{|_,n|n}.compact)
end
def test_public_method_with_zsuper_method
@@ -486,12 +697,505 @@ class TestMethod < Test::Unit::TestCase
define_singleton_method(:reverse, target.method(:reverse).to_proc)
end
end
- 1000.times {p = Bug6171.new('test'); 10000.times {p.reverse}}
+ 100.times {p = Bug6171.new('test'); 1000.times {p.reverse}}
EOC
end
+ def test_unbound_method_proc_coerce
+ # '&' coercion of an UnboundMethod raises TypeError
+ assert_raise(TypeError) do
+ Class.new do
+ define_method('foo', &Object.instance_method(:to_s))
+ end
+ end
+ end
+
def test___dir__
assert_instance_of String, __dir__
- assert_equal(File.dirname(__FILE__), __dir__)
+ assert_equal(File.dirname(File.realpath(__FILE__)), __dir__)
+ bug8436 = '[ruby-core:55123] [Bug #8436]'
+ assert_equal(__dir__, eval("__dir__", binding), bug8436)
+ bug8662 = '[ruby-core:56099] [Bug #8662]'
+ assert_equal("arbitrary", eval("__dir__", binding, "arbitrary/file.rb"), bug8662)
+ assert_equal("arbitrary", Object.new.instance_eval("__dir__", "arbitrary/file.rb"), bug8662)
+ end
+
+ def test_alias_owner
+ bug7613 = '[ruby-core:51105]'
+ bug7993 = '[Bug #7993]'
+ c = Class.new {
+ def foo
+ end
+ prepend Module.new
+ attr_reader :zot
+ }
+ x = c.new
+ class << x
+ alias bar foo
+ end
+ assert_equal(c, c.instance_method(:foo).owner)
+ assert_equal(c, x.method(:foo).owner)
+ assert_equal(x.singleton_class, x.method(:bar).owner)
+ assert_not_equal(x.method(:foo), x.method(:bar), bug7613)
+ assert_equal(c, x.method(:zot).owner, bug7993)
+ assert_equal(c, c.instance_method(:zot).owner, bug7993)
+ end
+
+ def test_included
+ m = Module.new {
+ def foo
+ end
+ }
+ c = Class.new {
+ def foo
+ end
+ include m
+ }
+ assert_equal(c, c.instance_method(:foo).owner)
+ end
+
+ def test_prepended
+ bug7836 = '[ruby-core:52160] [Bug #7836]'
+ bug7988 = '[ruby-core:53038] [Bug #7988]'
+ m = Module.new {
+ def foo
+ end
+ }
+ c = Class.new {
+ def foo
+ end
+ prepend m
+ }
+ assert_raise(NameError, bug7988) {Module.new{prepend m}.instance_method(:bar)}
+ true || c || bug7836
+ end
+
+ def test_gced_bmethod
+ assert_normal_exit %q{
+ require 'irb'
+ IRB::Irb.module_eval do
+ define_method(:eval_input) do
+ IRB::Irb.module_eval { alias_method :eval_input, :to_s }
+ GC.start
+ Kernel
+ end
+ end
+ IRB.start
+ }, '[Bug #7825]'
+ end
+
+ def test_singleton_method
+ feature8391 = '[ruby-core:54914] [Feature #8391]'
+ c1 = Class.new
+ c1.class_eval { def foo; :foo; end }
+ o = c1.new
+ def o.bar; :bar; end
+ assert_nothing_raised(NameError) {o.method(:foo)}
+ assert_raise(NameError, feature8391) {o.singleton_method(:foo)}
+ m = assert_nothing_raised(NameError, feature8391) {break o.singleton_method(:bar)}
+ assert_equal(:bar, m.call, feature8391)
+ end
+
+ def test_singleton_method_prepend
+ bug14658 = '[Bug #14658]'
+ c1 = Class.new
+ o = c1.new
+ def o.bar; :bar; end
+ class << o; prepend Module.new; end
+ m = assert_nothing_raised(NameError, bug14658) {o.singleton_method(:bar)}
+ assert_equal(:bar, m.call, bug14658)
+
+ o = Object.new
+ assert_raise(NameError, bug14658) {o.singleton_method(:bar)}
+ end
+
+ Feature9783 = '[ruby-core:62212] [Feature #9783]'
+
+ def assert_curry_three_args(m)
+ curried = m.curry
+ assert_equal(6, curried.(1).(2).(3), Feature9783)
+
+ curried = m.curry(3)
+ assert_equal(6, curried.(1).(2).(3), Feature9783)
+
+ assert_raise_with_message(ArgumentError, /wrong number/) {m.curry(2)}
+ end
+
+ def test_curry_method
+ c = Class.new {
+ def three_args(a,b,c) a + b + c end
+ }
+ assert_curry_three_args(c.new.method(:three_args))
+ end
+
+ def test_curry_from_proc
+ c = Class.new {
+ define_method(:three_args) {|x,y,z| x + y + z}
+ }
+ assert_curry_three_args(c.new.method(:three_args))
+ end
+
+ def assert_curry_var_args(m)
+ curried = m.curry(3)
+ assert_equal([1, 2, 3], curried.(1).(2).(3), Feature9783)
+
+ curried = m.curry(2)
+ assert_equal([1, 2], curried.(1).(2), Feature9783)
+
+ curried = m.curry(0)
+ assert_equal([1], curried.(1), Feature9783)
+ end
+
+ def test_curry_var_args
+ c = Class.new {
+ def var_args(*args) args end
+ }
+ assert_curry_var_args(c.new.method(:var_args))
+ end
+
+ def test_curry_from_proc_var_args
+ c = Class.new {
+ define_method(:var_args) {|*args| args}
+ }
+ assert_curry_var_args(c.new.method(:var_args))
+ end
+
+ Feature9781 = '[ruby-core:62202] [Feature #9781]'
+
+ def test_super_method
+ o = Derived.new
+ m = o.method(:foo).super_method
+ assert_equal(Base, m.owner, Feature9781)
+ assert_same(o, m.receiver, Feature9781)
+ assert_equal(:foo, m.name, Feature9781)
+ m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
+ assert_nil(m, Feature9781)
+ end
+
+ def test_super_method_unbound
+ m = Derived.instance_method(:foo)
+ m = m.super_method
+ assert_equal(Base.instance_method(:foo), m, Feature9781)
+ m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
+ assert_nil(m, Feature9781)
+
+ bug11419 = '[ruby-core:70254]'
+ m = Object.instance_method(:tap)
+ m = assert_nothing_raised(NameError, bug11419) {break m.super_method}
+ assert_nil(m, bug11419)
+ end
+
+ def test_super_method_module
+ m1 = Module.new {def foo; end}
+ c1 = Class.new(Derived) {include m1; def foo; end}
+ m = c1.instance_method(:foo)
+ assert_equal(c1, m.owner, Feature9781)
+ m = m.super_method
+ assert_equal(m1, m.owner, Feature9781)
+ m = m.super_method
+ assert_equal(Derived, m.owner, Feature9781)
+ m = m.super_method
+ assert_equal(Base, m.owner, Feature9781)
+ m2 = Module.new {def foo; end}
+ o = c1.new.extend(m2)
+ m = o.method(:foo)
+ assert_equal(m2, m.owner, Feature9781)
+ m = m.super_method
+ assert_equal(c1, m.owner, Feature9781)
+ assert_same(o, m.receiver, Feature9781)
+
+ c1 = Class.new {def foo; end}
+ c2 = Class.new(c1) {include m1; include m2}
+ m = c2.instance_method(:foo)
+ assert_equal(m2, m.owner)
+ m = m.super_method
+ assert_equal(m1, m.owner)
+ m = m.super_method
+ assert_equal(c1, m.owner)
+ assert_nil(m.super_method)
+ end
+
+ def test_super_method_removed
+ c1 = Class.new {private def foo; end}
+ c2 = Class.new(c1) {public :foo}
+ c3 = Class.new(c2) {def foo; end}
+ c1.class_eval {undef foo}
+ m = c3.instance_method(:foo)
+ m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
+ assert_nil(m, Feature9781)
+ end
+
+ def test_prepended_public_zsuper
+ mod = EnvUtil.labeled_module("Mod") {private def foo; :ok end}
+ mods = [mod]
+ obj = Object.new.extend(mod)
+ class << obj
+ public :foo
+ end
+ 2.times do |i|
+ mods.unshift(mod = EnvUtil.labeled_module("Mod#{i}") {def foo; end})
+ obj.singleton_class.prepend(mod)
+ end
+ m = obj.method(:foo)
+ assert_equal(mods, mods.map {m.owner.tap {m = m.super_method}})
+ assert_nil(m)
+ end
+
+ def test_super_method_with_prepended_module
+ bug = '[ruby-core:81666] [Bug #13656] should be the method of the parent'
+ c1 = EnvUtil.labeled_class("C1") {def m; end}
+ c2 = EnvUtil.labeled_class("C2", c1) {def m; end}
+ c2.prepend(EnvUtil.labeled_module("M"))
+ m1 = c1.instance_method(:m)
+ m2 = c2.instance_method(:m).super_method
+ assert_equal(m1, m2, bug)
+ assert_equal(c1, m2.owner, bug)
+ assert_equal(m1.source_location, m2.source_location, bug)
+ end
+
+ def test_super_method_after_bind
+ assert_nil String.instance_method(:length).bind(String.new).super_method,
+ '[ruby-core:85231] [Bug #14421]'
+ end
+
+ def test_super_method_alias
+ c0 = Class.new do
+ def m1
+ [:C0_m1]
+ end
+ def m2
+ [:C0_m2]
+ end
+ end
+
+ c1 = Class.new(c0) do
+ def m1
+ [:C1_m1] + super
+ end
+ alias m2 m1
+ end
+
+ c2 = Class.new(c1) do
+ def m2
+ [:C2_m2] + super
+ end
+ end
+ o1 = c2.new
+ assert_equal([:C2_m2, :C1_m1, :C0_m1], o1.m2)
+
+ m = o1.method(:m2)
+ assert_equal([:C2_m2, :C1_m1, :C0_m1], m.call)
+
+ m = m.super_method
+ assert_equal([:C1_m1, :C0_m1], m.call)
+
+ m = m.super_method
+ assert_equal([:C0_m1], m.call)
+
+ assert_nil(m.super_method)
+ end
+
+ def test_super_method_alias_to_prepended_module
+ m = Module.new do
+ def m1
+ [:P_m1] + super
+ end
+
+ def m2
+ [:P_m2] + super
+ end
+ end
+
+ c0 = Class.new do
+ def m1
+ [:C0_m1]
+ end
+ end
+
+ c1 = Class.new(c0) do
+ def m1
+ [:C1_m1] + super
+ end
+ prepend m
+ alias m2 m1
+ end
+
+ o1 = c1.new
+ assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], o1.m2)
+
+ m = o1.method(:m2)
+ assert_equal([:P_m2, :P_m1, :C1_m1, :C0_m1], m.call)
+
+ m = m.super_method
+ assert_equal([:P_m1, :C1_m1, :C0_m1], m.call)
+
+ m = m.super_method
+ assert_equal([:C1_m1, :C0_m1], m.call)
+
+ m = m.super_method
+ assert_equal([:C0_m1], m.call)
+
+ assert_nil(m.super_method)
+ end
+
+ # Bug 17780
+ def test_super_method_module_alias
+ m = Module.new do
+ def foo
+ end
+ alias :f :foo
+ end
+
+ method = m.instance_method(:f)
+ super_method = method.super_method
+ assert_nil(super_method)
+ end
+
+ def rest_parameter(*rest)
+ rest
+ end
+
+ def test_splat_long_array
+ n = 10_000_000
+ assert_equal n , rest_parameter(*(1..n)).size, '[Feature #10440]'
+ end
+
+ class C
+ D = "Const_D"
+ def foo
+ a = b = c = a = b = c = 12345
+ end
+ end
+
+ def test_to_proc_binding
+ bug11012 = '[ruby-core:68673] [Bug #11012]'
+
+ b = C.new.method(:foo).to_proc.binding
+ assert_equal([], b.local_variables, bug11012)
+ assert_equal("Const_D", b.eval("D"), bug11012) # Check CREF
+
+ assert_raise(NameError, bug11012){ b.local_variable_get(:foo) }
+ assert_equal(123, b.local_variable_set(:foo, 123), bug11012)
+ assert_equal(123, b.local_variable_get(:foo), bug11012)
+ assert_equal(456, b.local_variable_set(:bar, 456), bug11012)
+ assert_equal(123, b.local_variable_get(:foo), bug11012)
+ assert_equal(456, b.local_variable_get(:bar), bug11012)
+ assert_equal([:bar, :foo], b.local_variables.sort, bug11012)
+ end
+
+ class MethodInMethodClass
+ def m1
+ def m2
+ end
+
+ self.class.send(:define_method, :m3){} # [Bug #11754]
+ end
+ private
+ end
+
+ def test_method_in_method_visibility_should_be_public
+ assert_equal([:m1].sort, MethodInMethodClass.public_instance_methods(false).sort)
+ assert_equal([].sort, MethodInMethodClass.private_instance_methods(false).sort)
+
+ MethodInMethodClass.new.m1
+ assert_equal([:m1, :m2, :m3].sort, MethodInMethodClass.public_instance_methods(false).sort)
+ assert_equal([].sort, MethodInMethodClass.private_instance_methods(false).sort)
+ end
+
+ def test_define_method_with_symbol
+ assert_normal_exit %q{
+ define_method(:foo, &:to_s)
+ define_method(:bar, :to_s.to_proc)
+ }, '[Bug #11850]'
+ c = Class.new{
+ define_method(:foo, &:to_s)
+ define_method(:bar, :to_s.to_proc)
+ }
+ obj = c.new
+ assert_equal('1', obj.foo(1))
+ assert_equal('1', obj.bar(1))
+ end
+
+ def test_argument_error_location
+ body = <<-'END_OF_BODY'
+ eval <<-'EOS'
+ $line_lambda = __LINE__; $f = lambda do
+ _x = 1
+ end
+ $line_method = __LINE__; def foo
+ _x = 1
+ end
+ begin
+ $f.call(1)
+ rescue ArgumentError => e
+ assert_equal "(eval):#{$line_lambda.to_s}:in `block in <main>'", e.backtrace.first
+ end
+ begin
+ foo(1)
+ rescue ArgumentError => e
+ assert_equal "(eval):#{$line_method}:in `foo'", e.backtrace.first
+ end
+ EOS
+ END_OF_BODY
+
+ assert_separately [], body
+ # without trace insn
+ assert_separately [], "RubyVM::InstructionSequence.compile_option = {trace_instruction: false}\n" + body
+ end
+
+ def test_eqq
+ assert_operator(0.method(:<), :===, 5)
+ assert_not_operator(0.method(:<), :===, -5)
+ end
+
+ def test_compose_with_method
+ c = Class.new {
+ def f(x) x * 2 end
+ def g(x) x + 1 end
+ }
+ f = c.new.method(:f)
+ g = c.new.method(:g)
+
+ assert_equal(6, (f << g).call(2))
+ assert_equal(6, (g >> f).call(2))
+ end
+
+ def test_compose_with_proc
+ c = Class.new {
+ def f(x) x * 2 end
+ }
+ f = c.new.method(:f)
+ g = proc {|x| x + 1}
+
+ assert_equal(6, (f << g).call(2))
+ assert_equal(6, (g >> f).call(2))
+ end
+
+ def test_compose_with_callable
+ c = Class.new {
+ def f(x) x * 2 end
+ }
+ c2 = Class.new {
+ def call(x) x + 1 end
+ }
+ f = c.new.method(:f)
+ g = c2.new
+
+ assert_equal(6, (f << g).call(2))
+ assert_equal(5, (f >> g).call(2))
+ end
+
+ def test_compose_with_noncallable
+ c = Class.new {
+ def f(x) x * 2 end
+ }
+ f = c.new.method(:f)
+
+ assert_raise(NoMethodError) {
+ (f << 5).call(2)
+ }
+ assert_raise(NoMethodError) {
+ (f >> 5).call(2)
+ }
end
end