Commit | Line | Data |
d9d1529d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d7a80104 |
6 | use Test::More; |
37088308 |
7 | use Test::Fatal; |
d9d1529d |
8 | use Scalar::Util; |
9 | |
10 | { |
11 | package Foo; |
12 | use Moose; |
13 | use MooseX::Params::Validate; |
14 | |
15 | sub bar { |
16 | my ( $self, $args, $params ) = @_; |
17 | $params->{MX_PARAMS_VALIDATE_CACHE_KEY} |
18 | = Scalar::Util::refaddr($self); |
19 | return validated_hash( $args, %$params ); |
20 | } |
21 | } |
22 | |
23 | my $foo = Foo->new; |
24 | isa_ok( $foo, 'Foo' ); |
25 | |
37088308 |
26 | is( |
27 | exception { |
28 | $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); |
29 | }, |
30 | undef, |
31 | '... successfully applied the parameter validation' |
32 | ); |
d9d1529d |
33 | |
37088308 |
34 | like( |
35 | exception { |
36 | $foo->bar( [ baz => [ 1, 2, 3 ] ], { baz => { isa => 'ArrayRef' } } ); |
37 | }, |
38 | qr/\QThe 'baz' parameter/, |
39 | '... successfully re-used the parameter validation for this instance' |
40 | ); |
d9d1529d |
41 | |
42 | my $foo2 = Foo->new; |
43 | isa_ok( $foo2, 'Foo' ); |
44 | |
37088308 |
45 | is( |
46 | exception { |
47 | $foo2->bar( |
48 | [ baz => [ 1, 2, 3 ] ], |
49 | { baz => { isa => 'ArrayRef' } } |
50 | ); |
51 | }, |
52 | undef, |
53 | '... successfully applied the parameter validation' |
54 | ); |
d9d1529d |
55 | |
37088308 |
56 | like( |
57 | exception { |
58 | $foo2->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); |
59 | }, |
60 | qr/\QThe 'baz' parameter/, |
61 | '... successfully re-used the parameter validation for this instance' |
62 | ); |
d9d1529d |
63 | |
37088308 |
64 | is( |
65 | exception { |
66 | $foo->bar( [ baz => 1 ], { baz => { isa => 'Int' } } ); |
67 | }, |
68 | undef, |
69 | '... successfully applied the parameter validation (just checking)' |
70 | ); |
d9d1529d |
71 | |
d7a80104 |
72 | done_testing(); |