Functional.pm

filter: Statementstmt 82.4% 89 / 108 Branchbran 47.0% 16 / 34 Conditioncond 33.3% 8 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 85.2% 29 / 34 Podpod 100% 14 / 14 totaltotal 68.4% 156 / 228 CC 26 SCAR 20.0
CC
26
Cov
68%
_test
21.3
group
12.0
notest
6.0
CRAP 7.4
↑ Summary Functional/Conf.pm »
1 package Test::Functional;
2  
3 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.06';
7  
8 =head1 NAME
9  
10 Test::Functional - Perl tests in a functional style.
11  
12 =head1 SYNOPSIS
13  
14 use Test::Functional;
15  
16 # make sure the bomb goes off
17 sub explode { die "BOOM" }
18 test { explode() } dies, "test-3";
19  
20 # implicit and explicit equivalence
21 test { 2 * 2 } 4, "test-1";
22 test { 2 * 2 } eqv 4, "test-1";
23  
24 # test blocks can be as simple or as involved as you want
25 test { 3 > 0 } true, "test-4";
26 test {
27 my $total = 0;
28 foreach my $person ($car->occupants) {
29 $total += $person->weight
30 }
31 $total < 600
32 } true, "test-5";
33  
34 # after the test runs, you also get the result.
35 my $horse = test { Horse->new } typeqv "Horse", "test-6";
36  
37 # you can make your own comparator functions, or use existing ones.
38 use Test::More import => [qw(like)];
39 sub islike {
40 my ($other) = @_;
41 return sub {
42 my ($got, $testname) = @_;
43 like($got, $other, $testname);
44 };
45 }
46 test { 'caterpillar' } islike(qr/cat/), 'is cat?';
47  
48 =head1 DESCRIPTION
49  
50 This modules uses (abuses?) the ability to create new syntax via perl
51 prototypes to create a testing system focused on functions rather than values.
52 Tests run blocks of Perl, and use comparator functions to test the output.
53 Despite being a different way of thinking about tests, it plays well with
54 L<Test::More> and friends.
55  
56 =cut
57 3 ▶use Data::Compare qw(Compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use Scalar::Quote qw(quote);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use Scalar::Util qw(blessed looks_like_number reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 ▶use Test::More import => ['is_deeply'];
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use Test::Functional::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 3 ▶use base 'Test::Builder::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 =head1 EXPORTS
66  
67 Since this module is going to be used for test scripts, its methods all export
68 by default. You can choose which you want using the standard directives:
69  
70 # import only eqv
71 use Test::Functional tests => 23, import => ['eqv'];
72  
73 # import all but notest
74 use Test::Functional tests => 23, import => ['!notest'];
75  
76 =cut
77 3 our @EXPORT = (
78 qw(test pretest notest group),
79 qw(eqv ineqv typeqv dies noop true false isdef isundef),
80 );
81  
82 # three global variables: two settings and a stack for test groups
83 3 my ($UNSTABLE, $FASTOUT, @STACK);
84  
85 =head1 CONFIGURE
86  
87 This package has two settings which can be altered to change performance:
88  
89 unstable - run tests which are normally skipped
90 fastout - cause the entire test to end after the first failure
91  
92 This package can be configured via L<Test::Functional::Conf> or the configure()
93 function.
94  
95 =over
96  
97 =item configure KEY => VALUE, ...
98  
99 Changes configuration values at run-time.
100  
101 =cut
102 3 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.7
Pod documented
103 3 my (%opts) = @_;
104 3 ▶ $UNSTABLE = $opts{unstable} if exists($opts{unstable});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"unstable"} 3 0 3
105 3 ▶ $FASTOUT = $opts{fastout} if exists($opts{fastout});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"fastout"} 3 0 3
106 }
107  
108 3 configure(
109 unstable => Test::Functional::Conf->unstable,
110 fastout => Test::Functional::Conf->fastout,
111 );
112  
113 =back
114  
115 =head1 TEST STRUCTURES
116  
117 =over
118  
119 =item B<test { BLOCK } [CONDITION,] NAME>
120  
121 This is the basic building block of Test::Functional. Each test function
122 contains an anonymous code block (which is expected to return a scalar
123 I<result>), a name for the test, and a condition (an optional subroutine to
124 check the result).
125  
126 In most cases, a test passes if the code block doesn't die, and if the condition
127 is true (or absent). There is a special condition I<dies> which expects the code
128 block to die, and fails unless it does so.
129  
130 Whether the test passes or fails, I<test> returns the value generated by
131 I<BLOCK>.
132  
133 =cut
134 378 ▶sub test(&$;$) {
Subroutine test: called CC=1 SCAR=0.0
Pod documented
135 378 return _test(0, @_);
136 }
137  
138 =item B<pretest { BLOCK } [CONDITION,] NAME>
139  
140 This works like I<test> except that if it fails, it will short-circuit all
141 testing at the current level. This means that top-level I<pretest> calls will
142 halt the entire test if they fail. One obvious example for this is:
143  
144 BEGIN { pretest { use Foo::Bar } "test-use" }
145 test { Foo::Bar::double(2) } eqv(4), "double(2)";
146 test { Foo::Bar::double(3) } eqv(6), "double(3)";
147 test { Foo::Bar::double(4) } eqv(8), "double(4)";
148  
149 If the C<use Foo::Bar> fails, the information that all the other tests are
150 failing is less useful. I<pretest> can also be combined with I<group>
151 (described later) to short-circuit a small set of related tests.
152  
153 =cut
154 0 ▶sub pretest(&$;$) {
Subroutine pretest: not called CC=1 SCAR=6.9
Pod documented
155 0 return _test(1, @_);
156 }
157  
158 =item B<notest { BLOCK } [CONDITION,] NAME>
159  
160 This is has exactly the same semantics as I<test>; the only difference is that
161 it normally doesn't run. If C<< Test::Functional::Conf->unstable >> is true,
162 then this test will run, otherwise it won't, and will just return undef.
163  
164 For test-driven development, it is useful to create failing tests using
165 I<notest> blocks; this prevents test regression. Once the implementation starts
166 working I<notest> can be switched to I<test>.
167  
168 =cut
169 0 ▶sub notest(&$;$) {
Subroutine notest: not called CC=2 SCAR=17.9
Pod documented
170 0 ▶ if($UNSTABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UNSTABLE) { } 0 0 0
171 0 return _test(0, @_);
172 } else {
173 0 my $t = __PACKAGE__->builder();
174 0 $t->skip("$_[-1]");
175 0 return undef;
176 }
177 }
178  
179 # $dies is a special code ref that we can test for equality. this code doesn't
180 # actually get run; it's more like a constant.
181 3 ▶my $dies = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
182  
183 # helper function for test, notest and pretest
184 378 ▶sub _test {
Subroutine _test: called CC=14 SCAR=30.6
185 378 my ($pre, $testfunc, $cmpfunc, $name) = @_;
186 378 ▶ if(scalar(@_) == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 3) { } 1 377 378
elsif (ref $cmpfunc ne 'CODE') { } 17 360 377
187 1 $name = $_[-1];
188 1 $cmpfunc = noop();
189 } elsif(ref($cmpfunc) ne 'CODE') {
190 17 $cmpfunc = eqv($cmpfunc);
191 }
192  
193 378 my $result = eval { &$testfunc() };
194 378 ▶ $name = @STACK ? join(".", @STACK) . ".$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
@STACK ? : 0 378 378
195  
196 378 my $t = __PACKAGE__->builder();
197 378 $t->level(3);
198 378 ▶ return _ok($@, $name, " failed to die") if $cmpfunc eq $dies;
Branch: 1 branch
BranchTrueFalseTotal
if $cmpfunc eq $dies 5 373 378
199 373 my $ok;
200 373 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 373 373
201 0 ▶ _fail($name, " died: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
202 0 $ok = 0;
203 } else {
204 373 $t->level(4);
205 373 $ok = &$cmpfunc($result, $name);
206 }
207 373 ▶ die if $pre && !$ok && @STACK;
Branch: 1 branch
BranchTrueFalseTotal
if $pre and not $ok and @STACK 0 373 373
Condition33%2/6
$pre and not $ok and @STACK
!ll&&!rl&&r
37300
$pre and not $ok
!ll&&!rl&&r
37300
MC/DC0%0/3
$pre and not $ok and @STACK
$pre not $ok @STACK
Truth table25%1/4
A and not(B) and C
A= $preB= not $okC= @STACK
ABCresult
0XX0
10X0
1100
1111
208 373 ▶ $t->BAIL_OUT("pretest failed") if !$ok && $pre;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $pre 0 373 373
Condition33%1/3
not $ok and $pre
!ll&&!rl&&r
37300
MC/DC0%0/2
not $ok and $pre
not $ok $pre
Truth table33%1/3
not(A) and B
A= not $okB= $pre
ABresult
0X0
100
111
209 373 ▶ $t->BAIL_OUT("fastout is on") if !$ok && $FASTOUT;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $FASTOUT 0 373 373
Condition33%1/3
not $ok and $FASTOUT
!ll&&!rl&&r
37300
MC/DC0%0/2
not $ok and $FASTOUT
not $ok $FASTOUT
Truth table33%1/3
not(A) and B
A= not $okB= $FASTOUT
ABresult
0X0
100
111
210 373 return $result;
211 }
212  
213 # helper function; wraps calls to builder->ok, displays failure messages, and
214 # helps keep our builder->level consistent.
215 342 ▶sub _ok {
Subroutine _ok: called CC=3 SCAR=11.8
216 342 my ($ok, $name, $failmsg) = @_;
217 342 my $t = __PACKAGE__->builder;
218 342 $t->ok($ok, $name);
219 342 ▶ $t->diag($failmsg) if !$ok && $failmsg;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $failmsg 0 342 342
Condition33%1/3
not $ok and $failmsg
!ll&&!rl&&r
34200
MC/DC0%0/2
not $ok and $failmsg
not $ok $failmsg
Truth table33%1/3
not(A) and B
A= not $okB= $failmsg
ABresult
0X0
100
111
220 342 return $ok;
221 }
222  
223 # similar to _ok, but deals with known failure.
224 0 ▶sub _fail {
Subroutine _fail: not called CC=2 SCAR=17.9
225 0 my ($name, $failmsg) = @_;
226 0 my $t = __PACKAGE__->builder;
227 0 $t->ok(0, $name);
228 0 ▶ $t->diag($failmsg) if $failmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $failmsg 0 0 0
229 0 return 0;
230 }
231  
232 =item B<group { BLOCK } NAME>
233  
234 Groups are blocks which wrap associated tests. Groups can be used to namespace
235 tests as well as to allow groups of tests to fail together. Here is a short
236 example:
237  
238 group {
239 my $a = coretest { Adder->new } typeqv 'Adder', "new";
240  
241 test { $a->add(4, 6) } 10, "4 + 6";
242 test { $a->add("cat", "dog") } dies, "mass hysteria";
243 test { $a->add() } isundef, "not a number";
244  
245 } "adder";
246  
247 If C<< Adder->new >> fails, the rest of the tests aren't producing useful
248 results, so they will be skipped. See the L<ETHOS> section for a more in-depth
249 discussion of the package in general, and the implications of test
250 short-circuiting in particular.
251  
252 =cut
253 0 ▶sub group(&$) {
Subroutine group: not called CC=3 SCAR=24.8
Pod documented
254 0 my ($func, $name) = @_;
255  
256 0 push(@STACK, $name);
257 0 eval { &$func() };
258 0 pop(@STACK);
259  
260 0 ▶ die if $@ && @STACK;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and @STACK 0 0 0
Condition0%0/3
$@ and @STACK
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and @STACK
$@ @STACK
Truth table0%0/3
A and B
A= $@B= @STACK
ABresult
0X0
100
111
261 }
262  
263 =back
264  
265 =head1 TEST CONDITIONS
266  
267 =over
268  
269 =item B<eqv OBJECT>
270  
271 Creates a function which tests that the result is exactly equivalent (eqv) to
272 I<OBJECT> (using Test::More::is_deeply). It works for both simple values and
273 nested data structures. See L<Test::More> for more details.
274  
275 If I<test> receives a condition which isn't a code-ref, it will be wrapped in an
276 I<eqv> call, since this is the most common case (testing that a result is the
277 expected value).
278  
279 =cut
280 35 ▶sub eqv($) {
Subroutine eqv: called CC=1 SCAR=0.0
Pod documented
281 35 my ($other) = @_;
282 35 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
283 35 my ($got, $name) = @_;
284 35 return is_deeply($got, $other, $name);
285 35 };
286 }
287  
288 =item B<ineqv OBJECT>
289  
290 Tests whether the result differs from (is inequivalent to) I<OBJECT> according
291 to Data::Compare. This is expected (hoped?) to be inverse of I<eqv>.
292  
293 =cut
294 306 ▶sub ineqv($) {
Subroutine ineqv: called CC=1 SCAR=0.0
Pod documented
295 306 my ($other) = @_;
296 306 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
297 306 my ($got, $name) = @_;
298 306 return _ok(!Compare($got, $other), $name, " objects were the same");
299 306 };
300 }
301  
302 =item B<typeqv TYPE>
303  
304 Creates a function which tests that the result is of (or inhereits from) the
305 provided I<TYPE> (that the result's type is equivalent to I<TYPE>). For
306 unblessed references, it checks that
307 C<ref($result) eq $type>. For blessed references it checks that
308 C<< $result->isa($type) >>. Results which are not references will always be
309 false.
310  
311 =cut
312 18 ▶sub typeqv($) {
Subroutine typeqv: called CC=1 SCAR=0.3
Pod documented
313 18 my($type) = @_;
314 18 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=17.8
315 18 my ($got, $name) = @_;
316 18 ▶ return _fail($name, " result was undef") unless defined($got);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 18 18
317 18 ▶ return _fail($name, " result was not a ref") unless ref($got);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $got 0 18 18
318 18 ▶ my $ok = ref($got) eq $type || blessed($got) && $got->isa($type);
Condition50%3/6
ref $got eq $type || builtin::blessed($got) && $got->isa($type)
l!l&&r!l&&!r
1080
builtin::blessed($got) and $got->isa($type)
!ll&&!rl&&r
008
MC/DC0%0/3
ref $got eq $type || builtin::blessed($got) && $got->isa($type)
ref $got eq $type builtin::blessed($got) $got->isa($type)
Truth table50%2/4
A || B and C
A= ref $got eq $typeB= builtin::blessed($got)C= $got->isa($type)
ABCresult
1XX1
0111
00X0
0100
319 18 return _ok($ok, $name, " result was not of type $type");
320 18 };
321 }
322  
323 =item B<dies>
324  
325 Verifies that the test's code block died. It is unique amongst test conditions
326 in that it doesn't test the result, but rather tests C<$@>. Any result other
327 than a die succeeds.
328  
329 =cut
330 5 ▶sub dies() {
Subroutine dies: called CC=1 SCAR=0.0
Pod documented
331 5 return $dies;
332 };
333  
334 =item B<noop>
335  
336 This is the "default" condition; if no condition is given to a test then this
337 condition is used. As long as the code block does not die, the test passes.
338  
339 =cut
340 2 ▶sub noop() {
Subroutine noop: called CC=1 SCAR=0.0
Pod documented
341 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
342 2 my ($got, $name) = @_;
343 2 return _ok(1, $name);
344 2 };
345 }
346  
347 =item B<true>
348  
349 Verifies that the result is a true value.
350  
351 =cut
352 3 ▶sub true() {
Subroutine true: called CC=1 SCAR=0.0
Pod documented
353 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
354 3 my ($got, $name) = @_;
355 3 return _ok($got, $name);
356 3 };
357 }
358  
359 =item B<false>
360  
361 Verifies that the result is a false value.
362  
363 =cut
364 3 ▶sub false() {
Subroutine false: called CC=1 SCAR=0.0
Pod documented
365 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
366 3 my ($got, $name) = @_;
367 3 return _ok(!$got, $name);
368 3 };
369 }
370  
371 =item B<isdef>
372  
373 Checks that the result is defined (not undef).
374  
375 =cut
376 4 ▶sub isdef() {
Subroutine isdef: called CC=1 SCAR=0.0
Pod documented
377 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
378 4 my ($got, $name) = @_;
379 4 return _ok(defined($got), $name);
380 4 };
381 }
382  
383 =item B<isundef>
384  
385 Checks that the result is undefined.
386  
387 =cut
388 1 ▶sub isundef() {
Subroutine isundef: called CC=1 SCAR=0.0
Pod documented
389 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
390 1 my ($got, $name) = @_;
391 1 return _ok(!defined($got), $name);
392 1 };
393 }
394  
395 =back
396  
397 =head1 CUSTOM TEST CONDITIONS
398  
399 Anonymous subroutines can be used in place of the provided test conditions.
400 These functions take two arguments: the test result and the test's name. Here
401 are some examples:
402  
403 use Test::More;
404  
405 sub over21 {
406 my ($result, $name) = @_;
407 return cmp_ok($result, '>=', 21, $name);
408 }
409 test { $alice->age } \&over21, 'can alice drink?';
410 test { $bob->age } \&over21, 'can bob drink?';
411  
412 These examples are kind of clunky, but you get the idea. Using anything
413 complicated will probably require reading the source, and/or learning how to
414 use L<Test::Builder>. In particular, it's important to make sure
415 C<< builder->level >> is set correctly.
416  
417 =head1 ETHOS
418  
419 This package exists to address some specific concerns I've had while writing
420 tests using other frameworks. As such, it has some pretty major differences from
421 the other testing frameworks out there.
422  
423 Most Perl tests are written as perl scripts which test Perl code by calling
424 functions or methods, and then using various Test packages to look at the
425 result. This approach has some problems:
426  
427 =over
428  
429 =item 1
430  
431 Test scripts can make bad assumptions or have bugs, causing problems that
432 aren't obviously linked to a particular test clause and which can be hard to
433 track down and fix.
434  
435 =item 2
436  
437 Writing defensive test scripts involves a bunch of relatively boiler-plate
438 eval-blocks and C<$@> tests, as well as effectively doubling the number of tests
439 that are "run" without meaningfully doubling the test coverage.
440  
441 =item 3
442  
443 In some cases a small early error causes tons of test clauses to spew useless
444 messages about failing; this loses sight of the basic issue that caused the
445 problem (syntax error, missing module, etc).
446  
447 =back
448  
449 Test::Functional addresses these concerns: it enables the programmer to write
450 all the "meat" of the test script inside anonymous subs which are tests [1].
451 Since each test checks both that the code did not die and that the result was
452 what was expected, the tester doesn't have to worry about what kind of failure
453 might occur, just about the expected outcome [2]. Especially when trying to test
454 other people's code (gray box testing?) this feature is invaluable.
455  
456 The various features to prematurely end the test (using I<pretest()> and/or
457 C<< $Test::Functional::Conf->fastout >>) can help the developer to focus on the
458 problem at hand, rather than having to filter through spew [3]. This is
459 especially nice during test-driven development, or when trying to increase
460 coverage for an old and crufty module.
461  
462 =head1 AUTHOR
463  
464 Erik Osheim C<< <erik at osheim.org> >>
465  
466 =head1 BUGS
467  
468 The syntax takes some getting used to.
469  
470 I should create default wrappers for things such as I<like> and I<compare> from
471 L<Test::More>. Currently I mostly use I<true> but that gives less debugging
472 information.
473  
474 I wrote these tests to suit my needs, so I am sure there are cases I haven't
475 thought of or encountered. Also, I'm sure I have a lot to learn about the
476 intricacies of L<Test::Harness> and L<Test::Module>. Please contact me (via
477 email or L<http://rt.cpan.org>) with any comments, advice, or problems.
478  
479 =head1 ACKNOWLEDGEMENTS
480  
481 This module is based on Test::Builder::Module, and relies heavily on the work
482 done by Michael Schwern. It also uses Data::Compare by David Cantrell.
483  
484 =head1 COPYRIGHT & LICENSE
485  
486 Copyright 2009 Erik Osheim, all rights reserved.
487  
488 This program is free software; you can redistribute it and/or modify it
489 under the same terms as Perl itself.
490  
491 =cut
492  
493 3 1;
↑ Summary Functional/Conf.pm »