9 use List::MoreUtils 'all';
10 use Moose::Util 'does_role', 'find_meta';
12 our $VERSION = '1.9900';
13 $VERSION = eval $VERSION;
14 our $AUTHORITY = 'cpan:STEVAN';
23 Sub::Exporter::setup_exporter({
25 groups => { default => \@exports }
28 ## the test builder instance ...
30 my $Test = Test::Builder->new;
35 my ($class_or_obj, $message) = @_;
37 $message ||= "The object has a meta";
39 if (find_meta($class_or_obj)) {
40 return $Test->ok(1, $message)
43 return $Test->ok(0, $message);
48 my ($class_or_obj, $does, $message) = @_;
50 $message ||= "The object does $does";
52 if (does_role($class_or_obj, $does)) {
53 return $Test->ok(1, $message)
56 return $Test->ok(0, $message);
60 sub has_attribute_ok ($$;$) {
61 my ($class_or_obj, $attr_name, $message) = @_;
63 $message ||= "The object does has an attribute named $attr_name";
65 my $meta = find_meta($class_or_obj);
67 if ($meta->find_attribute_by_name($attr_name)) {
68 return $Test->ok(1, $message)
71 return $Test->ok(0, $message);
75 sub with_immutable (&@) {
77 my $before = $Test->current_test;
79 Class::MOP::class_of($_)->make_immutable for @_;
81 my $num_tests = $Test->current_test - $before;
82 return all { $_ } ($Test->summary)[-$num_tests..-1];
93 Test::Moose - Test functions for Moose specific features
97 use Test::More plan => 1;
100 meta_ok($class_or_obj, "... Foo has a ->meta");
101 does_ok($class_or_obj, $role, "... Foo does the Baz role");
102 has_attribute_ok($class_or_obj, $attr_name, "... Foo has the 'bar' attribute");
106 This module provides some useful test functions for Moose based classes. It
107 is an experimental first release, so comments and suggestions are very welcome.
109 =head1 EXPORTED FUNCTIONS
113 =item B<meta_ok ($class_or_object)>
115 Tests if a class or object has a metaclass.
117 =item B<does_ok ($class_or_object, $role, ?$message)>
119 Tests if a class or object does a certain role, similar to what C<isa_ok>
120 does for the C<isa> method.
122 =item B<has_attribute_ok($class_or_object, $attr_name, ?$message)>
124 Tests if a class or object has a certain attribute, similar to what C<can_ok>
125 does for the methods.
127 =item B<with_immutable { CODE } @class_names>
129 Runs B<CODE> (which should contain normal tests) twice, and make each
130 class in C<@class_names> immutable in between the two runs.
138 =item Convert the Moose test suite to use this module.
140 =item Here is a list of possible functions to write
144 =item immutability predicates
146 =item anon-class predicates
148 =item discovering original method from modified method
150 =item attribute metaclass predicates (attribute_isa?)
166 See L<Moose/BUGS> for details on reporting bugs.
170 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
172 Stevan Little E<lt>stevan@iinteractive.comE<gt>
174 =head1 COPYRIGHT AND LICENSE
176 Copyright 2007-2009 by Infinity Interactive, Inc.
178 L<http://www.iinteractive.com>
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.