1use strict; 2use warnings; 3 4use File::Spec; 5use lib File::Spec->catdir( 't', 'lib' ); 6 7use PVTests; 8use Test::More tests => 10; 9 10use Attribute::Params::Validate; 11use Params::Validate qw(:all); 12 13 14sub foo :Validate( c => { type => SCALAR } ) 15{ 16 my %data = @_; 17 return $data{c}; 18} 19 20sub bar :Validate( c => { type => SCALAR } ) method 21{ 22 my $self = shift; 23 my %data = @_; 24 return $data{c}; 25} 26 27sub baz :Validate( foo => { type => ARRAYREF, callbacks => { '5 elements' => sub { @{shift()} == 5 } } } ) 28{ 29 my %data = @_; 30 return $data{foo}->[0]; 31} 32 33sub buz : ValidatePos( 1 ) 34{ 35 return $_[0]; 36} 37 38sub quux :ValidatePos( { type => SCALAR }, 1 ) 39{ 40 return $_[0]; 41} 42 43my $res = eval { foo( c => 1 ) }; 44is( $@, q{}, 45 "Call foo with a scalar" ); 46 47is( $res, 1, 48 'Check return value from foo( c => 1 )' ); 49 50eval { foo( c => [] ) }; 51 52like( $@, qr/The 'c' parameter .* was an 'arrayref'/, 53 'Check exception thrown from foo( c => [] )' ); 54 55$res = eval { main->bar( c => 1 ) }; 56is( $@, q{}, 57 'Call bar with a scalar' ); 58 59is( $res, 1, 60 'Check return value from bar( c => 1 )' ); 61 62eval { baz( foo => [1,2,3,4] ) }; 63 64like( $@, qr/The 'foo' parameter .* did not pass the '5 elements' callback/, 65 'Check exception thrown from baz( foo => [1,2,3,4] )' ); 66 67$res = eval { baz( foo => [5,4,3,2,1] ) }; 68 69is( $@, q{}, 70 'Call baz( foo => [5,4,3,2,1] )' ); 71 72is( $res, 5, 73 'Check return value from baz( foo => [5,4,3,2,1] )' ); 74 75eval { buz( [], 1 ) }; 76 77like( $@, qr/2 parameters were passed to .* but 1 was expected/, 78 'Check exception thrown from quux( [], 1 )' ); 79 80$res = eval { quux( 1, [] ) }; 81 82is( $@, q{}, 83 'Call quux' ); 84