File: optional.t

package info (click to toggle)
libfunction-parameters-perl 2.001003-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 948 kB
  • sloc: perl: 6,478; makefile: 3
file content (54 lines) | stat: -rw-r--r-- 1,376 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!perl

# Test the $arg = undef optional syntax.

use strict;
use warnings FATAL => 'all';

use Test::More;

{
    package Stuff;

    use Test::More;
    use Test::Fatal;
    use Function::Parameters qw(:strict);

    method whatever($this = undef) {
        return $this;
    }

    is( Stuff->whatever(23),    23 );

    method things($this = 99) {
        return $this;
    }

    is( Stuff->things(),        99 );

    method some_optional($that, $this = undef) {
        return $that + ($this || 0);
    }

    is( Stuff->some_optional(18, 22), 18 + 22 );
    is( Stuff->some_optional(18), 18 );


    method named_params(:$this = undef, :$that = undef) {}

    is exception { Stuff->named_params(this => 0) }, undef, 'can leave out some named params';
    is exception { Stuff->named_params(         ) }, undef, 'can leave out all named params';


    # are slurpy parameters optional by default?
    # (throwing in a default just for a little feature interaction test)
    method slurpy_param($this, $that = 0, @other) {}

    my @a = ();
    is exception { Stuff->slurpy_param(0, 0, @a) }, undef, 'can pass empty array to slurpy param';
    is exception { Stuff->slurpy_param(0, 0    ) }, undef, 'can omit slurpy param altogether';
    is exception { Stuff->slurpy_param(0       ) }, undef, 'can omit other optional params as well as slurpy param';
}


done_testing;