2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestAlias < Test::Unit::TestCase
|
|
|
|
class Alias0
|
2006-12-31 10:02:22 -05:00
|
|
|
def foo
|
|
|
|
"foo"
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
class Alias1 < Alias0
|
2003-09-04 12:18:59 -04:00
|
|
|
alias bar foo
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
def foo
|
|
|
|
"foo+#{super}"
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
class Alias2 < Alias1
|
2003-09-04 12:18:59 -04:00
|
|
|
alias baz foo
|
|
|
|
undef foo
|
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
|
|
|
class Alias3 < Alias2
|
2003-09-04 12:18:59 -04:00
|
|
|
def foo
|
2006-12-31 10:02:22 -05:00
|
|
|
super
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
def bar
|
2006-12-31 10:02:22 -05:00
|
|
|
super
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
def quux
|
2006-12-31 10:02:22 -05:00
|
|
|
super
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alias
|
|
|
|
x = Alias2.new
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_equal "foo", x.bar
|
|
|
|
assert_equal "foo+foo", x.baz
|
|
|
|
assert_equal "foo+foo", x.baz # test_check for cache
|
2003-09-05 11:15:43 -04:00
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
x = Alias3.new
|
2006-12-31 10:02:22 -05:00
|
|
|
assert_raise(NoMethodError) { x.foo }
|
|
|
|
assert_equal "foo", x.bar
|
|
|
|
assert_raise(NoMethodError) { x.quux }
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2006-12-31 10:02:22 -05:00
|
|
|
class C
|
|
|
|
def m
|
|
|
|
$SAFE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-07-16 04:44:23 -04:00
|
|
|
def test_nonexistmethod
|
|
|
|
assert_raise(NameError){
|
|
|
|
Class.new{
|
|
|
|
alias_method :foobarxyzzy, :barbaz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2009-07-18 22:00:27 -04:00
|
|
|
|
|
|
|
def test_send_alias
|
|
|
|
x = "abc"
|
|
|
|
class << x
|
|
|
|
alias_method :try, :__send__
|
|
|
|
end
|
|
|
|
assert_equal("ABC", x.try(:upcase), '[ruby-dev:38824]')
|
|
|
|
end
|
2009-11-17 09:53:32 -05:00
|
|
|
|
|
|
|
def test_special_const_alias
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
1.instance_eval do
|
|
|
|
alias to_string to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-08 09:10:58 -05:00
|
|
|
|
|
|
|
def test_alias_with_zsuper_method
|
|
|
|
c = Class.new
|
|
|
|
c.class_eval do
|
|
|
|
def foo
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
def bar
|
|
|
|
:ng
|
|
|
|
end
|
|
|
|
private :foo
|
|
|
|
end
|
|
|
|
d = Class.new(c)
|
|
|
|
d.class_eval do
|
|
|
|
public :foo
|
|
|
|
alias bar foo
|
|
|
|
end
|
|
|
|
assert_equal(:ok, d.new.bar)
|
|
|
|
end
|
2012-08-03 21:04:05 -04:00
|
|
|
|
|
|
|
module SuperInAliasedModuleMethod
|
|
|
|
module M
|
|
|
|
def foo
|
|
|
|
super << :M
|
|
|
|
end
|
|
|
|
|
|
|
|
alias bar foo
|
|
|
|
end
|
|
|
|
|
|
|
|
class Base
|
|
|
|
def foo
|
|
|
|
[:Base]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Derived < Base
|
|
|
|
include M
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# [ruby-dev:46028]
|
|
|
|
def test_super_in_aliased_module_method # fails in 1.8
|
|
|
|
assert_equal([:Base, :M], SuperInAliasedModuleMethod::Derived.new.bar)
|
|
|
|
end
|
2013-12-16 02:46:48 -05:00
|
|
|
|
2013-12-23 22:43:19 -05:00
|
|
|
def test_alias_wb_miss
|
2013-12-16 02:46:48 -05:00
|
|
|
assert_normal_exit %q{
|
|
|
|
require 'stringio'
|
|
|
|
GC.verify_internal_consistency
|
|
|
|
GC.start
|
|
|
|
class StringIO
|
|
|
|
alias_method :read_nonblock, :sysread
|
|
|
|
end
|
|
|
|
GC.verify_internal_consistency
|
|
|
|
}
|
|
|
|
end
|
2014-03-19 23:31:28 -04:00
|
|
|
|
|
|
|
def test_cyclic_zsuper
|
|
|
|
bug9475 = '[ruby-core:60431] [Bug #9475]'
|
|
|
|
|
|
|
|
a = Module.new do
|
|
|
|
def foo
|
|
|
|
"A"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
b = Class.new do
|
|
|
|
include a
|
|
|
|
attr_reader :b
|
|
|
|
|
|
|
|
def foo
|
|
|
|
@b ||= 0
|
|
|
|
raise SystemStackError if (@b += 1) > 1
|
|
|
|
# "foo from B"
|
|
|
|
super + "B"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
c = Class.new(b) do
|
|
|
|
alias orig_foo foo
|
|
|
|
|
|
|
|
def foo
|
|
|
|
# "foo from C"
|
|
|
|
orig_foo + "C"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
b.class_eval do
|
|
|
|
alias orig_foo foo
|
|
|
|
attr_reader :b2
|
|
|
|
|
|
|
|
def foo
|
|
|
|
@b2 ||= 0
|
|
|
|
raise SystemStackError if (@b2 += 1) > 1
|
|
|
|
# "foo from B (again)"
|
|
|
|
orig_foo + "B2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised(SystemStackError, bug9475) do
|
|
|
|
assert_equal("ABC", c.new.foo, bug9475)
|
|
|
|
end
|
|
|
|
end
|
2014-03-22 22:03:51 -04:00
|
|
|
|
|
|
|
def test_alias_in_module
|
|
|
|
bug9663 = '[ruby-core:61635] [Bug #9663]'
|
|
|
|
|
|
|
|
assert_separately(['-', bug9663], <<-'end;')
|
|
|
|
bug = ARGV[0]
|
|
|
|
|
|
|
|
m = Module.new do
|
|
|
|
alias orig_to_s to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
o = Object.new.extend(m)
|
|
|
|
assert_equal(o.to_s, o.orig_to_s, bug)
|
|
|
|
end;
|
|
|
|
end
|
2015-05-30 14:45:28 -04:00
|
|
|
|
|
|
|
class C0; def foo; end; end
|
|
|
|
class C1 < C0; alias bar foo; end
|
|
|
|
|
|
|
|
def test_alias_method_equation
|
|
|
|
obj = C1.new
|
|
|
|
assert_equal(obj.method(:bar), obj.method(:foo))
|
|
|
|
assert_equal(obj.method(:foo), obj.method(:bar))
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|