Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 192 lines (151 sloc) 6.942 kb
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
1 use v6;
2 use Test;
c3d70da [t/spec] tests for RT #69798
moritz authored
3 plan *;
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
4
5 # type based dispatching
6 #
7 #L<S06/"Longname parameters">
8 #L<S12/"Multisubs and Multimethods">
9
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN acc...
bbkr authored
10 multi foo (5) { "Constant" }
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
11 multi foo (Int $bar) { "Int " ~ $bar }
12 multi foo (Str $bar) { "Str " ~ $bar }
38d8646 [t/spec] Catch up on s/Num/Rat/ spec change for thingies like 1.4.
jnthn authored
13 multi foo (Rat $bar) { "Rat " ~ $bar }
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
14 multi foo (Bool $bar) { "Bool " ~ $bar }
5bea9b0 [type-based.t] s/Rule/Regex/
lwall authored
15 multi foo (Regex $bar) { "Regex " ~ WHAT( $bar ) } # since Rule's don't stringify
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
16 multi foo (Sub $bar) { "Sub " ~ $bar() }
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored
17 multi foo (@bar) { "Positional " ~ join(', ', @bar) }
18 multi foo (%bar) { "Associative " ~ join(', ', %bar.keys.sort) }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
19 multi foo (IO $fh) { "IO" } #OK not used
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN acc...
bbkr authored
20 multi foo (Inf) { "Inf" }
21 multi foo (NaN) { "NaN" }
22
23 is foo(5), 'Constant', 'dispatched to the constant sub';
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
24
25 is(foo(2), 'Int 2', 'dispatched to the Int sub');
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN acc...
bbkr authored
26 is(foo('test'), 'Str test', 'dispatched to the Str sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
27
28 my $num = '4';
38d8646 [t/spec] Catch up on s/Num/Rat/ spec change for thingies like 1.4.
jnthn authored
29 is(foo(1.4), 'Rat 1.4', 'dispatched to the Num sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
30 is(foo(1 == 1), 'Bool 1', 'dispatched to the Bool sub');
a524b51 [t/spec]: protoobjects stringify to shortname plus parens (S12)
pmichaud authored
31 is(foo(/a/),'Regex Regex()','dispatched to the Rule sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
32 is(foo(sub { 'baz' }), 'Sub baz', 'dispatched to the Sub sub');
33
34 my @array = ('foo', 'bar', 'baz');
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored
35 is(foo(@array), 'Positional foo, bar, baz', 'dispatched to the Positional sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
36
37 my %hash = ('foo' => 1, 'bar' => 2, 'baz' => 3);
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored
38 is(foo(%hash), 'Associative bar, baz, foo', 'dispatched to the Associative sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
39
40 is(foo($*ERR), 'IO', 'dispatched to the IO sub');
41
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN acc...
bbkr authored
42 is foo(Inf), 'Inf', 'dispatched to the Inf sub';
43 is foo(NaN), 'NaN', 'dispatched to the NaN sub';
44
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
45 # You're allowed to omit the "sub" when declaring a multi sub.
46 # L<S06/"Routine modifiers">
47
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
48 Multi declared_wo_sub (Int $x) { 1 } #OK not used
49 multi declared_wo_sub (Str $x) { 2 } #OK not used
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
50 is declared_wo_sub(42), 1, "omitting 'sub' when declaring 'multi sub's works (1)";
51 is declared_wo_sub("42"), 2, "omitting 'sub' when declaring 'multi sub's works (2)";
52
53 # Test for slurpy MMDs
54 proto mmd {} # L<S06/"Routine modifiers">
55 multi mmd () { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
56 multi mmd (*$x, *@xs) { 2 } #OK not used
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
57
58 is(mmd(), 1, 'Slurpy MMD to nullary');
59 is(mmd(1,2,3), 2, 'Slurpy MMD to listop via args');
60 is(mmd(1..3), 2, 'Slurpy MMD to listop via list');
61
3088a5a [t/spec] simple multi dispatch tests for sigil-implied type constraints
moritz authored
62 {
63 my %h = (:a<b>, :c<d>);
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
64 multi sub sigil-t (&code) { 'Callable' } #OK not used
65 multi sub sigil-t ($any) { 'Any' } #OK not used
66 multi sub sigil-t (@ary) { 'Positional' } #OK not used
67 multi sub sigil-t (%h) { 'Associative' } #OK not used
3088a5a [t/spec] simple multi dispatch tests for sigil-implied type constraints
moritz authored
68 is sigil-t(1), 'Any', 'Sigil-based dispatch (Any)';
69 is sigil-t({ $_ }), 'Callable', 'Sigil-based dispatch (Callable)';
70 is sigil-t(<a b c>), 'Positional','Sigil-based dispatch (Arrays)';
71 is sigil-t(%h), 'Associative','Sigil-based dispatch (Associative)';
72
73 }
7c332e4 [t/spec] tests for rt #63276
moritz authored
74
75 {
76
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
77 class Scissor { }
78 class Paper { }
79 class Stone { }
7c332e4 [t/spec] tests for rt #63276
moritz authored
80
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
81 multi wins(Scissor $x, Paper $y) { 1 } #OK not used
82 multi wins(::T $x, T $y) { 0 } #OK not used
83 multi wins($x, $y) { -1 } #OK not used
7c332e4 [t/spec] tests for rt #63276
moritz authored
84
85 is wins(Scissor.new, Paper.new), 1, 'Basic sanity';
86 is wins(Paper.new, Paper.new), 0, 'multi dispatch with ::T generics';
87 is wins(Paper.new, Scissor.new), -1, 'fallback if there is a ::T variant';
88
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
89 multi wins2(Scissor $x, Paper $y) { 1 } #OK not used
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
90 multi wins2($x, $y where { $x.WHAT eq $y.WHAT }) { 0 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
91 multi wins2($x, $y) { -1 } #OK not used
15faec1 fix a function call in S06-multi/type-based.t
moritz authored
92 is wins2(Scissor.new, Paper.new), 1, 'Basic sanity 2';
93 is wins2(Paper.new, Paper.new), 0, 'multi dispatch with faked generics';
94 is wins2(Paper.new, Scissor.new), -1, 'fallback if there is a faked generic';
c3d70da [t/spec] tests for RT #69798
moritz authored
95
96 # now try again with anonymous parameters (see RT #69798)
97 multi wins_anon(Scissor $, Paper $) { 1 }
98 multi wins_anon(Paper $, Stone $) { 1 }
99 multi wins_anon(Stone $, Scissor $) { 1 }
100 multi wins_anon(::T $, T $) { 0 }
101 multi wins_anon( $, $) { -1 }
102
103 is wins_anon(Scissor, Paper), 1, 'MMD with anonymous parameters (1)';
104 is wins_anon(Paper, Paper), 0, 'MMD with anonymous parameters (2)';
105 is wins_anon(Stone, Paper), -1, 'MMD with anonymous parameters (3)';
106
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored
107 }
108
c3d70da [t/spec] tests for RT #69798
moritz authored
109
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored
110 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
111 multi m($x,$y where { $x==$y }) { 0 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
112 multi m($x,$y) { 1 } #OK not used
7c332e4 [t/spec] tests for rt #63276
moritz authored
113
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored
114 is m(2, 3), 1, 'subset types involving mulitple parameters (fallback)';
115 is m(1, 1), 0, 'subset types involving mulitple parameters (success)';
7c332e4 [t/spec] tests for rt #63276
moritz authored
116 }
117
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored
118 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
119 multi f2 ($) { 1 }
120 multi f2 ($, $) { 2 }
121 multi f2 ($, $, $) { 3 }
122 multi f2 ($, $, @) { '3+' }
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored
123 is f2(3), 1, 'arity-based dispatch to ($)';
124 is f2('foo', f2(3)), 2, 'arity-based dispatch to ($, $)';
125 is f2('foo', 4, 8), 3, 'arity-based dispatch to ($, $, $)';
126 is f2('foo', 4, <a b>), '3+', 'arity-based dispatch to ($, $, @)';
127 }
128
129 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
130 multi f3 ($ where 0 ) { 1 }
131 multi f3 ($x) { $x + 1 }
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored
132 is f3(0), 1, 'can dispatch to "$ where 0"';
133 is f3(3), 4, '... and the ordinary dispatch still works';
134 }
7c332e4 [t/spec] tests for rt #63276
moritz authored
135
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored
136 # multi dispatch on typed containers
e39b744 [t/spec] Re-fudging of various multi-dispatch tests; we handle the major...
jnthn authored
137 #?rakudo skip 'typed array and hash containers are NYI'
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
138 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
139 multi f4 (Int @a ) { 'Array of Int' } #OK not used
140 multi f4 (Str @a ) { 'Array of Str' } #OK not used
141 multi f4 (Array @a) { 'Array of Array' } #OK not used
142 multi f4 (Int %a) { 'Hash of Int' } #OK not used
143 multi f4 (Str %a) { 'Hash of Str' } #OK not used
144 multi f4 (Array %a) { 'Hash of Array' } #OK not used
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
145
146 my Int @a = 3, 4;
147 my Str @b = <foo bar>;
148 my Array @c = [1, 2], [3, 4];
149
fb132dd [t/spec] add (failing) tests for dispatch on typed hashes
moritz authored
150 my Int %a = a => 1, b => 2;
151 my Str %b = :a<b>, :b<c>;
152 my Array %c = a => [1, 2], b => [3, 4];
153
154 is f4(%a), 'Hash of Int', 'can dispatch on typed Hash (Int)';
155 is f4(%b), 'Hash of Str', 'can dispatch on typed Hash (Str)';
156 is f4(%c), 'Hash of Array', 'can dispatch on typed Hash (Array)';
157
158 is f4(@a), 'Array of Int', 'can dispatch on typed Array (Int)';
159 is f4(@b), 'Array of Str', 'can dispatch on typed Array (Str)';
160 is f4(@c), 'Array of Array', 'can dispatch on typed Array (Array)';
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
161 }
162
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored
163 # make sure that multi sub dispatch also works if the sub is defined
164 # in a class (was a Rakudo regression, RT #65674)
165
166 {
167 class A {
9dee84a [t/spec] Happy our again!
jnthn authored
168 our multi sub a(Int $x) { 'Int ' ~ $x }
169 our multi sub a(Str $x) { 'Str ' ~ $x }
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored
170 }
171
172 is A::a(3), 'Int 3', 'multis in classes (1)';
173 is A::a('f'), 'Str f', 'multis in classes (2)';
174 dies_ok { A::a([4, 5]) }, 'multis in classes (3)';
175 }
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
176
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored
177 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored
178 multi x(@a, @b where { @a.elems == @b.elems }) { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
179 multi x(@a, @b) { 2 } #OK not used
9c826fd [t/spec] more test descriptions
moritz authored
180 is x([1,2],[3,4]), 1, 'where-clause that uses multiple params (1)';
181 is x([1],[2,3,4]), 2, 'where-clause that uses multiple params (1)';
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored
182
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
183 multi y(::T $x, T $y) { 1 } #OK not used
184 multi y($x, $y) { 2 } #OK not used
9c826fd [t/spec] more test descriptions
moritz authored
185 is y(1, 2), 1, 'generics in multis (+)';
186 is y(1, 2.5), 2, 'generics in multis (-)';
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored
187 }
188
c3d70da [t/spec] tests for RT #69798
moritz authored
189 done_testing;
190
7c332e4 [t/spec] tests for rt #63276
moritz authored
191 # vim: ft=perl6
Something went wrong with that request. Please try again.