-
Notifications
You must be signed in to change notification settings - Fork 0
/
Type.pm
585 lines (408 loc) · 10.8 KB
/
Type.pm
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package Test::Type;
use strict;
use warnings;
use Carp qw();
use Data::Validate::Type;
use Exporter 'import';
use Test::More qw();
=head1 NAME
Test::Type - Functions to validate data types in test files.
=head1 VERSION
Version 1.2.0
=cut
our $VERSION = '1.2.0';
=head1 SYNOPSIS
use Test::Type;
# Test strings.
ok_string( $variable );
ok_string(
$variable,
name => 'My variable',
);
# Test arrayrefs.
ok_arrayref( $variable );
ok_arrayref(
$variable,
name => 'My variable',
);
# Test hashrefs.
ok_hashref( $variable );
ok_hashref(
$variable,
name => 'Test variable',
);
# Test coderefs.
ok_coderef( $variable );
ok_coderef(
$variable,
name => 'Test variable',
);
# Test numbers.
ok_number( $variable );
ok_number(
$variable,
name => 'Test variable',
);
# Test instances.
ok_instance(
$variable,
class => $class,
);
ok_instance(
$variable,
name => 'Test variable',
class => $class,
);
# Test regular expressions.
ok_regex( $variable );
ok_regex(
$variable,
name => 'Test regular expression',
);
=cut
our @EXPORT = ## no critic (Modules::ProhibitAutomaticExportation)
(
'ok_arrayref',
'ok_coderef',
'ok_hashref',
'ok_instance',
'ok_number',
'ok_string',
'ok_regex',
);
=head1 FUNCTIONS
=head2 ok_string()
Test if the variable passed is a string.
ok_string(
$variable,
);
ok_string(
$variable,
name => 'My variable',
);
ok_string(
$variable,
name => 'My variable',
allow_empty => 1,
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=item * allow_empty
Boolean, default 1. Allow the string to be empty or not.
=back
=cut
sub ok_string
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
my $allow_empty = delete( $args{'allow_empty'} );
$allow_empty = 1 if !defined( $allow_empty );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
my @test_properties = ();
push( @test_properties, $allow_empty ? 'allow empty' : 'non-empty' );
my $test_properties = scalar( @test_properties ) == 0
? ''
: ' (' . join( ', ', @test_properties ) . ')';
return Test::More::ok(
Data::Validate::Type::is_string(
$variable,
allow_empty => $allow_empty,
),
$name . ' is a string' . $test_properties . '.',
);
}
=head2 ok_arrayref()
Test if the variable passed is an arrayref that can be dereferenced into an
array.
ok_arrayref( $variable );
ok_arrayref(
$variable,
name => 'My variable',
);
ok_arrayref(
$variable,
allow_empty => 1,
no_blessing => 0,
);
# Check if the variable is an arrayref of hashrefs.
ok_arrayref(
$variable,
allow_empty => 1,
no_blessing => 0,
element_validate_type =>
sub
{
return Data::Validate::Type::is_hashref( $_[0] );
},
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=item * allow_empty
Boolean, default 1. Allow the array to be empty or not.
=item * no_blessing
Boolean, default 0. Require that the variable is not blessed.
=item * element_validate_type
None by default. Set it to a coderef to validate the elements in the array.
The coderef will be passed the element to validate as first parameter, and it
must return a boolean indicating whether the element was valid or not.
=back
=cut
sub ok_arrayref
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
my $allow_empty = delete( $args{'allow_empty'} );
$allow_empty = 1 if !defined( $allow_empty );
my $no_blessing = delete( $args{'no_blessing'} );
$no_blessing = 0 if !defined( $no_blessing );
my $element_validate_type = delete( $args{'element_validate_type'} );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
my @test_properties = ();
push( @test_properties, $allow_empty ? 'allow empty' : 'non-empty' );
push( @test_properties, $no_blessing ? 'no blessing' : 'allow blessed' );
push( @test_properties, 'validate elements' )
if $element_validate_type;
my $test_properties = scalar( @test_properties ) == 0
? ''
: ' (' . join( ', ', @test_properties ) . ')';
return Test::More::ok(
Data::Validate::Type::is_arrayref(
$variable,
allow_empty => $allow_empty,
no_blessing => $no_blessing,
element_validate_type => $element_validate_type,
),
$name . ' is an arrayref' . $test_properties . '.',
);
}
=head2 ok_hashref()
Test if the variable passed is a hashref that can be dereferenced into a hash.
ok_hashref( $variable );
ok_hashref(
$variable,
name => 'Test variable',
);
ok_hashref(
$variable,
allow_empty => 1,
no_blessing => 0,
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=item * allow_empty
Boolean, default 1. Allow the array to be empty or not.
=item * no_blessing
Boolean, default 0. Require that the variable is not blessed.
=back
=cut
sub ok_hashref
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
my $allow_empty = delete( $args{'allow_empty'} );
$allow_empty = 1 if !defined( $allow_empty );
my $no_blessing = delete( $args{'no_blessing'} );
$no_blessing = 0 if !defined( $no_blessing );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
my @test_properties = ();
push( @test_properties, $allow_empty ? 'allow empty' : 'non-empty' );
push( @test_properties, $no_blessing ? 'no blessing' : 'allow blessed' );
my $test_properties = scalar( @test_properties ) == 0
? ''
: ' (' . join( ', ', @test_properties ) . ')';
return Test::More::ok(
Data::Validate::Type::is_hashref(
$variable,
allow_empty => $allow_empty,
no_blessing => $no_blessing,
),
$name . ' is a hashref' . $test_properties . '.',
);
}
=head2 ok_coderef()
Test if the variable passed is an coderef that can be dereferenced into a block
of code.
ok_coderef( $variable );
ok_coderef(
$variable,
name => 'Test variable',
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=back
=cut
sub ok_coderef
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
return Test::More::ok(
Data::Validate::Type::is_coderef(
$variable,
),
$name . ' is a coderef.',
);
}
=head2 ok_number()
Test if the variable passed is a number.
ok_number( $variable );
ok_number(
$variable,
name => 'Test variable',
);
ok_number(
$variable,
positive => 1,
);
ok_number(
$variable,
strictly_positive => 1,
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=item * strictly_positive
Boolean, default 0. Set to 1 to check for a strictly positive number.
=item * positive
Boolean, default 0. Set to 1 to check for a positive number.
=back
=cut
sub ok_number
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
my $strictly_positive = delete( $args{'strictly_positive'} );
$strictly_positive = 0 if !defined( $strictly_positive );
my $positive = delete( $args{'positive'} );
$positive = 0 if !defined( $positive );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
my @test_properties = ();
push( @test_properties, 'strictly positive' )
if $strictly_positive;
push( @test_properties, 'positive' )
if $positive;
my $test_properties = scalar( @test_properties ) == 0
? ''
: ' (' . join( ', ', @test_properties ) . ')';
return Test::More::ok(
Data::Validate::Type::is_number(
$variable,
strictly_positive => $strictly_positive,
positive => $positive,
),
$name . ' is a number' . $test_properties . '.',
);
}
=head2 ok_instance()
Test if the variable is an instance of the given class.
Note that this handles inheritance properly, so it will succeed if the
variable is an instance of a subclass of the class given.
ok_instance(
$variable,
class => $class,
);
ok_instance(
$variable,
name => 'Test variable',
class => $class,
);
Parameters:
=over 4
=item * name
Optional, the name of the variable being tested.
=item * class
Required, the name of the class to check the variable against.
=back
=cut
sub ok_instance
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
my $class = delete( $args{'class'} );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
return Test::More::ok(
Data::Validate::Type::is_instance(
$variable,
class => $class,
),
$name . ' is an instance of ' . $class . '.',
);
}
=head2 ok_regex()
Test if the variable is a regular expression.
ok_regex( $variable );
=cut
sub ok_regex
{
my ( $variable, %args ) = @_;
# Verify arguments and set defaults.
my $name = delete( $args{'name'} );
$name = 'Variable' if !defined( $name );
Carp::croak( 'Unknown parameter(s): ' . join( ', ', keys %args ) . '.' )
if scalar( keys %args ) != 0;
return Test::More::ok(
Data::Validate::Type::is_regex( $variable ),
$name . ' is a regular expression.',
);
}
=head1 BUGS
Please report any bugs or feature requests to C<bug-test-dist-versionsync at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=test-type>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::Type
You can also look for information at:
=over
=item *
GitHub (report bugs there)
L<https://github.com/guillaumeaubert/Test-Type/issues>
=item *
AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/test-type>
=item *
CPAN Ratings
L<http://cpanratings.perl.org/d/test-type>
=item *
Search CPAN
L<https://metacpan.org/release/Test-Type>
=back
=head1 AUTHOR
L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
C<< <aubertg at cpan.org> >>.
=head1 COPYRIGHT & LICENSE
Copyright 2012-2017 Guillaume Aubert.
This code is free software; you can redistribute it and/or modify it under the
same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the LICENSE file for more details.
=cut
1;