9 # Note that setting coerce => 1 for the Num type tests that we don't try to do
10 # coercions for a type which doesn't have any coercions.
14 use Moose::Util::TypeConstraints;
15 use MooseX::Params::Validate;
17 subtype 'Size' => as 'Int' => where { $_ >= 0 };
19 coerce 'Size' => from 'ArrayRef' => via { scalar @{$_} };
23 my %params = validated_hash(
25 size1 => { isa => 'Size', coerce => 1 },
26 size2 => { isa => 'Size', coerce => 0 },
27 number => { isa => 'Num', coerce => 1 },
29 [ $params{size1}, $params{size2}, $params{number} ];
32 # added to test 'optional' on validated_hash
35 my %params = validated_hash(
37 size1 => { isa => 'Size', coerce => 1, optional => 1 },
38 size2 => { isa => 'Size', coerce => 0, optional => 1 },
39 number => { isa => 'Num', coerce => 1, optional => 1 },
41 [ $params{size1}, $params{size2}, $params{number} ];
46 my ( $size1, $size2, $number ) = validated_list(
48 size1 => { isa => 'Size', coerce => 1 },
49 size2 => { isa => 'Size', coerce => 0 },
50 number => { isa => 'Num', coerce => 1 },
52 [ $size1, $size2, $number ];
57 my ( $size1, $size2, $number ) = validated_list(
59 size1 => { isa => 'Size', coerce => 1, optional => 1 },
60 size2 => { isa => 'Size', coerce => 0, optional => 1 },
61 number => { isa => 'Num', coerce => 1, optional => 1 },
63 [ $size1, $size2, $number ];
68 my ( $size1, $size2, $number ) = pos_validated_list(
70 { isa => 'Size', coerce => 1, optional => 1 },
71 { isa => 'Size', coerce => 0, optional => 1 },
72 { isa => 'Num', coerce => 1, optional => 1 },
74 [ $size1, $size2, $number ];
79 isa_ok( $foo, 'Foo' );
82 $foo->bar( size1 => 10, size2 => 20, number => 30 ),
84 'got the return value right without coercions'
88 $foo->bar( size1 => [ 1, 2, 3 ], size2 => 20, number => 30 ),
90 'got the return value right with coercions for size1'
94 exception { $foo->bar( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) }
95 , qr/\QThe 'size2' parameter/, '... the size2 param cannot be coerced' );
98 exception { $foo->bar( size1 => 30, size2 => 10, number => 'something' ) }
99 , qr/\QThe 'number' parameter/,
100 '... the number param cannot be coerced because there is no coercion defined for Num'
104 $foo->baz( size1 => 10, size2 => 20, number => 30 ),
106 'got the return value right without coercions'
110 $foo->baz( size1 => [ 1, 2, 3 ], size2 => 20, number => 30 ),
112 'got the return value right with coercions for size1'
116 exception { $foo->baz( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) }
117 , qr/\QThe 'size2' parameter/, '... the size2 param cannot be coerced' );
120 exception { $foo->baz( size1 => 30, size2 => 10, number => 'something' ) }
121 , qr/\QThe 'number' parameter/,
122 '... the number param cannot be coerced'
126 $foo->baropt( size2 => 4 ),
128 '... validated_hash does not try to coerce keys which are not provided'
132 $foo->quux( size2 => 4 ),
134 '... validated_list does not try to coerce keys which are not provided'
138 $foo->ran_out( 1, 2, 3 ),
140 'got the return value right without coercions'
144 $foo->ran_out( [1], 2, 3 ),
146 'got the return value right with coercion for the first param'
150 exception { $foo->ran_out( [ 1, 2 ], [ 1, 2 ] ) }, qr/\QParameter #2/,
151 '... did not attempt to coerce the second parameter'
156 [ undef, undef, undef ],
157 'did not try to coerce non-existent parameters'