docs for with_immutable
[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
2cc69b87 127=item B<with_immutable { CODE } @class_names>
128
129Runs B<CODE> (which should contain normal tests) twice, and immutablizes each
130class in C<@class_names> in between the two runs.
131
7125b244 132=back
133
134=head1 TODO
135
136=over 4
137
138=item Convert the Moose test suite to use this module.
139
140=item Here is a list of possible functions to write
141
142=over 4
143
144=item immutability predicates
145
146=item anon-class predicates
147
148=item discovering original method from modified method
149
150=item attribute metaclass predicates (attribute_isa?)
151
152=back
9a641848 153
154=back
155
156=head1 SEE ALSO
157
158=over 4
159
160=item L<Test::More>
161
162=back
163
164=head1 BUGS
165
d03bd989 166All complex software has bugs lurking in it, and this module is no
9a641848 167exception. If you find a bug please either email me, or add the bug
168to cpan-RT.
169
170=head1 AUTHOR
171
172Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
173
7125b244 174Stevan Little E<lt>stevan@iinteractive.comE<gt>
175
9a641848 176=head1 COPYRIGHT AND LICENSE
177
2840a3b2 178Copyright 2007-2009 by Infinity Interactive, Inc.
9a641848 179
180L<http://www.iinteractive.com>
181
182This library is free software; you can redistribute it and/or modify
d03bd989 183it under the same terms as Perl itself.
9a641848 184
185=cut
186