зеркало из https://github.com/github/ruby.git
* lib/delegate.rb (Delegator): include copy of Kernel.
[ruby-dev:40314] * lib/delegate.rb (Delegator#{dup,clone}): class of copy should be Delegator. [ruby-dev:40313] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26580 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
1c4fe5395d
Коммит
7873c05684
|
@ -1,3 +1,11 @@
|
||||||
|
Fri Feb 5 16:38:08 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/delegate.rb (Delegator): include copy of Kernel.
|
||||||
|
[ruby-dev:40314]
|
||||||
|
|
||||||
|
* lib/delegate.rb (Delegator#{dup,clone}): class of copy should be
|
||||||
|
Delegator. [ruby-dev:40313]
|
||||||
|
|
||||||
Fri Feb 5 09:26:54 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
Fri Feb 5 09:26:54 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* bignum.c (big_op): remove unused variables.
|
* bignum.c (big_op): remove unused variables.
|
||||||
|
|
|
@ -115,15 +115,15 @@
|
||||||
# implementation, see SimpleDelegator.
|
# implementation, see SimpleDelegator.
|
||||||
#
|
#
|
||||||
class Delegator < BasicObject
|
class Delegator < BasicObject
|
||||||
|
kernel = ::Kernel.dup
|
||||||
|
kernel.class_eval do
|
||||||
|
[:to_s,:inspect,:=~,:!~,:===,:<=>].each do |m|
|
||||||
|
undef_method m
|
||||||
|
end
|
||||||
|
end
|
||||||
|
include kernel
|
||||||
|
|
||||||
# :stopdoc:
|
# :stopdoc:
|
||||||
def class
|
|
||||||
(class << self; self; end).superclass
|
|
||||||
end
|
|
||||||
|
|
||||||
def extend(*mods)
|
|
||||||
(class << self; self; end).class_eval {include(*mods)}
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.const_missing(n)
|
def self.const_missing(n)
|
||||||
::Object.const_get(n)
|
::Object.const_get(n)
|
||||||
end
|
end
|
||||||
|
@ -141,7 +141,7 @@ class Delegator < BasicObject
|
||||||
def method_missing(m, *args, &block)
|
def method_missing(m, *args, &block)
|
||||||
begin
|
begin
|
||||||
target = self.__getobj__
|
target = self.__getobj__
|
||||||
unless target.respond_to?(m, true)
|
unless target.respond_to?(m)
|
||||||
super(m, *args, &block)
|
super(m, *args, &block)
|
||||||
else
|
else
|
||||||
target.__send__(m, *args, &block)
|
target.__send__(m, *args, &block)
|
||||||
|
@ -211,9 +211,13 @@ class Delegator < BasicObject
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# clone/dup support for the object returned by \_\_getobj\_\_.
|
# :nodoc:
|
||||||
def initialize_copy(other)
|
def dup
|
||||||
self.__setobj__(other.__getobj__.clone)
|
self.class.new(__getobj__.dup)
|
||||||
|
end
|
||||||
|
# :nodoc:
|
||||||
|
def clone
|
||||||
|
self.class.new(__getobj__.clone)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Freeze self and target at once.
|
# Freeze self and target at once.
|
||||||
|
|
|
@ -23,11 +23,15 @@ class TestDelegateClass < Test::Unit::TestCase
|
||||||
def test_delegateclass_class
|
def test_delegateclass_class
|
||||||
myclass=Myclass.new([])
|
myclass=Myclass.new([])
|
||||||
assert_equal(Myclass,myclass.class)
|
assert_equal(Myclass,myclass.class)
|
||||||
|
assert_equal(Myclass,myclass.dup.class,'[ruby-dev:40313]')
|
||||||
|
assert_equal(Myclass,myclass.clone.class,'[ruby-dev:40313]')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_simpledelegator_class
|
def test_simpledelegator_class
|
||||||
simple=SimpleDelegator.new([])
|
simple=SimpleDelegator.new([])
|
||||||
assert_equal(SimpleDelegator,simple.class)
|
assert_equal(SimpleDelegator,simple.class)
|
||||||
|
assert_equal(SimpleDelegator,simple.dup.class)
|
||||||
|
assert_equal(SimpleDelegator,simple.clone.class)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
|
@ -44,7 +48,6 @@ class TestDelegateClass < Test::Unit::TestCase
|
||||||
def m
|
def m
|
||||||
:m
|
:m
|
||||||
end
|
end
|
||||||
private
|
|
||||||
def delegate_test_m
|
def delegate_test_m
|
||||||
:m
|
:m
|
||||||
end
|
end
|
||||||
|
@ -54,13 +57,16 @@ class TestDelegateClass < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_override
|
def test_override
|
||||||
|
foo = Foo.new
|
||||||
|
foo2 = SimpleDelegator.new(foo)
|
||||||
|
bar = Bar.new(foo)
|
||||||
assert_equal(:o, Object.new.m)
|
assert_equal(:o, Object.new.m)
|
||||||
assert_equal(:m, Foo.new.m)
|
assert_equal(:m, foo.m)
|
||||||
assert_equal(:m, SimpleDelegator.new(Foo.new).m)
|
assert_equal(:m, foo2.m)
|
||||||
assert_equal(:m, Bar.new(Foo.new).m)
|
assert_equal(:m, bar.m)
|
||||||
bug = '[ruby-dev:39154]'
|
bug = '[ruby-dev:39154]'
|
||||||
assert_equal(:m, SimpleDelegator.new(Foo.new).__send__(:delegate_test_m), bug)
|
assert_equal(:m, foo2.send(:delegate_test_m), bug)
|
||||||
assert_equal(:m, Bar.new(Foo.new).__send__(:delegate_test_m), bug)
|
assert_equal(:m, bar.send(:delegate_test_m), bug)
|
||||||
end
|
end
|
||||||
|
|
||||||
class IV < DelegateClass(Integer)
|
class IV < DelegateClass(Integer)
|
||||||
|
@ -85,4 +91,34 @@ class TestDelegateClass < Test::Unit::TestCase
|
||||||
d = SimpleDelegator.new(a)
|
d = SimpleDelegator.new(a)
|
||||||
assert_nothing_raised(bug2679) {d.dup[0] += 1}
|
assert_nothing_raised(bug2679) {d.dup[0] += 1}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_frozen
|
||||||
|
d = SimpleDelegator.new([1, :foo])
|
||||||
|
d.freeze
|
||||||
|
assert_raise(RuntimeError, '[ruby-dev:40314]#1') {d.__setobj__("foo")}
|
||||||
|
assert_equal([1, :foo], d)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_instance_method
|
||||||
|
s = SimpleDelegator.new("foo")
|
||||||
|
m = s.method("upcase")
|
||||||
|
s.__setobj__([1,2,3])
|
||||||
|
assert_raise(NoMethodError, '[ruby-dev:40314]#3') {m.call}
|
||||||
|
end
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
private
|
||||||
|
def delegate_test_private
|
||||||
|
:m
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_private_method
|
||||||
|
foo = Foo.new
|
||||||
|
d = SimpleDelegator.new(foo)
|
||||||
|
assert_raise(NoMethodError) {foo.delegate_test_private}
|
||||||
|
assert_equal(:m, foo.send(:delegate_test_private))
|
||||||
|
assert_raise(NoMethodError, '[ruby-dev:40314]#4') {d.delegate_test_private}
|
||||||
|
assert_raise(NoMethodError, '[ruby-dev:40314]#5') {d.send(:delegate_test_private)}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Загрузка…
Ссылка в новой задаче