Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 134 lines (110 sloc) 4.802 kb
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
1 use v6;
2
3 use Test;
4
f30f0d5 [t/spec] tests for RT #75136: declaring a multi just returns the current...
moritz authored
5 plan *;
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
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
19 is(bar(), "empty", "multi sub with no signature");
20 is(bar(42), "one", "multi sub with parameter list");
21
22 # multi without a routine type
23 multi baz { "empty" }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
24 multi baz($a) { "one" } #OK not used
972003e [spectest] Add some tests for various bits of the multi sub syntax.
jnthn authored
25 is(baz(), "empty", "multi with no signature");
26 is(baz(42), "one", "multi with parameter list");
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
27
28 # multi with some parameters not counting in dispatch (;;) - note that if the
1e2c1f0 [t/spec] 11 typos in 6 files.
Util authored
29 # second parameter is counted as part of the dispatch, then invoking with 2
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
30 # ints means they are tied candidates as one isn't narrower than the other.
31 # (Note Int is narrower than Num - any two types where one is narrower than
32 # the other will do it, though.)
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
33 multi foo(Int $a, Num $b) { 1 } #OK not used
34 multi foo(Num $a, Int $b) { 2 } #OK not used
35 multi bar(Int $a;; Num $b) { 1 } #OK not used
36 multi bar(Num $a;; Int $b) { 2 } #OK not used
d329ac8 [t/spec] Add a test for the ;; syntax.
jnthn authored
37 my $lived = 0;
38 try { foo(1,1); $lived = 1 }
39 is($lived, 0, "dispatch tied as expected");
40 is(bar(1,1), 1, "not tied as only first type in the dispatch");
d9ac4d6 [rakudo] Tests to make sure that declaring anonymous routines with only,...
jnthn authored
41
42 # not allowed to declare anonymous routines with only, multi or proto.
ae47545 [t/spec] fix plan in S06-multi/syntax.t; use eval_dies_ok for terser tes...
moritz authored
43 eval_dies_ok 'only sub {}', 'anonymous only sub is an error';
44 eval_dies_ok 'multi sub {}', 'anonymous multi sub is an error';
45 eval_dies_ok 'proto sub {}', 'anonymous proto sub is an error';
46 eval_dies_ok 'only {}', 'anonymous only is an error';
47 eval_dies_ok 'multi {}', 'anonymous multi is an error';
48 eval_dies_ok 'proto {}', 'anonymous proto is an error';
49 eval_dies_ok 'class A { only method {} }', 'anonymous only method is an error';
50 eval_dies_ok 'class B { multi method {} }', 'anonymous multi method is an error';
51 eval_dies_ok 'class C { proto method {} }', 'anonymous proto method is an error';
d535647 [t/spec] Test for .WHAT on a multi.
jnthn authored
52
2970d76 [syntax.t] relax isa Multi to ~~ Multi
lwall authored
53 ok(&foo ~~ Multi, 'a multi does Multi');
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
54 ok(&foo ~~ Callable, 'a multi does Callable');
96e97df [t/spec] Generalize a test I 'corrected' a couple of days back.
jnthn authored
55 ok(~&foo ~~ /foo/, 'a multi stringifies sensibly');
992bbb1 add test for override of builtin, RT 58948
ron authored
56
c15d1cf [t/spec/S06-multi/syntax.t] unfudged two tests
masak authored
57 # note - example in ticket [perl #58948] a bit more elaborate
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
58 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
59 multi sub max($a, $b, $c) {return 9} #OK not used
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
60
285bebe [t/spec] Corrections and an unfudging for a multi test.
jnthn authored
61 lives_ok { max(1, 2, 3) }, 'use multi method to override builtin lives';
7d5f283 improve testing for mmd builtin override RT #58948
ron authored
62 is eval('max(1, 2, 3)'), 9, 'use multi method to override builtin';
63 }
692c417 [t/spec] Several tests to cover a couple of now-resolved RT tickets.
jnthn authored
64
65 # named and slurpy interaction - there have been bugs in the past on this front
66 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
67 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
68 is nsi_1(1), 'nsi 1', 'interaction between named and slurpy (1)';
69 is nsi_1(1, 2, 3, 4, 5), 'nsi 1', 'interaction between named and slurpy (2)';
70
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
71 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
72 is nsi_2(:baz(Bool::True), 1, 2, 3), 'nsi 2', 'interaction between named and slurpy (3)';
73 is nsi_2(1, 2, 3), 'nsi 2', 'interaction between named and slurpy (4)';
74 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
75
aa7a009 [t/spec] Test for RT #68234
kyle authored
76 # RT #68234
77 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
78 multi rt68234(:$key!) { 'with key' }; #OK not used
79 multi rt68234(*%_) { 'unknown' }; #OK not used
aa7a009 [t/spec] Test for RT #68234
kyle authored
80 is rt68234(:key), 'with key', 'can find multi method with key';
81 #?rakudo skip 'RT #68234'
82 is rt68234(:unknown), 'unknown', 'can find multi method with slurpy';
83 }
84
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
85 # RT #68158
86 {
87 multi rt68158() { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
88 multi rt68158(*@x) { 2 } #OK not used
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
89 is rt68158(), 1, 'non-slurpy wins over slurpy';
90 is rt68158(9), 2, 'slurpy called when non-slurpy can not bind';
91 }
92
93 # RT #64922
94 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
95 multi rt64922($x, %h?) { 1 } #OK not used
96 multi rt64922(@x) { 2 } #OK not used
7dc19f9 [t/spec] Tests for a copule of arity based multi-dispatch bugs.
jnthn authored
97 is rt64922(1), 1, 'optional parameter does not break type-based candidate sorting';
98 is rt64922([1,2]), 2, 'optional parameter does not break type-based candidate sorting';
99 }
100
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
101 # RT #65672
102 {
103 multi rt65672() { 99 }
104 multi rt65672($x) { $x }
105 sub rt65672caller( &x ) { &x() }
106 is rt65672caller( &rt65672 ), 99, 'multi can be passed as callable';
107 }
108
fa251ba [t/spec] Add a test for the multi leakage but I accidentally in the Raku...
jnthn authored
109 # We had a bug where the multiness leaked into a sub, so we got errors
110 # about anonymous methods not being allowed to be multi.
111 {
112 multi sub kangaroo() { return method () { self * 2 } }
113 my $m = kangaroo();
114 is 21.$m(), 42, 'can write anonymous methods inside multi subs';
115 }
116
f30f0d5 [t/spec] tests for RT #75136: declaring a multi just returns the current...
moritz authored
117
118 # RT #75136
119 # a multi declaration should only return the current candidate, not the whole
120 # set of candidates.
121 {
122 multi sub koala(Int $x) { 42 * $x };
123
124 my $x = multi sub koala(Str $x) { 42 ~ $x }
125 is $x.candidates.elems,
126 1, 'multi sub declaration returns just the current candidate';
127 is $x('moep'), '42moep', 'and that candidate works';
128 dies_ok { $x(23) }, '... and does not contain the full multiness';
129 }
130
71ee448 [t/spec] Tests for RT #65672, multi does Callable
kyle authored
131 done_testing;
132
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
133 # vim: ft=perl6
Something went wrong with that request. Please try again.