Test::Type - Functions to validate data types in test files.
Version 1.2.0
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', );
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:
name
Optional, the name of the variable being tested.
allow_empty
Boolean, default 1. Allow the string to be empty or not.
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] ); }, );
Boolean, default 1. Allow the array to be empty or not.
no_blessing
Boolean, default 0. Require that the variable is not blessed.
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.
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, );
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', );
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, );
strictly_positive
Boolean, default 0. Set to 1 to check for a strictly positive number.
positive
Boolean, default 0. Set to 1 to check for a positive number.
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, );
class
Required, the name of the class to check the variable against.
Test if the variable is a regular expression.
ok_regex( $variable );
Please report any bugs or feature requests to bug-test-dist-versionsync at rt.cpan.org, or through the web interface at 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.
bug-test-dist-versionsync at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Test::Type
You can also look for information at:
GitHub (report bugs there)
https://github.com/guillaumeaubert/Test-Type/issues
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/test-type
CPAN Ratings
http://cpanratings.perl.org/d/test-type
Search CPAN
https://metacpan.org/release/Test-Type
Guillaume Aubert, <aubertg at cpan.org>.
<aubertg at cpan.org>
Copyright 2012-2014 Guillaume Aubert.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
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 GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/
To install Test::Type, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Type
CPAN shell
perl -MCPAN -e shell install Test::Type
For more information on module installation, please visit the detailed CPAN module installation guide.