1 # frozen_string_literal: true
5 class TestDelegateClass < Test::Unit::TestCase
11 obj = DelegateClass(Array).new([])
12 obj.instance_eval { @m = :m }
14 assert_equal(:m, obj.m, "[ruby-dev:33116]")
17 def test_delegate_class_block
18 klass = DelegateClass(Array) do
21 assert_equal(1, klass.new([1]).foo)
24 def test_systemcallerror_eq
25 e = SystemCallError.new(0)
26 assert((SimpleDelegator.new(e) == e) == (e == SimpleDelegator.new(e)), "[ruby-dev:34808]")
29 class Myclass < DelegateClass(Array);end
31 def test_delegateclass_class
32 myclass=Myclass.new([])
33 assert_equal(Myclass,myclass.class)
34 assert_equal(Myclass,myclass.dup.class,'[ruby-dev:40313]')
35 assert_equal(Myclass,myclass.clone.class,'[ruby-dev:40313]')
38 def test_simpledelegator_class
39 simple=SimpleDelegator.new([])
40 assert_equal(SimpleDelegator,simple.class)
41 assert_equal(SimpleDelegator,simple.dup.class)
42 assert_equal(SimpleDelegator,simple.clone.class)
45 def test_simpledelegator_clone
46 simple=SimpleDelegator.new([])
50 assert_predicate clone, :frozen?
51 assert_predicate clone.__getobj__, :frozen?
52 assert_equal true, Kernel.instance_method(:frozen?).bind(clone).call
54 clone = simple.clone(freeze: false)
55 assert_not_predicate clone, :frozen?
56 assert_not_predicate clone.__getobj__, :frozen?
57 assert_equal false, Kernel.instance_method(:frozen?).bind(clone).call
79 class Bar < DelegateClass(Foo)
84 foo2 = SimpleDelegator.new(foo)
86 assert_equal(:o, Object.new.m)
87 assert_equal(:m, foo.m)
88 assert_equal(:m, foo2.m)
89 assert_equal(:m, bar.m)
90 bug = '[ruby-dev:39154]'
91 assert_equal(:m, foo2.send(:delegate_test_m), bug)
92 assert_equal(:m, bar.send(:delegate_test_m), bug)
96 def parent_public; end
100 def parent_protected; end
104 def parent_private; end
107 class Child < DelegateClass(Parent)
111 def parent_public_added; end
115 def parent_protected_added; end
119 def parent_private_added; end
122 def test_public_instance_methods
123 ignores = Object.public_instance_methods | Delegator.public_instance_methods
124 assert_equal([:parent_public, :parent_public_added], (Child.public_instance_methods - ignores).sort)
125 assert_equal([:parent_public, :parent_public_added], (Child.new(Parent.new).public_methods - ignores).sort)
128 def test_protected_instance_methods
129 ignores = Object.protected_instance_methods | Delegator.protected_instance_methods
130 assert_equal([:parent_protected, :parent_protected_added], (Child.protected_instance_methods - ignores).sort)
131 assert_equal([:parent_protected, :parent_protected_added], (Child.new(Parent.new).protected_methods - ignores).sort)
134 def test_instance_methods
135 ignores = Object.instance_methods | Delegator.instance_methods
136 assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.instance_methods - ignores).sort)
137 assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.new(Parent.new).methods - ignores).sort)
140 def test_DelegateClass_instance_method
141 assert_instance_of UnboundMethod, Child.instance_method(:parent_public)
142 assert_instance_of UnboundMethod, Child.instance_method(:parent_public_added)
143 assert_instance_of UnboundMethod, Child.instance_method(:parent_protected)
144 assert_instance_of UnboundMethod, Child.instance_method(:parent_protected_added)
145 assert_raise(NameError) { Child.instance_method(:parent_private) }
146 assert_raise(NameError) { Child.instance_method(:parent_private_added) }
147 assert_instance_of UnboundMethod, Child.instance_method(:to_s)
150 def test_DelegateClass_public_instance_method
151 assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public)
152 assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public_added)
153 assert_raise(NameError) { Child.public_instance_method(:parent_protected) }
154 assert_raise(NameError) { Child.public_instance_method(:parent_protected_added) }
155 assert_raise(NameError) { Child.instance_method(:parent_private) }
156 assert_raise(NameError) { Child.instance_method(:parent_private_added) }
157 assert_instance_of UnboundMethod, Child.public_instance_method(:to_s)
160 class IV < DelegateClass(Integer)
170 bug1744 = '[ruby-core:24211]'
172 assert_equal(1, c.var)
173 d = Marshal.load(Marshal.dump(c))
174 assert_equal(1, d.var, bug1744)
178 bug2679 = '[ruby-dev:40242]'
179 a = [42, :hello].freeze
180 d = SimpleDelegator.new(a)
181 assert_nothing_raised(bug2679) {d.dup[0] += 1}
182 assert_raise(FrozenError) {d.clone[0] += 1}
184 assert(d.clone.frozen?)
185 assert(!d.dup.frozen?)
189 d = SimpleDelegator.new([1, :foo])
191 assert_raise(FrozenError, '[ruby-dev:40314]#1') {d.__setobj__("foo")}
192 assert_equal([1, :foo], d)
195 def test_instance_method
196 s = SimpleDelegator.new("foo")
197 m = s.method("upcase")
198 s.__setobj__([1,2,3])
199 assert_raise(NoMethodError, '[ruby-dev:40314]#3') {m.call}
203 s = SimpleDelegator.new("foo")
204 assert_equal([], s.methods(false))
206 assert_equal([:bar], s.methods(false))
210 s0 = SimpleDelegator.new("foo")
211 s1 = SimpleDelegator.new("bar")
212 s2 = SimpleDelegator.new("foo")
213 assert_operator(s0, :eql?, s0)
214 assert_operator(s0, :eql?, "foo")
215 assert_operator(s0, :eql?, s2)
216 assert_not_operator(s0, :eql?, s1)
217 assert_not_operator(s0, :eql?, "bar")
220 def test_keyword_and_hash
225 def foo.foo(*args, **kw)
228 d = SimpleDelegator.new(foo)
229 assert_equal([[], {}], d.foo)
230 assert_equal([], d.bar)
231 assert_equal([[], {:a=>1}], d.foo(:a=>1))
232 assert_equal([{:a=>1}], d.bar(:a=>1))
233 assert_equal([[{:a=>1}], {}], d.foo({:a=>1}))
234 assert_equal([{:a=>1}], d.bar({:a=>1}))
239 def delegate_test_private
244 def test_private_method
246 d = SimpleDelegator.new(foo)
247 assert_raise(NoMethodError) {foo.delegate_test_private}
248 assert_equal(:m, foo.send(:delegate_test_private))
249 assert_raise(NoMethodError, '[ruby-dev:40314]#4') {d.delegate_test_private}
250 assert_raise(NoMethodError, '[ruby-dev:40314]#5') {d.send(:delegate_test_private)}
253 def test_global_function
259 d = SimpleDelegator.new(obj)
260 assert_nothing_raised(ArgumentError) {obj.open}
261 assert_nothing_raised(ArgumentError) {d.open}
262 assert_nothing_raised(ArgumentError) {d.send(:open)}
265 def test_send_method_in_delegator
266 d = Class.new(SimpleDelegator) do
271 assert_equal("foo", d.send(:foo))
274 def test_unset_simple_delegator
275 d = SimpleDelegator.allocate
276 assert_raise_with_message(ArgumentError, /not delegated/) {
281 def test_unset_delegate_class
283 assert_raise_with_message(ArgumentError, /not delegated/) {
288 class Bug9155 < DelegateClass(Integer)
289 def initialize(value)
290 super(Integer(value))
294 def test_global_method_if_no_target
295 bug9155 = '[ruby-core:58572] [Bug #9155]'
296 x = assert_nothing_raised(ArgumentError, bug9155) {break Bug9155.new(1)}
297 assert_equal(1, x.to_i, bug9155)
301 Name = '[ruby-core:59718] [Bug #9403]'
302 SD = SimpleDelegator.new(new)
310 alias aliased_name callee_name
315 dc = DelegateClass(self)
323 alias aliased_name callee_name
331 def test_method_in_simple_delegator
332 assert_equal(:method_name, Bug9403::SD.method_name, Bug9403::Name)
335 def test_callee_in_simple_delegator
336 assert_equal(:callee_name, Bug9403::SD.callee_name, Bug9403::Name)
337 assert_equal(:aliased_name, Bug9403::SD.aliased_name, Bug9403::Name)
340 def test_dir_in_simple_delegator
341 assert_equal(__dir__, Bug9403::SD.dir_name, Bug9403::Name)
344 def test_method_in_delegator_class
345 assert_equal(:method_name, Bug9403::DC.method_name, Bug9403::Name)
348 def test_callee_in_delegator_class
349 assert_equal(:callee_name, Bug9403::DC.callee_name, Bug9403::Name)
350 assert_equal(:aliased_name, Bug9403::DC.aliased_name, Bug9403::Name)
353 def test_dir_in_delegator_class
354 assert_equal(__dir__, Bug9403::DC.dir_name, Bug9403::Name)
357 def test_module_methods_vs_kernel_methods
358 delegate = SimpleDelegator.new(Object.new)
359 assert_raise(NoMethodError) do
367 delegate = SimpleDelegator.new(o)
368 assert_equal(1, delegate.bar)
369 assert_raise(NoMethodError, /undefined method `foo' for/) { delegate.foo }
372 def test_basicobject_respond_to
378 def o.respond_to?(method, include_private=false)
379 return false if method == :bar
380 ::Kernel.instance_method(:respond_to?).bind_call(self, method, include_private)
382 delegate = SimpleDelegator.new(o)
383 refute delegate.respond_to?(:bar)
386 def test_keyword_argument
387 k = EnvUtil.labeled_class("Target") do
388 def test(a, k:) [a, k]; end
390 a = DelegateClass(k).new(k.new)
391 assert_equal([1, 0], a.test(1, k: 0))