/
composition.t
79 lines (63 loc) 路 1.54 KB
/
composition.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
use v6;
use Test;
plan 11;
# L<S12/Roles/"Roles may be composed into a class at compile time">
role rA {
method mA1 {
'mA1';
}
method mA2 {
'mA2';
}
};
role rB {
method mB1 {
'mB1';
}
method mB2 {
'mB2';
}
};
class C1 does rA {
method mC1 {
'mC1';
}
};
my $x = C1.new();
is $x.mC1, 'mC1', 'Can call method of class with mixed in role';
is $x.mA1, 'mA1', 'Call first method from role';
is $x.mA2, 'mA2', 'Call second method from role';
class C2 does rA does rB {
method mC2 {
'mC2';
}
}
my $y = C2.new();
is $y.mC2, 'mC2', 'Can call method of class with two roles mixed in';
is $y.mA1, 'mA1', 'Can call mixed in method (two roles) 1';
is $y.mA2, 'mA2', 'Can call mixed in method (two roles) 2';
is $y.mB1, 'mB1', 'Can call mixed in method (two roles) 3';
is $y.mB2, 'mB2', 'Can call mixed in method (two roles) 4';
{
class D1 does rA {
method mA1 {
'D1.mA1';
}
}
my $z = D1.new();
is $z.mA1, 'D1.mA1', 'Can override method in a role with method in a class';
}
# diamond composition, see RT #63330
#?rakudo skip 'RT #63330'
{
role DA {
method foo { "OH HAI" };
}
role DB does DA { }
role DC does DA { }
class DD does DB does DC { };
is DD.new.foo, 'OH HAI', 'diamond role composition';
class DE is DB is DC { };
is DE.new.foo, 'OH HAI', 'same with punning and inheritance';
}
# vim: syn=perl6