rename file to match other tests
[gitmo/MooseX-Params-Validate.git] / t / 008_positional.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 31;
7 use 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
63 my $foo = Foo->new;
64 isa_ok( $foo, 'Foo' );
65
66 is( $foo->baz($foo)->[0], $foo, '... first param must be a Foo instance' );
67
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';
74
75 is( $foo->baz( $foo, $foo )->[1], $foo,
76     '... second param must do Roles::Blah' );
77
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';
84
85 is( $foo->baz( $foo, $foo, $foo )->[2], $foo,
86     '... third param must do Roles::Blah' );
87
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';
94
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';
105
106 is_deeply(
107     $foo->bar($foo),
108     [$foo],
109     '... the first param in &bar got a Foo instance'
110 );
111
112 is_deeply(
113     $foo->bar( $foo, [] ),
114     [ $foo, [] ],
115     '... the first and second param in &bar got correct args'
116 );
117
118 is_deeply(
119     $foo->bar( $foo, {} ),
120     [ $foo,          {} ],
121     '... the first param and baz param in &bar got correct args'
122 );
123
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';
132
133 is_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
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]';
149