Sweet.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 92.1% 70 / 76 CC 4 SCAR 4.8
CC
4
Cov
95%
init_meta
4.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.6
« Sweet/Exception/FailedMetatestConstruction.pm ↑ Summary Sweet/Keyword/Test.pm »
1 package Test::Sweet;
2 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 $Test::Sweet::VERSION = '0.03';
4 }
5 # ABSTRACT: Moose-based Test::Class replacement
6 3 our $VERSION;
7  
8 4 ▶use Moose ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Moose::Util::MetaRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Test::Sweet::Meta::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Test::Sweet::Meta::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Devel::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Test::Sweet::Keyword::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 Moose::Exporter->setup_import_methods();
19  
20 6 ▶sub init_meta {
Subroutine init_meta: called CC=4 SCAR=14.1
Pod undocumented
21 6 my ($me, %options) = @_;
22 6 my $for = $options{for_class};
23  
24 # work on both roles and classes
25 6 my $meta;
26 6 ▶ if ($for->can('meta')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for->can('meta')) { } 6 0 6
27 6 $meta = $for->meta;
28 } else {
29 # XXX: should Moose::Role be hard-coded here?
30 0 $meta = Moose::Role->init_meta(for_class => $for);
31 }
32  
33 6 setup_sugar_for($for);
34 6 ▶ load_extra_modules_into($for) unless $options{no_extra_modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_extra_modules"} 6 0 6
35  
36 6 Moose::Util::MetaRole::apply_metaroles(
37 for => $for,
38 class_metaroles => { class => ['Test::Sweet::Meta::Class'] },
39 role_metaroles => { role => ['Test::Sweet::Meta::Class'] },
40 );
41  
42 6 ▶ if($meta->isa('Class::MOP::Class')){
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->isa("Class::MOP::Class")) 4 2 6
43 # don't apply the object metaroles to roles; only to classes
44 4 Moose::Util::MetaRole::apply_base_class_roles(
45 for => $for,
46 roles => ['Test::Sweet::Runnable'],
47 );
48 }
49 }
50  
51 6 ▶sub setup_sugar_for {
Subroutine setup_sugar_for: called CC=1 SCAR=0.0
Pod undocumented
52 6 my $pkg = shift;
53 6 Test::Sweet::Keyword::Test->install_methodhandler(
54 into => $pkg,
55 );
56 }
57  
58 6 ▶sub load_extra_modules_into {
Subroutine load_extra_modules_into: called CC=1 SCAR=0.0
Pod undocumented
59 6 my $pkg = shift;
60 3 ▶ eval "package $pkg; use Test::More; use Test::Exception";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
61 }
62  
63 3 1;
64  
65  
66 =pod
67  
68 =head1 NAME
69  
70 Test::Sweet - Moose-based Test::Class replacement
71  
72 =head1 VERSION
73  
74 version 0.03
75  
76 =head1 SYNOPSIS
77  
78 Write test classes:
79  
80 use MooseX::Declare;
81 class t::RecordBasic with t::lib::FakeDatabase {
82 use Test::Sweet;
83  
84 test add_record {
85 $self->database->insert( 42 => 'OH HAI' );
86 ok $self->database->get_record('42'), 'can get record 42';
87 }
88  
89 test delete_record {
90 ok $self->database->exists('42'), 'still have record 42';
91 lives_ok {
92 $self->database->delete('42')
93 } 'deleting 42 lives';
94 ok !$self->database->exists('42'), 'record 42 is gone';
95 }
96 }
97  
98 Run them:
99  
100 $ mx-run -Ilib t::RecordBasic
101  
102 And get the valid TAP output:
103  
104 1..2
105 ok 1 - can get record 42
106 1..1
107 ok 1 - subtest add_record
108 ok 1 - still have record 42
109 ok 2 - deleting 42 lives
110 ok 3 - record 42 is gone
111 1..3
112 ok 2 - subtest delete_record
113  
114 No more counting tests; this module does it for you and ensures that
115 you are protected against premature death. (Well, your test suite,
116 anyway.)
117  
118 You can also have command-line args for your tests; they are parsed
119 with L<MooseX::Getopt|MooseX::Getopt> (if you have it installed; try
120 "mx-run t::YourTest --help").
121  
122 =head1 DESCRIPTION
123  
124 C<Test::Sweet> lets you organize tests into Moose classes and Moose
125 roles. You just need to create a normal class or role and say C<use
126 Test::Sweet> somewhere. This adds the necessary methods to your
127 metaclass, makes your class do C<Test::Sweet::Runnable> (so that you
128 can run it with L<MooseX::Runnable|MooseX::Runnable>'s
129 L<mx-run|mx-run> command), and makes the C<test> keyword available for
130 your use. (The imports are package-scoped, of course, but the C<test>
131 keyword is lexically scoped.)
132  
133 Normal methods are defined normally. Methods that run tests are
134 defined like methods, but with the C<test> keyword instead of C<sub>
135 or C<method>. In the test methods, you can use any
136 L<Test::Builder|Test::Builder>-aware test methods. You get all of
137 L<Test::More|Test::More> and L<Test::Exception|Test::Exception> by
138 default.
139  
140 Tests can be called as methods any time the test suite is running,
141 including in BUILD and DEMOLISH. Everything will Just Work. The
142 method will get the arguments you pass, you will get the return value,
143 and this module will do what's necessary to ensure that Test::Builder
144 knows what is going on. It's a Moose class and tests are just special
145 methods. Method modifiers work too. (But don't run tests directly in
146 the method modifier body yet; just call other C<test> methods.)
147  
148 To run all tests in a class (hierarchy), just call the C<run> method.
149  
150 Tests are ordered as follows. All test method from the superclasses
151 are run first, then your tests are run in the order they appear in the
152 file (this is guaranteed, not a side-effect of anything), then any
153 tests you composed in from roles are run. If anything in the
154 hierarchy overrides a test method from somewhere else in the
155 hierarchy, the overriding method will be run when the original method
156 would be.
157  
158 Here's an example:
159  
160 class A { use Test::Sweet; test first { pass 'first' } };
161 class B extends A { use Test::Sweet; test second { pass 'second' } };
162  
163 When you call C<< A->run >>, "first" will be run.
164  
165 When you call C<< B->run >>, "first" will run, then "second" will run.
166  
167 If you change B to look like:
168  
169 class B extends A {
170 test second { pass 'second' }
171 test first { pass 'blah' }
172 }
173  
174 When you run C<< B->run >>, first will be called first but will print
175 "blah", and second will be called second. (If you remove the "extends
176 A", they will run in the order they appear in B, of course; second
177 then first.)
178  
179 =head2 METATEST CLASSES AND TRAITS
180  
181 With this, you can inherit tests from classes or roles, but you can't
182 inherit parts of tests. For example, you may want many tests that
183 will not kill the test suite if they die, but they will fail a subtest
184 in that case. With Test traits, you can implement this behavior
185 reusably; as a CPAN module, as a role in your application, or as a
186 role defined inside the test.
187  
188 The first thing you need to do is to create a meta-test trait. This
189 is a role that is applied to the metatest class, which is what
190 actually runs each test. This class has "run" method which runs the
191 code you typed into the test file. Metatest traits modify this method
192 (but can have other methods and attributes).
193  
194 Here is the role that implements the metatest trait that will add a
195 subtest that passes if the body lives, or fails if the body dies:
196  
197 role Test::Sweet::Meta::Test::Trait::TestLives {
198 use Test::More;
199  
200 around run(@args){
201 my $lived = 0;
202 eval {
203 $self->$orig(@args);
204 $lived = 1;
205 };
206 ok $lived, 'test lived ok';
207 }
208 }
209  
210 (Note that the C<Test::Sweet::Meta::Test::Trait::> namespace is the
211 default place for these traits. You can use any namespace you like,
212 however; you just need to prefix the name of the trait with a C<+>
213 when you use something other than this default.)
214  
215 Now you can use this to modify tests:
216  
217 class t::Whatever {
218 use Test::Sweet;
219  
220 test perhaps_it_lives (TestLives) {
221 ok 1, 'got here';
222 rand > 0.5 and die 'OH NOES';
223 }
224 }
225  
226 You can also use these roles to provide per-test setup and teardown
227 (per-test-class setup and teardown is just BUILD and DEMOLISH).
228 Here's an example test that logs a message when a test starts and when
229 it finishes:
230  
231 role Log with MooseX::LogDispatch {
232 method BUILD { $self->logger->info('Test starting') }
233 method DEMOLISH { $self->logger->info('Test ending') }
234 }
235  
236 Now tests that use this metatest trait:
237  
238 test foo (+Log) {
239 ...
240 }
241  
242 will print a log message when the test starts and when it ends. (You
243 can actually do this with C<around run> too.)
244  
245 The test metainstance is always available inside your test as
246 C<$test>:
247  
248 test foo (+Log) {
249 $test->logger->info('Inside the test.');
250 }
251  
252 (You also get C<$self> inside tests, if you didn't notice that
253 already. C<$self> is the test class instance, C<$test> is the
254 metatest object, and C<< $self->meta >> is the test metaclass. So
255 much meta...)
256  
257 Finally, the examples above only used one trait per test, but you can
258 apply as many as you want:
259  
260 test foo (+Foo, Bar, Baz, +Quux, OH::Hai) {
261 ...
262 }
263  
264 =head1 REPOSITORY
265  
266 L<http://github.com/jrockway/test-sweet>
267  
268 Patches (or pull requests) are very welcome. You should also discuss
269 this module on the moose irc channel at L<irc://irc.perl.org/#moose>;
270 nothing is set in stone yet, and your feedback is requested.
271  
272 =head1 TODO
273  
274 Convince C<prove> to run the <.pm> files directly.
275  
276 Write code to organize classes into test suites; and run the test
277 suites easily. (Classes and tests should be tagged, so you can run
278 C<run-test-suite t::Suite --no-slow-tests> or something.)
279  
280 More testing. There are undoubtedly corner cases that are
281 undiscovered and unhandled.
282  
283 Document the full meta-protocol. For now, look at the C<t::Traits>
284 test.
285  
286 Syntax sugar for creating traits and attributes on those traits.
287  
288 =head1 SEE ALSO
289  
290 L<http://github.com/jrockway/test-sweet-dbic> shows what sort of
291 reusability you can get with C<Test::Sweet>... with 5 minutes of
292 hacking.
293  
294 Read this module's test suite (in the C<t/> directory) for example of
295 how to make C<prove> understand C<Test::Sweet> classes.
296  
297 =head1 AUTHOR
298  
299 Jonathan Rockway C<< <jrockway@cpan.org> >>
300  
301 =head1 COPYRIGHT
302  
303 Copyright (c) 2009 Jonathan Rockway.
304  
305 This module is free software, you may redistribute it under the same
306 terms as Perl itself.
307  
308 =head1 AUTHOR
309  
310 Jonathan Rockway <jrockway@cpan.org>
311  
312 =head1 COPYRIGHT AND LICENSE
313  
314 This software is copyright (c) 2011 by Jonathan Rockway.
315  
316 This is free software; you can redistribute it and/or modify it under
317 the same terms as the Perl 5 programming language system itself.
318  
319 =cut
320  
321  
322 __END__
« Sweet/Exception/FailedMetatestConstruction.pm ↑ Summary Sweet/Keyword/Test.pm »