|
NAMETest::Moose::More - More tools for testing Moose packages VERSIONThis document describes version 0.050 of Test::Moose::More - released September 20, 2017 as part of Test-Moose-More. SYNOPSIS use Test::Moose::More;
is_class_ok 'Some::Class';
is_role_ok 'Some::Role';
has_method_ok 'Some::Class', 'foo';
# ... etc
DESCRIPTIONThis package contains a number of additional tests that can be employed against Moose classes/roles. It is intended to replace Test::Moose in your tests, and re-exports any tests that it has and we do not, yet. Export GroupsBy default, this package exports all test functions. You can be more selective, however, and there are a number of export groups (aside from the default ":all") to help you achieve those dreams!
TEST FUNCTIONSmeta_ok $thingTests $thing to see if it has a metaclass; $thing may be the class name or instance of the class you wish to check. Passes if $thing has a metaclass. no_meta_ok $thingTests $thing to see if it does not have a metaclass; $thing may be the class name or instance of the class you wish to check. Passes if $thing does not have a metaclass. does_ok $thing, < $role | \@roles >, [ $message ]Checks to see if $thing does the given roles. $thing may be the class name or instance of the class you wish to check. Note that the message will be taken verbatim unless it contains %s somewhere; this will be replaced with the name of the role being tested for. does_not_ok $thing, < $role | \@roles >, [ $message ]Checks to see if $thing does not do the given roles. $thing may be the class name or instance of the class you wish to check. Note that the message will be taken verbatim unless it contains %s somewhere; this will be replaced with the name of the role being tested for. has_attribute_ok $thing, $attribute_name, [ $message ]Checks $thing for an attribute named $attribute_name; $thing may be a class name, instance, or role name. has_method_ok $thing, @methodsQueries $thing's metaclass to see if $thing has the methods named in @methods. Note: This does not include inherited methods; see "has_method" in Class::MOP::Class. has_no_method_ok $thing, @methodsQueries $thing's metaclass to ensure $thing does not provide the methods named in @methods. Note: This does not include inherited methods; see "has_method" in Class::MOP::Class. has_method_from_anywhere_ok $thing, @methodsQueries $thing's metaclass to see if $thing has the methods named in @methods. Note: This does include inherited methods; see "find_method_by_name" in Class::MOP::Class. has_no_method_from_anywhere_ok $thing, @methodsQueries $thing's metaclass to ensure $thing does not provide the methods named in @methods. Note: This does include inherited methods; see "find_method_by_name" in Class::MOP::Class. method_from_pkg_ok $thing, $method, $orig_pkgGiven a thing (role, class, etc) and a method, test that it originally came from $orig_pkg. method_not_from_pkg_ok $thing, $method, $orig_pkgGiven a thing (role, class, etc) and a method, test that it did not come from $orig_pkg. method_is_accessor_ok $thing, $methodGiven a thing (role, class, etc) and a method, test that the method is an accessor -- that is, it descends from Class::MOP::Method::Accessor. method_is_not_accessor_ok $thing, $methodGiven a thing (role, class, etc) and a method, test that the method is not an accessor -- that is, it does not descend from Class::MOP::Method::Accessor. definition_context_ok $meta, \%dcValidates the definition context of a metaclass instance. This is a strict comparison. role_wraps_around_method_ok $role, @methodsQueries $role's metaclass to see if $role wraps the methods named in @methods with an around method modifier. role_wraps_before_method_ok $role, @methodsQueries $role's metaclass to see if $role wraps the methods named in @methods with an before method modifier. role_wraps_after_method_ok $role, @methodsQueries $role's metaclass to see if $role wraps the methods named in @methods with an after method modifier. requires_method_ok $thing, @methodsQueries $thing's metaclass to see if $thing requires the methods named in @methods. Note that this really only makes sense if $thing is a role. does_not_require_method_ok $thing, @methodsQueries $thing's metaclass to ensure $thing does not require the methods named in @methods. Note that this really only makes sense if $thing is a role. is_immutable_ok $thingPasses if $thing is immutable. is_not_immutable_ok $thingPasses if $thing is not immutable; that is, is mutable. is_pristine_ok $thingPasses if $thing is pristine. See "is_pristine" in Class::MOP::Class. is_not_pristine_ok $thingPasses if $thing is not pristine. See "is_pristine" in Class::MOP::Class. is_role_ok $thingPasses if "$thing's" metaclass is a Moose::Meta::Role. is_class_ok $thingPasses if "$thing's" metaclass is a Moose::Meta::Class. is_anon_ok $thingPasses if $thing is "anonymous". is_not_anon_ok $thingPasses if $thing is not "anonymous". check_sugar_removed_ok $thingEnsures that all the standard Moose sugar is no longer directly callable on a given package. check_sugar_ok $thingChecks and makes sure a class/etc can still do all the standard Moose sugar. does_metaroles_ok $thing => { $mop => [ @traits ], ... };Validate the metaclasses associated with a class/role metaclass. e.g., if I wanted to validate that the attribute trait for MooseX::AttributeShortcuts is actually applied, I could do this: { package TestClass; use Moose; use MooseX::AttributeShortcuts; }
use Test::Moose::More;
use Test::More;
does_metaroles_ok TestClass => {
attribute => ['MooseX::AttributeShortcuts::Trait::Attribute'],
};
done_testing;
This function will accept either class or role metaclasses for $thing. The MOPs available for classes (Moose::Meta::Class) are: The MOPs available for roles (Moose::Meta::Role) are:
Note! Neither this function nor does_not_metaroles_ok() attempts to validate that the MOP type passed in is a member of the above lists. There's no gain here in implementing such a check, and a negative to be had: specifying an invalid MOP type will result in immediate explosions, while it's entirely possible other MOP types will be added (either to core, via traits, or "let's subclass Moose::Meta::Class/etc and implement something new"). does_not_metaroles_ok $thing => { $mop => [ @traits ], ... };As with "does_metaroles_ok", but test that the metaroles are not consumed, a la "does_not_ok". attribute_options_okValidates that an attribute is set up as expected; like validate_attribute(), but only concerns itself with attribute options. Note that some of these options will skip if used against attributes defined in a role.
VALIDATION METHODSvalidate_thingRuns a bunch of tests against the given $thing, as defined: validate_thing $thing => (
attributes => [ ... ],
methods => [ ... ],
isa => [ ... ],
# ensures sugar is/is-not present
sugar => 0,
# ensures $thing does these roles
does => [ ... ],
# ensures $thing does not do these roles
does_not => [ ... ],
);
$thing can be the name of a role or class, an object instance, or a metaclass.
validate_roleThe same as validate_thing(), but ensures $thing is a role, and allows for additional role-specific tests. validate_role $thing => (
required_methods => [ ... ],
# ...and all other options from validate_thing()
);
validate_classThe same as validate_thing(), but ensures $thing is a class, and allows for additional class-specific tests. validate_class $thing => (
isa => [ ... ],
attributes => [ ... ],
methods => [ ... ],
# ensures sugar is/is-not present
sugar => 0,
# ensures $thing does these roles
does => [ ... ],
# ensures $thing does not do these roles
does_not => [ ... ],
# ...and all other options from validate_thing()
);
validate_attributevalidate_attribute() allows you to test how an attribute looks once built and attached to a class. Let's say you have an attribute defined like this: has foo => (
traits => [ 'TestRole' ],
is => 'ro',
isa => 'Int',
builder => '_build_foo',
lazy => 1,
);
You can use validate_attribute() to ensure that it's built out in the way you expect: validate_attribute TestClass => foo => (
# tests the attribute metaclass instance to ensure it does the roles
-does => [ 'TestRole' ],
# tests the attribute metaclass instance's inheritance
-isa => [ 'Moose::Meta::Attribute' ], # for demonstration's sake
traits => [ 'TestRole' ],
isa => 'Int',
does => 'Bar',
handles => { },
reader => 'foo',
builder => '_build_foo',
default => undef,
init_arg => 'foo',
lazy => 1,
required => undef,
);
Options passed to validate_attribute() prefixed with "-" test the attribute's metaclass instance rather than a setting on the attribute; that is, "-does" ensures that the metaclass does a particular role (e.g. MooseX::AttributeShortcuts), while "does" tests the setting of the attribute to require the value do a given role. This function takes all the options "attribute_options_ok" takes, as well as the following:
SEE ALSOPlease see those modules/websites for more information related to this module.
BUGSPlease report any bugs or feature requests on the bugtracker website <https://github.com/RsrchBoy/Test-Moose-More/issues> When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. AUTHORChris Weyl <cweyl@alumni.drew.edu> CONTRIBUTORS
COPYRIGHT AND LICENSEThis software is Copyright (c) 2017, 2016, 2015, 2014, 2013, 2012 by Chris Weyl. This is free software, licensed under: The GNU Lesser General Public License, Version 2.1, February 1999
|