give with_immutable a reasonable return value
[gitmo/Moose.git] / lib / Test / Moose.pm
CommitLineData
9a641848 1package Test::Moose;
2
9a641848 3use strict;
4use warnings;
5
7125b244 6use Sub::Exporter;
7use Test::Builder;
004222dc 8
92c04d5e 9use List::MoreUtils 'all';
6532ca5a 10use Moose::Util 'does_role', 'find_meta';
9a641848 11
6d0815b5 12our $VERSION = '0.93';
75b95414 13$VERSION = eval $VERSION;
7125b244 14our $AUTHORITY = 'cpan:STEVAN';
9a641848 15
7125b244 16my @exports = qw[
17 meta_ok
d03bd989 18 does_ok
7125b244 19 has_attribute_ok
f2ca7ada 20 with_immutable
7125b244 21];
9a641848 22
7125b244 23Sub::Exporter::setup_exporter({
24 exports => \@exports,
25 groups => { default => \@exports }
26});
9a641848 27
6532ca5a 28## the test builder instance ...
9a641848 29
6532ca5a 30my $Test = Test::Builder->new;
9a641848 31
7125b244 32## exported functions
9a641848 33
7125b244 34sub meta_ok ($;$) {
35 my ($class_or_obj, $message) = @_;
d03bd989 36
7125b244 37 $message ||= "The object has a meta";
d03bd989 38
6532ca5a 39 if (find_meta($class_or_obj)) {
7125b244 40 return $Test->ok(1, $message)
41 }
42 else {
d03bd989 43 return $Test->ok(0, $message);
7125b244 44 }
9a641848 45}
46
47sub does_ok ($$;$) {
7125b244 48 my ($class_or_obj, $does, $message) = @_;
d03bd989 49
7125b244 50 $message ||= "The object does $does";
d03bd989 51
6532ca5a 52 if (does_role($class_or_obj, $does)) {
7125b244 53 return $Test->ok(1, $message)
54 }
55 else {
d03bd989 56 return $Test->ok(0, $message);
7125b244 57 }
58}
9a641848 59
7125b244 60sub has_attribute_ok ($$;$) {
61 my ($class_or_obj, $attr_name, $message) = @_;
d03bd989 62
7125b244 63 $message ||= "The object does has an attribute named $attr_name";
d03bd989 64
65 my $meta = find_meta($class_or_obj);
66
7125b244 67 if ($meta->find_attribute_by_name($attr_name)) {
68 return $Test->ok(1, $message)
69 }
70 else {
d03bd989 71 return $Test->ok(0, $message);
72 }
9a641848 73}
74
f2ca7ada 75sub with_immutable (&@) {
76 my $block = shift;
92c04d5e 77 my $before = $Test->current_test;
f2ca7ada 78 $block->();
79 $_->meta->make_immutable for @_;
80 $block->();
92c04d5e 81 my $num_tests = $Test->current_test - $before;
82 return all { $_ } ($Test->summary)[-$num_tests..-1];
f2ca7ada 83}
84
9a641848 851;
86
87__END__
88
89=pod
90
91=head1 NAME
92
93Test::Moose - Test functions for Moose specific features
94
95=head1 SYNOPSIS
96
7125b244 97 use Test::More plan => 1;
d03bd989 98 use Test::Moose;
9a641848 99
7125b244 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");
9a641848 103
7125b244 104=head1 DESCRIPTION
105
d03bd989 106This module provides some useful test functions for Moose based classes. It
7125b244 107is an experimental first release, so comments and suggestions are very welcome.
108
109=head1 EXPORTED FUNCTIONS
9a641848 110
111=over 4
112
7125b244 113=item B<meta_ok ($class_or_object)>
114
115Tests if a class or object has a metaclass.
116
117=item B<does_ok ($class_or_object, $role, ?$message)>
9a641848 118
d03bd989 119Tests if a class or object does a certain role, similar to what C<isa_ok>
7125b244 120does for the C<isa> method.
9a641848 121
7125b244 122=item B<has_attribute_ok($class_or_object, $attr_name, ?$message)>
123
d03bd989 124Tests if a class or object has a certain attribute, similar to what C<can_ok>
7125b244 125does for the methods.
126
127=back
128
129=head1 TODO
130
131=over 4
132
133=item Convert the Moose test suite to use this module.
134
135=item Here is a list of possible functions to write
136
137=over 4
138
139=item immutability predicates
140
141=item anon-class predicates
142
143=item discovering original method from modified method
144
145=item attribute metaclass predicates (attribute_isa?)
146
147=back
9a641848 148
149=back
150
151=head1 SEE ALSO
152
153=over 4
154
155=item L<Test::More>
156
157=back
158
159=head1 BUGS
160
d03bd989 161All complex software has bugs lurking in it, and this module is no
9a641848 162exception. If you find a bug please either email me, or add the bug
163to cpan-RT.
164
165=head1 AUTHOR
166
167Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
168
7125b244 169Stevan Little E<lt>stevan@iinteractive.comE<gt>
170
9a641848 171=head1 COPYRIGHT AND LICENSE
172
2840a3b2 173Copyright 2007-2009 by Infinity Interactive, Inc.
9a641848 174
175L<http://www.iinteractive.com>
176
177This library is free software; you can redistribute it and/or modify
d03bd989 178it under the same terms as Perl itself.
9a641848 179
180=cut
181