File: optional.t

package info (click to toggle)
libmethod-signatures-perl 20170211-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: perl: 3,863; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download | duplicates (7)
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
55
#!/usr/bin/perl -w

# Test the $arg? optional syntax.

use strict;
use warnings;

use Test::More;

{
    package Stuff;

    use Test::More;
    use Test::Exception;
    use Method::Signatures;

    method whatever($this?) {
        return $this;
    }

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

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

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

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

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


    # are named parameters optional by default?
    method named_params(:$this, :$that) {}

    lives_ok { Stuff->named_params(this => 0) } 'can leave out some named params';
    lives_ok { Stuff->named_params(         ) } '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 = ();
    lives_ok { Stuff->slurpy_param(0, 0, @a) } 'can pass empty array to slurpy param';
    lives_ok { Stuff->slurpy_param(0, 0    ) } 'can omit slurpy param altogether';
    lives_ok { Stuff->slurpy_param(0       ) } 'can omit other optional params as well as slurpy param';
}


done_testing;