1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8# Skip the test before Method::Signatures can try to compile it and blow up.
9BEGIN {
10    plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010;
11}
12
13{
14    package Stuff;
15
16    use Test::More;
17    use Method::Signatures;
18
19    method add($this = 23 when '', $that = 42 when '') {
20        no warnings 'uninitialized';
21        return $this + $that;
22    }
23
24    method minus($this is ro = 23 when '', $that is ro = 42 when "") {
25        return $this - $that;
26    }
27
28    is( Stuff->add(),      23 + 42 );
29    is( Stuff->add(''),    23 + 42 );
30    is( Stuff->add(undef),      42 );
31    is( Stuff->add(99),    99 + 42 );
32    is( Stuff->add(2,3),   5 );
33
34    is( Stuff->minus(),         23 - 42 );
35    is( Stuff->minus(''),       23 - 42 );
36    is( Stuff->minus(99),       99 - 42 );
37    is( Stuff->minus(2, 3),     2 - 3 );
38
39
40    # Test again that empty string doesn't override defaults
41    method echo($message = "what?" when q{}) {
42        return $message
43    }
44
45    is( Stuff->echo(),          "what?" );
46    is( Stuff->echo(''),        "what?" );
47    is( Stuff->echo("who?"),    'who?'  );
48
49
50    # Test that you can reference earlier args in a default
51    method copy_cat($this, $that = $this when '') {
52        return $that;
53    }
54
55    is( Stuff->copy_cat("wibble"), "wibble" );
56    is( Stuff->copy_cat("wibble", ""), "wibble" );
57    is( Stuff->copy_cat(23, 42),   42 );
58}
59
60{
61    package Bar;
62    use Test::More;
63    use Method::Signatures;
64
65    method hello($msg = "Hello, world!" when '') {
66        return $msg;
67    }
68
69    is( Bar->hello,               "Hello, world!" );
70    is( Bar->hello(q{}),        "Hello, world!" );
71    is( Bar->hello("Greetings!"), "Greetings!" );
72
73
74    method hi($msg = q,Hi, when '') {
75        return $msg;
76    }
77
78    is( Bar->hi,                "Hi" );
79    is( Bar->hi(q{}),         "Hi" );
80    is( Bar->hi("Yo"),          "Yo" );
81
82
83    method list(@args = (1,2,3) when ()) {
84        return @args;
85    }
86
87    is_deeply [Bar->list()],      [1,2,3];
88
89
90    method code($num, $code = sub { $num + 2 } when '') {
91        return $code->();
92    }
93
94    is( Bar->code(42), 44 );
95}
96
97done_testing;
98