v0.18
[gitmo/MooseX-Params-Validate.git] / t / 009_wrapped.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
8
9 {
10     package Foo;
11     use Moose;
12     use MooseX::Params::Validate;
13
14     sub foo {
15         my $self   = shift;
16         my %params = validated_hash(
17             \@_,
18             foo => { isa => 'Str' },
19         );
20         return $params{foo};
21     }
22
23     around 'foo' => sub {
24         my $orig = shift;
25         my $self = shift;
26         my %p    = @_;
27
28         my @args = ( bar => delete $p{bar} );
29
30         my %params = validated_hash(
31             \@args,
32             bar => { isa => 'Str' },
33         );
34
35         $params{bar}, $self->$orig(%p);
36     };
37
38     around 'foo' => sub {
39         my $orig = shift;
40         my $self = shift;
41         my %p    = @_;
42
43         my @args = ( quux => delete $p{quux} );
44
45         my %params = validated_hash(
46             \@args,
47             quux => { isa => 'Str' },
48         );
49
50         $params{quux}, $self->$orig(%p);
51     };
52 }
53
54 {
55     my $foo = Foo->new;
56
57     is_deeply(
58         [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ],
59         [ 3, 2, 1 ],
60         'multiple around wrappers can safely be cached'
61     );
62
63     is_deeply(
64         [ $foo->foo( foo => 1, bar => 2, quux => 3 ) ],
65         [ 3, 2, 1 ],
66         'multiple around wrappers can safely be cached (2nd time)'
67     );
68 }
69
70 done_testing();