Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 168 lines (133 sloc) 5.536 kb
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
1 use v6;
2 use Test;
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored
3 plan 45;
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
10 multi foo (Int $bar) { "Int " ~ $bar }
11 multi foo (Str $bar) { "Str " ~ $bar }
12 multi foo (Num $bar) { "Num " ~ $bar }
13 multi foo (Bool $bar) { "Bool " ~ $bar }
5bea9b0 [type-based.t] s/Rule/Regex/
lwall authored
14 multi foo (Regex $bar) { "Regex " ~ WHAT( $bar ) } # since Rule's don't stringify
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
15 multi foo (Sub $bar) { "Sub " ~ $bar() }
afa0332 [t/spec] fix wrong usage of Hash %h and Array @a
moritz authored
16 multi foo (@bar) { "Array " ~ join(', ', @bar) }
17 multi foo (%bar) { "Hash " ~ join(', ', %bar.keys.sort) }
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
18 multi foo (IO $fh) { "IO" }
19
20 is(foo('test'), 'Str test', 'dispatched to the Str sub');
21 is(foo(2), 'Int 2', 'dispatched to the Int sub');
22
23 my $num = '4';
4372a1e [spec] partial fudge (and correction) of S06-multi/type-based.t, unfudge...
moritz authored
24 is(foo(1.4), 'Num 1.4', 'dispatched to the Num sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
25 is(foo(1 == 1), 'Bool 1', 'dispatched to the Bool sub');
a524b51 [t/spec]: protoobjects stringify to shortname plus parens (S12)
pmichaud authored
26 is(foo(/a/),'Regex Regex()','dispatched to the Rule sub');
b9eb591 [spec] moved blocks/multi_sub.t to spec/
moritz authored
27 is(foo(sub { 'baz' }), 'Sub baz', 'dispatched to the Sub sub');
28
29 my @array = ('foo', 'bar', 'baz');
30 is(foo(@array), 'Array foo, bar, baz', 'dispatched to the Array sub');
31
32 my %hash = ('foo' => 1, 'bar' => 2, 'baz' => 3);
33 is(foo(%hash), 'Hash bar, baz, foo', 'dispatched to the Hash sub');
34
35 is(foo($*ERR), 'IO', 'dispatched to the IO sub');
36
37 # You're allowed to omit the "sub" when declaring a multi sub.
38 # L<S06/"Routine modifiers">
39
40 multi declared_wo_sub (Int $x) { 1 }
41 multi declared_wo_sub (Str $x) { 2 }
42 is declared_wo_sub(42), 1, "omitting 'sub' when declaring 'multi sub's works (1)";
43 is declared_wo_sub("42"), 2, "omitting 'sub' when declaring 'multi sub's works (2)";
44
45 # Test for slurpy MMDs
46 proto mmd {} # L<S06/"Routine modifiers">
47 multi mmd () { 1 }
48 multi mmd (*$x, *@xs) { 2 }
49
50 is(mmd(), 1, 'Slurpy MMD to nullary');
51 is(mmd(1,2,3), 2, 'Slurpy MMD to listop via args');
52 is(mmd(1..3), 2, 'Slurpy MMD to listop via list');
53
3088a5a [t/spec] simple multi dispatch tests for sigil-implied type constraints
moritz authored
54 {
55 my %h = (:a<b>, :c<d>);
56 multi sub sigil-t (&code) { 'Callable' }
57 multi sub sigil-t ($any) { 'Any' }
58 multi sub sigil-t (@ary) { 'Positional' }
59 multi sub sigil-t (%h) { 'Associative' }
60 is sigil-t(1), 'Any', 'Sigil-based dispatch (Any)';
61 is sigil-t({ $_ }), 'Callable', 'Sigil-based dispatch (Callable)';
62 is sigil-t(<a b c>), 'Positional','Sigil-based dispatch (Arrays)';
63 is sigil-t(%h), 'Associative','Sigil-based dispatch (Associative)';
64
65 }
7c332e4 [t/spec] tests for rt #63276
moritz authored
66
67 {
68
69 class Scissor { };
70 class Paper { };
71
72 multi wins(Scissor $x, Paper $y) { 1 };
73 multi wins(::T $x, T $y) { 0 };
74 multi wins($x, $y) { -1 };
75
76 is wins(Scissor.new, Paper.new), 1, 'Basic sanity';
77 is wins(Paper.new, Paper.new), 0, 'multi dispatch with ::T generics';
78 is wins(Paper.new, Scissor.new), -1, 'fallback if there is a ::T variant';
79
80 multi wins2(Scissor $x, Paper $y) { 1 };
81 multi wins2($x, $y where { $x.WHAT eq $y.WHAT }) { 0 };
82 multi wins2($x, $y) { -1 };
83 is wins(Scissor.new, Paper.new), 1, 'Basic sanity 2';
84 is wins(Paper.new, Paper.new), 0, 'multi dispatch with faked generics';
85 is wins(Paper.new, Scissor.new), -1, 'fallback if there is a faked generic';
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored
86 }
87
88 {
89 multi m($x,$y where { $x==$y }) { 0 };
90 multi m($x,$y) { 1 };
7c332e4 [t/spec] tests for rt #63276
moritz authored
91
0dcb2aa [t/spec] simple tests for multis and subset types, bacek++
moritz authored
92 is m(2, 3), 1, 'subset types involving mulitple parameters (fallback)';
93 is m(1, 1), 0, 'subset types involving mulitple parameters (success)';
7c332e4 [t/spec] tests for rt #63276
moritz authored
94 }
95
502162b [t/spec] exercise bare "oh I feel so Haskell" sigils in signatures
moritz authored
96 {
97 multi f2 ($) { 1 };
98 multi f2 ($, $) { 2 };
99 multi f2 ($, $, $) { 3 };
100 multi f2 ($, $, @) { '3+' };
101 is f2(3), 1, 'arity-based dispatch to ($)';
102 is f2('foo', f2(3)), 2, 'arity-based dispatch to ($, $)';
103 is f2('foo', 4, 8), 3, 'arity-based dispatch to ($, $, $)';
104 is f2('foo', 4, <a b>), '3+', 'arity-based dispatch to ($, $, @)';
105 }
106
107 {
108 multi f3 ($ where 0 ) { 1 };
109 multi f3 ($x) { $x + 1 };
110 is f3(0), 1, 'can dispatch to "$ where 0"';
111 is f3(3), 4, '... and the ordinary dispatch still works';
112 }
7c332e4 [t/spec] tests for rt #63276
moritz authored
113
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored
114 # multi dispatch on typed containers
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
115
116 {
117 multi f4 (Int @a ) { 'Array of Int' }
118 multi f4 (Str @a ) { 'Array of Str' }
119 multi f4 (Array @a) { 'Array of Array' }
fb132dd [t/spec] add (failing) tests for dispatch on typed hashes
moritz authored
120 multi f4 (Int %a) { 'Hash of Int' }
121 multi f4 (Str %a) { 'Hash of Str' }
122 multi f4 (Array %a) { 'Hash of Array' }
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
123
124 my Int @a = 3, 4;
125 my Str @b = <foo bar>;
126 my Array @c = [1, 2], [3, 4];
127
fb132dd [t/spec] add (failing) tests for dispatch on typed hashes
moritz authored
128 my Int %a = a => 1, b => 2;
129 my Str %b = :a<b>, :b<c>;
130 my Array %c = a => [1, 2], b => [3, 4];
131
132 is f4(%a), 'Hash of Int', 'can dispatch on typed Hash (Int)';
133 is f4(%b), 'Hash of Str', 'can dispatch on typed Hash (Str)';
134 is f4(%c), 'Hash of Array', 'can dispatch on typed Hash (Array)';
135
136 is f4(@a), 'Array of Int', 'can dispatch on typed Array (Int)';
137 is f4(@b), 'Array of Str', 'can dispatch on typed Array (Str)';
138 is f4(@c), 'Array of Array', 'can dispatch on typed Array (Array)';
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
139 }
140
8854e88 [t/spec] RT #65674, multi subs in classes
moritz authored
141 # make sure that multi sub dispatch also works if the sub is defined
142 # in a class (was a Rakudo regression, RT #65674)
143
144 {
145 class A {
146 multi sub a(Int $x) { 'Int ' ~ $x }
147 multi sub a(Str $x) { 'Str ' ~ $x }
148 }
149
150 is A::a(3), 'Int 3', 'multis in classes (1)';
151 is A::a('f'), 'Str f', 'multis in classes (2)';
152 dies_ok { A::a([4, 5]) }, 'multis in classes (3)';
153 }
5e0b5ea [t/spec] basic tests for dispatch on typed arrays
moritz authored
154
8bddfc6 [t/spec] Added a few tests I don't completely understand based on an IRC
kyle authored
155 {
156 multi x(@a, @b where { @a.elems == @b.elems }) { 1 };
157 multi x(@a, @b) { 2 };
158 is x([1,2],[3,4]), 1;
159 is x([1],[2,3,4]), 2;
160
161 multi y(::T $x, T $y) { 1 };
162 multi y($x, $y) { 2 };
163 is y(1, 2), 1;
164 is y(1, 2.5), 2;
165 }
166
7c332e4 [t/spec] tests for rt #63276
moritz authored
167 # vim: ft=perl6
Something went wrong with that request. Please try again.