6 use Test::More tests => 15;
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} ];
47 my ( $size1, $size2, $number ) = validated_list(
49 size1 => { isa => 'Size', coerce => 1 },
50 size2 => { isa => 'Size', coerce => 0 },
51 number => { isa => 'Num', coerce => 1 },
53 [ $size1, $size2, $number ];
59 my ( $size1, $size2, $number ) = validated_list(
61 size1 => { isa => 'Size', coerce => 1, optional => 1 },
62 size2 => { isa => 'Size', coerce => 0, optional => 1 },
63 number => { isa => 'Num', coerce => 1, optional => 1 },
65 [ $size1, $size2, $number ];
70 my ( $size1, $size2, $number ) = pos_validated_list(
72 { isa => 'Size', coerce => 1, optional => 1 },
73 { isa => 'Size', coerce => 0, optional => 1 },
74 { isa => 'Num', coerce => 1, optional => 1 },
76 [ $size1, $size2, $number ];
81 isa_ok( $foo, 'Foo' );
84 $foo->bar( size1 => 10, size2 => 20, number => 30 ),
86 'got the return value right without coercions'
90 $foo->bar( size1 => [ 1, 2, 3 ], size2 => 20, number => 30 ),
92 'got the return value right with coercions for size1'
95 throws_ok { $foo->bar( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) }
96 qr/\QThe 'size2' parameter/,
97 '... the size2 param cannot be coerced';
99 throws_ok { $foo->bar( size1 => 30, size2 => 10, number => 'something' ) }
100 qr/\QThe 'number' parameter/,
101 '... 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'
115 throws_ok { $foo->baz( size1 => 30, size2 => [ 1, 2, 3 ], number => 30 ) }
116 qr/\QThe 'size2' parameter/,
117 '... the size2 param cannot be coerced';
119 throws_ok { $foo->baz( size1 => 30, size2 => 10, number => 'something' ) }
120 qr/\QThe 'number' parameter/,
121 '... the number param cannot be coerced';
124 $foo->baropt( size2 => 4 ),
126 '... validated_hash does not try to coerce keys which are not provided'
130 $foo->quux( size2 => 4 ),
132 '... validated_list does not try to coerce keys which are not provided'
136 $foo->ran_out( 1, 2, 3 ),
138 'got the return value right without coercions'
142 $foo->ran_out( [1], 2, 3 ),
144 'got the return value right with coercion for the first param'
147 throws_ok { $foo->ran_out( [ 1, 2 ], [ 1, 2 ] ) }
149 '... did not attempt to coerce the second parameter';
154 [ undef, undef, undef ],
155 'did not try to coerce non-existent parameters'