Document how to pass new attribute values at instance-role application time
[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
7125b244 12my @exports = qw[
13 meta_ok
d03bd989 14 does_ok
7125b244 15 has_attribute_ok
f2ca7ada 16 with_immutable
7125b244 17];
9a641848 18
7125b244 19Sub::Exporter::setup_exporter({
20 exports => \@exports,
21 groups => { default => \@exports }
22});
9a641848 23
6532ca5a 24## the test builder instance ...
9a641848 25
6532ca5a 26my $Test = Test::Builder->new;
9a641848 27
7125b244 28## exported functions
9a641848 29
7125b244 30sub meta_ok ($;$) {
31 my ($class_or_obj, $message) = @_;
d03bd989 32
7125b244 33 $message ||= "The object has a meta";
d03bd989 34
6532ca5a 35 if (find_meta($class_or_obj)) {
7125b244 36 return $Test->ok(1, $message)
37 }
38 else {
d03bd989 39 return $Test->ok(0, $message);
7125b244 40 }
9a641848 41}
42
43sub does_ok ($$;$) {
7125b244 44 my ($class_or_obj, $does, $message) = @_;
d03bd989 45
7125b244 46 $message ||= "The object does $does";
d03bd989 47
6532ca5a 48 if (does_role($class_or_obj, $does)) {
7125b244 49 return $Test->ok(1, $message)
50 }
51 else {
d03bd989 52 return $Test->ok(0, $message);
7125b244 53 }
54}
9a641848 55
7125b244 56sub has_attribute_ok ($$;$) {
57 my ($class_or_obj, $attr_name, $message) = @_;
d03bd989 58
7125b244 59 $message ||= "The object does has an attribute named $attr_name";
d03bd989 60
61 my $meta = find_meta($class_or_obj);
62
7125b244 63 if ($meta->find_attribute_by_name($attr_name)) {
64 return $Test->ok(1, $message)
65 }
66 else {
d03bd989 67 return $Test->ok(0, $message);
68 }
9a641848 69}
70
f2ca7ada 71sub with_immutable (&@) {
72 my $block = shift;
92c04d5e 73 my $before = $Test->current_test;
9153f80b 74 my $passing_before = (Test::Builder->VERSION < 1.005 ? 0 : $Test->history->pass_count) || 0;
75
f2ca7ada 76 $block->();
ed544690 77 Class::MOP::class_of($_)->make_immutable for @_;
f2ca7ada 78 $block->();
9153f80b 79
92c04d5e 80 my $num_tests = $Test->current_test - $before;
9153f80b 81 my $all_passed = Test::Builder->VERSION < 1.005
82 ? all { $_ } ($Test->summary)[-$num_tests..-1]
83 : $num_tests == $Test->history->pass_count - $passing_before;
84 return $all_passed;
f2ca7ada 85}
86
9a641848 871;
88
ad46f524 89# ABSTRACT: Test functions for Moose specific features
90
9a641848 91__END__
92
93=pod
94
9a641848 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
d8d59909 129Runs B<CODE> (which should contain normal tests) twice, and make each
130class in C<@class_names> immutable in between the two runs.
2cc69b87 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
d4048ef3 166See L<Moose/BUGS> for details on reporting bugs.
9a641848 167
9a641848 168=cut
169