Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (77 sloc) 3.222 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
9e7fb12 @moritz test type checking of default values, RT #74758
moritz authored
6 plan 23;
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">
b896f12 [S06-signature/optional.t] Added tests for RT #76022.
Kodi Arfer authored
40 eval_dies_ok 'sub wrong1 ($a?, $b) {...}', 'optional params before required ones are forbidden';
41 # RT #76022
42 {
43 eval_dies_ok 'sub wrong2 ($a = 1, $b) {...}', "...even if they're only optional by virtue of a default";
44 eval_dies_ok 'sub wrong3 ($a = 0, $b) {...}', '...and the default is 0';
45 }
288e046 [t] and [t/spec]
moritz authored
46
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
47 sub foo_53814($w, $x?, :$y = 2) { $w~"|"~$x~"|"~$y };
48 dies_ok {foo_53814(1,Mu,'something_extra',:y(3))},
c013159 [t/spec]: Test descriptions must not contain '#'.
pmichaud authored
49 'die on too many parameters (was once bug RT 53814)';
9de901a [t/spec] Test for RT 54804
kyle authored
50
7256424 Mark failing test as todo
baest authored
51
52 #?rakudo todo 'RT 54804'
9de901a [t/spec] Test for RT 54804
kyle authored
53 {
a297d27 Put the sub definition inside the eval to make it callable
baest authored
54
55 # old test is bogus, nullterm only allowed at the end of a list
56 # is rt54804( 1, , 3, ), '1|undef|3|undef',
57 # 'two commas parse as if undef is between them';
58 eval_dies_ok q/sub rt54804( $v, $w?, $x?, $y? ) {
9de901a [t/spec] Test for RT 54804
kyle authored
59 (defined( $v ) ?? $v !! 'undef')
60 ~ '|' ~
61 (defined( $w ) ?? $w !! 'undef')
62 ~ '|' ~
63 (defined( $x ) ?? $x !! 'undef')
64 ~ '|' ~
65 (defined( $y ) ?? $y !! 'undef')
66 }
a297d27 Put the sub definition inside the eval to make it callable
baest authored
67 rt54804( 1, , 3, )/, "two commas in a row doesn't parse";
9de901a [t/spec] Test for RT 54804
kyle authored
68 }
9ff2f3a [t/spec] Test for RT 66822: required param after optional param
kyle authored
69
70 eval_dies_ok( 'sub rt66822($opt?, $req) { "$opt, $req" }',
71 "Can't put required parameter after optional parameters" );
72
d09d566 @sorear Add tests for Niecza #49
sorear authored
73 # Niecza bug#49
74 sub opt_array1(@x?) { @x.elems }
75 sub opt_array2(@x? is copy) { @x.elems }
76 sub opt_hash1(%x?) { %x.keys.elems }
77 sub opt_hash2(%x? is copy) { %x.keys.elems }
78 is opt_array1(), 0, "optional array not passed is empty";
79 is opt_array2(), 0, "optional array not passed is empty (copy)";
80 is opt_hash1(), 0, "optional hash not passed is empty";
81 is opt_hash2(), 0, "optional hash not passed is empty (copy)";
82
398be5a @moritz required params need to come before optional ones (RT #71110)
moritz authored
83 # RT #71110
84 eval_dies_ok 'sub opt($a = 1, $b) { }',
85 'Cannot put required parameter after optional parameters';
d09d566 @sorear Add tests for Niecza #49
sorear authored
86
9e7fb12 @moritz test type checking of default values, RT #74758
moritz authored
87 # RT #74758
2f1dec0 @coke niecza fudging
coke authored
88 #?niecza skip 'Nominal type check failed in binding Int $x = "str"'
9e7fb12 @moritz test type checking of default values, RT #74758
moritz authored
89 {
90 sub opt-type1(Int $x?) { $x };
91 ok opt-type1() === Int,
92 'optional param with type constraints gets the right value';
93 sub opt-type2(Int $x = 'str') { };
94 nok eval('opt-type2()'), 'default values are type-checked';
95 }
96
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
97 # vim: ft=perl6
Something went wrong with that request. Please try again.