6 use Test::More tests => 31;
12 use MooseX::Params::Validate;
18 my ( $self, %params ) = validated_hash(
20 bar => { isa => 'Str', default => 'Moose' },
22 return "Horray for $params{bar}!";
27 use Moose::Util::TypeConstraints;
28 use MooseX::Params::Validate;
38 { isa => 'ArrayRef | HashRef', optional => 1 },
39 { isa => 'ArrayRef[Int]', optional => 1 },
50 isa => subtype( 'Object' => where { $_->isa('Foo') } ),
53 { does => 'Roles::Blah', optional => 1 },
55 does => role_type('Roles::Blah'),
64 isa_ok( $foo, 'Foo' );
66 is( $foo->baz($foo)->[0], $foo, '... first param must be a Foo instance' );
68 throws_ok { $foo->baz(10) } qr/\QParameter #1 ("10")/,
69 '... the first param in &baz must be a Foo instance';
70 throws_ok { $foo->baz('foo') } qr/\QParameter #1 ("foo")/,
71 '... the first param in &baz must be a Foo instance';
72 throws_ok { $foo->baz( [] ) } qr/\QParameter #1/,
73 '... the first param in &baz must be a Foo instance';
75 is( $foo->baz( $foo, $foo )->[1], $foo,
76 '... second param must do Roles::Blah' );
78 throws_ok { $foo->baz( $foo, 10 ) } qr/\QParameter #2 ("10")/,
79 '... the second param in &baz must be do Roles::Blah';
80 throws_ok { $foo->baz( $foo, 'foo' ) } qr/\QParameter #2 ("foo")/,
81 '... the second param in &baz must be do Roles::Blah';
82 throws_ok { $foo->baz( $foo, [] ) } qr/\QParameter #2/,
83 '... the second param in &baz must be do Roles::Blah';
85 is( $foo->baz( $foo, $foo, $foo )->[2], $foo,
86 '... third param must do Roles::Blah' );
88 throws_ok { $foo->baz( $foo, $foo, 10 ) } qr/\QParameter #3 ("10")/,
89 '... the third param in &baz must be do Roles::Blah';
90 throws_ok { $foo->baz( $foo, $foo, "foo" ) } qr/\QParameter #3 ("foo")/,
91 '... the third param in &baz must be do Roles::Blah';
92 throws_ok { $foo->baz( $foo, $foo, [] ) } qr/\QParameter #3/,
93 '... the third param in &baz must be do Roles::Blah';
95 throws_ok { $foo->bar } qr/\Q0 parameters were passed/,
96 '... bar has a required params';
97 throws_ok { $foo->bar(10) } qr/\QParameter #1 ("10")/,
98 '... the first param in &bar must be a Foo instance';
99 throws_ok { $foo->bar('foo') } qr/\QParameter #1 ("foo")/,
100 '... the first param in &bar must be a Foo instance';
101 throws_ok { $foo->bar( [] ) } qr/\QParameter #1/,
102 '... the first param in &bar must be a Foo instance';
103 throws_ok { $foo->bar() } qr/\Q0 parameters were passed/,
104 '... bar has a required first param';
109 '... the first param in &bar got a Foo instance'
113 $foo->bar( $foo, [] ),
115 '... the first and second param in &bar got correct args'
119 $foo->bar( $foo, {} ),
121 '... the first param and baz param in &bar got correct args'
124 throws_ok { $foo->bar( $foo, undef ) } qr/\QParameter #2 (undef)/,
125 '... second param requires a ArrayRef | HashRef';
126 throws_ok { $foo->bar( $foo, 10 ) } qr/\QParameter #2 ("10")/,
127 '... second param requires a ArrayRef | HashRef';
128 throws_ok { $foo->bar( $foo, 'Foo' ) } qr/\QParameter #2 ("Foo")/,
129 '... second param requires a ArrayRef | HashRef';
130 throws_ok { $foo->bar( $foo, \( my $var ) ) } qr/\QParameter #2/,
131 '... second param requires a ArrayRef | HashRef';
134 $foo->bar( $foo, {}, [ 1, 2, 3 ] ),
135 [ $foo, {}, [ 1, 2, 3 ] ],
136 '... the first param in &bar got a Foo instance'
139 throws_ok { $foo->bar( $foo, {}, undef ) } qr/\QParameter #3 (undef)/,
140 '... third param a ArrayRef[Int]';
141 throws_ok { $foo->bar( $foo, {}, 10 ) } qr/\QParameter #3 ("10")/,
142 '... third param a ArrayRef[Int]';
143 throws_ok { $foo->bar( $foo, {}, 'Foo' ) } qr/\QParameter #3 ("Foo")/,
144 '... third param a ArrayRef[Int]';
145 throws_ok { $foo->bar( $foo, {}, \( my $var ) ) } qr/\QParameter #3/,
146 '... third param a ArrayRef[Int]';
147 throws_ok { $foo->bar( $foo, {}, [qw/one two three/] ) } qr/\QParameter #3/,
148 '... third param a ArrayRef[Int]';