Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-method/test/method.rb

raw
1 class Base
2 def foo() :base end
3 end
4
5 class Derived < Base
6 def foo() :derived end
7 end
8
9 class Interpreter
10 attr_accessor :ret
11
12 def do_a() @ret += "there, "; end
13 def do_d() @ret += "Hello "; end
14 def do_e() @ret += "!\n"; end
15 def do_v() @ret += "Dave"; end
16 Dispatcher = {
17 "a" => instance_method(:do_a),
18 "d" => instance_method(:do_d),
19 "e" => instance_method(:do_e),
20 "v" => instance_method(:do_v)
21 }
22 def interpret(string)
23 @ret = ""
24 string.split("").each {|b| Dispatcher[b].bind(self).call }
25 end
26 end
27
28 assert 'demo' do
29 interpreter = Interpreter.new
30 interpreter.interpret('dave')
31 assert_equal "Hello there, Dave!\n", interpreter.ret
32 end
33
34 assert 'Method#arity' do
35 Class.new {
36 attr_accessor :done
37 def initialize; @done = false; end
38 def m0() end
39 def m1(a) end
40 def m2(a, b) end
41 def mo1(a = nil, &b) end
42 def mo2(a, b = nil) end
43 def mo3(*a) end
44 def mo4(a, *b, &c) end
45 def mo5(a, *b, c) end
46 def mo6(a, *b, c, &d) end
47 def mo7(a, b = nil, *c, d, &e) end
48 def ma1((a), &b) nil && a end
49
50 def run
51 assert_equal(0, method(:m0).arity)
52 assert_equal(1, method(:m1).arity)
53 assert_equal(2, method(:m2).arity)
54 assert_equal(-1, method(:mo1).arity)
55 assert_equal(-2, method(:mo2).arity)
56 assert_equal(-1, method(:mo3).arity)
57 assert_equal(-2, method(:mo4).arity)
58 assert_equal(-3, method(:mo5).arity)
59 assert_equal(-3, method(:mo6).arity)
60 assert_equal(-3, method(:mo7).arity)
61 assert_equal(1, method(:ma1).arity)
62
63 assert_equal(-1, method(:__send__).arity)
64 assert_equal(-1, method(:nothing).arity)
65 end
66
67 def respond_to_missing?(m, b)
68 m == :nothing
69 end
70 }.new.run
71 end
72
73 assert 'Method and UnboundMethod should not be have a `new` method' do
74 assert_raise(NoMethodError){ Method.new }
75 assert_raise(NoMethodError){ UnboundMethod.new }
76 end
77
78 assert 'instance' do
79 assert_kind_of Method, 1.method(:+)
80 assert_kind_of UnboundMethod, Integer.instance_method(:+)
81 end
82
83 assert 'Method#call' do
84 assert_equal 3, 1.method(:+).call(2)
85 assert_equal "ab", "a".method(:+)["b"]
86 klass = Class.new {
87 def foo; 42; end
88 }
89 klass2 = Class.new(klass) {
90 def foo; super; end
91 }
92 assert_equal 42, klass2.new.method(:foo).call
93
94 i = Class.new {
95 def bar
96 yield 3
97 end
98 }.new
99 assert_raise(LocalJumpError) { i.method(:bar).call }
100 assert_equal 3, i.method(:bar).call { |i| i }
101
102 assert_raise(ArgumentError) { nil.method(:__id__).call nil, 1 }
103 assert_raise(ArgumentError) { nil.method(:__id__).call nil, opts: 1 }
104 end
105
106 assert 'Method#call for regression' do
107 obj = BasicObject.new
108 assert_equal String, Kernel.instance_method(:inspect).bind(obj).call().class, "https://github.com/ksss/mruby-method/issues/4"
109 end
110
111 assert 'Method#call with undefined method' do
112 c = Class.new {
113 attr_accessor :m, :argv
114 def respond_to_missing?(m, b)
115 m == :foo
116 end
117
118 def method_missing(m, *argv)
119 @m = m
120 @argv = argv
121 super
122 end
123 }
124 cc = c.new
125 assert_raise(NameError) { cc.method(:nothing) }
126 assert_kind_of Method, cc.method(:foo)
127 assert_raise(NoMethodError) { cc.method(:foo).call(:arg1, :arg2) }
128 assert_equal :foo, cc.m
129 assert_equal [:arg1, :arg2], cc.argv
130
131 cc = c.new
132 m = cc.method(:foo)
133 c.class_eval do
134 def foo
135 :ng
136 end
137 end
138 assert_raise(NoMethodError) { m.call(:arg1, :arg2) }
139 end
140
141 assert 'Method#source_location' do
142 skip if proc{}.source_location.nil?
143
144 filename = __FILE__
145 klass = Class.new
146
147 lineno = __LINE__ + 1
148 klass.define_method(:find_me_if_you_can) {}
149 assert_equal [filename, lineno], klass.new.method(:find_me_if_you_can).source_location
150
151 lineno = __LINE__ + 1
152 class <<klass; define_method(:s_find_me_if_you_can) {}; end
153 assert_equal [filename, lineno], klass.method(:s_find_me_if_you_can).source_location
154
155 klass = Class.new { def respond_to_missing?(m, b); m == :nothing; end }
156 assert_nil klass.new.method(:nothing).source_location
157 end
158
159 assert 'UnboundMethod#source_location' do
160 skip if proc{}.source_location.nil?
161
162 filename = __FILE__
163 klass = Class.new {
164 def respond_to_missing?(m, b)
165 m == :nothing
166 end
167 }
168
169 lineno = __LINE__ + 1
170 klass.define_method(:find_me_if_you_can) {}
171 assert_equal [filename, lineno], klass.instance_method(:find_me_if_you_can).source_location
172 assert_nil klass.new.method(:nothing).unbind.source_location
173 end
174
175 assert 'Method#parameters' do
176 klass = Class.new {
177 def foo(a, b=nil, *c) end
178 def respond_to_missing?(m, b)
179 m == :missing
180 end
181 }
182 assert_equal [[:req, :a], [:opt, :b], [:rest, :c]], klass.new.method(:foo).parameters
183 assert_equal [[:rest]], klass.new.method(:missing).parameters
184 end
185
186 assert 'UnboundMethod#parameters' do
187 klass = Class.new {
188 def foo(a, b=nil, *c) end
189 def respond_to_missing?(m, b)
190 m == :nothing
191 end
192 }
193 assert_equal [[:req, :a], [:opt, :b], [:rest, :c]], klass.instance_method(:foo).parameters
194 assert_equal [[:rest]], klass.new.method(:nothing).unbind.parameters
195 end
196
197 assert 'Method#to_proc' do
198 m = 3.method(:+)
199 assert_kind_of Proc, m.to_proc
200 assert_equal 7, m.call(4)
201
202 o = Object.new
203 def o.foo(a, b=nil, *c)
204 [a, b, c]
205 end
206 assert_equal [:bar, nil, []], o.method(:foo).to_proc.call(:bar)
207 # We can fix this issue but leave until the problem
208 # assert_equal o.method(:foo).arity, o.method(:foo).to_proc.arity
209
210 def o.bar
211 yield 39
212 end
213 assert_equal 42, o.bar(&3.method(:+))
214
215 def o.baz(x, y, z, *w, u:, v:, **opts, &blk)
216 { x:, y:, z:, w:, u:, v:, opts:, blk: }
217 end
218 blk = -> { }
219 values = { x: 1, y: 2, z: 3, w: [4, 5, 6], u: 7, v: 8, opts: { s: 9, t: 10 }, blk: blk }
220 assert_equal values, o.method(:baz).to_proc.call(1, 2, 3, 4, 5, 6, u: 7, v: 8, s: 9, t: 10, &blk)
221 assert_equal values, o.method(:baz).to_proc.call(1, 2, 3, 4, 5, 6, **{ u: 7, v: 8, s: 9, t: 10 }, &blk)
222 assert_equal values, o.method(:baz).to_proc.call(*[1, 2, 3, 4, 5, 6], u: 7, v: 8, s: 9, t: 10, &blk)
223 assert_equal values, o.method(:baz).to_proc.call(*[1, 2, 3, 4, 5, 6], **{ u: 7, v: 8, s: 9, t: 10 }, &blk)
224
225 assert_raise(ArgumentError) { nil.method(:__id__).to_proc.call nil, 1 }
226 assert_raise(ArgumentError) { nil.method(:__id__).to_proc.call nil, opts: 1 }
227 end
228
229 assert 'to_s' do
230 o = Object.new
231 def o.foo; end
232 m = o.method(:foo)
233 assert_match("#<UnboundMethod: #{ class << o; self; end.inspect }#foo*>", m.unbind.inspect)
234
235 c = Class.new
236 c.class_eval { def foo; end; }
237 m = c.new.method(:foo)
238 assert_match("#<Method: #{ c.inspect }#foo*>", m.inspect)
239 m = c.instance_method(:foo)
240 assert_match("#<UnboundMethod: #{ c.inspect }#foo*>", m.inspect)
241 end
242
243 assert 'owner' do
244 c = Class.new do
245 def foo; end
246 def self.bar; end
247 end
248 m = Module.new do
249 def baz; end
250 end
251 c.include(m)
252 c2 = Class.new(c)
253
254 assert_equal(c, c.instance_method(:foo).owner)
255 assert_equal(c, c2.instance_method(:foo).owner)
256
257 assert_equal(c, c.new.method(:foo).owner)
258 assert_equal(c, c2.new.method(:foo).owner)
259 assert_equal((class <<c; self; end), c2.method(:bar).owner)
260 end
261
262 assert 'owner missing' do
263 c = Class.new do
264 def respond_to_missing?(name, bool)
265 name == :foo
266 end
267 end
268 c2 = Class.new(c)
269 assert_equal(c, c.new.method(:foo).owner)
270 assert_equal(c2, c2.new.method(:foo).owner)
271 end
272
273 assert 'receiver name owner' do
274 o = Object.new
275 def o.foo; end
276 m = o.method(:foo)
277 assert_equal(o, m.receiver)
278 assert_equal(:foo, m.name)
279 assert_equal(class << o; self; end, m.owner)
280 assert_equal(:foo, m.unbind.name)
281 assert_equal(class << o; self; end, m.unbind.owner)
282 end
283
284 assert 'Method#unbind' do
285 assert_equal(:derived, Derived.new.foo)
286 um = Derived.new.method(:foo).unbind
287 assert_kind_of(UnboundMethod, um)
288 Derived.class_eval do
289 def foo() :changed end
290 end
291 assert_equal(:changed, Derived.new.foo)
292 assert_equal(:changed, Derived.new.foo{})
293 assert_equal(:derived, um.bind(Derived.new).call)
294 assert_raise(TypeError) do
295 um.bind(Base.new)
296 end
297
298 # TODO:
299 # Block passed method not handled correctly with workaround.
300 # See comment near `mrb_funcall_with_block` for detail.
301 # assert_equal(:derived, um.bind(Derived.new).call{})
302 end
303
304 assert 'Kernel#method' do
305 c1 = Class.new {
306 def foo; :foo; end
307 }
308 o = c1.new
309 assert_kind_of Method, o.method(:foo)
310 assert_kind_of Method, o.method('foo')
311 assert_raise(TypeError) { o.method(nil) }
312 assert_raise(NameError) { o.method('bar') }
313 assert_raise(NameError) { o.method(:bar) }
314 end
315
316 assert "Module#instance_method" do
317 assert_kind_of UnboundMethod, Object.instance_method(:object_id)
318 assert_raise(NameError) { Object.instance_method(:nothing) }
319 c = Class.new {
320 def respond_to_missing?(m, b)
321 false
322 end
323 }
324 assert_raise(NameError) { c.instance_method(:nothing) }
325 end
326
327 assert 'Kernel#singleton_method' do
328 c1 = Class.new {
329 def foo; :foo; end
330 }
331 o = c1.new
332 def o.bar; :bar; end
333 assert_kind_of Method, o.method(:foo)
334 assert_raise(NameError) { o.singleton_method(:foo) }
335 assert_kind_of Method, o.singleton_method(:bar)
336 assert_raise(TypeError) { o.singleton_method(nil) }
337 m = assert_nothing_raised(NameError) { break o.singleton_method(:bar) }
338 assert_equal(:bar, m.call)
339 end
340
341 assert 'Method#super_method' do
342 o = Derived.new
343 m = o.method(:foo).super_method
344 assert_equal(Base, m.owner)
345 assert_true(o.equal? m.receiver)
346 assert_equal(:foo, m.name)
347 assert_nil(m.super_method)
348
349 c = Class.new {
350 def foo; end
351 }
352 o = c.new
353 o.extend Module.new {
354 def foo; end
355 }
356 assert_equal c, o.method(:foo).super_method.owner
357 assert_equal :foo, o.method(:foo).super_method.name
358 assert_equal o, o.method(:foo).super_method.receiver
359 end
360
361 assert 'Method#==' do
362 o = Object.new
363 class << o
364 def foo; end
365 end
366 assert_not_equal(o.method(:foo), nil)
367 m = o.method(:foo)
368 def m.foo; end
369 # TODO: assert_not_equal(o.method(:foo), m)
370 assert_equal(o.method(:foo), o.method(:foo))
371 # TODO: assert_false(o.method(:foo).eql? m)
372 assert_true(o.method(:foo).eql? o.method(:foo))
373
374 assert_false(0.method(:+) == 1.method(:+))
375 assert_false(0.method(:+) == 0.method(:-))
376 a = 0.method(:+)
377 assert_true(a.method(:==) == a.method(:eql?))
378 end
379
380 assert "Method#initialize_copy" do
381 c = Class.new {
382 def foo
383 end
384 }.new
385 m1 = c.method(:foo)
386 m2 = m1.clone
387 assert_equal(m1, m2)
388 end
389
390 assert "Method#<< and Method#>>" do
391 obj = Object.new
392 class << obj
393 def mul2(n); n * 2; end
394 def add3(n); n + 3; end
395 end
396
397 f = obj.method(:mul2)
398 g = obj.method(:add3)
399
400 m1 = f << g
401 assert_kind_of Proc, m1
402 assert_equal 16, m1.call(5)
403
404 m2 = f >> g
405 assert_kind_of Proc, m2
406 assert_equal 13, m2.call(5)
407 end
408
409 assert 'UnboundMethod#arity' do
410 c = Class.new {
411 def foo(a, b)
412 end
413
414 def respond_to_missing?(m, b)
415 m == :nothing
416 end
417 }
418 assert_equal 2, c.instance_method(:foo).arity
419 assert_equal(-1, c.new.method(:nothing).unbind.arity)
420 end
421
422 assert 'UnboundMethod#==' do
423 assert_false(Integer.instance_method(:+) == Integer.instance_method(:-))
424 assert_true(Integer.instance_method(:+) == Integer.instance_method(:+))
425 assert_true(UnboundMethod.instance_method(:==) == UnboundMethod.instance_method(:eql?))
426 skip unless Object.const_defined?(:Float)
427 assert_false(Integer.instance_method(:+) == Float.instance_method(:+))
428 end
429
430 assert 'UnboundMethod#super_method' do
431 m = Derived.instance_method(:foo)
432 m = m.super_method
433 assert_equal(Base.instance_method(:foo), m)
434 assert_nil(m.super_method)
435
436 m = Object.instance_method(:object_id)
437 assert_nil(m.super_method)
438 end
439
440 assert 'UnboundMethod#bind' do
441 m = Module.new{ def meth() :meth end }.instance_method(:meth)
442 assert_raise(ArgumentError) { m.bind }
443 assert_kind_of Method, m.bind(1)
444 assert_kind_of Method, m.bind(:sym)
445 assert_kind_of Method, m.bind(Object.new)
446 assert_equal(:meth, m.bind(1).call)
447 assert_equal(:meth, m.bind(:sym).call)
448 assert_equal(:meth, m.bind(Object.new).call)
449 sc = nil
450 Class.new {
451 sc = class << self
452 def foo
453 end
454 self
455 end
456 }
457 assert_raise(TypeError) { sc.instance_method(:foo).bind([]) }
458 assert_raise(TypeError) { Array.instance_method(:each).bind(1) }
459 assert_kind_of Method, Object.instance_method(:object_id).bind(Object.new)
460 end
461
462 assert 'UnboundMethod#bind_call' do
463 m = Array.instance_method(:size)
464 assert_equal(:size, m.name)
465 assert_equal(0, m.bind_call([]))
466 assert_equal(1, m.bind_call([1]))
467 assert_equal(2, m.bind_call([1,2]))
468
469 o = Object.new
470 def m(x, y, z, *w, u:, v:, **opts, &blk)
471 { x:, y:, z:, w:, u:, v:, opts:, blk: }
472 end
473 m = o.method(:m).unbind
474 blk = -> { }
475 values = { x: 1, y: 2, z: 3, w: [4, 5, 6], u: 7, v: 8, opts: { s: 9, t: 10 }, blk: blk }
476 assert_equal values, m.bind_call(o, 1, 2, 3, 4, 5, 6, u: 7, v: 8, s: 9, t: 10, &blk)
477 assert_equal values, m.bind_call(o, 1, 2, 3, 4, 5, 6, **{ u: 7, v: 8, s: 9, t: 10 }, &blk)
478 assert_equal values, m.bind_call(o, *[1, 2, 3, 4, 5, 6], u: 7, v: 8, s: 9, t: 10, &blk)
479 assert_equal values, m.bind_call(o, *[1, 2, 3, 4, 5, 6], **{ u: 7, v: 8, s: 9, t: 10 }, &blk)
480 assert_raise(ArgumentError) { m.bind_call }
481
482 assert_raise(ArgumentError) { BasicObject.instance_method(:__id__).bind_call nil, 1 }
483 assert_raise(ArgumentError) { BasicObject.instance_method(:__id__).bind_call nil, opts: 1 }
484 end