Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 131 lines (110 sloc) 4.221 kb
94a9756 [t/spec] first real signature introspection tests
moritz authored
1 use v6;
2 use Test;
3 plan *;
4
5 # L<S06/Signature Introspection>
6
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
7 sub j(*@i) {
8 @i.map({ ?$_}).join(' ');
9 }
10
94a9756 [t/spec] first real signature introspection tests
moritz authored
11 {
be253a0 [t/spec] more signature introspection
moritz authored
12 sub a($x, Int $y?, :$z) { };
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
13 ok &a.signature.params ~~ Positional, '.params does Positional';
94a9756 [t/spec] first real signature introspection tests
moritz authored
14 my @l = &a.signature.params;
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
15 #?rakudo todo 'types or autothreading'
13cc9c4 [t/spec] 71 typos in 32 files.
Util authored
16 ok ?(all(@l) ~~ Parameter), 'And all items are Parameters';
94a9756 [t/spec] first real signature introspection tests
moritz authored
17 is +@l, 3, 'we have three of them';
18 is ~(@l>>.name), '$x $y $z', 'can get the names with sigils';
19 ok @l[0].type === Any, 'Could get first type';
20 ok @l[1].type === Int, 'Could get second type';
21
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
22 is j(@l>>.readonly), '1 1 1', 'they are all read-only';
23 is j(@l>>.rw), '0 0 0', '... none rw';
24 is j(@l>>.copy), '0 0 0', '... none copy';
25 is j(@l>>.ref), '0 0 0', '... none ref';
26 is j(@l>>.slurpy), '0 0 0', '... none slurpy';
27 is j(@l>>.optional), '0 1 1', '... some optional';
28 is j(@l>>.invocant), '0 0 0', '... none invocant';
29 is j(@l>>.named), '0 0 1', '... one named';
be253a0 [t/spec] more signature introspection
moritz authored
30 }
94a9756 [t/spec] first real signature introspection tests
moritz authored
31
be253a0 [t/spec] more signature introspection
moritz authored
32 {
33 sub b(:x($a)! is rw, :$y is ref, :$z is copy) { };
34 my @l = &b.signature.params;
35 #?rakudo todo 'is ref'
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
36 is j(@l>>.readonly), '0 0 0', '(second sig) none are all read-only';
37 is j(@l>>.rw), '1 0 0', '... one rw';
be253a0 [t/spec] more signature introspection
moritz authored
38 #?rakudo todo 'is ref'
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
39 is j(@l>>.ref), '0 1 0', '... one ref';
40 is j(@l>>.copy), '0 0 1', '... one copy';
41 is j(@l>>.slurpy), '0 0 0', '... none slurpy';
42 is j(@l>>.optional), '0 1 1', '... some optional';
43 is j(@l>>.invocant), '0 0 0', '... none invocant';
44 is j(@l>>.named), '1 1 1', '... one named';
be253a0 [t/spec] more signature introspection
moritz authored
45
46 is ~@l[0].named_names, 'x', 'named_names work';
47 is ~@l[0].name, '$a', '.name works for renamed params';
48 }
49
50 {
51 sub d(*@pos, *%named) { };
52 my @l = &d.signature.params;
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
53 is j(@l>>.named), '0 1', '.named for slurpies';
54 is j(@l>>.slurpy), '1 1', '.slurpy';
be253a0 [t/spec] more signature introspection
moritz authored
55 is ~(@l>>.name), '@pos %named', '.name for slurpies';
56 }
57
58
59 {
60 sub d(:x(:y(:z($a)))) { };
61 is ~&d.signature.params.[0].named_names.sort, 'x y z', 'multi named_names';
62 is ~&d.signature.params.[0].name, '$a', '... and .name still works';
94a9756 [t/spec] first real signature introspection tests
moritz authored
63 }
64
9305b95 [t/spec] failing tests for .default
moritz authored
65 {
66 sub e($x = 3; $y = { 2 + $x }) { };
67 my @l = &e.signature.params>>.default;
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
68 #?rakudo todo 'types or autothreading'
9305b95 [t/spec] failing tests for .default
moritz authored
69 ok ?( all(@l) ~~ Code ), '.default returns closure';
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
70 is @l[0].(), 3, 'first closure works';
27c639d [t/spec] A little unfudging of some now-passing tests.
jnthn authored
71 #?rakudo skip 'default closure when no call made fails lexical lookup with NPMCA'
9305b95 [t/spec] failing tests for .default
moritz authored
72 is @l[1].().(), 5, 'closure as default value captured outer default value';
73 }
74
19d86b0 [t/spec] .constraints tests
moritz authored
75 {
76 sub f(Int $x where { $_ % 2 == 0 }) { };
77 my $p = &f.signature.params[0];
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
78 #?rakudo todo 'constraints'
19d86b0 [t/spec] .constraints tests
moritz authored
79 ok 4 ~~ $p.constraints, '.constraints (+)';
80 ok 5 !~~ $p.constraints, '.constraints (-)';
81 ok 5 ~~ (-> $x { }).signature.params[0].constraints,
82 '.constraints on unconstraint param should still smartmatch truely';
83 sub g(Any $x where Int) { };
7aebfb0 [t/spec] explicitly coerce values to bool before comparison
moritz authored
84 #?rakudo todo 'constraints'
ef39b84 [t/spec] non-closure constraints
moritz authored
85 ok 3 ~~ &g.signature.params[0].constraints,
86 'smartmach against non-closure constraint (+)';
87 ok !(3.5 ~~ &g.signature.params[0].constraints),
88 'smartmach against non-closure constraint (-)';
19d86b0 [t/spec] .constraints tests
moritz authored
89 }
94a9756 [t/spec] first real signature introspection tests
moritz authored
90
9ba1733 [t/spec] Test for RT #70720, :(3) has the 3 available to introspection
moritz authored
91 # RT #70720
92 {
93 is :(3).params[0].constraints, 3, ':(3) contains the 3';
94 ok :(3).params[0].type === Int, ':(3) has a parameter of type Int';
95 }
96
3d2a4fc [t/spec] type_captures
moritz authored
97 {
98 sub h(::T $x, T $y) { };
99 my @l = &h.signature.params;
100 is @l[0].type_captures, 'T', '.type_captures';
101 lives_ok { @l[1].type }, "can access a type_capture'd type";
102 }
103
f1645a1 [t/spec] test that subsignatures appear in the output of Signature.perl
moritz authored
104 {
105 sub i(%h($a, $b)) { };
106 my $s = &i.signature.perl;
107 ok $s ~~ /'$a' >> /, '.perl on a nested signature contains variables of the subsignature (1)';
108 ok $s ~~ /'$b' >> /, '.perl on a nested signature contains variables of the subsignature (2)';
109
110 }
111
e70afa0 [t/spec] tests for RT #76116, parcel and capture binding in signature in...
moritz authored
112 {
113 my $x;
114 ok :(|$x).params[0].capture, 'prefix | makes .capture true';
115 ok :(|$x).perl ~~ / '|' /, 'prefix | appears in .perl output';
116
117 #?rakudo 2 skip 'parcel binding'
118 ok :(\|$x).params[0].parcel, 'prefix \| makes .parcel true';
119 ok :(\|$x).perl ~~ / '\|' /, 'prefix \| appears in .perl output';
120 }
121
d63ef9c [t/spec] test for RT #69492
moritz authored
122 # RT #69492
123 {
124 sub foo(:$) {};
125 ok &foo.signature.perl ~~ / ':' /, '.perl of a signature with anonymous named parameter';
126 }
127
94a9756 [t/spec] first real signature introspection tests
moritz authored
128 done_testing;
129
130 # vim: ft=perl6
Something went wrong with that request. Please try again.