2004-11-18 03:06:09 -05:00
|
|
|
require 'test/unit'
|
2012-09-24 20:59:29 -04:00
|
|
|
require_relative 'envutil'
|
2004-11-18 03:06:09 -05:00
|
|
|
|
|
|
|
class TestSuper < Test::Unit::TestCase
|
|
|
|
class Base
|
|
|
|
def single(a) a end
|
|
|
|
def double(a, b) [a,b] end
|
|
|
|
def array(*a) a end
|
2005-08-07 10:57:19 -04:00
|
|
|
def optional(a = 0) a end
|
2013-03-07 00:21:40 -05:00
|
|
|
def keyword(**a) a end
|
2004-11-18 03:06:09 -05:00
|
|
|
end
|
|
|
|
class Single1 < Base
|
|
|
|
def single(*) super end
|
|
|
|
end
|
|
|
|
class Single2 < Base
|
|
|
|
def single(a,*) super end
|
|
|
|
end
|
|
|
|
class Double1 < Base
|
|
|
|
def double(*) super end
|
|
|
|
end
|
|
|
|
class Double2 < Base
|
|
|
|
def double(a,*) super end
|
|
|
|
end
|
|
|
|
class Double3 < Base
|
|
|
|
def double(a,b,*) super end
|
|
|
|
end
|
|
|
|
class Array1 < Base
|
|
|
|
def array(*) super end
|
|
|
|
end
|
|
|
|
class Array2 < Base
|
|
|
|
def array(a,*) super end
|
|
|
|
end
|
|
|
|
class Array3 < Base
|
|
|
|
def array(a,b,*) super end
|
|
|
|
end
|
|
|
|
class Array4 < Base
|
|
|
|
def array(a,b,c,*) super end
|
|
|
|
end
|
2005-08-07 10:57:19 -04:00
|
|
|
class Optional1 < Base
|
|
|
|
def optional(a = 1) super end
|
|
|
|
end
|
|
|
|
class Optional2 < Base
|
|
|
|
def optional(a, b = 1) super end
|
|
|
|
end
|
|
|
|
class Optional3 < Base
|
|
|
|
def single(a = 1) super end
|
|
|
|
end
|
2007-05-10 02:17:07 -04:00
|
|
|
class Optional4 < Base
|
|
|
|
def array(a = 1, *) super end
|
|
|
|
end
|
|
|
|
class Optional5 < Base
|
|
|
|
def array(a = 1, b = 2, *) super end
|
|
|
|
end
|
2013-03-07 00:21:40 -05:00
|
|
|
class Keyword1 < Base
|
|
|
|
def keyword(foo: "keyword1") super end
|
|
|
|
end
|
|
|
|
class Keyword2 < Base
|
|
|
|
def keyword(foo: "keyword2")
|
|
|
|
foo = "changed1"
|
|
|
|
x = super
|
|
|
|
foo = "changed2"
|
|
|
|
y = super
|
|
|
|
[x, y]
|
|
|
|
end
|
|
|
|
end
|
2004-11-18 03:06:09 -05:00
|
|
|
|
|
|
|
def test_single1
|
|
|
|
assert_equal(1, Single1.new.single(1))
|
|
|
|
end
|
|
|
|
def test_single2
|
|
|
|
assert_equal(1, Single2.new.single(1))
|
|
|
|
end
|
|
|
|
def test_double1
|
|
|
|
assert_equal([1, 2], Double1.new.double(1, 2))
|
|
|
|
end
|
|
|
|
def test_double2
|
|
|
|
assert_equal([1, 2], Double2.new.double(1, 2))
|
|
|
|
end
|
|
|
|
def test_double3
|
|
|
|
assert_equal([1, 2], Double3.new.double(1, 2))
|
|
|
|
end
|
|
|
|
def test_array1
|
|
|
|
assert_equal([], Array1.new.array())
|
|
|
|
assert_equal([1], Array1.new.array(1))
|
|
|
|
end
|
|
|
|
def test_array2
|
|
|
|
assert_equal([1], Array2.new.array(1))
|
|
|
|
assert_equal([1,2], Array2.new.array(1, 2))
|
|
|
|
end
|
|
|
|
def test_array3
|
|
|
|
assert_equal([1,2], Array3.new.array(1, 2))
|
|
|
|
assert_equal([1,2,3], Array3.new.array(1, 2, 3))
|
|
|
|
end
|
|
|
|
def test_array4
|
|
|
|
assert_equal([1,2,3], Array4.new.array(1, 2, 3))
|
|
|
|
assert_equal([1,2,3,4], Array4.new.array(1, 2, 3, 4))
|
|
|
|
end
|
2005-08-07 10:57:19 -04:00
|
|
|
def test_optional1
|
|
|
|
assert_equal(9, Optional1.new.optional(9))
|
|
|
|
assert_equal(1, Optional1.new.optional)
|
|
|
|
end
|
|
|
|
def test_optional2
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
# call Base#optional with 2 arguments; the 2nd arg is supplied
|
|
|
|
assert_equal(9, Optional2.new.optional(9))
|
|
|
|
end
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
# call Base#optional with 2 arguments
|
|
|
|
assert_equal(9, Optional2.new.optional(9, 2))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def test_optional3
|
|
|
|
assert_equal(9, Optional3.new.single(9))
|
|
|
|
# call Base#single with 1 argument; the arg is supplied
|
|
|
|
assert_equal(1, Optional3.new.single)
|
|
|
|
end
|
2007-05-10 02:17:07 -04:00
|
|
|
def test_optional4
|
|
|
|
assert_equal([1], Optional4.new.array)
|
|
|
|
assert_equal([9], Optional4.new.array(9))
|
|
|
|
assert_equal([9, 8], Optional4.new.array(9, 8))
|
|
|
|
end
|
|
|
|
def test_optional5
|
|
|
|
assert_equal([1, 2], Optional5.new.array)
|
|
|
|
assert_equal([9, 2], Optional5.new.array(9))
|
|
|
|
assert_equal([9, 8], Optional5.new.array(9, 8))
|
|
|
|
assert_equal([9, 8, 7], Optional5.new.array(9, 8, 7))
|
|
|
|
end
|
2013-03-07 00:21:40 -05:00
|
|
|
def test_keyword1
|
|
|
|
assert_equal({foo: "keyword1"}, Keyword1.new.keyword)
|
|
|
|
bug8008 = '[ruby-core:53114] [Bug #8008]'
|
|
|
|
assert_equal({foo: bug8008}, Keyword1.new.keyword(foo: bug8008))
|
|
|
|
end
|
|
|
|
def test_keyword2
|
|
|
|
assert_equal([{foo: "changed1"}, {foo: "changed2"}], Keyword2.new.keyword)
|
|
|
|
end
|
2004-12-07 13:26:12 -05:00
|
|
|
|
|
|
|
class A
|
|
|
|
def tt(aa)
|
|
|
|
"A#tt"
|
|
|
|
end
|
|
|
|
|
|
|
|
def uu(a)
|
|
|
|
class << self
|
|
|
|
define_method(:tt) do |sym|
|
2007-06-24 09:05:51 -04:00
|
|
|
super(sym)
|
2004-12-07 13:26:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_define_method
|
2004-12-07 13:26:12 -05:00
|
|
|
a = A.new
|
|
|
|
a.uu(12)
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal("A#tt", a.tt(12), "[ruby-core:3856]")
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(RuntimeError, /implicit argument passing of super from method defined by define_method/, "[ruby-core:24244]") {
|
2009-07-09 21:25:26 -04:00
|
|
|
lambda {
|
2012-08-06 03:00:19 -04:00
|
|
|
Class.new {
|
|
|
|
define_method(:a) {super}
|
|
|
|
}.new.a
|
2009-07-09 21:25:26 -04:00
|
|
|
}.call
|
|
|
|
}
|
2004-12-07 13:26:12 -05:00
|
|
|
end
|
2010-08-21 10:38:17 -04:00
|
|
|
|
|
|
|
class SubSeq
|
|
|
|
def initialize
|
|
|
|
@first=11
|
|
|
|
@first or fail
|
|
|
|
end
|
|
|
|
|
2011-05-15 07:55:52 -04:00
|
|
|
def subseq
|
2010-08-21 10:38:17 -04:00
|
|
|
@first or fail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Indexed
|
|
|
|
def subseq
|
|
|
|
SubSeq.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Overlaid = proc do
|
|
|
|
class << self
|
|
|
|
def subseq
|
|
|
|
super.instance_eval(& Overlaid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_overlaid
|
|
|
|
assert_nothing_raised('[ruby-dev:40959]') do
|
|
|
|
overlaid = proc do |obj|
|
|
|
|
def obj.reverse
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
overlaid.call(str = "123")
|
|
|
|
overlaid.call(ary = [1,2,3])
|
|
|
|
str.reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised('[ruby-core:27230]') do
|
|
|
|
mid=Indexed.new
|
|
|
|
mid.instance_eval(&Overlaid)
|
|
|
|
mid.subseq
|
|
|
|
mid.subseq
|
|
|
|
end
|
|
|
|
end
|
2012-08-03 06:27:21 -04:00
|
|
|
|
|
|
|
module DoubleInclude
|
|
|
|
class Base
|
|
|
|
def foo
|
|
|
|
[:Base]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Override
|
|
|
|
def foo
|
|
|
|
super << :Override
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class A < Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class B < A
|
|
|
|
end
|
|
|
|
|
|
|
|
B.send(:include, Override)
|
|
|
|
A.send(:include, Override)
|
|
|
|
end
|
|
|
|
|
|
|
|
# [Bug #3351]
|
|
|
|
def test_double_include
|
2012-08-03 12:32:52 -04:00
|
|
|
assert_equal([:Base, :Override], DoubleInclude::B.new.foo)
|
|
|
|
# should be changed as follows?
|
|
|
|
# assert_equal([:Base, :Override, :Override], DoubleInclude::B.new.foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
module DoubleInclude2
|
|
|
|
class Base
|
|
|
|
def foo
|
|
|
|
[:Base]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Override
|
|
|
|
def foo
|
|
|
|
super << :Override
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class A < Base
|
|
|
|
def foo
|
|
|
|
super << :A
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class B < A
|
|
|
|
def foo
|
|
|
|
super << :B
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
B.send(:include, Override)
|
|
|
|
A.send(:include, Override)
|
|
|
|
end
|
|
|
|
|
2012-08-03 12:54:19 -04:00
|
|
|
def test_double_include2
|
2012-08-03 12:32:52 -04:00
|
|
|
assert_equal([:Base, :Override, :A, :Override, :B],
|
|
|
|
DoubleInclude2::B.new.foo)
|
2012-08-03 06:27:21 -04:00
|
|
|
end
|
2012-08-06 03:00:19 -04:00
|
|
|
|
|
|
|
def test_super_in_instance_eval
|
|
|
|
super_class = Class.new {
|
|
|
|
def foo
|
|
|
|
return [:super, self]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sub_class = Class.new(super_class) {
|
|
|
|
def foo
|
|
|
|
x = Object.new
|
|
|
|
x.instance_eval do
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
obj = sub_class.new
|
2013-01-10 02:51:35 -05:00
|
|
|
assert_raise(TypeError) do
|
2012-08-23 00:00:25 -04:00
|
|
|
obj.foo
|
|
|
|
end
|
2012-08-06 03:00:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_super_in_instance_eval_with_define_method
|
|
|
|
super_class = Class.new {
|
|
|
|
def foo
|
|
|
|
return [:super, self]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sub_class = Class.new(super_class) {
|
|
|
|
define_method(:foo) do
|
|
|
|
x = Object.new
|
|
|
|
x.instance_eval do
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
obj = sub_class.new
|
2013-01-10 02:51:35 -05:00
|
|
|
assert_raise(TypeError) do
|
2012-08-23 00:00:25 -04:00
|
|
|
obj.foo
|
|
|
|
end
|
2012-08-06 03:00:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_super_in_orphan_block
|
|
|
|
super_class = Class.new {
|
|
|
|
def foo
|
|
|
|
return [:super, self]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sub_class = Class.new(super_class) {
|
|
|
|
def foo
|
|
|
|
x = Object.new
|
|
|
|
lambda { super() }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
obj = sub_class.new
|
2012-08-23 00:00:25 -04:00
|
|
|
assert_equal([:super, obj], obj.foo.call)
|
2012-08-06 03:00:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_super_in_orphan_block_with_instance_eval
|
|
|
|
super_class = Class.new {
|
|
|
|
def foo
|
|
|
|
return [:super, self]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sub_class = Class.new(super_class) {
|
|
|
|
def foo
|
|
|
|
x = Object.new
|
|
|
|
x.instance_eval do
|
|
|
|
lambda { super() }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
obj = sub_class.new
|
2013-01-10 02:51:35 -05:00
|
|
|
assert_raise(TypeError) do
|
2012-08-06 03:00:19 -04:00
|
|
|
obj.foo.call
|
|
|
|
end
|
|
|
|
end
|
2012-08-22 12:51:08 -04:00
|
|
|
|
|
|
|
def test_yielding_super
|
|
|
|
a = Class.new { def yielder; yield; end }
|
|
|
|
x = Class.new { define_singleton_method(:hello) { 'hi' } }
|
|
|
|
y = Class.new(x) {
|
|
|
|
define_singleton_method(:hello) {
|
|
|
|
m = a.new
|
|
|
|
m.yielder { super() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_equal 'hi', y.hello
|
|
|
|
end
|
2012-08-23 00:00:25 -04:00
|
|
|
|
|
|
|
def test_super_in_thread
|
|
|
|
hoge = Class.new {
|
|
|
|
def bar; 'hoge'; end
|
|
|
|
}
|
|
|
|
foo = Class.new(hoge) {
|
|
|
|
def bar; Thread.new { super }.join.value; end
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal 'hoge', foo.new.bar
|
|
|
|
end
|
2012-09-24 20:59:29 -04:00
|
|
|
|
2012-09-24 21:10:32 -04:00
|
|
|
def assert_super_in_block(type)
|
2012-09-24 20:59:29 -04:00
|
|
|
bug7064 = '[ruby-core:47680]'
|
2012-09-24 21:10:32 -04:00
|
|
|
assert_normal_exit "#{type} {super}", bug7064
|
2012-09-24 20:59:29 -04:00
|
|
|
end
|
|
|
|
|
2012-09-24 21:10:32 -04:00
|
|
|
def test_super_in_at_exit
|
|
|
|
assert_super_in_block("at_exit")
|
|
|
|
end
|
2012-09-24 20:59:29 -04:00
|
|
|
def test_super_in_END
|
2012-09-24 21:10:32 -04:00
|
|
|
assert_super_in_block("END")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_super_in_BEGIN
|
|
|
|
assert_super_in_block("BEGIN")
|
2012-09-24 20:59:29 -04:00
|
|
|
end
|
2013-09-02 14:21:13 -04:00
|
|
|
|
|
|
|
class X
|
|
|
|
def foo(*args)
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Y < X
|
|
|
|
define_method(:foo) do |*args|
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_super_splat
|
2013-09-07 17:48:40 -04:00
|
|
|
# [ruby-list:49575]
|
2013-09-02 14:21:13 -04:00
|
|
|
y = Y.new
|
|
|
|
assert_equal([1, 2], y.foo(1, 2))
|
|
|
|
assert_equal([1, false], y.foo(1, false))
|
|
|
|
assert_equal([1, 2, 3, 4, 5], y.foo(1, 2, 3, 4, 5))
|
|
|
|
assert_equal([false, true], y.foo(false, true))
|
|
|
|
assert_equal([false, false], y.foo(false, false))
|
|
|
|
assert_equal([1, 2, 3, false, 5], y.foo(1, 2, 3, false, 5))
|
|
|
|
end
|
2013-12-28 18:21:14 -05:00
|
|
|
|
|
|
|
def test_missing_super_in_method_module
|
|
|
|
bug9315 = '[ruby-core:59358] [Bug #9315]'
|
|
|
|
a = Module.new do
|
|
|
|
def foo
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
b = Class.new do
|
|
|
|
include a
|
|
|
|
end
|
|
|
|
assert_raise(NoMethodError, bug9315) do
|
|
|
|
b.new.method(:foo).call
|
|
|
|
end
|
|
|
|
end
|
2014-01-06 09:34:18 -05:00
|
|
|
|
|
|
|
def test_module_super_in_method_module
|
|
|
|
bug9315 = '[ruby-core:59589] [Bug #9315]'
|
|
|
|
a = Module.new do
|
|
|
|
def foo
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
c = Class.new do
|
|
|
|
def foo
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o = c.new.extend(a)
|
|
|
|
assert_nothing_raised(NoMethodError, bug9315) do
|
|
|
|
assert_equal(:ok, o.method(:foo).call, bug9315)
|
|
|
|
end
|
|
|
|
end
|
2014-01-08 08:53:18 -05:00
|
|
|
|
|
|
|
def test_missing_super_in_module_unbound_method
|
|
|
|
bug9377 = '[ruby-core:59619] [Bug #9377]'
|
|
|
|
|
|
|
|
a = Module.new do
|
|
|
|
def foo; super end
|
|
|
|
end
|
|
|
|
|
|
|
|
m = a.instance_method(:foo).bind(Object.new.extend(a))
|
|
|
|
assert_raise(NoMethodError, bug9377) do
|
|
|
|
m.call
|
|
|
|
end
|
|
|
|
end
|
2004-11-18 03:06:09 -05:00
|
|
|
end
|