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