Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 46 lines (32 sloc) 1.421 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
6 plan 11;
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';
82205cd [t/spec] oops, fudged the wrong test
moritz authored
30 #?rakudo 2 skip 'optional typed params, RT #61528'
6533150 [t/spec] more tests for passing optional arguments
moritz authored
31 is t_opt2('a'), 'a|undef', 'Can pass one of two optional params';
32 is t_opt2(), 'undef|undef', 'Can leave out all two optional params';
33
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
34 sub opt_typed(Int $p?) { defined($p) ?? $p !! 'undef' };
35
36 is opt_typed(2), 2, 'can pass optional typed param';
37 #?rakudo skip 'optional typed params, RT #61528'
38 is opt_typed() , 'undef', 'can leave out optional typed param';
39
288e046 [t] and [t/spec]
moritz authored
40 # L<S06/Parameters and arguments/"required positional parameters must come
41 # before those bound to optional positional">
42
43 eval_dies_ok 'sub wrong ($a?, $b)', 'options params before required ones are forbidden';
44
4ef6e53 [t/spec] tests for optional parameters, typed and untyped (RT #61528)
moritz authored
45 # vim: ft=perl6
Something went wrong with that request. Please try again.