File: when.t

package info (click to toggle)
libmethod-signatures-perl 20170211-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 672 kB
  • sloc: perl: 3,860; makefile: 2
file content (52 lines) | stat: -r--r--r-- 1,209 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

# Skip the test before Method::Signatures can try to compile it and blow up.
BEGIN {
    plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010;
}

use Method::Signatures;

subtest "when {}" => sub {
    func empty_hash( HashRef[Int] $ref = { foo => 23, bar => 42 } when {} ) {
        return $ref;
    }

    is_deeply empty_hash(),                    { foo => 23, bar => 42 };
    is_deeply empty_hash({}),                  { foo => 23, bar => 42 };
    is_deeply empty_hash({ this => 23 }),      { this => 23 };
};


subtest "when []" => sub {
    func empty_array( ArrayRef[Int] $ref = [1,2,3] when [] ) {
        return $ref;
    }

    is_deeply empty_array(),                    [1,2,3];
    is_deeply empty_array([]),                  [1,2,3];
    is_deeply empty_array([4,5,6]),             [4,5,6];
};


subtest "Defaults are applied before type check" => sub {
    package Baz;
    use Test::More;
    use Method::Signatures;

    func hi(
        Str $place //= "World"
    ) {
        return "Hi, $place!\n";
    }

    is hi(),      "Hi, World!\n";
    is hi(undef), "Hi, World!\n";
};

done_testing;