Commit | Line | Data |
633048d5 |
1 | #!perl |
2 | |
3 | # Test the $arg = undef optional syntax. |
4 | |
5 | use strict; |
6 | use warnings FATAL => 'all'; |
7 | |
8 | use Test::More; |
9 | |
10 | { |
11 | package Stuff; |
12 | |
13 | use Test::More; |
14 | use Test::Fatal; |
15 | use Function::Parameters qw(:strict); |
16 | |
17 | method whatever($this = undef) { |
18 | return $this; |
19 | } |
20 | |
21 | is( Stuff->whatever(23), 23 ); |
22 | |
23 | method things($this = 99) { |
24 | return $this; |
25 | } |
26 | |
27 | is( Stuff->things(), 99 ); |
28 | |
29 | method some_optional($that, $this = undef) { |
30 | return $that + ($this || 0); |
31 | } |
32 | |
33 | is( Stuff->some_optional(18, 22), 18 + 22 ); |
34 | is( Stuff->some_optional(18), 18 ); |
35 | |
36 | |
98e6239b |
37 | method named_params(:$this = undef, :$that = undef) {} |
38 | |
39 | is exception { Stuff->named_params(this => 0) }, undef, 'can leave out some named params'; |
40 | is exception { Stuff->named_params( ) }, undef, 'can leave out all named params'; |
633048d5 |
41 | |
42 | |
43 | # are slurpy parameters optional by default? |
44 | # (throwing in a default just for a little feature interaction test) |
45 | method slurpy_param($this, $that = 0, @other) {} |
46 | |
47 | my @a = (); |
48 | is exception { Stuff->slurpy_param(0, 0, @a) }, undef, 'can pass empty array to slurpy param'; |
49 | is exception { Stuff->slurpy_param(0, 0 ) }, undef, 'can omit slurpy param altogether'; |
50 | is exception { Stuff->slurpy_param(0 ) }, undef, 'can omit other optional params as well as slurpy param'; |
51 | } |
52 | |
53 | |
54 | done_testing; |