Skip to content
Newer
Older
100644 284 lines (231 sloc) 9.86 KB
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
1 use v6;
2 use Test;
9d891a0 @jnthn Further coverage of RT #121426.
jnthn authored Oct 6, 2015
3 plan 66;
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
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 …
bbkr authored Jun 28, 2010
10 multi foo (5) { "Constant" }
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
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 Feb 9, 2010
13 multi foo (Rat $bar) { "Rat " ~ $bar }
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
14 multi foo (Bool $bar) { "Bool " ~ $bar }
7d3cc6e @jnthn A couple of spec updates to S06-multi/syntax.t.
jnthn authored Jul 29, 2011
15 multi foo (Regex $bar) { "Regex " ~ gist(WHAT( $bar )) } # since Rule's don't stringify
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
16 multi foo (Sub $bar) { "Sub " ~ $bar() }
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored Aug 24, 2009
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 ar…
lwall authored Jul 15, 2010
19 multi foo (IO $fh) { "IO" } #OK not used
b741ac7 @coke niecza (auto)unfudge
coke authored Jan 10, 2012
20 #?niecza emit # foo (Inf) NYI
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN …
bbkr authored Jun 28, 2010
21 multi foo (Inf) { "Inf" }
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
22 #?niecza emit # foo (5) NYI
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN …
bbkr authored Jun 28, 2010
23 multi foo (NaN) { "NaN" }
24
25 is foo(5), 'Constant', 'dispatched to the constant sub';
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
26
27 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 …
bbkr authored Jun 28, 2010
28 is(foo('test'), 'Str test', 'dispatched to the Str sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
29
30 my $num = '4';
38d8646 [t/spec] Catch up on s/Num/Rat/ spec change for thingies like 1.4.
jnthn authored Feb 9, 2010
31 is(foo(1.4), 'Rat 1.4', 'dispatched to the Num sub');
672abda @moritz [type-based.t] fix another Bool stringification usage
moritz authored Sep 22, 2010
32 is(foo(1 == 1), 'Bool ' ~ True, 'dispatched to the Bool sub');
bebc699 @moritz switch from Type() to (Type) (work in progress)
moritz authored Feb 23, 2013
33 is(foo(/a/),'Regex (Regex)','dispatched to the Rule sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
34 is(foo(sub { 'baz' }), 'Sub baz', 'dispatched to the Sub sub');
35
36 my @array = ('foo', 'bar', 'baz');
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored Aug 24, 2009
37 is(foo(@array), 'Positional foo, bar, baz', 'dispatched to the Positional sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
38
39 my %hash = ('foo' => 1, 'bar' => 2, 'baz' => 3);
2ec15ab [spec] tweak S06-multi/type-based.t tests comments to be correct
ihrd authored Aug 24, 2009
40 is(foo(%hash), 'Associative bar, baz, foo', 'dispatched to the Associative sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
41
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
42 #?niecza skip '$*ERR is apparently not IO'
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
43 is(foo($*ERR), 'IO', 'dispatched to the IO sub');
44
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
45 #?niecza 2 skip 'We turned these off because of a niecza bug'
1e12d97 [t/spec/S06-multi/type-based.t] tests for RT #72750 Make Inf and NaN …
bbkr authored Jun 28, 2010
46 is foo(Inf), 'Inf', 'dispatched to the Inf sub';
47 is foo(NaN), 'NaN', 'dispatched to the NaN sub';
48
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
49 # You're allowed to omit the "sub" when declaring a multi sub.
50 # L<S06/"Routine modifiers">
51
7be6b56 [t/spec] wtf? How'd this end up as Multi, not multi? Corrected test.
jnthn authored Jul 15, 2010
52 multi declared_wo_sub (Int $x) { 1 } #OK not used
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
53 multi declared_wo_sub (Str $x) { 2 } #OK not used
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
54 is declared_wo_sub(42), 1, "omitting 'sub' when declaring 'multi sub's works (1)";
55 is declared_wo_sub("42"), 2, "omitting 'sub' when declaring 'multi sub's works (2)";
56
57 # Test for slurpy MMDs
91f1c57 @moritz it helps to have the proto re-dispatch to its candidates
moritz authored Aug 9, 2011
58 # L<S06/"Routine modifiers">
59 proto mmd(*@) {*}
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
60 multi mmd () { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
61 multi mmd (*$x, *@xs) { 2 } #OK not used
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored Jul 27, 2008
62
63 is(mmd(), 1, 'Slurpy MMD to nullary');
64 is(mmd(1,2,3), 2, 'Slurpy MMD to listop via args');
65 is(mmd(1..3), 2, 'Slurpy MMD to listop via list');
66
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
67 #?niecza skip 'two or more Anys error'
3088a5a [t/spec] simple multi dispatch tests for sigil-implied type constraints
moritz authored Jan 19, 2009
68 {
69 my %h = (:a<b>, :c<d>);
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
70 multi sub sigil-t (&code) { 'Callable' } #OK not used
71 multi sub sigil-t ($any) { 'Any' } #OK not used
72 multi sub sigil-t (@ary) { 'Positional' } #OK not used
73 multi sub sigil-t (%h) { 'Associative' } #OK not used
3088a5a [t/spec] simple multi dispatch tests for sigil-implied type constraints
moritz authored Jan 19, 2009
74 is sigil-t(1), 'Any', 'Sigil-based dispatch (Any)';
75 is sigil-t({ $_ }), 'Callable', 'Sigil-based dispatch (Callable)';
76 is sigil-t(<a b c>), 'Positional','Sigil-based dispatch (Arrays)';
77 is sigil-t(%h), 'Associative','Sigil-based dispatch (Associative)';
78
79 }
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
80
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
81 #?niecza skip 'GLOBAL::T does not name any package'
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
82 {
83
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored Oct 16, 2009
84 class Scissor { }
85 class Paper { }
86 class Stone { }
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
87
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
88 multi wins(Scissor $x, Paper $y) { 1 } #OK not used
89 multi wins(::T $x, T $y) { 0 } #OK not used
90 multi wins($x, $y) { -1 } #OK not used
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
91
92 is wins(Scissor.new, Paper.new), 1, 'Basic sanity';
93 is wins(Paper.new, Paper.new), 0, 'multi dispatch with ::T generics';
94 is wins(Paper.new, Scissor.new), -1, 'fallback if there is a ::T variant';
95
7c3f87a @moritz RT #114394, type captures and containers
moritz authored Aug 7, 2012
96 # RT #114394
97 sub p($a, $b) { wins($a, $b) };
636e4d6 @coke prefer RT #ticket
coke authored Nov 1, 2015
98 is p(Paper, Paper), 0, 'Type captures and containers mix (RT #114394)';
7c3f87a @moritz RT #114394, type captures and containers
moritz authored Aug 7, 2012
99
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
100 multi wins2(Scissor $x, Paper $y) { 1 } #OK not used
f85b8bf Fix a bunch of typeobject stringifications.
kboga authored Jul 29, 2011
101 multi wins2($x, $y where { $x.WHAT.gist eq $y.WHAT.gist }) { 0 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
102 multi wins2($x, $y) { -1 } #OK not used
15faec1 fix a function call in S06-multi/type-based.t
moritz authored May 7, 2010
103 is wins2(Scissor.new, Paper.new), 1, 'Basic sanity 2';
104 is wins2(Paper.new, Paper.new), 0, 'multi dispatch with faked generics';
105 is wins2(Paper.new, Scissor.new), -1, 'fallback if there is a faked generic';
c3d70da [t/spec] tests for RT #69798
moritz authored Oct 15, 2009
106
107 # now try again with anonymous parameters (see RT #69798)
108 multi wins_anon(Scissor $, Paper $) { 1 }
109 multi wins_anon(Paper $, Stone $) { 1 }
110 multi wins_anon(Stone $, Scissor $) { 1 }
111 multi wins_anon(::T $, T $) { 0 }
112 multi wins_anon( $, $) { -1 }
113
114 is wins_anon(Scissor, Paper), 1, 'MMD with anonymous parameters (1)';
115 is wins_anon(Paper, Paper), 0, 'MMD with anonymous parameters (2)';
116 is wins_anon(Stone, Paper), -1, 'MMD with anonymous parameters (3)';
117
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored Feb 17, 2009
118 }
119
120 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored Oct 16, 2009
121 multi m($x,$y where { $x==$y }) { 0 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
122 multi m($x,$y) { 1 } #OK not used
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
123
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored Feb 17, 2009
124 is m(2, 3), 1, 'subset types involving mulitple parameters (fallback)';
125 is m(1, 1), 0, 'subset types involving mulitple parameters (success)';
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
126 }
127
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored Mar 4, 2009
128 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored Oct 16, 2009
129 multi f2 ($) { 1 }
130 multi f2 ($, $) { 2 }
131 multi f2 ($, $, $) { 3 }
132 multi f2 ($, $, @) { '3+' }
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored Mar 4, 2009
133 is f2(3), 1, 'arity-based dispatch to ($)';
134 is f2('foo', f2(3)), 2, 'arity-based dispatch to ($, $)';
135 is f2('foo', 4, 8), 3, 'arity-based dispatch to ($, $, $)';
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
136 #?niecza skip 'Ambiguous dispatch for &f2'
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored Mar 4, 2009
137 is f2('foo', 4, <a b>), '3+', 'arity-based dispatch to ($, $, @)';
138 }
139
140 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored Oct 16, 2009
141 multi f3 ($ where 0 ) { 1 }
142 multi f3 ($x) { $x + 1 }
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored Mar 4, 2009
143 is f3(0), 1, 'can dispatch to "$ where 0"';
144 is f3(3), 4, '... and the ordinary dispatch still works';
145 }
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
146
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
147 # multi dispatch on typed containers
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
148 #?niecza skip 'Ambiguous dispatch for &f4'
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored Apr 8, 2009
149 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
150 multi f4 (Int @a ) { 'Array of Int' } #OK not used
151 multi f4 (Str @a ) { 'Array of Str' } #OK not used
152 multi f4 (Array @a) { 'Array of Array' } #OK not used
153 multi f4 (Int %a) { 'Hash of Int' } #OK not used
154 multi f4 (Str %a) { 'Hash of Str' } #OK not used
155 multi f4 (Array %a) { 'Hash of Array' } #OK not used
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored Apr 8, 2009
156
157 my Int @a = 3, 4;
158 my Str @b = <foo bar>;
159 my Array @c = [1, 2], [3, 4];
160
fb132dd [t/spec] add (failing) tests for dispatch on typed hashes
moritz authored Apr 8, 2009
161 my Int %a = a => 1, b => 2;
162 my Str %b = :a<b>, :b<c>;
163 my Array %c = a => [1, 2], b => [3, 4];
164
165 is f4(%a), 'Hash of Int', 'can dispatch on typed Hash (Int)';
166 is f4(%b), 'Hash of Str', 'can dispatch on typed Hash (Str)';
167 is f4(%c), 'Hash of Array', 'can dispatch on typed Hash (Array)';
168
169 is f4(@a), 'Array of Int', 'can dispatch on typed Array (Int)';
170 is f4(@b), 'Array of Str', 'can dispatch on typed Array (Str)';
171 is f4(@c), 'Array of Array', 'can dispatch on typed Array (Array)';
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored Apr 8, 2009
172 }
173
9d891a0 @jnthn Further coverage of RT #121426.
jnthn authored Oct 6, 2015
174 # Multi-dispach on declared return type
175 # RT #121426
176 {
177 sub i() returns Int { 3 }
178 sub s() returns Str { 'little pigs' }
179
180 multi by-return-type(Int &x) { 'Int ' ~ x() }
181 multi by-return-type(Str &x) { 'Str ' ~ x() }
182
183 is by-return-type(&s), 'Str little pigs', 'can dispatch on typed routine (Str)';
184 is by-return-type(&i), 'Int 3', 'can dispatch on typed routine (Int)';
185 }
186
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
187 # make sure that multi sub dispatch also works if the sub is defined
5bd323a @usev6 Fix and unfudge test for RT #65674
usev6 authored May 25, 2015
188 # in a class (was a Rakudo regression)
189 # RT #65674
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
190 #?niecza skip 'Two definitions found for symbol ::GLOBAL::A::&a'
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
191 {
5bd323a @usev6 Fix and unfudge test for RT #65674
usev6 authored May 25, 2015
192 throws-like q[
193 class RT65674 {
194 our multi sub a(Int $x) { 'Int ' ~ $x }
195 our multi sub a(Str $x) { 'Str ' ~ $x }
196 }
197 ], X::Declaration::Scope::Multi, 'no individual multi candidates in "our" scope';
198
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
199 class A {
5bd323a @usev6 Fix and unfudge test for RT #65674
usev6 authored May 25, 2015
200 our proto sub a(|) { * }
9dee84a [t/spec] Happy our again!
jnthn authored Apr 4, 2010
201 our multi sub a(Int $x) { 'Int ' ~ $x }
202 our multi sub a(Str $x) { 'Str ' ~ $x }
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
203 }
204
205 is A::a(3), 'Int 3', 'multis in classes (1)';
206 is A::a('f'), 'Str f', 'multis in classes (2)';
5bd323a @usev6 Fix and unfudge test for RT #65674
usev6 authored May 25, 2015
207 throws-like 'A::a([4, 5])', X::Multi::NoMatch, 'multis in classes (3)';
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored Jun 12, 2009
208 }
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored Apr 8, 2009
209
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored Jul 3, 2009
210 {
7d7a3a1 [type-based.t] supply missing Stone class, remove spurious semicolons
lwall authored Oct 16, 2009
211 multi x(@a, @b where { @a.elems == @b.elems }) { 1 }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
212 multi x(@a, @b) { 2 } #OK not used
9c826fd [t/spec] more test descriptions
moritz authored Jul 5, 2009
213 is x([1,2],[3,4]), 1, 'where-clause that uses multiple params (1)';
214 is x([1],[2,3,4]), 2, 'where-clause that uses multiple params (1)';
2dc589e @jnthn nom unfudges
jnthn authored Aug 13, 2011
215 }
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored Jul 3, 2009
216
3beca23 @colomon Fudge for niecza.
colomon authored Jan 8, 2012
217 #?niecza skip 'GLOBAL::T does not name any package'
2dc589e @jnthn nom unfudges
jnthn authored Aug 13, 2011
218 {
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
219 multi y(::T $x, T $y) { 1 } #OK not used
220 multi y($x, $y) { 2 } #OK not used
9c826fd [t/spec] more test descriptions
moritz authored Jul 5, 2009
221 is y(1, 2), 1, 'generics in multis (+)';
222 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 Jul 3, 2009
223 }
224
984611e @jnthn Add test for RT#107638.
jnthn authored Jan 13, 2012
225 #?niecza skip 'no native types yet'
226 {
227 # This once wrongly reported a multi-dispatch circularity.
89f0409 @TimToady warning suppression, mostly "not used"
TimToady authored Sep 3, 2012
228 multi rt107638(int $a) { 'ok' } #OK not used
229 multi rt107638(Str $a where 1) { } #OK not used
984611e @jnthn Add test for RT#107638.
jnthn authored Jan 13, 2012
230 ok rt107638(1), 'native types and where clauses do not cause spurious circularities';
231 }
232
0f4a710 @jnthn Basic test for new multi/coercion semantics.
jnthn authored Apr 4, 2015
233 # Coercion types introduce two candidates
234 {
235 proto foo($) {*}
236 multi foo(Int() $) { 1 }
237 multi foo(Cool $) { 2 }
238
239 my class NotCool { method Int() { 42 } }
240
241 is foo(42), 1, 'Int hits Int() candidate as if it were an Int candidate';
242 is foo("omg"), 2, 'Str hits Cool candidate';
243 is foo(NotCool), 1, 'Set hits Int() candidate due to it accepting Any and coercing';
244 }
245
9c40515 @dnmfarrell fixed appended RT number to be within quoted string
dnmfarrell authored May 4, 2015
246 #?rakudo todo "coercions need to fail like constraints RT #124772"
0bd84e5 @TimToady stretch goals for coercive multis
TimToady authored Apr 4, 2015
247 {
248 proto main ($) {*}
249 multi main ($x) { say $x.WHAT }
250
251 # (another problem is that these particular coercions are far too dwimmy for this purpose)
252 multi main (Complex() $x) { $x }
253 multi main ( Num() $x) { $x }
254 multi main ( Rat() $x) { $x }
255 multi main ( Int() $x) { $x }
256
257 ok try { main '3.14' } eqv 3.14, "can distinguish Rat-like string in main";
258 ok try { main '123e3' } eqv 123e3, "can distinguish Num-like string in main";
259 ok try { main '42' } eqv 42, "can distinguish Int-like string in main";
260 ok try { main '2+1i' } eqv 2+1i, "can distinguish Complex-like string in main";
261 }
262
40fa9bf @jnthn Tests covering RT #123623.
jnthn authored Jun 20, 2015
263 # RT #123623
264 {
265 multi foo($a where { $_ == False } = True) { }
266 lives-ok { foo(False) }, 'Combination of where clause plus default parses correctly';
267 }
268
35619ce @jnthn Add test coverage for RT #125483.
jnthn authored Jun 29, 2015
269 # RT #125483
270 {
271 multi a (;; Any $b) { "one" }
272 multi a (;; Int $a) { "two" }
273 throws-like { a(42) }, X::Multi::Ambiguous,
274 'arguments after ;; not considered by multi-dispatch';
275 }
276
9583915 @jnthn Test for RT #126003.
jnthn authored Sep 7, 2015
277 # RT #126003
278 {
279 multi sub want-array(@array) { @array.elems }
280 is want-array("abc".comb), 3, 'Can pass a Seq to an @-sigil arg in a multi';
281 }
282
7c332e4 [t/spec] tests for rt #63276
moritz authored Feb 17, 2009
283 # vim: ft=perl6
Something went wrong with that request. Please try again.