Skip to content
Newer
Older
100644 104 lines (89 sloc) 3.36 KB
1f4cbff [t/spec] Add tests for dispatch based upon literal values written in …
jnthn authored Jun 30, 2009
1 use v6;
2
3 use Test;
4
295f51d @skids Add tests for RT's 127025 127024 127129, with a couple fudges
skids authored Jan 4, 2016
5 plan 21;
1f4cbff [t/spec] Add tests for dispatch based upon literal values written in …
jnthn authored Jun 30, 2009
6
7 # L<S06/Routine modifiers/>
8 # L<S06/Parameters and arguments/>
9
10 # Simple case.
11 {
12 multi m1("foo") { 1 }
13 multi m1("bar") { 2 }
14
15 is m1("foo"), 1, "literal Str in signature matches value correctly";
16 is m1("bar"), 2, "literal Str in signature matches value correctly";
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored May 19, 2015
17 dies-ok { m1("baz") }, "dies if no matching value even if type matches";
1f4cbff [t/spec] Add tests for dispatch based upon literal values written in …
jnthn authored Jun 30, 2009
18 }
19
20 # More complex case. Here we check that the multis get the right narrowness,
21 # based upon the type of the literal, and are narrower than a candidate of
22 # the same type because they have constraints.
23 {
24 multi m2(1) { "a" }
25 multi m2(2) { "b" }
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored Jul 15, 2010
26 multi m2(Int $x) { "c" } #OK not used
27 multi m2($x) { "d" } #OK not used
08154ac @TimToady test negative parameter values
TimToady authored Sep 18, 2015
28 multi m2(-1) { "e" }
1f4cbff [t/spec] Add tests for dispatch based upon literal values written in …
jnthn authored Jun 30, 2009
29
30 is m2(1), "a", 'literal Int in signature matches value correctly';
31 is m2(2), "b", 'literal Int in signature matches value correctly';
32 is m2(3), "c", 'fallback to Int variant which is less narrow than constrained one';
33 is m2("x"), "d", 'if not an Int at all, fall back to Any candidate';
08154ac @TimToady test negative parameter values
TimToady authored Sep 19, 2015
34 is m2(-1), "e", 'negative literal Int in signature matches value correctly';
1f4cbff [t/spec] Add tests for dispatch based upon literal values written in …
jnthn authored Jun 30, 2009
35 }
36
5b94e54 @moritz [value-based.t] test for RT #88562, multi dispatch with int literals,…
moritz authored Apr 14, 2011
37 # RT #88562
38 {
39 multi m3(0 , $ ) { 'a' };
89f0409 @TimToady warning suppression, mostly "not used"
TimToady authored Sep 3, 2012
40 multi m3(Int $n, Str $a = 'A') { 'b' }; #OK not used
5b94e54 @moritz [value-based.t] test for RT #88562, multi dispatch with int literals,…
moritz authored Apr 14, 2011
41
42 is m3(2, 'A'), 'b', 'literal Int, anonymous parameters and default values mix';
43 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored Aug 8, 2009
44
39684f0 @moritz dispatch test involving literals and optional parameters (RT #88562)
moritz authored Sep 30, 2011
45 {
46 multi sub foo(0, $) { 'B' };
89f0409 @TimToady warning suppression, mostly "not used"
TimToady authored Sep 3, 2012
47 multi sub foo(Int $n, Str $a="A") { $a }; #OK not used
39684f0 @moritz dispatch test involving literals and optional parameters (RT #88562)
moritz authored Sep 30, 2011
48 is foo(2,"A"), 'A', 'Literals and optionals mix';
49 }
50
38f9286 @moritz tests for RT #107348, sub f(True) could not be called s f(True)
moritz authored Jan 6, 2012
51 # not quite a multi, but also value based
52 # RT #107348
53
54 {
55 sub f(True) { 'a' }
56 is f(True), 'a', 'can call a sub f(True) with True as argument';
57 is f(False), 'a', 'works with False too, since False ~~ True';
709e414 @lizmat s/dies_ok/dies-ok/g
lizmat authored May 19, 2015
58 dies-ok { EVAL 'f(1)' }, 'type constraint is still Bool';
38f9286 @moritz tests for RT #107348, sub f(True) could not be called s f(True)
moritz authored Jan 6, 2012
59 }
60
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored Aug 8, 2009
61 # vim: ft=perl6
295f51d @skids Add tests for RT's 127025 127024 127129, with a couple fudges
skids authored Jan 5, 2016
62
63 # RT #127129 and RT #127024 and RT#127025
64 {
65 role A {
66 multi method f(1) { 1 }
67 multi method f(2) { 2 }
68 }
69 role B {
70 multi method f(3) { 3 }
71 }
72 role C {
73 }
74 lives-ok { class C1 does A does C {} },
75 "Multi-role mix with value constrained parameter";
76 lives-ok { class C2 does A does B {} },
77 "Multis from diferent roles with value constrained parameter";
78
79 my $checked = '';
80 role D {
81 multi method f($ where { $checked ~= "d1"; $_ == 1 }) { 1 }
82 multi method f($ where { $checked ~= "d2"; $_ == 2 }) { 2 }
83 }
84 role E {
85 multi method f($ where { $checked ~= "e"; $_ == 3 }) { 3 }
86 }
87 lives-ok { class C3 does D does C {}; C3.new.f(2) },
88 "Multi-role mix with only where clauses different";
89 ok $checked ~~ /^d1d2/,
90 "Multis from same role declaration order tiebreaker";
91 $checked = '';
92 lives-ok { class C4 does D does E {}; C4.new.f(3) },
93 "Multis from different roles with only where clauses different";
94 #?rakudo todo "Wrong tiebreaker order (S12, actually)"
95 ok $checked ~~ /^d1d2e/,
96 "Multis from different roles declaration order tiebreaker";
97 $checked = '';
98 lives-ok { class C5 does E does D {}; C4.new.f(3) },
99 "Multis from different roles with only where clauses different (2)";
100 # Design review needed on this one -- is inclusion order considered as declaration order?
101 #?rakudo todo "Wrong tiebreaker order (S12, actually)"
102 ok $checked ~~ /^d1d2e/,
103 "Multis from different roles declaration order tiebreaker";
104 }
Something went wrong with that request. Please try again.