9 use List::MoreUtils 'all';
10 use Moose::Util 'does_role', 'find_meta';
12 our $AUTHORITY = 'cpan:STEVAN';
21 Sub::Exporter::setup_exporter({
23 groups => { default => \@exports }
26 ## the test builder instance ...
28 my $Test = Test::Builder->new;
33 my ($class_or_obj, $message) = @_;
35 $message ||= "The object has a meta";
37 if (find_meta($class_or_obj)) {
38 return $Test->ok(1, $message)
41 return $Test->ok(0, $message);
46 my ($class_or_obj, $does, $message) = @_;
48 $message ||= "The object does $does";
50 if (does_role($class_or_obj, $does)) {
51 return $Test->ok(1, $message)
54 return $Test->ok(0, $message);
58 sub has_attribute_ok ($$;$) {
59 my ($class_or_obj, $attr_name, $message) = @_;
61 $message ||= "The object does has an attribute named $attr_name";
63 my $meta = find_meta($class_or_obj);
65 if ($meta->find_attribute_by_name($attr_name)) {
66 return $Test->ok(1, $message)
69 return $Test->ok(0, $message);
73 sub with_immutable (&@) {
75 my $before = $Test->current_test;
77 Class::MOP::class_of($_)->make_immutable for @_;
79 my $num_tests = $Test->current_test - $before;
80 return all { $_ } ($Test->summary)[-$num_tests..-1];
85 # ABSTRACT: Test functions for Moose specific features
93 use Test::More plan => 1;
96 meta_ok($class_or_obj, "... Foo has a ->meta");
97 does_ok($class_or_obj, $role, "... Foo does the Baz role");
98 has_attribute_ok($class_or_obj, $attr_name, "... Foo has the 'bar' attribute");
102 This module provides some useful test functions for Moose based classes. It
103 is an experimental first release, so comments and suggestions are very welcome.
105 =head1 EXPORTED FUNCTIONS
109 =item B<meta_ok ($class_or_object)>
111 Tests if a class or object has a metaclass.
113 =item B<does_ok ($class_or_object, $role, ?$message)>
115 Tests if a class or object does a certain role, similar to what C<isa_ok>
116 does for the C<isa> method.
118 =item B<has_attribute_ok($class_or_object, $attr_name, ?$message)>
120 Tests if a class or object has a certain attribute, similar to what C<can_ok>
121 does for the methods.
123 =item B<with_immutable { CODE } @class_names>
125 Runs B<CODE> (which should contain normal tests) twice, and make each
126 class in C<@class_names> immutable in between the two runs.
134 =item Convert the Moose test suite to use this module.
136 =item Here is a list of possible functions to write
140 =item immutability predicates
142 =item anon-class predicates
144 =item discovering original method from modified method
146 =item attribute metaclass predicates (attribute_isa?)
162 See L<Moose/BUGS> for details on reporting bugs.