Commit | Line | Data |
---|---|---|
7b13a3f5 | 1 | package Test; |
809908f7 MS |
2 | |
3 | require 5.004; | |
4 | ||
5 | use strict; | |
6 | ||
7b13a3f5 | 7 | use Carp; |
809908f7 MS |
8 | use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK $ntest $TestLevel), #public-ish |
9 | qw($TESTOUT $ONFAIL %todo %history $planned @FAILDETAIL)#private-ish | |
10 | ); | |
11 | ||
12 | $VERSION = '1.17'; | |
7b13a3f5 JP |
13 | require Exporter; |
14 | @ISA=('Exporter'); | |
809908f7 MS |
15 | |
16 | @EXPORT = qw(&plan &ok &skip); | |
17 | @EXPORT_OK = qw($ntest $TESTOUT); | |
7b13a3f5 | 18 | |
3238f5fe | 19 | $TestLevel = 0; # how many extra stack frames to skip |
7b13a3f5 | 20 | $|=1; |
7b13a3f5 | 21 | $ntest=1; |
f2ac83ee | 22 | $TESTOUT = *STDOUT{IO}; |
7b13a3f5 | 23 | |
3238f5fe JP |
24 | # Use of this variable is strongly discouraged. It is set mainly to |
25 | # help test coverage analyzers know which test is running. | |
7b13a3f5 JP |
26 | $ENV{REGRESSION_TEST} = $0; |
27 | ||
809908f7 MS |
28 | |
29 | =head1 NAME | |
30 | ||
31 | Test - provides a simple framework for writing test scripts | |
32 | ||
33 | =head1 SYNOPSIS | |
34 | ||
35 | use strict; | |
36 | use Test; | |
37 | ||
38 | # use a BEGIN block so we print our plan before MyModule is loaded | |
39 | BEGIN { plan tests => 14, todo => [3,4] } | |
40 | ||
41 | # load your module... | |
42 | use MyModule; | |
43 | ||
44 | ok(0); # failure | |
45 | ok(1); # success | |
46 | ||
47 | ok(0); # ok, expected failure (see todo list, above) | |
48 | ok(1); # surprise success! | |
49 | ||
50 | ok(0,1); # failure: '0' ne '1' | |
51 | ok('broke','fixed'); # failure: 'broke' ne 'fixed' | |
52 | ok('fixed','fixed'); # success: 'fixed' eq 'fixed' | |
53 | ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/ | |
54 | ||
55 | ok(sub { 1+1 }, 2); # success: '2' eq '2' | |
56 | ok(sub { 1+1 }, 3); # failure: '2' ne '3' | |
57 | ok(0, int(rand(2)); # (just kidding :-) | |
58 | ||
59 | my @list = (0,0); | |
60 | ok @list, 3, "\@list=".join(',',@list); #extra diagnostics | |
61 | ok 'segmentation fault', '/(?i)success/'; #regex match | |
62 | ||
63 | skip($feature_is_missing, ...); #do platform specific test | |
64 | ||
65 | =head1 DESCRIPTION | |
66 | ||
67 | L<Test::Harness|Test::Harness> expects to see particular output when it | |
68 | executes tests. This module aims to make writing proper test scripts just | |
69 | a little bit easier (and less error prone :-). | |
70 | ||
71 | ||
72 | =head2 Functions | |
73 | ||
74 | All the following are exported by Test by default. | |
75 | ||
76 | =over 4 | |
77 | ||
78 | =item B<plan> | |
79 | ||
80 | BEGIN { plan %theplan; } | |
81 | ||
82 | This should be the first thing you call in your test script. It | |
83 | declares your testing plan, how many there will be, if any of them | |
84 | should be allowed to fail, etc... | |
85 | ||
86 | Typical usage is just: | |
87 | ||
88 | use Test; | |
89 | BEGIN { plan tests => 23 } | |
90 | ||
91 | Things you can put in the plan: | |
92 | ||
93 | tests The number of tests in your script. | |
94 | This means all ok() and skip() calls. | |
95 | todo A reference to a list of tests which are allowed | |
96 | to fail. See L</TODO TESTS>. | |
97 | onfail A subroutine reference to be run at the end of | |
98 | the test script should any of the tests fail. | |
99 | See L</ONFAIL>. | |
100 | ||
101 | You must call plan() once and only once. | |
102 | ||
103 | =cut | |
104 | ||
7b13a3f5 JP |
105 | sub plan { |
106 | croak "Test::plan(%args): odd number of arguments" if @_ & 1; | |
8b3be1d1 | 107 | croak "Test::plan(): should not be called more than once" if $planned; |
809908f7 MS |
108 | |
109 | local($\, $,); # guard against -l and other things that screw with | |
110 | ||
111 | ||
7b13a3f5 JP |
112 | my $max=0; |
113 | for (my $x=0; $x < @_; $x+=2) { | |
114 | my ($k,$v) = @_[$x,$x+1]; | |
115 | if ($k =~ /^test(s)?$/) { $max = $v; } | |
116 | elsif ($k eq 'todo' or | |
117 | $k eq 'failok') { for (@$v) { $todo{$_}=1; }; } | |
8b3be1d1 JP |
118 | elsif ($k eq 'onfail') { |
119 | ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE"; | |
120 | $ONFAIL = $v; | |
121 | } | |
7b13a3f5 JP |
122 | else { carp "Test::plan(): skipping unrecognized directive '$k'" } |
123 | } | |
124 | my @todo = sort { $a <=> $b } keys %todo; | |
125 | if (@todo) { | |
f2ac83ee | 126 | print $TESTOUT "1..$max todo ".join(' ', @todo).";\n"; |
7b13a3f5 | 127 | } else { |
f2ac83ee | 128 | print $TESTOUT "1..$max\n"; |
7b13a3f5 | 129 | } |
8b3be1d1 | 130 | ++$planned; |
809908f7 MS |
131 | |
132 | # Never used. | |
133 | return undef; | |
7b13a3f5 JP |
134 | } |
135 | ||
809908f7 MS |
136 | |
137 | =begin _private | |
138 | ||
139 | =item B<_to_value> | |
140 | ||
141 | my $value = _to_value($input); | |
142 | ||
143 | Converts an ok parameter to its value. Typically this just means | |
144 | running it if its a code reference. You should run all inputed | |
145 | values through this. | |
146 | ||
147 | =cut | |
148 | ||
149 | sub _to_value { | |
3238f5fe | 150 | my ($v) = @_; |
809908f7 | 151 | return (ref $v or '') eq 'CODE' ? $v->() : $v; |
3238f5fe JP |
152 | } |
153 | ||
809908f7 MS |
154 | =end _private |
155 | ||
156 | =item B<ok> | |
157 | ||
158 | ok(1 + 1 == 2); | |
159 | ok($have, $expect); | |
160 | ok($have, $expect, $diagnostics); | |
161 | ||
162 | This is the reason for Test's existance. Its the basic function that | |
163 | handles printing "ok" or "not ok" along with the current test number. | |
164 | ||
165 | In its most basic usage, it simply takes an expression. If its true, | |
166 | the test passes, if false, the test fails. Simp. | |
167 | ||
168 | ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2 | |
169 | ok( $foo =~ /bar/ ); # ok if $foo contains 'bar' | |
170 | ok( baz($x + $y) eq 'Armondo' ); # ok if baz($x + $y) returns | |
171 | # 'Armondo' | |
172 | ok( @a == @b ); # ok if @a and @b are the same length | |
173 | ||
174 | The expression is evaluated in scalar context. So the following will | |
175 | work: | |
176 | ||
177 | ok( @stuff ); # ok if @stuff has any elements | |
178 | ok( !grep !defined $_, @stuff ); # ok if everything in @stuff is | |
179 | # defined. | |
180 | ||
181 | A special case is if the expression is a subroutine reference. In | |
182 | that case, it is executed and its value (true or false) determines if | |
183 | the test passes or fails. | |
184 | ||
185 | In its two argument form it compares the two values to see if they | |
186 | equal (with C<eq>). | |
187 | ||
188 | ok( "this", "that" ); # not ok, 'this' ne 'that' | |
189 | ||
190 | If either is a subroutine reference, that is run and used as a | |
191 | comparison. | |
192 | ||
193 | Should $expect either be a regex reference (ie. qr//) or a string that | |
194 | looks like a regex (ie. '/foo/') ok() will perform a pattern match | |
195 | against it rather than using eq. | |
196 | ||
197 | ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/ | |
198 | ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/; | |
199 | ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i; | |
200 | ||
201 | Finally, an optional set of $diagnostics will be printed should the | |
202 | test fail. This should usually be some useful information about the | |
203 | test pertaining to why it failed or perhaps a description of the test. | |
204 | Or both. | |
205 | ||
206 | ok( grep($_ eq 'something unique', @stuff), 1, | |
207 | "Something that should be unique isn't!\n". | |
208 | '@stuff = '.join ', ', @stuff | |
209 | ); | |
210 | ||
211 | Unfortunately, a diagnostic cannot be used with the single argument | |
212 | style of ok(). | |
213 | ||
214 | All these special cases can cause some problems. See L</BUGS and CAVEATS>. | |
215 | ||
216 | =cut | |
217 | ||
8b3be1d1 JP |
218 | sub ok ($;$$) { |
219 | croak "ok: plan before you test!" if !$planned; | |
809908f7 MS |
220 | |
221 | local($\,$,); # guard against -l and other things that screw with | |
222 | ||
223 | ||
3238f5fe JP |
224 | my ($pkg,$file,$line) = caller($TestLevel); |
225 | my $repetition = ++$history{"$file:$line"}; | |
226 | my $context = ("$file at line $line". | |
8b3be1d1 | 227 | ($repetition > 1 ? " fail \#$repetition" : '')); |
3238f5fe | 228 | my $ok=0; |
809908f7 MS |
229 | my $result = _to_value(shift); |
230 | my ($expected,$diag,$isregex,$regex); | |
3238f5fe | 231 | if (@_ == 0) { |
8b3be1d1 | 232 | $ok = $result; |
3238f5fe | 233 | } else { |
809908f7 | 234 | $expected = _to_value(shift); |
59e80644 JP |
235 | if (!defined $expected) { |
236 | $ok = !defined $result; | |
237 | } elsif (!defined $result) { | |
238 | $ok = 0; | |
239 | } elsif ((ref($expected)||'') eq 'Regexp') { | |
f2ac83ee | 240 | $ok = $result =~ /$expected/; |
809908f7 | 241 | $regex = $expected; |
f2ac83ee | 242 | } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or |
809908f7 | 243 | (undef, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) { |
8b3be1d1 | 244 | $ok = $result =~ /$regex/; |
3238f5fe | 245 | } else { |
3238f5fe JP |
246 | $ok = $result eq $expected; |
247 | } | |
8b3be1d1 | 248 | } |
f2ac83ee GS |
249 | my $todo = $todo{$ntest}; |
250 | if ($todo and $ok) { | |
251 | $context .= ' TODO?!' if $todo; | |
252 | print $TESTOUT "ok $ntest # ($context)\n"; | |
8b3be1d1 | 253 | } else { |
809908f7 MS |
254 | # Issuing two seperate prints() causes problems on VMS. |
255 | if (!$ok) { | |
256 | print $TESTOUT "not ok $ntest\n"; | |
e5420382 | 257 | } |
809908f7 MS |
258 | else { |
259 | print $TESTOUT "ok $ntest\n"; | |
e5420382 | 260 | } |
8b3be1d1 JP |
261 | |
262 | if (!$ok) { | |
263 | my $detail = { 'repetition' => $repetition, 'package' => $pkg, | |
f2ac83ee | 264 | 'result' => $result, 'todo' => $todo }; |
8b3be1d1 | 265 | $$detail{expected} = $expected if defined $expected; |
809908f7 MS |
266 | |
267 | # Get the user's diagnostic, protecting against multi-line | |
268 | # diagnostics. | |
269 | $diag = $$detail{diagnostic} = _to_value(shift) if @_; | |
270 | $diag =~ s/\n/\n#/g if defined $diag; | |
271 | ||
f2ac83ee | 272 | $context .= ' *TODO*' if $todo; |
8b3be1d1 | 273 | if (!defined $expected) { |
3238f5fe | 274 | if (!$diag) { |
f2ac83ee | 275 | print $TESTOUT "# Failed test $ntest in $context\n"; |
3238f5fe | 276 | } else { |
f2ac83ee | 277 | print $TESTOUT "# Failed test $ntest in $context: $diag\n"; |
3238f5fe | 278 | } |
8b3be1d1 JP |
279 | } else { |
280 | my $prefix = "Test $ntest"; | |
59e80644 JP |
281 | print $TESTOUT "# $prefix got: ". |
282 | (defined $result? "'$result'":'<UNDEF>')." ($context)\n"; | |
8b3be1d1 | 283 | $prefix = ' ' x (length($prefix) - 5); |
809908f7 MS |
284 | if (defined $regex) { |
285 | $expected = 'qr{'.$regex.'}'; | |
286 | } | |
287 | else { | |
f2ac83ee GS |
288 | $expected = "'$expected'"; |
289 | } | |
8b3be1d1 | 290 | if (!$diag) { |
f2ac83ee | 291 | print $TESTOUT "# $prefix Expected: $expected\n"; |
3238f5fe | 292 | } else { |
f2ac83ee | 293 | print $TESTOUT "# $prefix Expected: $expected ($diag)\n"; |
3238f5fe JP |
294 | } |
295 | } | |
8b3be1d1 | 296 | push @FAILDETAIL, $detail; |
7b13a3f5 | 297 | } |
7b13a3f5 JP |
298 | } |
299 | ++ $ntest; | |
300 | $ok; | |
301 | } | |
302 | ||
809908f7 MS |
303 | sub skip ($;$$$) { |
304 | local($\, $,); # guard against -l and other things that screw with | |
305 | ||
306 | ||
307 | my $whyskip = _to_value(shift); | |
308 | if (!@_ or $whyskip) { | |
309 | $whyskip = '' if $whyskip =~ m/^\d+$/; | |
310 | $whyskip =~ s/^[Ss]kip(?:\s+|$)//; # backwards compatibility, old | |
311 | # versions required the reason | |
312 | # to start with 'skip' | |
313 | # We print in one shot for VMSy reasons. | |
314 | my $ok = "ok $ntest # skip"; | |
315 | $ok .= " $whyskip" if length $whyskip; | |
316 | $ok .= "\n"; | |
317 | print $TESTOUT $ok; | |
318 | ++ $ntest; | |
319 | return 1; | |
7b13a3f5 | 320 | } else { |
809908f7 MS |
321 | # backwards compatiblity (I think). skip() used to be |
322 | # called like ok() and was expected to fail, which is weird. | |
323 | warn <<WARN if $^W; | |
324 | This looks like a skip() using the very old interface. Please upgrade to | |
325 | the documented interface as this has been deprecated. | |
326 | WARN | |
327 | ||
8b3be1d1 | 328 | local($TestLevel) = $TestLevel+1; #ignore this stack frame |
809908f7 | 329 | return &ok(@_); |
7b13a3f5 JP |
330 | } |
331 | } | |
332 | ||
809908f7 MS |
333 | =back |
334 | ||
335 | =cut | |
336 | ||
8b3be1d1 JP |
337 | END { |
338 | $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL; | |
339 | } | |
340 | ||
7b13a3f5 JP |
341 | 1; |
342 | __END__ | |
343 | ||
3238f5fe | 344 | =head1 TEST TYPES |
7b13a3f5 JP |
345 | |
346 | =over 4 | |
347 | ||
348 | =item * NORMAL TESTS | |
349 | ||
f2ac83ee | 350 | These tests are expected to succeed. If they don't something's |
3238f5fe | 351 | screwed up! |
7b13a3f5 JP |
352 | |
353 | =item * SKIPPED TESTS | |
354 | ||
f2ac83ee GS |
355 | Skip is for tests that might or might not be possible to run depending |
356 | on the availability of platform specific features. The first argument | |
357 | should evaluate to true (think "yes, please skip") if the required | |
358 | feature is not available. After the first argument, skip works | |
3238f5fe | 359 | exactly the same way as do normal tests. |
7b13a3f5 JP |
360 | |
361 | =item * TODO TESTS | |
362 | ||
f2ac83ee GS |
363 | TODO tests are designed for maintaining an B<executable TODO list>. |
364 | These tests are expected NOT to succeed. If a TODO test does succeed, | |
365 | the feature in question should not be on the TODO list, now should it? | |
7b13a3f5 | 366 | |
f2ac83ee | 367 | Packages should NOT be released with succeeding TODO tests. As soon |
7b13a3f5 | 368 | as a TODO test starts working, it should be promoted to a normal test |
f2ac83ee GS |
369 | and the newly working feature should be documented in the release |
370 | notes or change log. | |
7b13a3f5 JP |
371 | |
372 | =back | |
373 | ||
8b3be1d1 JP |
374 | =head1 ONFAIL |
375 | ||
376 | BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } } | |
377 | ||
f2ac83ee GS |
378 | While test failures should be enough, extra diagnostics can be |
379 | triggered at the end of a test run. C<onfail> is passed an array ref | |
380 | of hash refs that describe each test failure. Each hash will contain | |
381 | at least the following fields: C<package>, C<repetition>, and | |
382 | C<result>. (The file, line, and test number are not included because | |
f610777f | 383 | their correspondence to a particular test is tenuous.) If the test |
f2ac83ee GS |
384 | had an expected value or a diagnostic string, these will also be |
385 | included. | |
386 | ||
387 | The B<optional> C<onfail> hook might be used simply to print out the | |
388 | version of your package and/or how to report problems. It might also | |
389 | be used to generate extremely sophisticated diagnostics for a | |
390 | particularly bizarre test failure. However it's not a panacea. Core | |
391 | dumps or other unrecoverable errors prevent the C<onfail> hook from | |
392 | running. (It is run inside an C<END> block.) Besides, C<onfail> is | |
393 | probably over-kill in most cases. (Your test code should be simpler | |
8b3be1d1 JP |
394 | than the code it is testing, yes?) |
395 | ||
809908f7 MS |
396 | |
397 | =head1 BUGS and CAVEATS | |
398 | ||
399 | ok()'s special handling of subroutine references is an unfortunate | |
400 | "feature" that can't be removed due to compatibility. | |
401 | ||
402 | ok()'s use of string eq can sometimes cause odd problems when comparing | |
403 | numbers, especially if you're casting a string to a number: | |
404 | ||
405 | $foo = "1.0"; | |
406 | ok( $foo, 1 ); # not ok, "1.0" ne 1 | |
407 | ||
408 | Your best bet is to use the single argument form: | |
409 | ||
410 | ok( $foo == 1 ); # ok "1.0" == 1 | |
411 | ||
412 | ok()'s special handing of strings which look like they might be | |
413 | regexes can also cause unexpected behavior. An innocent: | |
414 | ||
415 | ok( $fileglob, '/path/to/some/*stuff/' ); | |
416 | ||
417 | will fail since Test.pm considers the second argument to a regex. | |
418 | Again, best bet is to use the single argument form: | |
419 | ||
420 | ok( $fileglob eq '/path/to/some/*stuff/' ); | |
421 | ||
422 | ||
423 | =head1 TODO | |
424 | ||
425 | Add todo(). | |
426 | ||
427 | Allow named tests. | |
428 | ||
429 | Implement noplan(). | |
430 | ||
431 | ||
7b13a3f5 JP |
432 | =head1 SEE ALSO |
433 | ||
809908f7 MS |
434 | L<Test::Simple>, L<Test::More>, L<Test::Harness>, L<Devel::Cover> |
435 | ||
436 | L<Test::Unit> is an interesting alternative testing library. | |
437 | ||
7b13a3f5 JP |
438 | |
439 | =head1 AUTHOR | |
440 | ||
809908f7 MS |
441 | Copyright (c) 1998-2000 Joshua Nathaniel Pritikin. All rights reserved. |
442 | Copyright (c) 2001 Michael G Schwern. | |
443 | ||
444 | Current maintainer, Michael G Schwern <schwern@pobox.com> | |
7b13a3f5 JP |
445 | |
446 | This package is free software and is provided "as is" without express | |
447 | or implied warranty. It may be used, redistributed and/or modified | |
448 | under the terms of the Perl Artistic License (see | |
449 | http://www.perl.com/perl/misc/Artistic.html) | |
450 | ||
451 | =cut |