2012-12-01 04:16:52 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2005-08-08 07:43:56 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestMethod < Test::Unit::TestCase
|
2008-02-13 10:03:59 -05:00
|
|
|
def setup
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
2005-08-08 07:43:56 -04:00
|
|
|
def m0() end
|
|
|
|
def m1(a) end
|
|
|
|
def m2(a, b) end
|
|
|
|
def mo1(a = nil, &b) end
|
|
|
|
def mo2(a, b = nil) end
|
|
|
|
def mo3(*a) end
|
|
|
|
def mo4(a, *b, &c) end
|
2007-05-01 00:35:58 -04:00
|
|
|
def mo5(a, *b, c) end
|
|
|
|
def mo6(a, *b, c, &d) end
|
2008-11-27 23:19:37 -05:00
|
|
|
def mo7(a, b = nil, *c, d, &e) end
|
2013-12-25 06:11:42 -05:00
|
|
|
def ma1((a), &b) nil && a end
|
2013-12-25 06:28:56 -05:00
|
|
|
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
|
2005-08-08 07:43:56 -04:00
|
|
|
|
|
|
|
class Base
|
|
|
|
def foo() :base end
|
|
|
|
end
|
|
|
|
class Derived < Base
|
|
|
|
def foo() :derived end
|
|
|
|
end
|
2008-11-22 07:34:21 -05:00
|
|
|
class T
|
|
|
|
def initialize; end
|
2012-12-01 04:16:52 -05:00
|
|
|
def initialize_copy(*) super end
|
|
|
|
def initialize_clone(*) super end
|
|
|
|
def initialize_dup(*) super end
|
|
|
|
def respond_to_missing?(*) super end
|
2008-11-22 07:34:21 -05:00
|
|
|
def normal_method; end
|
|
|
|
end
|
|
|
|
module M
|
|
|
|
def func; end
|
|
|
|
module_function :func
|
2012-06-25 03:57:42 -04:00
|
|
|
def meth; :meth end
|
2008-11-22 07:34:21 -05:00
|
|
|
end
|
2005-08-08 07:43:56 -04:00
|
|
|
|
2010-07-05 03:11:44 -04:00
|
|
|
def mv1() end
|
|
|
|
def mv2() end
|
|
|
|
private :mv2
|
|
|
|
def mv3() end
|
|
|
|
protected :mv3
|
|
|
|
|
|
|
|
class Visibility
|
|
|
|
def mv1() end
|
|
|
|
def mv2() end
|
|
|
|
private :mv2
|
|
|
|
def mv3() end
|
|
|
|
protected :mv3
|
|
|
|
end
|
|
|
|
|
2005-08-08 07:43:56 -04:00
|
|
|
def test_arity
|
|
|
|
assert_equal(0, method(:m0).arity)
|
|
|
|
assert_equal(1, method(:m1).arity)
|
|
|
|
assert_equal(2, method(:m2).arity)
|
|
|
|
assert_equal(-1, method(:mo1).arity)
|
|
|
|
assert_equal(-2, method(:mo2).arity)
|
|
|
|
assert_equal(-1, method(:mo3).arity)
|
|
|
|
assert_equal(-2, method(:mo4).arity)
|
2007-05-01 00:35:58 -04:00
|
|
|
assert_equal(-3, method(:mo5).arity)
|
|
|
|
assert_equal(-3, method(:mo6).arity)
|
2013-12-25 06:28:56 -05:00
|
|
|
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)
|
2005-08-08 07:43:56 -04:00
|
|
|
end
|
|
|
|
|
2009-07-28 06:41:11 -04:00
|
|
|
def test_arity_special
|
|
|
|
assert_equal(-1, method(:__send__).arity)
|
|
|
|
end
|
|
|
|
|
2005-08-08 07:43:56 -04:00
|
|
|
def test_unbind
|
|
|
|
assert_equal(:derived, Derived.new.foo)
|
|
|
|
um = Derived.new.method(:foo).unbind
|
|
|
|
assert_instance_of(UnboundMethod, um)
|
|
|
|
Derived.class_eval do
|
|
|
|
def foo() :changed end
|
|
|
|
end
|
|
|
|
assert_equal(:changed, Derived.new.foo)
|
|
|
|
assert_equal(:derived, um.bind(Derived.new).call)
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
um.bind(Base.new)
|
|
|
|
end
|
|
|
|
end
|
2007-08-17 12:12:03 -04:00
|
|
|
|
|
|
|
def test_callee
|
|
|
|
assert_equal(:test_callee, __method__)
|
|
|
|
assert_equal(:m, Class.new {def m; __method__; end}.new.m)
|
|
|
|
assert_equal(:m, Class.new {def m; tap{return __method__}; end}.new.m)
|
|
|
|
assert_equal(:m, Class.new {define_method(:m) {__method__}}.new.m)
|
|
|
|
assert_equal(:m, Class.new {define_method(:m) {tap{return __method__}}}.new.m)
|
|
|
|
assert_nil(eval("class TestCallee; __method__; end"))
|
2012-07-03 22:11:37 -04:00
|
|
|
|
|
|
|
assert_equal(:test_callee, __callee__)
|
|
|
|
[
|
|
|
|
["method", Class.new {def m; __callee__; end},],
|
|
|
|
["block", Class.new {def m; tap{return __callee__}; end},],
|
|
|
|
["define_method", Class.new {define_method(:m) {__callee__}}],
|
|
|
|
["define_method block", Class.new {define_method(:m) {tap{return __callee__}}}],
|
|
|
|
].each do |mesg, c|
|
|
|
|
c.class_eval {alias m2 m}
|
|
|
|
o = c.new
|
|
|
|
assert_equal(:m, o.m, mesg)
|
|
|
|
assert_equal(:m2, o.m2, mesg)
|
|
|
|
end
|
|
|
|
assert_nil(eval("class TestCallee; __callee__; end"))
|
2007-08-17 12:12:03 -04:00
|
|
|
end
|
2008-02-13 10:03:59 -05:00
|
|
|
|
2011-05-05 02:32:37 -04:00
|
|
|
def test_method_in_define_method_block
|
|
|
|
bug4606 = '[ruby-core:35386]'
|
|
|
|
c = Class.new do
|
|
|
|
[:m1, :m2].each do |m|
|
|
|
|
define_method(m) do
|
|
|
|
__method__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(:m1, c.new.m1, bug4606)
|
|
|
|
assert_equal(:m2, c.new.m2, bug4606)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_in_block_in_define_method_block
|
|
|
|
bug4606 = '[ruby-core:35386]'
|
|
|
|
c = Class.new do
|
|
|
|
[:m1, :m2].each do |m|
|
|
|
|
define_method(m) do
|
|
|
|
tap { return __method__ }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal(:m1, c.new.m1, bug4606)
|
|
|
|
assert_equal(:m2, c.new.m2, bug4606)
|
|
|
|
end
|
|
|
|
|
2008-02-13 10:03:59 -05:00
|
|
|
def test_body
|
|
|
|
o = Object.new
|
|
|
|
def o.foo; end
|
2008-06-29 13:26:16 -04:00
|
|
|
assert_nothing_raised { RubyVM::InstructionSequence.disasm(o.method(:foo)) }
|
2009-12-23 01:22:48 -05:00
|
|
|
assert_nothing_raised { RubyVM::InstructionSequence.disasm("x".method(:upcase)) }
|
2011-08-20 00:26:20 -04:00
|
|
|
assert_nothing_raised { RubyVM::InstructionSequence.disasm(method(:to_s).to_proc) }
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new
|
|
|
|
c1 = Class.new
|
|
|
|
c1.class_eval { def foo; :foo; end }
|
|
|
|
c2 = Class.new(c1)
|
|
|
|
c2.class_eval { private :foo }
|
|
|
|
o = c2.new
|
|
|
|
o.extend(Module.new)
|
|
|
|
assert_raise(NameError) { o.method(:bar) }
|
|
|
|
assert_raise(NameError) { o.public_method(:foo) }
|
|
|
|
assert_equal(:foo, o.method(:foo).call)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eq
|
|
|
|
o = Object.new
|
|
|
|
class << o
|
|
|
|
def foo; end
|
|
|
|
alias bar foo
|
|
|
|
def baz; end
|
|
|
|
end
|
|
|
|
assert_not_equal(o.method(:foo), nil)
|
|
|
|
m = o.method(:foo)
|
|
|
|
def m.foo; end
|
|
|
|
assert_not_equal(o.method(:foo), m)
|
|
|
|
assert_equal(o.method(:foo), o.method(:foo))
|
|
|
|
assert_equal(o.method(:foo), o.method(:bar))
|
|
|
|
assert_not_equal(o.method(:foo), o.method(:baz))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
o = Object.new
|
|
|
|
def o.foo; end
|
|
|
|
assert_kind_of(Integer, o.method(:foo).hash)
|
2012-02-20 19:13:44 -05:00
|
|
|
assert_equal(Array.instance_method(:map).hash, Array.instance_method(:collect).hash)
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
|
|
|
|
2013-12-13 10:18:12 -05:00
|
|
|
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
|
|
|
|
|
2013-12-13 23:02:00 -05:00
|
|
|
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
|
|
|
|
|
2008-02-13 10:03:59 -05:00
|
|
|
def test_receiver_name_owner
|
|
|
|
o = Object.new
|
|
|
|
def o.foo; end
|
|
|
|
m = o.method(:foo)
|
|
|
|
assert_equal(o, m.receiver)
|
2008-04-27 22:12:21 -04:00
|
|
|
assert_equal(:foo, m.name)
|
2008-02-13 10:03:59 -05:00
|
|
|
assert_equal(class << o; self; end, m.owner)
|
2008-05-11 06:53:52 -04:00
|
|
|
assert_equal(:foo, m.unbind.name)
|
2008-05-10 21:40:30 -04:00
|
|
|
assert_equal(class << o; self; end, m.unbind.owner)
|
2013-02-13 04:10:12 -05:00
|
|
|
class << o
|
|
|
|
alias bar foo
|
|
|
|
end
|
|
|
|
m = o.method(:bar)
|
|
|
|
assert_equal(:bar, m.name)
|
2013-02-13 04:12:04 -05:00
|
|
|
assert_equal(:foo, m.original_name)
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_instance_method
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def foo; :foo; end
|
|
|
|
private :foo
|
|
|
|
end
|
|
|
|
o = c.new
|
|
|
|
o.method(:foo).unbind
|
|
|
|
assert_raise(NoMethodError) { o.foo }
|
|
|
|
c.instance_method(:foo).bind(o)
|
|
|
|
assert_equal(:foo, o.instance_eval { foo })
|
|
|
|
assert_raise(NameError) { c.public_instance_method(:foo) }
|
|
|
|
def o.bar; end
|
|
|
|
m = o.method(:bar).unbind
|
|
|
|
assert_raise(TypeError) { m.bind(Object.new) }
|
2012-12-12 23:06:40 -05:00
|
|
|
|
|
|
|
feature4254 = '[ruby-core:34267]'
|
|
|
|
m = M.instance_method(:meth)
|
|
|
|
assert_equal(:meth, m.bind(Object.new).call, feature4254)
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_define_method
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval { def foo; :foo; end }
|
|
|
|
o = c.new
|
|
|
|
def o.bar; :bar; end
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
c.class_eval { define_method(:foo, :foo) }
|
|
|
|
end
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
c.class_eval { define_method }
|
|
|
|
end
|
|
|
|
c2 = Class.new(c)
|
|
|
|
c2.class_eval { define_method(:baz, o.method(:foo)) }
|
|
|
|
assert_equal(:foo, c2.new.baz)
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Class.new.class_eval { define_method(:foo, o.method(:foo)) }
|
|
|
|
end
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Class.new.class_eval { define_method(:bar, o.method(:bar)) }
|
|
|
|
end
|
2013-03-31 04:18:04 -04:00
|
|
|
end
|
2008-02-13 10:03:59 -05:00
|
|
|
|
2013-03-31 04:18:04 -04:00
|
|
|
def test_define_singleton_method
|
2008-02-13 10:03:59 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
o.instance_eval { define_singleton_method(:foo) { :foo } }
|
|
|
|
assert_equal(:foo, o.foo)
|
2010-01-22 10:03:32 -05:00
|
|
|
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Class.new.class_eval { define_method(:foo, Object.new) }
|
|
|
|
end
|
2011-01-10 00:32:59 -05:00
|
|
|
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
Module.new.module_eval {define_method(:foo, Base.instance_method(:foo))}
|
|
|
|
end
|
2013-03-31 04:18:04 -04:00
|
|
|
end
|
2011-01-10 00:32:59 -05:00
|
|
|
|
2013-08-04 21:02:22 -04:00
|
|
|
def test_define_singleton_method_with_extended_method
|
|
|
|
bug8686 = "[ruby-core:56174]"
|
|
|
|
|
|
|
|
m = Module.new do
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def a
|
|
|
|
"a"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-25 06:11:42 -05:00
|
|
|
assert_nothing_raised(bug8686) do
|
2013-08-04 21:02:22 -04:00
|
|
|
m.define_singleton_method(:a, m.method(:a))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-31 04:18:04 -04:00
|
|
|
def test_define_method_transplating
|
2012-06-25 03:57:42 -04:00
|
|
|
feature4254 = '[ruby-core:34267]'
|
|
|
|
m = Module.new {define_method(:meth, M.instance_method(:meth))}
|
|
|
|
assert_equal(:meth, Object.new.extend(m).meth, feature4254)
|
|
|
|
c = Class.new {define_method(:meth, M.instance_method(:meth))}
|
|
|
|
assert_equal(:meth, c.new.meth, feature4254)
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
|
|
|
|
2013-03-31 04:18:09 -04:00
|
|
|
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
|
|
|
|
|
2013-12-24 02:28:11 -05:00
|
|
|
def test_define_method_in_private_scope
|
|
|
|
bug9005 = '[ruby-core:57747] [Bug #9005]'
|
|
|
|
c = Class.new
|
|
|
|
class << c
|
|
|
|
public :define_method
|
|
|
|
end
|
2013-12-24 04:19:11 -05:00
|
|
|
TOPLEVEL_BINDING.eval("proc{|c|c.define_method(:x) {|x|throw x}}").call(c)
|
2013-12-24 02:28:11 -05:00
|
|
|
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
|
|
|
|
|
2011-06-25 11:05:37 -04:00
|
|
|
def test_super_in_proc_from_define_method
|
|
|
|
c1 = Class.new {
|
|
|
|
def m
|
|
|
|
:m1
|
|
|
|
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
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-02-13 10:03:59 -05:00
|
|
|
def test_clone
|
|
|
|
o = Object.new
|
|
|
|
def o.foo; :foo; end
|
|
|
|
m = o.method(:foo)
|
|
|
|
def m.bar; :bar; end
|
|
|
|
assert_equal(:foo, m.clone.call)
|
|
|
|
assert_equal(:bar, m.clone.bar)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
o = Object.new
|
|
|
|
def o.foo; end
|
|
|
|
m = o.method(:foo)
|
|
|
|
assert_equal("#<Method: #{ o.inspect }.foo>", m.inspect)
|
|
|
|
m = o.method(:foo)
|
|
|
|
assert_equal("#<UnboundMethod: #{ class << o; self; end.inspect }#foo>", m.unbind.inspect)
|
|
|
|
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval { def foo; end; }
|
|
|
|
m = c.new.method(:foo)
|
|
|
|
assert_equal("#<Method: #{ c.inspect }#foo>", m.inspect)
|
|
|
|
m = c.instance_method(:foo)
|
|
|
|
assert_equal("#<UnboundMethod: #{ c.inspect }#foo>", m.inspect)
|
|
|
|
|
|
|
|
c2 = Class.new(c)
|
|
|
|
c2.class_eval { private :foo }
|
|
|
|
m2 = c2.new.method(:foo)
|
|
|
|
assert_equal("#<Method: #{ c2.inspect }(#{ c.inspect })#foo>", m2.inspect)
|
2013-02-13 04:10:12 -05:00
|
|
|
|
|
|
|
bug7806 = '[ruby-core:52048] [Bug #7806]'
|
|
|
|
c3 = Class.new(c)
|
|
|
|
c3.class_eval { alias bar foo }
|
|
|
|
m3 = c3.new.method(:bar)
|
2013-12-13 08:29:23 -05:00
|
|
|
assert_equal("#<Method: #{c3.inspect}(#{c.inspect})#bar(foo)>", m3.inspect, bug7806)
|
2008-02-13 10:03:59 -05:00
|
|
|
end
|
2008-05-14 08:52:17 -04:00
|
|
|
|
|
|
|
def test_callee_top_level
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "p __callee__", %w(nil), [])
|
2008-05-14 08:52:17 -04:00
|
|
|
end
|
|
|
|
|
2010-05-19 07:18:12 -04:00
|
|
|
def test_caller_top_level
|
|
|
|
assert_in_out_err([], "p caller", %w([]), [])
|
|
|
|
end
|
|
|
|
|
2008-05-14 08:52:17 -04:00
|
|
|
def test_caller_negative_level
|
|
|
|
assert_raise(ArgumentError) { caller(-1) }
|
|
|
|
end
|
2008-06-02 12:22:47 -04:00
|
|
|
|
|
|
|
def test_attrset_ivar
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval { attr_accessor :foo }
|
|
|
|
o = c.new
|
|
|
|
o.method(:foo=).call(42)
|
|
|
|
assert_equal(42, o.foo)
|
|
|
|
assert_raise(ArgumentError) { o.method(:foo=).call(1, 2, 3) }
|
|
|
|
assert_raise(ArgumentError) { o.method(:foo).call(1) }
|
|
|
|
end
|
2008-11-22 07:34:21 -05:00
|
|
|
|
|
|
|
def test_default_accessibility
|
2013-12-13 04:18:05 -05:00
|
|
|
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'
|
2008-11-22 07:34:21 -05:00
|
|
|
end
|
2008-11-27 23:19:37 -05:00
|
|
|
|
|
|
|
define_method(:pm0) {||}
|
|
|
|
define_method(:pm1) {|a|}
|
|
|
|
define_method(:pm2) {|a, b|}
|
|
|
|
define_method(:pmo1) {|a = nil, &b|}
|
|
|
|
define_method(:pmo2) {|a, b = nil|}
|
|
|
|
define_method(:pmo3) {|*a|}
|
|
|
|
define_method(:pmo4) {|a, *b, &c|}
|
|
|
|
define_method(:pmo5) {|a, *b, c|}
|
|
|
|
define_method(:pmo6) {|a, *b, c, &d|}
|
|
|
|
define_method(:pmo7) {|a, b = nil, *c, d, &e|}
|
2013-12-25 06:11:42 -05:00
|
|
|
define_method(:pma1) {|(a), &b| nil && a}
|
2013-12-25 06:28:56 -05:00
|
|
|
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|}
|
2008-11-27 23:19:37 -05:00
|
|
|
|
|
|
|
def test_bound_parameters
|
|
|
|
assert_equal([], method(:m0).parameters)
|
|
|
|
assert_equal([[:req, :a]], method(:m1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:req, :b]], method(:m2).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:opt, :a], [:block, :b]], method(:mo1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:opt, :b]], method(:mo2).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:rest, :a]], method(:mo3).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:block, :c]], method(:mo4).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c]], method(:mo5).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:mo6).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:mo7).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:req], [:block, :b]], method(:ma1).parameters)
|
2013-12-25 06:30:15 -05:00
|
|
|
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)
|
2008-11-27 23:19:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unbound_parameters
|
|
|
|
assert_equal([], self.class.instance_method(:m0).parameters)
|
|
|
|
assert_equal([[:req, :a]], self.class.instance_method(:m1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:req, :b]], self.class.instance_method(:m2).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:opt, :a], [:block, :b]], self.class.instance_method(:mo1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:opt, :b]], self.class.instance_method(:mo2).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:rest, :a]], self.class.instance_method(:mo3).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:block, :c]], self.class.instance_method(:mo4).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c]], self.class.instance_method(:mo5).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:mo6).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:mo7).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:req], [:block, :b]], self.class.instance_method(:ma1).parameters)
|
2013-12-25 06:30:15 -05:00
|
|
|
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)
|
2008-11-27 23:19:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bmethod_bound_parameters
|
|
|
|
assert_equal([], method(:pm0).parameters)
|
|
|
|
assert_equal([[:req, :a]], method(:pm1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:req, :b]], method(:pm2).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:opt, :a], [:block, :b]], method(:pmo1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:opt, :b]], method(:pmo2).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:rest, :a]], method(:pmo3).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:block, :c]], method(:pmo4).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c]], method(:pmo5).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:pmo6).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:pmo7).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:req], [:block, :b]], method(:pma1).parameters)
|
2013-12-25 06:30:15 -05:00
|
|
|
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)
|
2008-11-27 23:19:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bmethod_unbound_parameters
|
|
|
|
assert_equal([], self.class.instance_method(:pm0).parameters)
|
|
|
|
assert_equal([[:req, :a]], self.class.instance_method(:pm1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:req, :b]], self.class.instance_method(:pm2).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:opt, :a], [:block, :b]], self.class.instance_method(:pmo1).parameters)
|
|
|
|
assert_equal([[:req, :a], [:opt, :b]], self.class.instance_method(:pmo2).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:rest, :a]], self.class.instance_method(:pmo3).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:block, :c]], self.class.instance_method(:pmo4).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c]], self.class.instance_method(:pmo5).parameters)
|
|
|
|
assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:pmo6).parameters)
|
2009-02-13 04:10:28 -05:00
|
|
|
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:pmo7).parameters)
|
2008-11-27 23:19:37 -05:00
|
|
|
assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters)
|
2013-12-25 06:30:15 -05:00
|
|
|
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)
|
2008-11-27 23:19:37 -05:00
|
|
|
end
|
2010-01-08 09:40:38 -05:00
|
|
|
|
|
|
|
def test_public_method_with_zsuper_method
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def foo
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
private :foo
|
|
|
|
end
|
|
|
|
d = Class.new(c)
|
|
|
|
d.class_eval do
|
|
|
|
public :foo
|
|
|
|
end
|
|
|
|
assert_equal(:ok, d.new.public_method(:foo).call)
|
|
|
|
end
|
2010-06-11 22:58:47 -04:00
|
|
|
|
|
|
|
def test_public_methods_with_extended
|
|
|
|
m = Module.new do def m1; end end
|
|
|
|
a = Class.new do def a; end end
|
|
|
|
bug = '[ruby-dev:41553]'
|
|
|
|
obj = a.new
|
|
|
|
assert_equal([:a], obj.public_methods(false), bug)
|
|
|
|
obj.extend(m)
|
|
|
|
assert_equal([:m1, :a], obj.public_methods(false), bug)
|
|
|
|
end
|
2010-07-05 03:11:44 -04:00
|
|
|
|
|
|
|
def test_visibility
|
|
|
|
assert_equal('method', defined?(mv1))
|
|
|
|
assert_equal('method', defined?(mv2))
|
|
|
|
assert_equal('method', defined?(mv3))
|
|
|
|
|
|
|
|
assert_equal('method', defined?(self.mv1))
|
2010-07-07 00:06:32 -04:00
|
|
|
assert_equal(nil, defined?(self.mv2))
|
2010-07-05 03:11:44 -04:00
|
|
|
assert_equal('method', defined?(self.mv3))
|
|
|
|
|
|
|
|
assert_equal(true, respond_to?(:mv1))
|
|
|
|
assert_equal(false, respond_to?(:mv2))
|
2012-02-13 09:02:26 -05:00
|
|
|
assert_equal(false, respond_to?(:mv3))
|
2010-07-05 03:11:44 -04:00
|
|
|
|
2010-07-07 00:06:32 -04:00
|
|
|
assert_equal(true, respond_to?(:mv1, true))
|
|
|
|
assert_equal(true, respond_to?(:mv2, true))
|
|
|
|
assert_equal(true, respond_to?(:mv3, true))
|
|
|
|
|
2010-07-05 03:11:44 -04:00
|
|
|
assert_nothing_raised { mv1 }
|
|
|
|
assert_nothing_raised { mv2 }
|
|
|
|
assert_nothing_raised { mv3 }
|
|
|
|
|
|
|
|
assert_nothing_raised { self.mv1 }
|
|
|
|
assert_raise(NoMethodError) { self.mv2 }
|
|
|
|
assert_nothing_raised { self.mv3 }
|
|
|
|
|
|
|
|
v = Visibility.new
|
|
|
|
|
|
|
|
assert_equal('method', defined?(v.mv1))
|
2010-07-07 00:06:32 -04:00
|
|
|
assert_equal(nil, defined?(v.mv2))
|
|
|
|
assert_equal(nil, defined?(v.mv3))
|
2010-07-05 03:11:44 -04:00
|
|
|
|
|
|
|
assert_equal(true, v.respond_to?(:mv1))
|
|
|
|
assert_equal(false, v.respond_to?(:mv2))
|
2012-02-13 09:02:26 -05:00
|
|
|
assert_equal(false, v.respond_to?(:mv3))
|
2010-07-05 03:11:44 -04:00
|
|
|
|
2010-07-07 00:06:32 -04:00
|
|
|
assert_equal(true, v.respond_to?(:mv1, true))
|
|
|
|
assert_equal(true, v.respond_to?(:mv2, true))
|
|
|
|
assert_equal(true, v.respond_to?(:mv3, true))
|
|
|
|
|
2010-07-05 03:11:44 -04:00
|
|
|
assert_nothing_raised { v.mv1 }
|
|
|
|
assert_raise(NoMethodError) { v.mv2 }
|
|
|
|
assert_raise(NoMethodError) { v.mv3 }
|
|
|
|
|
|
|
|
assert_nothing_raised { v.__send__(:mv1) }
|
|
|
|
assert_nothing_raised { v.__send__(:mv2) }
|
|
|
|
assert_nothing_raised { v.__send__(:mv3) }
|
|
|
|
|
|
|
|
assert_nothing_raised { v.instance_eval { mv1 } }
|
|
|
|
assert_nothing_raised { v.instance_eval { mv2 } }
|
|
|
|
assert_nothing_raised { v.instance_eval { mv3 } }
|
|
|
|
end
|
2012-03-18 04:17:39 -04:00
|
|
|
|
|
|
|
def test_bound_method_entry
|
|
|
|
bug6171 = '[ruby-core:43383]'
|
|
|
|
assert_ruby_status([], <<-EOC, bug6171)
|
|
|
|
class Bug6171
|
|
|
|
def initialize(target)
|
|
|
|
define_singleton_method(:reverse, target.method(:reverse).to_proc)
|
|
|
|
end
|
|
|
|
end
|
2013-03-05 07:30:27 -05:00
|
|
|
100.times {p = Bug6171.new('test'); 1000.times {p.reverse}}
|
2012-03-18 04:17:39 -04:00
|
|
|
EOC
|
|
|
|
end
|
2012-11-02 21:37:50 -04:00
|
|
|
|
2015-01-06 15:56:49 -05:00
|
|
|
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
|
|
|
|
|
2012-11-02 21:37:50 -04:00
|
|
|
def test___dir__
|
|
|
|
assert_instance_of String, __dir__
|
2013-06-14 19:37:33 -04:00
|
|
|
assert_equal(File.dirname(File.realpath(__FILE__)), __dir__)
|
2013-06-14 19:37:35 -04:00
|
|
|
bug8436 = '[ruby-core:55123] [Bug #8436]'
|
|
|
|
assert_equal(__dir__, eval("__dir__", binding), bug8436)
|
2013-07-21 20:48:38 -04:00
|
|
|
bug8662 = '[ruby-core:56099] [Bug #8662]'
|
|
|
|
assert_equal("arbitrary", eval("__dir__", binding, "arbitrary/file.rb"), bug8662)
|
2014-03-18 10:06:34 -04:00
|
|
|
assert_equal("arbitrary", Object.new.instance_eval("__dir__", "arbitrary/file.rb"), bug8662)
|
2012-11-02 21:37:50 -04:00
|
|
|
end
|
2012-12-27 21:23:11 -05:00
|
|
|
|
|
|
|
def test_alias_owner
|
|
|
|
bug7613 = '[ruby-core:51105]'
|
2013-12-13 08:29:23 -05:00
|
|
|
bug7993 = '[Bug #7993]'
|
2012-12-27 21:23:11 -05:00
|
|
|
c = Class.new {
|
|
|
|
def foo
|
|
|
|
end
|
2013-12-13 08:29:23 -05:00
|
|
|
prepend Module.new
|
|
|
|
attr_reader :zot
|
2012-12-27 21:23:11 -05:00
|
|
|
}
|
|
|
|
x = c.new
|
|
|
|
class << x
|
|
|
|
alias bar foo
|
|
|
|
end
|
2013-12-13 08:29:23 -05:00
|
|
|
assert_equal(c, c.instance_method(:foo).owner)
|
2012-12-27 21:23:11 -05:00
|
|
|
assert_equal(c, x.method(:foo).owner)
|
|
|
|
assert_equal(x.singleton_class, x.method(:bar).owner)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_equal(x.method(:foo), x.method(:bar), bug7613)
|
2013-12-13 08:29:23 -05:00
|
|
|
assert_equal(c, x.method(:zot).owner, bug7993)
|
|
|
|
assert_equal(c, c.instance_method(:zot).owner, bug7993)
|
2012-12-27 21:23:11 -05:00
|
|
|
end
|
2013-02-13 04:38:49 -05:00
|
|
|
|
|
|
|
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]'
|
2013-03-05 06:29:27 -05:00
|
|
|
bug7988 = '[ruby-core:53038] [Bug #7988]'
|
2013-02-13 04:38:49 -05:00
|
|
|
m = Module.new {
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
}
|
|
|
|
c = Class.new {
|
|
|
|
def foo
|
|
|
|
end
|
|
|
|
prepend m
|
|
|
|
}
|
2013-03-05 06:29:27 -05:00
|
|
|
assert_raise(NameError, bug7988) {Module.new{prepend m}.instance_method(:bar)}
|
2013-12-25 06:11:42 -05:00
|
|
|
true || c || bug7836
|
2013-02-13 04:38:49 -05:00
|
|
|
end
|
2013-02-16 02:41:55 -05:00
|
|
|
|
|
|
|
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
|
2013-03-25 01:39:32 -04:00
|
|
|
|
|
|
|
def test_unlinked_method_entry_in_method_object_bug
|
|
|
|
bug8100 = '[ruby-core:53640] [Bug #8100]'
|
2013-03-25 05:03:54 -04:00
|
|
|
begin
|
|
|
|
assert_normal_exit %q{
|
2013-03-25 01:39:32 -04:00
|
|
|
loop do
|
|
|
|
def x
|
|
|
|
"hello" * 1000
|
|
|
|
end
|
|
|
|
method(:x).call
|
|
|
|
end
|
2013-03-25 05:03:54 -04:00
|
|
|
}, bug8100, timeout: 2
|
|
|
|
rescue Timeout::Error => e
|
|
|
|
else
|
|
|
|
end
|
|
|
|
assert_raise(Timeout::Error, bug8100) {raise e if e}
|
2013-03-25 01:39:32 -04:00
|
|
|
end
|
2013-05-13 01:52:03 -04:00
|
|
|
|
|
|
|
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
|
2014-06-19 00:42:16 -04:00
|
|
|
|
|
|
|
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) {|a,b,c| a + b + c}
|
|
|
|
}
|
|
|
|
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
|
2014-07-26 12:22:41 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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
|
2014-11-02 13:40:45 -05:00
|
|
|
|
|
|
|
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
|
2014-11-25 13:44:07 -05:00
|
|
|
|
|
|
|
def test_insecure_method
|
|
|
|
m = "\u{5371 967a}"
|
|
|
|
c = Class.new do
|
|
|
|
proc {$SAFE=3;def foo;end}.call
|
|
|
|
alias_method m, "foo"
|
|
|
|
eval "def bar; #{m}; end"
|
|
|
|
end
|
|
|
|
obj = c.new
|
|
|
|
assert_raise_with_message(SecurityError, /#{m}/) do
|
|
|
|
obj.bar
|
|
|
|
end
|
|
|
|
end
|
2015-03-28 22:51:34 -04:00
|
|
|
|
2015-05-21 04:45:57 -04:00
|
|
|
class C
|
|
|
|
D = "Const_D"
|
|
|
|
def foo
|
|
|
|
a = b = c = 12345
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-28 22:51:34 -04:00
|
|
|
def test_to_proc_binding
|
|
|
|
bug11012 = '[ruby-core:68673] [Bug #11012]'
|
|
|
|
|
2015-05-21 04:45:57 -04:00
|
|
|
b = C.new.method(:foo).to_proc.binding
|
|
|
|
assert_equal([], b.local_variables)
|
|
|
|
assert_equal("Const_D", b.eval("D")) # Check CREF
|
|
|
|
|
|
|
|
assert_raise(NameError){ b.local_variable_get(:foo) }
|
|
|
|
assert_equal(123, b.local_variable_set(:foo, 123))
|
|
|
|
assert_equal(123, b.local_variable_get(:foo))
|
|
|
|
assert_equal(456, b.local_variable_set(:bar, 456))
|
|
|
|
assert_equal(123, b.local_variable_get(:foo))
|
|
|
|
assert_equal(456, b.local_variable_get(:bar))
|
2015-03-28 22:51:34 -04:00
|
|
|
end
|
2005-08-08 07:43:56 -04:00
|
|
|
end
|