1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8BEGIN {
9    plan skip_all => "Perl 5.10 or higher required to test block defaults", 1 if $] < 5.010;
10}
11
12# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error
13use Method::Signatures;
14
15
16{
17    package Stuff;
18
19    use Test::More;
20    use Method::Signatures;
21
22    method add($this = 23 when {$_ < 23}, $that = 42 when {42 < $_}) {
23        return $this + $that;
24    }
25
26    # Check that it recognizes hashes
27    method add_block($this = 23 when { 2 => 'bad' }, $that = 42 when { 42 < $_ } ) {
28        return $this + $that;
29    }
30
31    # Check that it disambiguates blocks
32    method add_dis($this = 23 when {; 2 => 'bad' }, $that = 42 when { 42 < $_ } ) {
33        return $this + $that;
34    }
35
36    method minus($this is ro = 23 when undef, $that is ro = 42 when {($_ % 2)}) {
37        return $this - $that;
38    }
39
40    is( Stuff->add(),      23 + 42 );
41    is( Stuff->add(undef), 23 + 42 );
42    is( Stuff->add(99),    99 + 42 );
43    is( Stuff->add(2,3),   23 + 3  );
44    is( Stuff->add(24,3),  24 + 3  );
45
46    is( Stuff->add_block(),      23 + 42 );
47    is( Stuff->add_block(99),    99 + 42 );
48    is( Stuff->add_block(2,3),   23 + 3  );
49    is( Stuff->add_block(4,3),    4 + 3  );
50    is( Stuff->add_block(24,3),  24 + 3  );
51
52    is( Stuff->add_dis(),      23 + 42 );
53    is( Stuff->add_dis(99),    23 + 42 );
54    is( Stuff->add_dis(2,3),   23 + 3  );
55    is( Stuff->add_dis(4,3),   23 + 3  );
56    is( Stuff->add_dis(24,3),  23 + 3  );
57
58    is( Stuff->minus(),         23 - 42 );
59    is( Stuff->minus(undef),    23 - 42 );
60    is( Stuff->minus(99),       99 - 42 );
61    is( Stuff->minus(2, 3),      2 - 42 );
62    is( Stuff->minus(2, 4),      2 - 4  );
63}
64
65done_testing;
66