Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 173 lines (145 sloc) 6.016 kb
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
1 use v6;
2
3 use Test;
4
aad3319 @lizmat Removed obsolete test for Multi type object
lizmat authored
5 plan 41;
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
6
c2f8fec [t] and [t/spec] (two train travels worth of changes):
moritz authored
7 # L<S06/Routine modifiers/>
8 # L<S06/Parameters and arguments/>
9
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
10 # multi sub with signature
11 multi sub foo() { "empty" }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
12 multi sub foo($a) { "one" } #OK not used
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
13 is(foo(), "empty", "multi sub with empty signature");
14 is(foo(42), "one", "multi sub with parameter list");
15
16 # multi sub without signature
17 multi sub bar { "empty" }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
18 multi sub bar($a) { "one" } #OK not used
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
19 #?niecza skip 'No candidates for dispatch to &bar'
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
20 is(bar(), "empty", "multi sub with no signature");
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
21 #?niecza skip 'Ambiguous dispatch for &bar'
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
22 is(bar(42), "one", "multi sub with parameter list");
23
24 # multi without a routine type
25 multi baz { "empty" }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
26 multi baz($a) { "one" } #OK not used
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
27 #?niecza skip 'No candidates for dispatch to &baz'
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
28 is(baz(), "empty", "multi with no signature");
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
29 #?niecza skip 'Ambiguous dispatch for &baz'
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
30 is(baz(42), "one", "multi with parameter list");
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
31
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
32 # multi without a routine type with signature
33 multi foobar() { "empty" }
34 multi foobar($a) { "one" } #OK not used
35 is(foobar(), "empty", "multi with empty signature");
36 is(foobar(42), "one", "multi with parameter list");
37
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
38 # multi with some parameters not counting in dispatch (;;) - note that if the
1e2c1f0 [t/spec] 11 typos in 6 files.
Util authored
39 # second parameter is counted as part of the dispatch, then invoking with 2
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
40 # ints means they are tied candidates as one isn't narrower than the other.
41 # (Note Int is narrower than Num - any two types where one is narrower than
42 # the other will do it, though.)
02418cf @jnthn Correct a test that relied on the old Int ~~ Num semantics.
jnthn authored
43 class T { }
44 class S is T { }
45 multi foo(S $a, T $b) { 1 } #OK not used
46 multi foo(T $a, S $b) { 2 } #OK not used
47 multi bar(S $a;; T $b) { 1 } #OK not used
48 multi bar(T $a;; S $b) { 2 } #OK not used
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
49 my $lived = 0;
02418cf @jnthn Correct a test that relied on the old Int ~~ Num semantics.
jnthn authored
50 try { foo(S,S); $lived = 1 }
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
51 is($lived, 0, "dispatch tied as expected");
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
52 #?niecza skip 'Ambiguous dispatch for &bar'
02418cf @jnthn Correct a test that relied on the old Int ~~ Num semantics.
jnthn authored
53 is(bar(S,S), 1, "not tied as only first type in the dispatch");
d9ac4d6 [rakudo] Tests to make sure that declaring anonymous routines with only,...
jnthn authored
54
55 # not allowed to declare anonymous routines with only, multi or proto.
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
56 #?niecza todo
ae47545 [t/spec] fix plan in S06-multi/syntax.t; use eval_dies_ok for terser tes...
moritz authored
57 eval_dies_ok 'only sub {}', 'anonymous only sub is an error';
58 eval_dies_ok 'multi sub {}', 'anonymous multi sub is an error';
59 eval_dies_ok 'proto sub {}', 'anonymous proto sub is an error';
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
60 #?niecza todo
ae47545 [t/spec] fix plan in S06-multi/syntax.t; use eval_dies_ok for terser tes...
moritz authored
61 eval_dies_ok 'only {}', 'anonymous only is an error';
62 eval_dies_ok 'multi {}', 'anonymous multi is an error';
63 eval_dies_ok 'proto {}', 'anonymous proto is an error';
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
64 #?niecza todo
ae47545 [t/spec] fix plan in S06-multi/syntax.t; use eval_dies_ok for terser tes...
moritz authored
65 eval_dies_ok 'class A { only method {} }', 'anonymous only method is an error';
66 eval_dies_ok 'class B { multi method {} }', 'anonymous multi method is an error';
67 eval_dies_ok 'class C { proto method {} }', 'anonymous proto method is an error';
d535647 [t/spec] Test for .WHAT on a multi.
jnthn authored
68
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
69 ok(&foo ~~ Callable, 'a multi does Callable');
0d9e55e @colomon Add a couple of trivial but obvious tests. Fudge for niecza.
colomon authored
70 #?niecza todo
96e97df [t/spec] Generalize a test I 'corrected' a couple of days back.
jnthn authored
71 ok(~&foo ~~ /foo/, 'a multi stringifies sensibly');
992bbb1 add test for override of builtin, RT 58948
ron authored
72
c15d1cf [t/spec/S06-multi/syntax.t] unfudged two tests
masak authored
73 # note - example in ticket [perl #58948] a bit more elaborate
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
74 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
75 multi sub max($a, $b, $c) {return 9} #OK not used
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
76
285bebe [t/spec] Corrections and an unfudging for a multi test.
jnthn authored
77 lives_ok { max(1, 2, 3) }, 'use multi method to override builtin lives';
04daf2d @FROGGS eval => EVAL in S05, S06 and S09
FROGGS authored
78 is EVAL('max(1, 2, 3)'), 9, 'use multi method to override builtin';
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
79 }
692c417 [t/spec] Several tests to cover a couple of now-resolved RT tickets.
jnthn authored
80
81 # named and slurpy interaction - there have been bugs in the past on this front
82 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
83 multi nsi_1(Int $x, Bool :$flag, *@vals) { "nsi 1" }; #OK not used
692c417 [t/spec] Several tests to cover a couple of now-resolved RT tickets.
jnthn authored
84 is nsi_1(1), 'nsi 1', 'interaction between named and slurpy (1)';
85 is nsi_1(1, 2, 3, 4, 5), 'nsi 1', 'interaction between named and slurpy (2)';
86
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
87 multi nsi_2(Bool :$baz = Bool::False, *@vals) { "nsi 2" }; #OK not used
692c417 [t/spec] Several tests to cover a couple of now-resolved RT tickets.
jnthn authored
88 is nsi_2(:baz(Bool::True), 1, 2, 3), 'nsi 2', 'interaction between named and slurpy (3)';
89 is nsi_2(1, 2, 3), 'nsi 2', 'interaction between named and slurpy (4)';
90 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
91
aa7a009 [t/spec] Test for RT #68234
kyle authored
92 # RT #68234
93 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
94 multi rt68234(:$key!) { 'with key' }; #OK not used
95 multi rt68234(*%_) { 'unknown' }; #OK not used
aa7a009 [t/spec] Test for RT #68234
kyle authored
96 is rt68234(:key), 'with key', 'can find multi method with key';
97 is rt68234(:unknown), 'unknown', 'can find multi method with slurpy';
98 }
99
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
100 # RT #68158
101 {
102 multi rt68158() { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
103 multi rt68158(*@x) { 2 } #OK not used
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
104 is rt68158(), 1, 'non-slurpy wins over slurpy';
105 is rt68158(9), 2, 'slurpy called when non-slurpy can not bind';
106 }
107
108 # RT #64922
109 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
110 multi rt64922($x, %h?) { 1 } #OK not used
111 multi rt64922(@x) { 2 } #OK not used
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
112 is rt64922(1), 1, 'optional parameter does not break type-based candidate sorting';
113 is rt64922([1,2]), 2, 'optional parameter does not break type-based candidate sorting';
114 }
115
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
116 # RT #65672
117 {
118 multi rt65672() { 99 }
119 multi rt65672($x) { $x }
120 sub rt65672caller( &x ) { &x() }
121 is rt65672caller( &rt65672 ), 99, 'multi can be passed as callable';
122 }
123
fa251ba [t/spec] Add a test for the multi leakage but I accidentally in the Raku...
jnthn authored
124 # We had a bug where the multiness leaked into a sub, so we got errors
125 # about anonymous methods not being allowed to be multi.
126 {
127 multi sub kangaroo() { return method () { self * 2 } }
128 my $m = kangaroo();
129 is 21.$m(), 42, 'can write anonymous methods inside multi subs';
130 }
131
f30f0d5 [t/spec] tests for RT #75136: declaring a multi just returns the current...
moritz authored
132
133 # RT #75136
134 # a multi declaration should only return the current candidate, not the whole
135 # set of candidates.
136 {
137 multi sub koala(Int $x) { 42 * $x };
138
139 my $x = multi sub koala(Str $x) { 42 ~ $x }
140 is $x.candidates.elems,
141 1, 'multi sub declaration returns just the current candidate';
142 is $x('moep'), '42moep', 'and that candidate works';
143 dies_ok { $x(23) }, '... and does not contain the full multiness';
144 }
145
f1c60ac @jnthn Add test for multi-dispatch involving a | in the signature.
jnthn authored
146 multi with_cap($a) { $a }
e6ba01a @TimToady remove sigils from | and \
TimToady authored
147 multi with_cap($a,$b,|cap) { return with_cap($a + $b, |cap) }
f1c60ac @jnthn Add test for multi-dispatch involving a | in the signature.
jnthn authored
148 is with_cap(1,2,3,4,5,6), 21, 'captures in multi sigs work';
149
c892865 @coke Add test for RT #114886
coke authored
150 #RT #114886 - order of declaration matters
151 {
152 multi sub fizzbuzz(Int $ where * %% 15) { 'FizzBuzz' };
153 multi sub fizzbuzz(Int $ where * %% 5) { 'Buzz' };
154 multi sub fizzbuzz(Int $ where * %% 3) { 'Fizz' };
155 multi sub fizzbuzz(Int $number) { $number };
156 is
157 (1,3,5,15).map(&fizzbuzz).join(" "),
158 <1 Fizz Buzz FizzBuzz>,
159 "ordered multi subs";
160 }
161
a2664ea @coke Add test for RT #68528
coke authored
162 # RT #68528
1afab0c @colomon Fudge for niecza.
colomon authored
163 #?niecza skip 'Ambiguous call to &rt68528'
a2664ea @coke Add test for RT #68528
coke authored
164 {
165 multi rt68528(:$a!, *%_) { return "first" };
166 multi rt68528(:$b, *%_) { return "second" };
167 is(rt68528(:a, :b), "first", "RT #68528 - first defined wins the tie");
168 }
c892865 @coke Add test for RT #114886
coke authored
169
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
170 done;
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
171
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
172 # vim: ft=perl6
Something went wrong with that request. Please try again.