Test::Proto::Role::Value - Role containing test case methods for any perl value
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.
eq
defined
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.
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.
p->true->ok("Strings are true"); # passes p->false->ok($undefined); # fails
Tests if the subject returns true or false in boolean context.
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
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.
p->try( sub { 'a' eq lc shift; } )->ok('A');
Used to execute arbitrary code. Passes if the return value is true.
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.
p->is_a('')->ok('b'); p->is_a('ARRAY')->ok([]); p->is_a('less')->ok(less);
A test which bundles isa and ref together.
isa
ref
If the subject is not a reference, undef or '' in the first argument passes.
undef
''
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.
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.
blessed
p->array->ok([1..10]); # passes p->array->ok($object); # fails, even if $object overloads @{}
Passes if the subject is an unblessed array.
p->hash->ok({a=>'1'}); # passes p->hash->ok($object); # fails, even if $object overloads @{}
Passes if the subject is an unblessed hash.
p->scalar->ok('a'); # passes p->scalar->ok(\''); # fails
Passes if the subject is an unblessed scalar.
p->scalar_ref->ok(\'a'); # passes p->scalar_ref->ok('a'); # fails
Passes if the subject is an unblessed scalar ref.
p->scalar->ok('a'); # passes p->scalar->ok(\'');
Passes if the subject is a blessed object.
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.
$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.
$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.
$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.
$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
$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.
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.
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.
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.
DOES NOT WORK
Tests that the subject is a weak reference using is_weak from Scalar::Util.
Tests that the subject is not a weak reference using is_weak from Scalar::Util.
The following functions will load if you have Data::DPath installed.
p->dpath_true('//answer[ val == 42 ]')
Evaluates the dpath expression and passes if it finds a match.
p->dpath_false('//answer[ !val ]')
Evaluates the dpath expression and passes if it does not find a match.
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.
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.