NAME
Test::Proto::Role::Value - Role containing test case methods for any perl value
SYNOPSIS
package MyProtoClass;
use Moo;
with 'Test::Proto::Role::Value';
This Moo Role provides methods to Test::Proto::Base for common test case methods like eq
, defined
, etc. which can potentially be used on any perl value/object.
METHODS
eq, ne, gt, lt, ge, le
p->eq('green')->ok('green'); # passes
p->lt('green')->ok('grape'); # passes
Performs the relevant string comparison on the subject, comparing against the text supplied.
num_eq, num_ne, num_gt, num_lt, num_ge, num_le
p->num_eq(0)->ok(0); # passes
p->num_lt(256)->ok(255); # passes
Performs the relevant string comparison on the subject, comparing against the number supplied.
true, false
p->true->ok("Strings are true"); # passes
p->false->ok($undefined); # fails
Tests if the subject returns true or false in boolean context.
defined, undefined
Tests if the subject is defined/undefined.
p->defined->ok("Pretty much anything"); # passes
Note that directly supplying undef into the protoype (as opposed to a variable containing undef, a function which returns undef, etc.) will exhibit different behaviour: it will attempt to use $_
instead. This is experimental behaviour.
$_ = 3;
$undef = undef;
p->undefined->ok(undef); # fails
p->undefined->ok($undef); # passes
like, unlike
p->like(qr/^a$/)->ok('a');
p->unlike(qr/^a$/)->ok('b');
The test subject is validated against the regular expression. Like tests for a match; unlike tests for nonmatching.
try
p->try( sub { 'a' eq lc shift; } )->ok('A');
Used to execute arbitrary code. Passes if the return value is true.
ref
p->ref(undef)->ok('b');
p->ref('less')->ok(less);
p->ref(qr/[a-z]+/)->ok(less);
Tests the result of the 'ref'. Any prototype will do here.
is_a
p->is_a('')->ok('b');
p->is_a('ARRAY')->ok([]);
p->is_a('less')->ok(less);
A test which bundles isa
and ref
together.
If the subject is not a reference, undef
or ''
in the first argument passes.
If the subject is a reference to a builtin type like HASH, the ref
of that type passes.
If the subject is a blessed reference, then isa
is used.
blessed
p->blessed->ok($object); # passes
p->blessed('Correct::Class')->ok($object); # passes
p->blessed->ok([]); # fails
Compares the prototype to the result of running blessed
from Scalar::Util on the test subject.
array
p->array->ok([1..10]); # passes
p->array->ok($object); # fails, even if $object overloads @{}
Passes if the subject is an unblessed array.
hash
p->hash->ok({a=>'1'}); # passes
p->hash->ok($object); # fails, even if $object overloads @{}
Passes if the subject is an unblessed hash.
scalar
p->scalar->ok('a'); # passes
p->scalar->ok(\''); # fails
Passes if the subject is an unblessed scalar.
scalar_ref
p->scalar_ref->ok(\'a'); # passes
p->scalar_ref->ok('a'); # fails
Passes if the subject is an unblessed scalar ref.
object
p->scalar->ok('a'); # passes
p->scalar->ok(\'');
Passes if the subject is a blessed object.
refaddr
p->refaddr(undef)->ok('b');
p->refaddr(p->gt(5))->ok($obj);
Tests the result of the 'refaddr' (from Scalar::Util). Any prototype will do here.
refaddr_of
$obj2 = $obj;
p->refaddr_of($obj)->ok($obj2); # passes
p->refaddr([])->ok([]); # fails
Tests the result of the 'refaddr' (from Scalar::Util) is the same as the refaddr of the object passed. Do not supply prototypes.
Note: This always passes for strings.
also
$positive = p->num_gt(0);
$integer->also($positive);
$integer->also(qr/[02468]$/);
$integer->ok(42); # passes
Tests that the subject also matches the protoype given. If the argument given is not a prototype, the argument is upgraded to become one.
any_of
$positive = p->num_gt(0);
$all = p->eq('all');
$integer->any_of([$positive, $all]);
$integer->ok(42); # passes
$integer->ok('all'); # passes
Tests that the subject also matches one of the protoypes given in the arrayref. If a member of the arrayref given is not a prototype, the argument is upgraded to become one.
all_of
$positive = p->num_gt(0);
$under_a_hundred = p->num_lt(100);
$integer->all_of([$positive, $under_a_hundred]);
$integer->ok(42); # passes
$integer->ok('101'); # fails
Tests that the subject also matches one of the protoypes given in the arrayref. If a member of the arrayref given is not a prototype, the argument is upgraded to become one.
none_of
$positive = p->num_gt(0);
$all = p->like(qr/[02468]$/);
$integer->none_of([$positive, $all]);
$integer->ok(-1); # passes
$integer->ok(-2); # fails
$integer->ok(1); # fails
Tests that the subject does not match any of the protoypes given in the arrayref. If a member of the arrayref given is not a prototype, the argument is upgraded to become one.
some_of
p->some_of([qr/cheap/, qr/fast/, qr/good/], 2, 'Pick two!');
Tests that the subject some, all, or none of the protoypes given in the arrayref; the number of successful matches is tested against the second argument. If a member of the arrayref given is not a prototype, the argument is upgraded to become one.
looks_like_number
p->looks_like_number->ok('3'); # passes
p->looks_like_number->ok('a'); # fails
If the test subject looks like a number according to Perl's internal rules (specifically, using Scalar::Util::looks_like_number), then pass.
looks_unlike_number
p->looks_unlike_number->ok('3'); # fails
p->looks_unlike_number->ok('a'); # passes
If the test subject looks like a number according to Perl's internal rules (specifically, using Scalar::Util::looks_like_number), then fail.
is_weak_ref
DOES NOT WORK
Tests that the subject is a weak reference using is_weak from Scalar::Util.
is_strong_ref
DOES NOT WORK
Tests that the subject is not a weak reference using is_weak from Scalar::Util.
Data::DPath
The following functions will load if you have Data::DPath installed.
dpath_true
p->dpath_true('//answer[ val == 42 ]')
Evaluates the dpath expression and passes if it finds a match.
dpath_false
p->dpath_false('//answer[ !val ]')
Evaluates the dpath expression and passes if it does not find a match.
dpath_results
p->dpath_false('//answer', pArray->array_any(42))
Evaluates the dpath expression and then uses the second argument (which should be upgradeable to a Test::Proto::ArrayRef) to validate the list of matches.
OTHER INFORMATION
For author, version, bug reports, support, etc, please see Test::Proto.