Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 71 lines (55 sloc) 2.256 kb
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
1 use v6;
2 use Test;
3
288e046 [t] and [t/spec]
moritz authored
4 # L<S06/Optional parameters/>
5
e34b5be [t/spec] Fix the plan I broke in optional.t
kyle authored
6 plan 14;
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
7
8 sub opt1($p?) { defined($p) ?? $p !! 'undef'; }
9
10 is opt1('abc'), 'abc', 'Can pass optional param';
11 is opt1(), 'undef', 'Can leave out optional param';
12
6533150 [t/spec] more tests for passing optional arguments
moritz authored
13 sub opt2($p?, $q?) {
14 (defined($p) ?? $p !! 'undef')
15 ~ '|'
16 ~ (defined($q) ?? $q !! 'undef');
17 }
18
19 is opt2('a', 'b'), 'a|b', 'Can pass all two optional params';
20 is opt2('a'), 'a|undef', 'Can pass one of two optional params';
21 is opt2(), 'undef|undef', 'Can leave out all two optional params';
22
23 sub t_opt2(Str $p?, Str $q?) {
24 (defined($p) ?? $p !! 'undef')
25 ~ '|'
26 ~ (defined($q) ?? $q !! 'undef');
27 }
28
29 is t_opt2('a', 'b'), 'a|b', 'Can pass all two optional params';
30 is t_opt2('a'), 'a|undef', 'Can pass one of two optional params';
31 is t_opt2(), 'undef|undef', 'Can leave out all two optional params';
32
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
33 sub opt_typed(Int $p?) { defined($p) ?? $p !! 'undef' };
34
35 is opt_typed(2), 2, 'can pass optional typed param';
36 is opt_typed() , 'undef', 'can leave out optional typed param';
37
288e046 [t] and [t/spec]
moritz authored
38 # L<S06/Parameters and arguments/"required positional parameters must come
39 # before those bound to optional positional">
f81e0ca [t/spec] fudge test that TimToady++ corrected and now does not pass o…
moritz authored
40 #?rakudo todo 'die on optional param before required'
bf3aec8 [S06-signature/optional.t] nullterm allowed only after final comma
lwall authored
41 eval_dies_ok 'sub wrong ($a?, $b) {...}', 'options params before required ones are forbidden';
288e046 [t] and [t/spec]
moritz authored
42
198d353 documented some tests and added some tests for RT #53814
ron authored
43 sub foo_53814($w, $x?, :$y = 2){ $w~"|"~$x~"|"~$y };
44 #?rakudo todo 'RT #53814'
45 {
46 isnt foo_53814(1,undef,'something_extra',:y(3)), '1||something_extra',
47 'should not pass positional param to named';
48 dies_ok {foo_53814(1,undef,'something_extra',:y(3))},
49 'according to ticket - answer should be no matching signature';
50 }
9de901a [t/spec] Test for RT 54804
kyle authored
51
52 #?rakudo todo 'RT# 54804'
53 {
54 sub rt54804( $v, $w?, $x?, $y? ) {
55 (defined( $v ) ?? $v !! 'undef')
56 ~ '|' ~
57 (defined( $w ) ?? $w !! 'undef')
58 ~ '|' ~
59 (defined( $x ) ?? $x !! 'undef')
60 ~ '|' ~
61 (defined( $y ) ?? $y !! 'undef')
62 }
63
bf3aec8 [S06-signature/optional.t] nullterm allowed only after final comma
lwall authored
64 # old test is bogus, nullterm only allowed at the end of a list
65 # is rt54804( 1, , 3, ), '1|undef|3|undef',
66 # 'two commas parse as if undef is between them';
67 eval_dies_ok 'rt54804( 1, , 3, )', "two commas in a row doesn't parse";
9de901a [t/spec] Test for RT 54804
kyle authored
68 }
69
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
70 # vim: ft=perl6
Something went wrong with that request. Please try again.