Use done_testing (require Test::More 0.88)
[gitmo/MooseX-Params-Validate.git] / t / 008_positional.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 return [
35 pos_validated_list(
36 \@_,
37 { isa => 'Foo' },
38 { isa => 'ArrayRef | HashRef', optional => 1 },
39 { isa => 'ArrayRef[Int]', optional => 1 },
40 )
41 ];
42 }
43
44 sub baz {
45 my $self = shift;
46 return [
47 pos_validated_list(
48 \@_,
49 {
50 isa => subtype( 'Object' => where { $_->isa('Foo') } ),
51 optional => 1
52 },
53 { does => 'Roles::Blah', optional => 1 },
54 {
55 does => role_type('Roles::Blah'),
56 optional => 1
57 },
58 )
59 ];
60 }
61}
62
63my $foo = Foo->new;
64isa_ok( $foo, 'Foo' );
65
66is( $foo->baz($foo)->[0], $foo, '... first param must be a Foo instance' );
67
68throws_ok { $foo->baz(10) } qr/\QParameter #1 ("10")/,
69 '... the first param in &baz must be a Foo instance';
70throws_ok { $foo->baz('foo') } qr/\QParameter #1 ("foo")/,
71 '... the first param in &baz must be a Foo instance';
72throws_ok { $foo->baz( [] ) } qr/\QParameter #1/,
73 '... the first param in &baz must be a Foo instance';
74
75is( $foo->baz( $foo, $foo )->[1], $foo,
76 '... second param must do Roles::Blah' );
77
78throws_ok { $foo->baz( $foo, 10 ) } qr/\QParameter #2 ("10")/,
79 '... the second param in &baz must be do Roles::Blah';
80throws_ok { $foo->baz( $foo, 'foo' ) } qr/\QParameter #2 ("foo")/,
81 '... the second param in &baz must be do Roles::Blah';
82throws_ok { $foo->baz( $foo, [] ) } qr/\QParameter #2/,
83 '... the second param in &baz must be do Roles::Blah';
84
85is( $foo->baz( $foo, $foo, $foo )->[2], $foo,
86 '... third param must do Roles::Blah' );
87
88throws_ok { $foo->baz( $foo, $foo, 10 ) } qr/\QParameter #3 ("10")/,
89 '... the third param in &baz must be do Roles::Blah';
90throws_ok { $foo->baz( $foo, $foo, "foo" ) } qr/\QParameter #3 ("foo")/,
91 '... the third param in &baz must be do Roles::Blah';
92throws_ok { $foo->baz( $foo, $foo, [] ) } qr/\QParameter #3/,
93 '... the third param in &baz must be do Roles::Blah';
94
95throws_ok { $foo->bar } qr/\Q0 parameters were passed/,
96 '... bar has a required params';
97throws_ok { $foo->bar(10) } qr/\QParameter #1 ("10")/,
98 '... the first param in &bar must be a Foo instance';
99throws_ok { $foo->bar('foo') } qr/\QParameter #1 ("foo")/,
100 '... the first param in &bar must be a Foo instance';
101throws_ok { $foo->bar( [] ) } qr/\QParameter #1/,
102 '... the first param in &bar must be a Foo instance';
103throws_ok { $foo->bar() } qr/\Q0 parameters were passed/,
104 '... bar has a required first param';
105
106is_deeply(
107 $foo->bar($foo),
108 [$foo],
109 '... the first param in &bar got a Foo instance'
110);
111
112is_deeply(
113 $foo->bar( $foo, [] ),
114 [ $foo, [] ],
115 '... the first and second param in &bar got correct args'
116);
117
118is_deeply(
119 $foo->bar( $foo, {} ),
120 [ $foo, {} ],
121 '... the first param and baz param in &bar got correct args'
122);
123
124throws_ok { $foo->bar( $foo, undef ) } qr/\QParameter #2 (undef)/,
125 '... second param requires a ArrayRef | HashRef';
126throws_ok { $foo->bar( $foo, 10 ) } qr/\QParameter #2 ("10")/,
127 '... second param requires a ArrayRef | HashRef';
128throws_ok { $foo->bar( $foo, 'Foo' ) } qr/\QParameter #2 ("Foo")/,
129 '... second param requires a ArrayRef | HashRef';
130throws_ok { $foo->bar( $foo, \( my $var ) ) } qr/\QParameter #2/,
131 '... second param requires a ArrayRef | HashRef';
132
133is_deeply(
134 $foo->bar( $foo, {}, [ 1, 2, 3 ] ),
135 [ $foo, {}, [ 1, 2, 3 ] ],
136 '... the first param in &bar got a Foo instance'
137);
138
139throws_ok { $foo->bar( $foo, {}, undef ) } qr/\QParameter #3 (undef)/,
140'... third param a ArrayRef[Int]';
141throws_ok { $foo->bar( $foo, {}, 10 ) } qr/\QParameter #3 ("10")/,
142'... third param a ArrayRef[Int]';
143throws_ok { $foo->bar( $foo, {}, 'Foo' ) } qr/\QParameter #3 ("Foo")/,
144'... third param a ArrayRef[Int]';
145throws_ok { $foo->bar( $foo, {}, \( my $var ) ) } qr/\QParameter #3/,
146'... third param a ArrayRef[Int]';
147throws_ok { $foo->bar( $foo, {}, [qw/one two three/] ) } qr/\QParameter #3/,
148'... third param a ArrayRef[Int]';
149
d7a80104 150done_testing();