Test::Proto::HashRef - Test Prototype for Hash References.
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)
my $test = Test::Proto::Base->new();
Creates a new Test::Proto::Base object.
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.
new
initialise
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.
my $result = $p->ok($input, '$input must be valid');
Like validate but attached to Test::Builder, like Test::More's ok.
ok
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.
validate
try
eval
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.
qr/^[a-z]+$/
is_like(qr/^[a-z]+$/)
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.
ref
isa
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_a
p->is_defined->ok($input);
Succeeds unless the value is undef.
undef
p->is_eq('ONION')->ok($input);
Tests for string equality.
p->is_ne('GARLIC')->ok($input);
Tests for string inequality.
p->is_deeply({ingredient=>'ONION', qty=>3})->ok($input);
Recursively test using Test::More::is_deeply
Test::More::is_deeply
p->is_like(qr/^[a-z]+$/)->ok($input);
Tests if the value matches a regex.
p->is_unlike(qr/^[a-z]+$/)->ok($input);
Tests if the value fails to match a regex.
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.
Test::Proto::Base
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.
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.
p->as_bool(1)->ok($input);
Coerces the value to a boolean, then tests the result against the prototpye which is the first argument.
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.
Test::Proto::Compare
$p->try(sub{return $_[0] ne '';})->ok($input, '$input must be nonempty');
Execute arbitrary code.
This is a subroutine which is an alias for Test::Proto::Fail-new()>.
Test::Proto::Fail-
This is a subroutine which is an alias for Test::Proto::Exception-new()>.
Test::Proto::Exception-
For author, version, bug reports, support, etc, please see Test::Proto.
To install Test::Proto, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Proto
CPAN shell
perl -MCPAN -e shell install Test::Proto
For more information on module installation, please visit the detailed CPAN module installation guide.