/
introspection.t
69 lines (52 loc) 路 2.11 KB
/
introspection.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 20;
#?rakudo: 20 skip "routine introspection NYI"
# L<S06/Other matters/Introspection>
# introspecting only subs
only sub only-sub($a, $b) { "only" }; #OK not used
# .candidates
is(&only-sub.candidates.elems,1,"an only subs lists itself in the .candidates");
is(&only-sub.candidates[0].(1,2),"only","an only subs lists itself in the .candidates");
# .cando
is(&only-sub.cando(\(1,2)).elems,1,"an only sub implements .cando");
is(&only-sub.cando(\(1,2)).[0].(1,2),"only","an only sub implements .cando");
# .signature
ok(\(1,2) ~~ &only-sub.signature,"an only sub implements .signature");
# introspecting multi subs
multi sub multi-sub(1,2) { "m1" };
multi sub multi-sub(1) { "m2" };
multi sub multi-sub() { "m3" };
# .candidates
is(&multi-sub.candidates.elems,3,"a multi sub returns all its candidates");
# .cando
is(&multi-sub.cando(\(1,2)).[0].(1,2),"m1","you can invoke through introspection");
is(&multi-sub.cando(\(1)).[0].(1),"m2","you can invoke through introspection");
is(&multi-sub.cando(\()).[0].(),"m3","you can invoke through introspection");
# .signature
{
my $sig = &multi-sub.signature;
ok(\(1,2) ~~ $sig,"junction sig matches first candidate");
ok(\(1) ~~ $sig,"junction sig matches second candidate");
ok(\() ~~ $sig, "junction sig matches third candidate");
}
# creating a multi in runtime
my $multi = Multi.new();
ok($multi,"One can create a new multi at run time");
# let's populate this runtime multi.
$multi.push(sub (1,2) { "m1" });
$multi.push(sub (1) { "m2" });
$multi.push(sub () { "m3" });
# .candidates
is($multi.candidates.elems,3,"runtime multi sub returns all its candidates");
# .cando
is($multi.cando(\(1,2)).[0].(1,2),"m1","you can invoke through introspection");
is($multi.cando(\(1)).[0].(1),"m2","you can invoke through introspection");
is($multi.cando(\()).[0].(),"m3","you can invoke through introspection");
# .signature
{
my $sig = $multi.signature;
ok(\(1,2) ~~ $sig,"junction sig matches first candidate");
ok(\(1) ~~ $sig,"junction sig matches second candidate");
ok(\() ~~ $sig, "junction sig matches third candidate");
}