The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Type - Functions to validate data types in test files.

VERSION

Version 1.1.3

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,
        );

FUNCTIONS

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:

  • name

    Optional, the name of the variable being tested.

  • allow_empty

    Boolean, default 1. Allow the string to be empty or not.

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:

  • name

    Optional, the name of the variable being tested.

  • allow_empty

    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.

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:

  • name

    Optional, the name of the variable being tested.

  • allow_empty

    Boolean, default 1. Allow the array to be empty or not.

  • no_blessing

    Boolean, default 0. Require that the variable is not blessed.

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:

  • name

    Optional, the name of the variable being tested.

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:

  • name

    Optional, the name of the variable being tested.

  • 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.

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:

  • name

    Optional, the name of the variable being tested.

  • class

    Required, the name of the class to check the variable against.

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Test::Type

You can also look for information at:

AUTHOR

Guillaume Aubert, <aubertg at cpan.org>.

COPYRIGHT & LICENSE

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/