/
defer-call.t
96 lines (84 loc) 路 2.61 KB
/
defer-call.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use v6;
use Test;
plan 14;
# L<S12/"Calling sets of methods"/"Any method can defer to the next candidate method in the list">
# Simple test, making sure callwith passes on parameters properly.
class A {
method a(*@A) {
(self.perl, @A)
}
}
class B is A {
method a() {
callwith("FIRST ARG", "SECOND ARG")
}
}
{
my $instance = B.new;
my @result = $instance.a();
is @result.elems, 3, 'nextwith passed on right number of parameters';
is @result[0], $instance.perl, 'invocant passed on correctly';
is @result[1], "FIRST ARG", 'first argument correct';
is @result[2], "SECOND ARG", 'second argument correct';
}
class Foo {
# $.tracker is used to determine the order of calls.
has $.tracker is rw;
multi method doit() {$.tracker ~= 'foo,'}
multi method doit(Int $num) {$.tracker ~= 'fooint,'} #OK not used
method show {$.tracker}
method clear {$.tracker = ''}
}
class BarCallSame is Foo {
multi method doit() {$.tracker ~= 'bar,'; callsame; $.tracker ~= 'ret1,'}
multi method doit(Int $num) {$.tracker ~= 'barint,'; callsame; $.tracker ~= 'ret2,'} #OK not used
}
{
my $o = BarCallSame.new;
$o.clear;
$o.doit;
is($o.show, 'bar,foo,ret1,', 'callsame inheritance test');
$o.clear;
is($o.show, '', 'sanity test for clearing');
$o.doit(5);
is($o.show, 'barint,fooint,ret2,', 'callsame multimethod/inheritance test');
}
class BarCallWithEmpty is Foo {
multi method doit() {$.tracker ~= 'bar,'; callwith(); $.tracker ~= 'ret1,'}
multi method doit(Int $num) {$.tracker ~= 'barint,'; callwith($num); $.tracker ~= 'ret2,'} #OK not used
}
{
my $o = BarCallWithEmpty.new;
$o.clear;
$o.doit;
is($o.show, 'bar,foo,ret1,', 'callwith() inheritance test');
$o.clear;
is($o.show, '', 'sanity test for clearing');
{
$o.doit(5);
is($o.show, 'barint,fooint,ret2,', 'callwith() multimethod/inheritance test');
}
}
class BarCallWithInt is Foo {
multi method doit() {$.tracker ~= 'bar,'; callwith(); $.tracker ~= 'ret1,'}
multi method doit(Int $num) {$.tracker ~= 'barint,'; callwith(42); $.tracker ~= 'ret2,'} #OK not used
}
{
my $o = BarCallWithInt.new;
$o.clear;
$o.doit;
is($o.show, 'bar,foo,ret1,', 'callwith(42) inheritance test');
$o.clear;
is($o.show, '', 'sanity test for clearing');
$o.doit(5);
is($o.show, 'barint,fooint,ret2,', 'callwith(42) multimethod/inheritance test');
}
# RT #69756
{
multi sub f(0) { };
multi sub f($n) {
callwith($n - 1);
}
lives_ok { f(3) }, 'can recurse several levels with callwith()';
}
# vim: ft=perl6