File: 001_basic.t

package info (click to toggle)
libmoosex-params-validate-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 511; makefile: 2
file content (277 lines) | stat: -rw-r--r-- 7,225 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
## no critic (Moose::RequireCleanNamespace, Modules::ProhibitMultiplePackages, Moose::RequireMakeImmutable)
use strict;
use warnings;

use Test::More 0.88;
use Test::Fatal;

{
    package Roles::Blah;
    use Moose::Role;
    use MooseX::Params::Validate;

    requires 'bar';
    requires 'baz';

    sub foo {
        my ( $self, %params ) = validated_hash(
            \@_,
            bar => { isa => 'Str', default => 'Moose' },
        );
        return "Hooray for $params{bar}!";
    }

    package Foo;
    use Moose;
    use Moose::Util::TypeConstraints;
    use MooseX::Params::Validate;

    with 'Roles::Blah';

    sub bar {
        my $self   = shift;
        my %params = validated_hash(
            \@_,
            foo   => { isa => 'Foo' },
            baz   => { isa => 'ArrayRef | HashRef', optional => 1 },
            gorch => { isa => 'ArrayRef[Int]', optional => 1 },
        );
        [ $params{foo}, $params{baz}, $params{gorch} ];
    }

    sub baz {
        my $self   = shift;
        my %params = validated_hash(
            \@_,
            foo => {
                isa      => subtype( 'Object' => where { $_->isa('Foo') } ),
                optional => 1
            },
            bar => { does => 'Roles::Blah', optional => 1 },
            boo => {
                does     => role_type('Roles::Blah'),
                optional => 1
            },
        );
        return $params{foo} || $params{bar} || $params{boo};
    }

    sub quux {
        my $self   = shift;
        my %params = validated_hash(
            \@_,
            foo => {
                isa       => 'ArrayRef',
                callbacks => {
                    'some random callback' =>
                        sub { !ref( $_[0] ) || @{ $_[0] } <= 2 },
                },
            },
        );

        return $params{foo};
    }

    sub boo {
        my $self   = shift;
        my %params = validated_hash(
            \@_,
            foo => {
                isa      => 'Str',
                optional => 1,
                depends  => ['bar']
            },
            bar => {
                isa      => 'Str',
                optional => 1,
                depends  => ['foo']
            },
        );
        return 'foobar';
    }
}

my $foo = Foo->new;
isa_ok( $foo, 'Foo' );

is( $foo->foo, 'Hooray for Moose!', '... got the right return value' );
is(
    $foo->foo( bar => 'Rolsky' ), 'Hooray for Rolsky!',
    '... got the right return value'
);

is( $foo->baz( foo => $foo ), $foo, '... foo param must be a Foo instance' );

like(
    exception { $foo->baz( foo => 10 ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &baz must be a Foo instance'
);
like(
    exception { $foo->baz( foo => 'foo' ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &baz must be a Foo instance'
);
like(
    exception { $foo->baz( foo => [] ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &baz must be a Foo instance'
);

is( $foo->baz( bar => $foo ), $foo, '... bar param must do Roles::Blah' );

like(
    exception { $foo->baz( bar => 10 ) },
    qr/\QThe 'bar' parameter/,
    '... the bar param in &baz must be do Roles::Blah'
);
like(
    exception { $foo->baz( bar => 'foo' ) },
    qr/\QThe 'bar' parameter/,
    '... the bar param in &baz must be do Roles::Blah'
);
like(
    exception { $foo->baz( bar => [] ) },
    qr/\QThe 'bar' parameter/,
    '... the bar param in &baz must be do Roles::Blah'
);

is( $foo->baz( boo => $foo ), $foo, '... boo param must do Roles::Blah' );

like(
    exception { $foo->baz( boo => 10 ) },
    qr/\QThe 'boo' parameter/,
    '... the boo param in &baz must be do Roles::Blah'
);
like(
    exception { $foo->baz( boo => 'foo' ) },
    qr/\QThe 'boo' parameter/,
    '... the boo param in &baz must be do Roles::Blah'
);
like(
    exception { $foo->baz( boo => [] ) },
    qr/\QThe 'boo' parameter/,
    '... the boo param in &baz must be do Roles::Blah'
);

like(
    exception { $foo->bar },
    qr/\QMandatory parameter 'foo'/,
    '... bar has a required param'
);
like(
    exception { $foo->bar( foo => 10 ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &bar must be a Foo instance'
);
like(
    exception { $foo->bar( foo => 'foo' ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &bar must be a Foo instance'
);
like(
    exception { $foo->bar( foo => [] ) },
    qr/\QThe 'foo' parameter/,
    '... the foo param in &bar must be a Foo instance'
);
like(
    exception { $foo->bar( baz => [] ) },
    qr/\QMandatory parameter 'foo'/,
    '.. the foo param is mandatory'
);

is_deeply(
    $foo->bar( foo => $foo ),
    [ $foo, undef, undef ],
    '... the foo param in &bar got a Foo instance'
);

is_deeply(
    $foo->bar( foo => $foo, baz => [] ),
    [ $foo, [], undef ],
    '... the foo param and baz param in &bar got a correct args'
);

is_deeply(
    $foo->bar( foo => $foo, baz => {} ),
    [ $foo, {}, undef ],
    '... the foo param and baz param in &bar got a correct args'
);

like(
    exception { $foo->bar( foo => $foo, baz => undef ) },
    qr/\QThe 'baz' parameter/,
    '... baz requires a ArrayRef | HashRef'
);
like(
    exception { $foo->bar( foo => $foo, baz => 10 ) },
    qr/\QThe 'baz' parameter/,
    '... baz requires a ArrayRef | HashRef'
);
like(
    exception { $foo->bar( foo => $foo, baz => 'Foo' ) },
    qr/\QThe 'baz' parameter/,
    '... baz requires a ArrayRef | HashRef'
);
like(
    exception { $foo->bar( foo => $foo, baz => \( my $var ) ) },
    qr/\QThe 'baz' parameter/, '... baz requires a ArrayRef | HashRef'
);

is_deeply(
    $foo->bar( foo => $foo, gorch => [ 1, 2, 3 ] ),
    [ $foo, undef, [ 1, 2, 3 ] ],
    '... the foo param in &bar got a Foo instance'
);

like(
    exception { $foo->bar( foo => $foo, gorch => undef ) },
    qr/\QThe 'gorch' parameter/,
    '... gorch requires a ArrayRef[Int]'
);
like(
    exception { $foo->bar( foo => $foo, gorch => 10 ) },
    qr/\QThe 'gorch' parameter/,
    '... gorch requires a ArrayRef[Int]'
);
like(
    exception { $foo->bar( foo => $foo, gorch => 'Foo' ) },
    qr/\QThe 'gorch' parameter/,
    '... gorch requires a ArrayRef[Int]'
);
like(
    exception { $foo->bar( foo => $foo, gorch => \( my $var ) ) },
    qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]'
);
like(
    exception { $foo->bar( foo => $foo, gorch => [qw/one two three/] ) },
    qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]'
);

like(
    exception { $foo->quux( foo => '123456790' ) },
    qr/\QThe 'foo' parameter/,
    '... foo parameter must be an ArrayRef'
);

like(
    exception { $foo->quux( foo => [ 1, 2, 3, 4 ] ) },
    qr/\QThe 'foo' parameter\E.+\Qsome random callback/,
    '... foo parameter additional callback requires that arrayref be 0-2 elements'
);

is( $foo->boo, 'foobar', '... boo dependent parameters optional' );

like(
    exception { $foo->boo( foo => 'foo' ) },
    qr/\QParameter 'foo' depends on parameter 'bar'/,
    '... boo parameter foo depends on bar parameter',
);

like(
    exception { $foo->boo( bar => 'bar' ) },
    qr/\QParameter 'bar' depends on parameter 'foo'/,
    '... boo parameter bar depends on foo parameter',
);

done_testing();