1#!/usr/bin/perl -w
2
3# Test the bare sigil syntax: $, @ and %
4
5use strict;
6use warnings;
7
8use Test::More;
9
10use Method::Signatures;
11
12{
13    package Placeholder;
14
15    use lib 't/lib';
16    use GenErrorRegex qw< required_error required_placeholder_error >;
17
18    use Test::More;
19    use Test::Exception;
20    use Method::Signatures;
21
22    method only_placeholder($) {
23        return $self;
24    }
25
26    is( Placeholder->only_placeholder(23),    'Placeholder' );
27
28#line 28
29    throws_ok { Placeholder->only_placeholder() } required_placeholder_error('Placeholder', 0, 'only_placeholder', LINE => 28),
30            'simple required placeholder error okay';
31
32    method add_first_and_last($first!, $, $last = 22) {
33        return $first + $last
34    }
35
36    is( Placeholder->add_first_and_last(18, 19, 20), 18 + 20 );
37    is( Placeholder->add_first_and_last(18, 19),     18 + 22 );
38
39#line 39
40    throws_ok { Placeholder->add_first_and_last() } required_error('Placeholder', '$first', 'add_first_and_last', LINE => 39),
41            'missing required/named param error okay';
42
43#line 43
44    throws_ok { Placeholder->add_first_and_last(18) } required_placeholder_error('Placeholder', 1, 'add_first_and_last', LINE => 43),
45            'missing required placeholder after required param error okay';
46
47    method slurpy($foo, @) {
48        $foo
49    }
50
51    is( Placeholder->slurpy(123), 123, 'slurpy, no extras');
52    is( Placeholder->slurpy(123, 456, 789), 123, 'slurpy with extras');
53
54    method slurpy_hash($foo, %) {
55        $foo
56    }
57
58    is( Placeholder->slurpy_hash(123), 123, 'slurpy_hash, no extras');
59    is( Placeholder->slurpy_hash(123, a => 1, b => 2), 123, 'slurpy_hash with extras');
60    throws_ok { Placeholder->slurpy_hash(123, 456, a => 1) }
61        qr{was given an odd number of arguments for a placeholder hash},
62        'slurpy_hash with odd number of extras throws exception';
63
64    method optional_placeholder($foo, $?, $bar?) {
65        return [ $foo, $bar ];
66    }
67
68    is_deeply( Placeholder->optional_placeholder(1), [ 1, undef ], 'optional_placeholder with 1 arg');
69    is_deeply( Placeholder->optional_placeholder(1, 2), [ 1, undef ], 'optional_placeholder with 2 args');
70    is_deeply( Placeholder->optional_placeholder(1, 2, 3), [ 1, 3 ], 'optional_placeholder with 3 args');
71}
72
73done_testing();
74