The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

VERSION

Version 1.3.0

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

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.

ok_regex()

Test if the variable is a regular expression.

ok_regex( $variable );

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