Add MX_PARAMS_VALIDATE_ALLOW_EXTRA
[gitmo/MooseX-Params-Validate.git] / t / 011_allow_extra.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
8
9 use MooseX::Params::Validate qw( validated_hash );
10
11 {
12     sub foo {
13         my %params = validated_hash(
14             \@_,
15             x => { isa => 'Int' },
16             y => { isa => 'Int' },
17         );
18         \%params;
19     }
20
21     sub bar {
22         my %params = validated_hash(
23             \@_,
24             x                              => { isa => 'Int' },
25             y                              => { isa => 'Int' },
26             MX_PARAMS_VALIDATE_ALLOW_EXTRA => 1,
27         );
28         \%params;
29     }
30 }
31
32 is_deeply(
33     bar( x => 42, y => 1 ),
34     { x => 42, y => 1 },
35     'bar returns expected values with no extra params'
36 );
37
38 is_deeply(
39     bar( x => 42, y => 1, z => 'whatever' ),
40     { x => 42, y => 1, z => 'whatever' },
41     'bar returns expected values with extra params'
42 );
43
44 like(
45     exception { foo( x => 42, y => 1, z => 'whatever' ) },
46     qr/The following parameter .+ listed in the validation options: z/,
47     'foo rejects extra params'
48 );
49
50 done_testing();