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

NAME

Test::Proto::HashRef - Test Prototype for Hash References.

SYNOPSIS

        my $p = Test::Proto::Base->new->is_eq(-5);
        $p->ok ($temperature) # will fail unless $temperature is -5
        $p->ok ($score) # you can use the same test multple times
        ok($p->validate($score)) # If you like your "ok"s first

This is a test prototype which requires that the value it is given is defined and is a hashref. It provides methods for interacting with hashrefs. (To test hashes, make them hashrefs and test them with this module)

METHODS

new

        my $test = Test::Proto::Base->new();

Creates a new Test::Proto::Base object.

initialise

When new is called, initialise is called on the object just before it is returned. This mostly exists so that subclasses wishing to add initial tests do not have to overload new.

validate

        my $result = $p->validate($input);
        warn $result unless $result;

Runs through the tests in the prototype and checks that they all pass. If they do, returns true. If not, returns the appropriate fail or exception object.

ok

        my $result = $p->ok($input, '$input must be valid');

Like validate but attached to Test::Builder, like Test::More's ok.

add_test

        my $result = $p->add_test(sub{return $_[0] ne '';})->ok($input, '$input must be nonempty');

Adds a test to the end of the list of tests to be performed when validate or ok is called. It is normally better to use try, as that wraps your code in an eval block.

upgrade

        my $pInt = $p->upgrade(qr/^[a-z]+$/);
        my $pAnswer = $p->upgrade(42);

Upgrading is an internal funciton but is documented here as it as an important concept. Basically, it is a Do What I Mean function for parameters to many arguments, e.g. if you require an array value to be qr/^[a-z]+$/, then rather than expecting an identical regex object, the regex is 'upgraded' to a Test::Proto::Base object with a single test: is_like(qr/^[a-z]+$/). This works for strings, arrayrefs and hashrefs too.

is_a

        p->is_a('SCALAR')->ok('String');
        p->is_a('HASH')->ok({a=>1});
        p->is_a('XML::LibXML::Node')->ok(XML::LibXML::Element->new());
        p->is_a('XML::LibXML::Element')->ok(XML::LibXML::Element->new());

Like Perl's ref and isa rolled into one.

is_also

        my $nonempty = p->is_like(qr/\w+/);
        my $lowercase = p->is_unlike(qr/[A-Z]/)->is_also($nonempty);

Allows you to effectively import all the tests of another prototype.

This is not to be confused with is_a!

is_defined

        p->is_defined->ok($input);

Succeeds unless the value is undef.

is_eq

        p->is_eq('ONION')->ok($input);

Tests for string equality.

is_ne

        p->is_ne('GARLIC')->ok($input);

Tests for string inequality.

is_deeply

        p->is_deeply({ingredient=>'ONION', qty=>3})->ok($input);

Recursively test using Test::More::is_deeply

is_like

        p->is_like(qr/^[a-z]+$/)->ok($input);

Tests if the value matches a regex.

is_unlike

        p->is_unlike(qr/^[a-z]+$/)->ok($input);

Tests if the value fails to match a regex.

clone

        my $keyword = p->is_unlike(qr/^[a-z]+$/);
        $keyword->clone->is_ne('undef')->ok($perlword);
        $keyword->clone->is_ne('null')->ok($jsonword);

Creates a clone of the current Test::Proto::Base object. Child tests are not recursively cloned, they remain references, but the list of tests can be added to independently.

as_string

        p->as_string(p->is_like(qr/<html>/))->ok($input);
        p->as_string(qr/<html>/)->ok($input);

Coerces the value to a string, then tests the result against the prototpye which is the first argument.

as_number

        p->as_number(p->eq(cNum,42))->ok($input);
        p->as_number(42)->ok($input);

Coerces the value to a number, then tests the result against the prototpye which is the first argument.

as_bool

        p->as_bool(1)->ok($input);

Coerces the value to a boolean, then tests the result against the prototpye which is the first argument.

eq, ne, gt, lt, ge, lt, le

        p->ge(c, 'a')->ok('b');
        p->ge(cNum, 2)->ok(10);

Tests sort order against a comparator. The first argument is a comparison function, see Test::Proto::Compare. The second argument is the comparator.

try

        $p->try(sub{return $_[0] ne '';})->ok($input, '$input must be nonempty');

Execute arbitrary code.

fail

This is a subroutine which is an alias for Test::Proto::Fail-new()>.

exception

This is a subroutine which is an alias for Test::Proto::Exception-new()>.

OTHER INFORMATION

For author, version, bug reports, support, etc, please see Test::Proto.