blib/lib/Test/Class/Most.pm

filter: Statementstmt 96.6% 86 / 89 Branchbran 72.7% 16 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 86.3% 126 / 146 CC 17 SCAR 18.9
CC
17
Cov
89%
import
14.4
__ANON__
3.1
is_abstract
2.0
CRAP 6.6
↑ Summary
1 package Test::Class::Most;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Test::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Test::Class::Most - Test Classes the easy way
11  
12 =head1 VERSION
13  
14 Version 0.08
15  
16 =cut
17  
18 1 our $VERSION = '0.08';
19 1 $VERSION = eval $VERSION;
20  
21 =head1 SYNOPSIS
22  
23 Instead of this:
24  
25 use strict;
26 use warnings;
27 use Test::Exception 0.88;
28 use Test::Differences 0.500;
29 use Test::Deep 0.106;
30 use Test::Warn 0.11;
31 use Test::More 0.88;
32  
33 use parent 'My::Test::Class';
34  
35 sub some_test : Tests { ... }
36  
37 You type this:
38  
39 use Test::Class::Most parent => 'My::Test::Class';
40  
41 sub some_test : Tests { ... }
42  
43 =head1 DESCRIPTION
44  
45 When people write test classes with the excellent C<Test::Class>, you often
46 see the following at the top of the code:
47  
48 package Some::Test::Class;
49  
50 use strict;
51 use warnings;
52 use base 'My::Test::Class';
53 use Test::More;
54 use Test::Exception;
55  
56 # and then the tests ...
57  
58 That's a lot of boilerplate and I don't like boilerplate. So now you can do
59 this:
60  
61 use Test::Class::Most parent => 'My::Test::Class';
62  
63 That automatically imports L<strict> and L<warnings> for you. It also gives
64 you all of the testing goodness from L<Test::Most>.
65  
66 =head1 CREATING YOUR OWN BASE CLASS
67  
68 You probably want to create your own base class for testing. To do this,
69 simply specify no import list:
70  
71 package My::Test::Class;
72 use Test::Class::Most; # we now inherit from Test::Class
73  
74 INIT { Test::Class->runtests }
75  
76 1;
77  
78 And then your other classes inherit as normal (well, the way we do it):
79  
80 package Tests::For::Foo;
81 use Test::Class::Most parent => 'My::Test::Class';
82  
83 And you can inherit from those other classes, too:
84  
85 package Tests::For::Foo::Child;
86 use Test::Class::Most parent => 'Tests::For::Foo';
87  
88 Of course, it's quite possible that you're a fan of multiple inheritance, so
89 you can do that, too (I was I<soooooo> tempted to not allow this, but I
90 figured I shouldn't force too many of my personal beliefs on you):
91  
92 package Tests::For::ISuckAtOO;
93 use Test::Class::Most parent => [qw/
94 Tests::For::Foo
95 Tests::For::Bar
96 Some::Other::Class::For::Increased::Stupidity
97 /];
98  
99 As a side note, it's recommended that even if you don't need test control
100 methods in your base class, put stubs in there:
101  
102 package My::Test::Class;
103 use Test::Class::Most; # we now inherit from Test::Class
104  
105 INIT { Test::Class->runtests }
106  
107 sub startup : Tests(startup) {}
108 sub setup : Tests(setup) {}
109 sub teardown : Tests(teardown) {}
110 sub shutdown : Tests(shutdown) {}
111  
112 1;
113  
114 This allows developers to I<always> be able to safely call parent test control
115 methods rather than wonder if they are there:
116  
117 package Tests::For::Customer;
118 use Test::Class::Most parent => 'My::Test::Class';
119  
120 sub setup : Tests(setup) {
121 my $test = shift;
122 $test->next::method; # safe due to stub in base class
123 ...
124 }
125  
126 =head1 ATTRIBUTES
127  
128 You can also specify "attributes" which are merely very simple getter/setters.
129  
130 use Test::Class::Most
131 parent => 'My::Test::Class',
132 attributes => [qw/customer items/],
133 is_abstract => 1;
134  
135 sub setup : Tests(setup) {
136 my $test = shift;
137 $test->SUPER::setup;
138 $test->customer( ... );
139 $test->items( ... );
140 }
141  
142 sub some_tests : Tests {
143 my $test = shift;
144 my $customer = $test->customer;
145 ...
146 }
147  
148 If called with no arguments, returns the current value. If called with one
149 argument, sets that argument as the current value. If called with more than
150 one argument, it croaks.
151  
152 =head1 ABSTRACT CLASSES
153  
154 You may pass an optional C<is_abstract> parameter in the import list. It takes
155 a boolean value. This value is advisory only and is not inherited. It defaults
156 to false if not provided.
157  
158 Sometimes you want to identify a test class as "abstract". It may have a bunch
159 of tests, but those should only run for its subclasses. You can pass
160 C<<is_abstract => 1>> in the import list. Then, to test if a given class or
161 instance of that class is "abstract":
162  
163 sub dont_run_in_abstract_base_class : Tests {
164 my $test = shift;
165 return if Test::Class::Most->is_abstract($test);
166 ...
167 }
168  
169 Note that C<is_abstract> is strictly B<advisory only>. You are expected
170 (required) to check the value yourself and take appropriate action.
171  
172 We recommend adding the following method to your base class:
173  
174 sub is_abstract {
175 my $test = shift;
176 return Test::Class::Most->is_abstract($test);
177 }
178  
179 And later in a subclass:
180  
181 if ( $test->is_abstract ) { ... }
182  
183 =head1 EXPORT
184  
185 All functions from L<Test::Most> are automatically exported into your
186 namespace.
187  
188 =cut
189  
190 {
191 1 my %IS_ABSTRACT;
192  
193 8 ▶ sub is_abstract {
Subroutine is_abstract: called CC=2 SCAR=7.0
Pod undocumented
194 8 my ( undef, $proto ) = @_;
195 8 ▶ my $test_class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
440
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
196 8 return $IS_ABSTRACT{$test_class};
197 }
198  
199 4 ▶ sub import {
Subroutine import: called CC=14 SCAR=26.7
200 4 my ( $class, %args ) = @_;
201 4 my $caller = caller;
202 1 ▶ eval "package $caller; use Test::Most;";
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
203 4 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
204 4 warnings->import;
205 4 strict->import;
206 4 ▶ if ( my $parent = delete $args{parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = delete $args{'parent'}) { } 3 1 4
207 3 ▶ if ( ref $parent && 'ARRAY' ne ref $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent and "ARRAY" ne ref $parent) 0 3 3
Condition66%2/3
ref $parent and "ARRAY" ne ref $parent
!ll&&!rl&&r
210
MC/DC0%0/2
ref $parent and "ARRAY" ne ref $parent
ref $parent "ARRAY" ne ref $parent
Truth table66%2/3
A and B
A= ref $parentB= "ARRAY" ne ref $parent
ABresult
0X0
100
111
208 0 croak(
209 "Argument to 'parent' must be a classname or array of classnames, not ($parent)"
210 );
211 }
212 3 ▶ $parent = [$parent] unless ref $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parent 2 1 3
213 3 foreach my $p (@$parent) {
214 1 ▶ eval "use $p";
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
215 4 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
216 }
217 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
218 3 push @{"${caller}::ISA"} => @$parent;
219 }
220 else {
221 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 push @{"${caller}::ISA"} => 'Test::Class';
223 }
224 4 ▶ if ( my $attributes = delete $args{attributes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attributes = delete $args{"attributes"}) 1 3 4
225 1 ▶ if ( ref $attributes && 'ARRAY' ne ref $attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attributes and "ARRAY" ne ref $attributes) 0 1 1
Condition33%1/3
ref $attributes and "ARRAY" ne ref $attributes
!ll&&!rl&&r
100
MC/DC0%0/2
ref $attributes and "ARRAY" ne ref $attributes
ref $attributes "ARRAY" ne ref $attributes
Truth table33%1/3
A and B
A= ref $attributesB= "ARRAY" ne ref $attributes
ABresult
0X0
100
111
226 0 croak(
227 "Argument to 'attributes' must be a classname or array of classnames, not ($attributes)"
228 );
229 }
230 1 ▶ $attributes = [$attributes] unless ref $attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attributes 1 0 1
231 1 foreach my $attr (@$attributes) {
232 1 my $method = "$caller\::$attr";
233 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 4 ▶ *$method = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
235 4 my $test = shift;
236 4 ▶ return $test->{$method} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 3 4
237 3 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
238 0 croak("You may not pass more than one argument to '$method'");
239 }
240 3 $test->{$method} = shift;
241 3 return $test;
242 1 };
243 }
244 }
245 4 ▶ if ( my $is_abstract = delete $args{is_abstract} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $is_abstract = delete $args{'is_abstract'}) { } 2 2 4
246 2 $IS_ABSTRACT{$caller} = $is_abstract;
247 }
248 else {
249 2 $IS_ABSTRACT{$caller} = 0;
250 }
251 }
252 }
253  
254 =head1 TUTORIAL
255  
256 If you're not familiar with using L<Test::Class>, please see my tutorial at:
257  
258 =over 4
259  
260 =item * L<http://www.modernperlbooks.com/mt/2009/03/organizing-test-suites-with-testclass.html>
261  
262 =item * L<http://www.modernperlbooks.com/mt/2009/03/reusing-test-code-with-testclass.html>
263  
264 =item * L<http://www.modernperlbooks.com/mt/2009/03/making-your-testing-life-easier.html>
265  
266 =item * L<http://www.modernperlbooks.com/mt/2009/03/using-test-control-methods-with-testclass.html>
267  
268 =item * L<http://www.modernperlbooks.com/mt/2009/03/working-with-testclass-test-suites.html>
269  
270 =back
271  
272  
273 =head1 AUTHOR
274  
275 Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
276  
277 =head1 BUGS
278  
279 Please report any bugs or feature requests to C<bug-test-class-most at
280 rt.cpan.org>, or through the web interface at
281 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Class-Most>. I will be
282 notified, and then you'll automatically be notified of progress on your bug as
283 I make changes.
284  
285 =head1 SUPPORT
286  
287 You can find documentation for this module with the perldoc command.
288  
289 perldoc Test::Class::Most
290  
291 You can also look for information at:
292  
293 =over 4
294  
295 =item * RT: CPAN's request tracker
296  
297 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Class-Most>
298  
299 =item * AnnoCPAN: Annotated CPAN documentation
300  
301 L<http://annocpan.org/dist/Test-Class-Most>
302  
303 =item * CPAN Ratings
304  
305 L<http://cpanratings.perl.org/d/Test-Class-Most>
306  
307 =item * Search CPAN
308  
309 L<http://search.cpan.org/dist/Test-Class-Most/>
310  
311 =back
312  
313 =head1 SEE ALSO
314  
315 =over 4
316  
317 =item * L<Test::Class>
318  
319 xUnit-style testing in Perl
320  
321 =item * L<Test::Most>
322  
323 The most popular CPAN test modules bundled into one module.
324  
325 =item * L<Modern::Perl>
326  
327 I stole this code. Thanks C<chromatic>!
328  
329 =back
330  
331 =head1 ACKNOWLEDGEMENTS
332  
333 Thanks to Adrian Howard for L<Test::Class>, Adam Kennedy for maintaining it
334 and C<chromatic> for L<Modern::Perl>.
335  
336 =head1 COPYRIGHT & LICENSE
337  
338 Copyright 2010 Curtis "Ovid" Poe, all rights reserved.
339  
340 This program is free software; you can redistribute it and/or modify it under
341 the same terms as Perl itself.
342  
343 =cut
344  
345 1 ▶no warnings 'void';
Subroutine BEGIN: called CC=1 SCAR=0.0
346 1 "Boilerplate is bad, m'kay";
↑ Summary