Update repo address
[gitmo/MooseX-Params-Validate.git] / t / 001_basic.t
CommitLineData
d9d1529d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d7a80104 6use Test::More;
d9d1529d 7use Test::Exception;
8
9{
10 package Roles::Blah;
11 use Moose::Role;
12 use MooseX::Params::Validate;
13
14 requires 'bar';
15 requires 'baz';
16
17 sub foo {
18 my ( $self, %params ) = validated_hash(
19 \@_,
20 bar => { isa => 'Str', default => 'Moose' },
21 );
22 return "Horray for $params{bar}!";
23 }
24
25 package Foo;
26 use Moose;
27 use Moose::Util::TypeConstraints;
28 use MooseX::Params::Validate;
29
30 with 'Roles::Blah';
31
32 sub bar {
33 my $self = shift;
34 my %params = validated_hash(
35 \@_,
36 foo => { isa => 'Foo' },
37 baz => { isa => 'ArrayRef | HashRef', optional => 1 },
38 gorch => { isa => 'ArrayRef[Int]', optional => 1 },
39 );
40 [ $params{foo}, $params{baz}, $params{gorch} ];
41 }
42
43 sub baz {
44 my $self = shift;
45 my %params = validated_hash(
46 \@_,
47 foo => {
48 isa => subtype( 'Object' => where { $_->isa('Foo') } ),
49 optional => 1
50 },
51 bar => { does => 'Roles::Blah', optional => 1 },
52 boo => {
53 does => role_type('Roles::Blah'),
54 optional => 1
55 },
56 );
57 return $params{foo} || $params{bar} || $params{boo};
58 }
59
60 sub quux {
61 my $self = shift;
62 my %params = validated_hash(
63 \@_,
64 foo => {
65 isa => 'ArrayRef',
66 callbacks => {
67 'some random callback' => sub { @{ $_[0] } <= 2 },
68 },
69 },
70 );
71
72 return $params{foo};
73 }
74}
75
76my $foo = Foo->new;
77isa_ok( $foo, 'Foo' );
78
79is( $foo->foo, 'Horray for Moose!', '... got the right return value' );
80is( $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!',
81 '... got the right return value' );
82
83is( $foo->baz( foo => $foo ), $foo, '... foo param must be a Foo instance' );
84
85throws_ok { $foo->baz( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/,
86 '... the foo param in &baz must be a Foo instance';
87throws_ok { $foo->baz( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/,
88 '... the foo param in &baz must be a Foo instance';
89throws_ok { $foo->baz( foo => [] ) } qr/\QThe 'foo' parameter/,
90 '... the foo param in &baz must be a Foo instance';
91
92is( $foo->baz( bar => $foo ), $foo, '... bar param must do Roles::Blah' );
93
94throws_ok { $foo->baz( bar => 10 ) } qr/\QThe 'bar' parameter ("10")/,
95'... the bar param in &baz must be do Roles::Blah';
96throws_ok { $foo->baz( bar => "foo" ) } qr/\QThe 'bar' parameter ("foo")/,
97'... the bar param in &baz must be do Roles::Blah';
98throws_ok { $foo->baz( bar => [] ) } qr/\QThe 'bar' parameter/,
99'... the bar param in &baz must be do Roles::Blah';
100
101is( $foo->baz( boo => $foo ), $foo, '... boo param must do Roles::Blah' );
102
103throws_ok { $foo->baz( boo => 10 ) } qr/\QThe 'boo' parameter ("10")/,
104'... the boo param in &baz must be do Roles::Blah';
105throws_ok { $foo->baz( boo => "foo" ) } qr/\QThe 'boo' parameter ("foo")/,
106'... the boo param in &baz must be do Roles::Blah';
107throws_ok { $foo->baz( boo => [] ) } qr/\QThe 'boo' parameter/,
108'... the boo param in &baz must be do Roles::Blah';
109
110throws_ok { $foo->bar } qr/\QMandatory parameter 'foo'/,
111 '... bar has a required param';
112throws_ok { $foo->bar( foo => 10 ) } qr/\QThe 'foo' parameter ("10")/,
113 '... the foo param in &bar must be a Foo instance';
114throws_ok { $foo->bar( foo => "foo" ) } qr/\QThe 'foo' parameter ("foo")/,
115 '... the foo param in &bar must be a Foo instance';
116throws_ok { $foo->bar( foo => [] ) } qr/\QThe 'foo' parameter/,
117 '... the foo param in &bar must be a Foo instance';
118throws_ok { $foo->bar( baz => [] ) } qr/\QMandatory parameter 'foo'/,,
119 '... bar has a required foo param';
120
121is_deeply(
122 $foo->bar( foo => $foo ),
123 [ $foo, undef, undef ],
124 '... the foo param in &bar got a Foo instance'
125);
126
127is_deeply(
128 $foo->bar( foo => $foo, baz => [] ),
129 [ $foo, [], undef ],
130 '... the foo param and baz param in &bar got a correct args'
131);
132
133is_deeply(
134 $foo->bar( foo => $foo, baz => {} ),
135 [ $foo, {}, undef ],
136 '... the foo param and baz param in &bar got a correct args'
137);
138
139throws_ok { $foo->bar( foo => $foo, baz => undef ) }
140qr/\QThe 'baz' parameter (undef)/,
141 '... baz requires a ArrayRef | HashRef';
142throws_ok { $foo->bar( foo => $foo, baz => 10 ) }
143qr/\QThe 'baz' parameter ("10")/,
144 '... baz requires a ArrayRef | HashRef';
145throws_ok { $foo->bar( foo => $foo, baz => 'Foo' ) }
146qr/\QThe 'baz' parameter ("Foo")/,
147 '... baz requires a ArrayRef | HashRef';
148throws_ok { $foo->bar( foo => $foo, baz => \( my $var ) ) }
149qr/\QThe 'baz' parameter/,
150 '... baz requires a ArrayRef | HashRef';
151
152is_deeply(
153 $foo->bar( foo => $foo, gorch => [ 1, 2, 3 ] ),
154 [ $foo, undef, [ 1, 2, 3 ] ],
155 '... the foo param in &bar got a Foo instance'
156);
157
158throws_ok { $foo->bar( foo => $foo, gorch => undef ) }
159qr/\QThe 'gorch' parameter (undef)/,
160 '... gorch requires a ArrayRef[Int]';
161throws_ok { $foo->bar( foo => $foo, gorch => 10 ) }
162qr/\QThe 'gorch' parameter ("10")/,
163 '... gorch requires a ArrayRef[Int]';
164throws_ok { $foo->bar( foo => $foo, gorch => 'Foo' ) }
165qr/\QThe 'gorch' parameter ("Foo")/,
166 '... gorch requires a ArrayRef[Int]';
167throws_ok { $foo->bar( foo => $foo, gorch => \( my $var ) ) }
168qr/\QThe 'gorch' parameter/,
169 '... gorch requires a ArrayRef[Int]';
170throws_ok { $foo->bar( foo => $foo, gorch => [qw/one two three/] ) }
171qr/\QThe 'gorch' parameter/,
172 '... gorch requires a ArrayRef[Int]';
173
174throws_ok { $foo->quux( foo => '123456790' ) }
175qr/\QThe 'foo' parameter\E.+\Qchecking type constraint/,
176'... foo parameter must be an ArrayRef';
177
178throws_ok { $foo->quux( foo => [ 1, 2, 3, 4 ] ) }
179qr/\QThe 'foo' parameter\E.+\Qsome random callback/,
180'... foo parameter additional callback requires that arrayref be 0-2 elements';
d7a80104 181
182done_testing();