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
|
|
|
|
|
|
|
|
def test_JVN_83768862
|
|
|
|
d = lambda {
|
|
|
|
$SAFE = 4
|
|
|
|
dclass = Class.new(C)
|
2007-11-04 15:36:20 -05:00
|
|
|
dclass.send(:alias_method, :mm, :m)
|
2006-12-31 10:02:22 -05:00
|
|
|
dclass.new
|
|
|
|
}.call
|
|
|
|
assert_raise(SecurityError) { d.mm }
|
2003-09-04 12:18:59 -04:00
|
|
|
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
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|