File: block_defaults.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 (65 lines) | stat: -rw-r--r-- 1,754 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
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

BEGIN {
    plan skip_all => "Perl 5.10 or higher required to test block defaults", 1 if $] < 5.010;
}

# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error
use Method::Signatures;


{
    package Stuff;

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

    method add($this = 23 when {$_ < 23}, $that = 42 when {42 < $_}) {
        return $this + $that;
    }

    # Check that it recognizes hashes
    method add_block($this = 23 when { 2 => 'bad' }, $that = 42 when { 42 < $_ } ) {
        return $this + $that;
    }

    # Check that it disambiguates blocks
    method add_dis($this = 23 when {; 2 => 'bad' }, $that = 42 when { 42 < $_ } ) {
        return $this + $that;
    }

    method minus($this is ro = 23 when undef, $that is ro = 42 when {($_ % 2)}) {
        return $this - $that;
    }

    is( Stuff->add(),      23 + 42 );
    is( Stuff->add(undef), 23 + 42 );
    is( Stuff->add(99),    99 + 42 );
    is( Stuff->add(2,3),   23 + 3  );
    is( Stuff->add(24,3),  24 + 3  );

    is( Stuff->add_block(),      23 + 42 );
    is( Stuff->add_block(99),    99 + 42 );
    is( Stuff->add_block(2,3),   23 + 3  );
    is( Stuff->add_block(4,3),    4 + 3  );
    is( Stuff->add_block(24,3),  24 + 3  );

    is( Stuff->add_dis(),      23 + 42 );
    is( Stuff->add_dis(99),    23 + 42 );
    is( Stuff->add_dis(2,3),   23 + 3  );
    is( Stuff->add_dis(4,3),   23 + 3  );
    is( Stuff->add_dis(24,3),  23 + 3  );

    is( Stuff->minus(),         23 - 42 );
    is( Stuff->minus(undef),    23 - 42 );
    is( Stuff->minus(99),       99 - 42 );
    is( Stuff->minus(2, 3),      2 - 42 );
    is( Stuff->minus(2, 4),      2 - 4  );
}

done_testing;