-
Notifications
You must be signed in to change notification settings - Fork 135
/
stubs.t
69 lines (57 loc) 路 2.63 KB
/
stubs.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
use v6;
use Test;
plan 28;
role WithStub { method a() { ... } };
role ProvidesStub1 { method a() { 1 } };
role ProvidesStub2 { method a() { 2 } };
role WithMultiStub { multi method a(Int) { ... }; };
dies-ok { EVAL 'class A does WithStub { }' },
'need to implement stubbed methods at role-into-class composition time';
lives-ok { EVAL 'role B does WithStub { }' },
'but roles are fine';
lives-ok { EVAL 'class C does WithStub { method a() { 3 } }' },
'directly implementing the stubbed method is fine';
lives-ok { EVAL 'class D does WithStub does ProvidesStub1 { }' },
'composing the stubbed method is fine';
dies-ok { EVAL 'class E does WithStub does ProvidesStub1 does ProvidesStub2 { }' },
'composing stub and 2 implementations dies again';
lives-ok { EVAL 'class F does WithStub does ProvidesStub1 does ProvidesStub2 {
method a() { 4 } }' },
'composing stub and 2 implementations allows custom implementation';
#?rakudo todo 'RT#124393 Stubbing individual multis as role interface NYI'
{
class G does WithMultiStub { multi method a(Int) { } };
lives-ok { EVAL 'G.a(1)' }, "No ambiguous dispatch on stubbed multi in interface";
dies-ok { EVAL 'class H does WithMultiStub { multi method a(Str) { } }' },
"Interface contract enforced on stubbed multi";
}
class ProvidesA { method a() { 5 } };
lives-ok { EVAL 'class ChildA is ProvidesA does WithStub { }' },
'stubbed method can come from parent class too';
lives-ok { EVAL 'class RT115212 does WithStub { has $.a }' }, 'stubbed method can come from accessor';
class HasA { has $.a }
lives-ok { EVAL 'class RT115212Child is HasA does WithStub { }' }, 'stubbed method can come from accessor in parent class';
# RT #119643
throws-like { EVAL 'my role F119643 { ... }; class C119643 does F119643 {}' },
X::Role::Parametric::NoSuchCandidate;
# RT #125606
{
my role WithPrivate { method !foo { "p" } };
my role WithPrivateStub { method !foo { ... } };
my class ClassPrivate does WithPrivate does WithPrivateStub { method bar {self!foo } };
is ClassPrivate.new.bar(), 'p', 'RT #125606: Stub resolution works for private methods too';
}
# RT #125694
{
my role A { method !foo(A:D:) { "success!" } };
my role B { method !foo { ... }; method bar {self!foo } };
my class C does B does A { }
is C.new.bar(), "success!", 'private method call in role dispatches on type of target class';
}
# RT #126606
{
my role R { method m() { ... } }
for <gist perl DUMP item Str Bool defined so not WHICH WHERE Stringy Numeric Real> {
lives-ok { R."$_"() }, ".$_ on role with requirement does not pun it and die";
}
}