Commit | Line | Data |
3f2ec160 |
1 | package Test::More; |
2 | |
d020a79a |
3 | use 5.004; |
3f2ec160 |
4 | |
d020a79a |
5 | use strict; |
33459055 |
6 | use Test::Builder; |
3f2ec160 |
7 | |
33459055 |
8 | |
9 | # Can't use Carp because it might cause use_ok() to accidentally succeed |
10 | # even though the module being used forgot to use Carp. Yes, this |
11 | # actually happened. |
12 | sub _carp { |
13 | my($file, $line) = (caller(1))[1,2]; |
a9153838 |
14 | warn @_, " at $file line $line\n"; |
3f2ec160 |
15 | } |
16 | |
33459055 |
17 | |
18 | |
3f2ec160 |
19 | require Exporter; |
33459055 |
20 | use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO); |
6686786d |
21 | $VERSION = '0.42'; |
3f2ec160 |
22 | @ISA = qw(Exporter); |
23 | @EXPORT = qw(ok use_ok require_ok |
a9153838 |
24 | is isnt like unlike is_deeply |
25 | cmp_ok |
26 | skip todo todo_skip |
3f2ec160 |
27 | pass fail |
28 | eq_array eq_hash eq_set |
d020a79a |
29 | $TODO |
30 | plan |
31 | can_ok isa_ok |
a9153838 |
32 | diag |
3f2ec160 |
33 | ); |
34 | |
33459055 |
35 | my $Test = Test::Builder->new; |
3f2ec160 |
36 | |
3f2ec160 |
37 | |
38 | # 5.004's Exporter doesn't have export_to_level. |
39 | sub _export_to_level |
40 | { |
41 | my $pkg = shift; |
42 | my $level = shift; |
a9153838 |
43 | (undef) = shift; # redundant arg |
3f2ec160 |
44 | my $callpkg = caller($level); |
45 | $pkg->export($callpkg, @_); |
46 | } |
47 | |
48 | |
49 | =head1 NAME |
50 | |
51 | Test::More - yet another framework for writing test scripts |
52 | |
53 | =head1 SYNOPSIS |
54 | |
55 | use Test::More tests => $Num_Tests; |
56 | # or |
57 | use Test::More qw(no_plan); |
58 | # or |
d020a79a |
59 | use Test::More skip_all => $reason; |
3f2ec160 |
60 | |
61 | BEGIN { use_ok( 'Some::Module' ); } |
62 | require_ok( 'Some::Module' ); |
63 | |
64 | # Various ways to say "ok" |
65 | ok($this eq $that, $test_name); |
66 | |
67 | is ($this, $that, $test_name); |
68 | isnt($this, $that, $test_name); |
a9153838 |
69 | |
70 | # Rather than print STDERR "# here's what went wrong\n" |
71 | diag("here's what went wrong"); |
72 | |
73 | like ($this, qr/that/, $test_name); |
74 | unlike($this, qr/that/, $test_name); |
75 | |
76 | cmp_ok($this, '==', $that, $test_name); |
3f2ec160 |
77 | |
33459055 |
78 | is_deeply($complex_structure1, $complex_structure2, $test_name); |
79 | |
d020a79a |
80 | SKIP: { |
81 | skip $why, $how_many unless $have_some_feature; |
82 | |
3f2ec160 |
83 | ok( foo(), $test_name ); |
84 | is( foo(42), 23, $test_name ); |
d020a79a |
85 | }; |
86 | |
87 | TODO: { |
88 | local $TODO = $why; |
3f2ec160 |
89 | |
3f2ec160 |
90 | ok( foo(), $test_name ); |
91 | is( foo(42), 23, $test_name ); |
d020a79a |
92 | }; |
93 | |
94 | can_ok($module, @methods); |
95 | isa_ok($object, $class); |
3f2ec160 |
96 | |
97 | pass($test_name); |
98 | fail($test_name); |
99 | |
100 | # Utility comparison functions. |
101 | eq_array(\@this, \@that); |
102 | eq_hash(\%this, \%that); |
103 | eq_set(\@this, \@that); |
104 | |
105 | # UNIMPLEMENTED!!! |
106 | my @status = Test::More::status; |
107 | |
d020a79a |
108 | # UNIMPLEMENTED!!! |
109 | BAIL_OUT($why); |
110 | |
3f2ec160 |
111 | |
112 | =head1 DESCRIPTION |
113 | |
a9153838 |
114 | B<STOP!> If you're just getting started writing tests, have a look at |
d020a79a |
115 | Test::Simple first. This is a drop in replacement for Test::Simple |
116 | which you can switch to once you get the hang of basic testing. |
3f2ec160 |
117 | |
a9153838 |
118 | The purpose of this module is to provide a wide range of testing |
119 | utilities. Various ways to say "ok" with better diagnostics, |
120 | facilities to skip tests, test future features and compare complicated |
121 | data structures. While you can do almost anything with a simple |
122 | C<ok()> function, it doesn't provide good diagnostic output. |
3f2ec160 |
123 | |
124 | |
125 | =head2 I love it when a plan comes together |
126 | |
127 | Before anything else, you need a testing plan. This basically declares |
128 | how many tests your script is going to run to protect against premature |
129 | failure. |
130 | |
4bd4e70a |
131 | The preferred way to do this is to declare a plan when you C<use Test::More>. |
3f2ec160 |
132 | |
133 | use Test::More tests => $Num_Tests; |
134 | |
135 | There are rare cases when you will not know beforehand how many tests |
136 | your script is going to run. In this case, you can declare that you |
137 | have no plan. (Try to avoid using this as it weakens your test.) |
138 | |
139 | use Test::More qw(no_plan); |
140 | |
141 | In some cases, you'll want to completely skip an entire testing script. |
142 | |
d020a79a |
143 | use Test::More skip_all => $skip_reason; |
3f2ec160 |
144 | |
d020a79a |
145 | Your script will declare a skip with the reason why you skipped and |
146 | exit immediately with a zero (success). See L<Test::Harness> for |
147 | details. |
3f2ec160 |
148 | |
33459055 |
149 | If you want to control what functions Test::More will export, you |
150 | have to use the 'import' option. For example, to import everything |
151 | but 'fail', you'd do: |
152 | |
153 | use Test::More tests => 23, import => ['!fail']; |
154 | |
155 | Alternatively, you can use the plan() function. Useful for when you |
156 | have to calculate the number of tests. |
157 | |
158 | use Test::More; |
159 | plan tests => keys %Stuff * 3; |
160 | |
161 | or for deciding between running the tests at all: |
162 | |
163 | use Test::More; |
164 | if( $^O eq 'MacOS' ) { |
4bd4e70a |
165 | plan skip_all => 'Test irrelevant on MacOS'; |
33459055 |
166 | } |
167 | else { |
168 | plan tests => 42; |
169 | } |
170 | |
171 | =cut |
172 | |
173 | sub plan { |
174 | my(@plan) = @_; |
175 | |
176 | my $caller = caller; |
177 | |
178 | $Test->exported_to($caller); |
179 | $Test->plan(@plan); |
180 | |
181 | my @imports = (); |
182 | foreach my $idx (0..$#plan) { |
183 | if( $plan[$idx] eq 'import' ) { |
184 | @imports = @{$plan[$idx+1]}; |
185 | last; |
186 | } |
187 | } |
188 | |
189 | __PACKAGE__->_export_to_level(1, __PACKAGE__, @imports); |
190 | } |
191 | |
192 | sub import { |
193 | my($class) = shift; |
194 | goto &plan; |
195 | } |
196 | |
3f2ec160 |
197 | |
198 | =head2 Test names |
199 | |
200 | By convention, each test is assigned a number in order. This is |
6686786d |
201 | largely done automatically for you. However, it's often very useful to |
3f2ec160 |
202 | assign a name to each test. Which would you rather see: |
203 | |
204 | ok 4 |
205 | not ok 5 |
206 | ok 6 |
207 | |
208 | or |
209 | |
210 | ok 4 - basic multi-variable |
211 | not ok 5 - simple exponential |
212 | ok 6 - force == mass * acceleration |
213 | |
214 | The later gives you some idea of what failed. It also makes it easier |
215 | to find the test in your script, simply search for "simple |
216 | exponential". |
217 | |
6686786d |
218 | All test functions take a name argument. It's optional, but highly |
3f2ec160 |
219 | suggested that you use it. |
220 | |
221 | |
222 | =head2 I'm ok, you're not ok. |
223 | |
224 | The basic purpose of this module is to print out either "ok #" or "not |
225 | ok #" depending on if a given test succeeded or failed. Everything |
226 | else is just gravy. |
227 | |
228 | All of the following print "ok" or "not ok" depending on if the test |
229 | succeeded or failed. They all also return true or false, |
230 | respectively. |
231 | |
232 | =over 4 |
233 | |
234 | =item B<ok> |
235 | |
236 | ok($this eq $that, $test_name); |
237 | |
238 | This simply evaluates any expression (C<$this eq $that> is just a |
239 | simple example) and uses that to determine if the test succeeded or |
240 | failed. A true expression passes, a false one fails. Very simple. |
241 | |
242 | For example: |
243 | |
244 | ok( $exp{9} == 81, 'simple exponential' ); |
245 | ok( Film->can('db_Main'), 'set_db()' ); |
246 | ok( $p->tests == 4, 'saw tests' ); |
247 | ok( !grep !defined $_, @items, 'items populated' ); |
248 | |
249 | (Mnemonic: "This is ok.") |
250 | |
251 | $test_name is a very short description of the test that will be printed |
252 | out. It makes it very easy to find a test in your script when it fails |
253 | and gives others an idea of your intentions. $test_name is optional, |
254 | but we B<very> strongly encourage its use. |
255 | |
256 | Should an ok() fail, it will produce some diagnostics: |
257 | |
258 | not ok 18 - sufficient mucus |
259 | # Failed test 18 (foo.t at line 42) |
260 | |
261 | This is actually Test::Simple's ok() routine. |
262 | |
263 | =cut |
264 | |
33459055 |
265 | sub ok ($;$) { |
266 | my($test, $name) = @_; |
267 | $Test->ok($test, $name); |
268 | } |
3f2ec160 |
269 | |
270 | =item B<is> |
271 | |
272 | =item B<isnt> |
273 | |
274 | is ( $this, $that, $test_name ); |
275 | isnt( $this, $that, $test_name ); |
276 | |
d020a79a |
277 | Similar to ok(), is() and isnt() compare their two arguments |
278 | with C<eq> and C<ne> respectively and use the result of that to |
279 | determine if the test succeeded or failed. So these: |
3f2ec160 |
280 | |
281 | # Is the ultimate answer 42? |
282 | is( ultimate_answer(), 42, "Meaning of Life" ); |
283 | |
284 | # $foo isn't empty |
285 | isnt( $foo, '', "Got some foo" ); |
286 | |
287 | are similar to these: |
288 | |
289 | ok( ultimate_answer() eq 42, "Meaning of Life" ); |
290 | ok( $foo ne '', "Got some foo" ); |
291 | |
292 | (Mnemonic: "This is that." "This isn't that.") |
293 | |
294 | So why use these? They produce better diagnostics on failure. ok() |
295 | cannot know what you are testing for (beyond the name), but is() and |
296 | isnt() know what the test was and why it failed. For example this |
d020a79a |
297 | test: |
3f2ec160 |
298 | |
299 | my $foo = 'waffle'; my $bar = 'yarblokos'; |
300 | is( $foo, $bar, 'Is foo the same as bar?' ); |
301 | |
302 | Will produce something like this: |
303 | |
304 | not ok 17 - Is foo the same as bar? |
305 | # Failed test 1 (foo.t at line 139) |
306 | # got: 'waffle' |
307 | # expected: 'yarblokos' |
308 | |
309 | So you can figure out what went wrong without rerunning the test. |
310 | |
311 | You are encouraged to use is() and isnt() over ok() where possible, |
312 | however do not be tempted to use them to find out if something is |
313 | true or false! |
314 | |
315 | # XXX BAD! $pope->isa('Catholic') eq 1 |
316 | is( $pope->isa('Catholic'), 1, 'Is the Pope Catholic?' ); |
317 | |
318 | This does not check if C<$pope->isa('Catholic')> is true, it checks if |
319 | it returns 1. Very different. Similar caveats exist for false and 0. |
320 | In these cases, use ok(). |
321 | |
322 | ok( $pope->isa('Catholic') ), 'Is the Pope Catholic?' ); |
323 | |
d020a79a |
324 | For those grammatical pedants out there, there's an C<isn't()> |
325 | function which is an alias of isnt(). |
3f2ec160 |
326 | |
327 | =cut |
328 | |
329 | sub is ($$;$) { |
33459055 |
330 | $Test->is_eq(@_); |
3f2ec160 |
331 | } |
332 | |
333 | sub isnt ($$;$) { |
a9153838 |
334 | $Test->isnt_eq(@_); |
3f2ec160 |
335 | } |
336 | |
337 | *isn't = \&isnt; |
338 | |
339 | |
340 | =item B<like> |
341 | |
342 | like( $this, qr/that/, $test_name ); |
343 | |
344 | Similar to ok(), like() matches $this against the regex C<qr/that/>. |
345 | |
346 | So this: |
347 | |
348 | like($this, qr/that/, 'this is like that'); |
349 | |
350 | is similar to: |
351 | |
352 | ok( $this =~ /that/, 'this is like that'); |
353 | |
354 | (Mnemonic "This is like that".) |
355 | |
356 | The second argument is a regular expression. It may be given as a |
4bd4e70a |
357 | regex reference (i.e. C<qr//>) or (for better compatibility with older |
3f2ec160 |
358 | perls) as a string that looks like a regex (alternative delimiters are |
359 | currently not supported): |
360 | |
361 | like( $this, '/that/', 'this is like that' ); |
362 | |
363 | Regex options may be placed on the end (C<'/that/i'>). |
364 | |
365 | Its advantages over ok() are similar to that of is() and isnt(). Better |
366 | diagnostics on failure. |
367 | |
368 | =cut |
369 | |
370 | sub like ($$;$) { |
33459055 |
371 | $Test->like(@_); |
3f2ec160 |
372 | } |
373 | |
a9153838 |
374 | |
375 | =item B<unlike> |
376 | |
377 | unlike( $this, qr/that/, $test_name ); |
378 | |
379 | Works exactly as like(), only it checks if $this B<does not> match the |
380 | given pattern. |
381 | |
382 | =cut |
383 | |
384 | sub unlike { |
385 | $Test->unlike(@_); |
386 | } |
387 | |
388 | |
389 | =item B<cmp_ok> |
390 | |
391 | cmp_ok( $this, $op, $that, $test_name ); |
392 | |
393 | Halfway between ok() and is() lies cmp_ok(). This allows you to |
394 | compare two arguments using any binary perl operator. |
395 | |
396 | # ok( $this eq $that ); |
397 | cmp_ok( $this, 'eq', $that, 'this eq that' ); |
398 | |
399 | # ok( $this == $that ); |
400 | cmp_ok( $this, '==', $that, 'this == that' ); |
401 | |
402 | # ok( $this && $that ); |
403 | cmp_ok( $this, '&&', $that, 'this || that' ); |
404 | ...etc... |
405 | |
406 | Its advantage over ok() is when the test fails you'll know what $this |
407 | and $that were: |
408 | |
409 | not ok 1 |
410 | # Failed test (foo.t at line 12) |
411 | # '23' |
412 | # && |
413 | # undef |
414 | |
6686786d |
415 | It's also useful in those cases where you are comparing numbers and |
a9153838 |
416 | is()'s use of C<eq> will interfere: |
417 | |
418 | cmp_ok( $big_hairy_number, '==', $another_big_hairy_number ); |
419 | |
420 | =cut |
421 | |
422 | sub cmp_ok($$$;$) { |
423 | $Test->cmp_ok(@_); |
424 | } |
425 | |
426 | |
d020a79a |
427 | =item B<can_ok> |
428 | |
429 | can_ok($module, @methods); |
430 | can_ok($object, @methods); |
431 | |
432 | Checks to make sure the $module or $object can do these @methods |
433 | (works with functions, too). |
434 | |
435 | can_ok('Foo', qw(this that whatever)); |
436 | |
437 | is almost exactly like saying: |
438 | |
439 | ok( Foo->can('this') && |
440 | Foo->can('that') && |
441 | Foo->can('whatever') |
442 | ); |
443 | |
444 | only without all the typing and with a better interface. Handy for |
445 | quickly testing an interface. |
446 | |
a9153838 |
447 | No matter how many @methods you check, a single can_ok() call counts |
448 | as one test. If you desire otherwise, use: |
449 | |
450 | foreach my $meth (@methods) { |
451 | can_ok('Foo', $meth); |
452 | } |
453 | |
d020a79a |
454 | =cut |
455 | |
456 | sub can_ok ($@) { |
457 | my($proto, @methods) = @_; |
458 | my $class= ref $proto || $proto; |
459 | |
a9153838 |
460 | unless( @methods ) { |
461 | my $ok = $Test->ok( 0, "$class->can(...)" ); |
462 | $Test->diag(' can_ok() called with no methods'); |
463 | return $ok; |
464 | } |
465 | |
d020a79a |
466 | my @nok = (); |
467 | foreach my $method (@methods) { |
33459055 |
468 | my $test = "'$class'->can('$method')"; |
a9153838 |
469 | local($!, $@); # don't interfere with caller's $@ |
470 | # eval sometimes resets $! |
d020a79a |
471 | eval $test || push @nok, $method; |
472 | } |
473 | |
474 | my $name; |
6686786d |
475 | $name = @methods == 1 ? "$class->can('$methods[0]')" |
d020a79a |
476 | : "$class->can(...)"; |
477 | |
33459055 |
478 | my $ok = $Test->ok( !@nok, $name ); |
d020a79a |
479 | |
a9153838 |
480 | $Test->diag(map " $class->can('$_') failed\n", @nok); |
d020a79a |
481 | |
33459055 |
482 | return $ok; |
d020a79a |
483 | } |
484 | |
485 | =item B<isa_ok> |
486 | |
33459055 |
487 | isa_ok($object, $class, $object_name); |
a9153838 |
488 | isa_ok($ref, $type, $ref_name); |
d020a79a |
489 | |
490 | Checks to see if the given $object->isa($class). Also checks to make |
491 | sure the object was defined in the first place. Handy for this sort |
492 | of thing: |
493 | |
494 | my $obj = Some::Module->new; |
495 | isa_ok( $obj, 'Some::Module' ); |
496 | |
497 | where you'd otherwise have to write |
498 | |
499 | my $obj = Some::Module->new; |
500 | ok( defined $obj && $obj->isa('Some::Module') ); |
501 | |
502 | to safeguard against your test script blowing up. |
503 | |
a9153838 |
504 | It works on references, too: |
505 | |
506 | isa_ok( $array_ref, 'ARRAY' ); |
507 | |
33459055 |
508 | The diagnostics of this test normally just refer to 'the object'. If |
509 | you'd like them to be more specific, you can supply an $object_name |
510 | (for example 'Test customer'). |
511 | |
d020a79a |
512 | =cut |
513 | |
33459055 |
514 | sub isa_ok ($$;$) { |
515 | my($object, $class, $obj_name) = @_; |
d020a79a |
516 | |
517 | my $diag; |
33459055 |
518 | $obj_name = 'The object' unless defined $obj_name; |
519 | my $name = "$obj_name isa $class"; |
d020a79a |
520 | if( !defined $object ) { |
33459055 |
521 | $diag = "$obj_name isn't defined"; |
d020a79a |
522 | } |
523 | elsif( !ref $object ) { |
33459055 |
524 | $diag = "$obj_name isn't a reference"; |
d020a79a |
525 | } |
a9153838 |
526 | else { |
527 | # We can't use UNIVERSAL::isa because we want to honor isa() overrides |
528 | local($@, $!); # eval sometimes resets $! |
529 | my $rslt = eval { $object->isa($class) }; |
530 | if( $@ ) { |
531 | if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) { |
532 | if( !UNIVERSAL::isa($object, $class) ) { |
533 | my $ref = ref $object; |
6686786d |
534 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; |
a9153838 |
535 | } |
536 | } else { |
537 | die <<WHOA; |
538 | WHOA! I tried to call ->isa on your object and got some weird error. |
539 | This should never happen. Please contact the author immediately. |
540 | Here's the error. |
541 | $@ |
542 | WHOA |
543 | } |
544 | } |
545 | elsif( !$rslt ) { |
546 | my $ref = ref $object; |
6686786d |
547 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; |
a9153838 |
548 | } |
d020a79a |
549 | } |
a9153838 |
550 | |
551 | |
d020a79a |
552 | |
33459055 |
553 | my $ok; |
d020a79a |
554 | if( $diag ) { |
33459055 |
555 | $ok = $Test->ok( 0, $name ); |
a9153838 |
556 | $Test->diag(" $diag\n"); |
d020a79a |
557 | } |
558 | else { |
33459055 |
559 | $ok = $Test->ok( 1, $name ); |
d020a79a |
560 | } |
33459055 |
561 | |
562 | return $ok; |
d020a79a |
563 | } |
564 | |
565 | |
3f2ec160 |
566 | =item B<pass> |
567 | |
568 | =item B<fail> |
569 | |
570 | pass($test_name); |
571 | fail($test_name); |
572 | |
573 | Sometimes you just want to say that the tests have passed. Usually |
574 | the case is you've got some complicated condition that is difficult to |
575 | wedge into an ok(). In this case, you can simply use pass() (to |
576 | declare the test ok) or fail (for not ok). They are synonyms for |
577 | ok(1) and ok(0). |
578 | |
579 | Use these very, very, very sparingly. |
580 | |
581 | =cut |
582 | |
d020a79a |
583 | sub pass (;$) { |
33459055 |
584 | $Test->ok(1, @_); |
3f2ec160 |
585 | } |
586 | |
d020a79a |
587 | sub fail (;$) { |
33459055 |
588 | $Test->ok(0, @_); |
3f2ec160 |
589 | } |
590 | |
591 | =back |
592 | |
a9153838 |
593 | =head2 Diagnostics |
594 | |
595 | If you pick the right test function, you'll usually get a good idea of |
596 | what went wrong when it failed. But sometimes it doesn't work out |
597 | that way. So here we have ways for you to write your own diagnostic |
598 | messages which are safer than just C<print STDERR>. |
599 | |
600 | =over 4 |
601 | |
602 | =item B<diag> |
603 | |
604 | diag(@diagnostic_message); |
605 | |
606 | Prints a diagnostic message which is guaranteed not to interfere with |
607 | test output. Handy for this sort of thing: |
608 | |
609 | ok( grep(/foo/, @users), "There's a foo user" ) or |
610 | diag("Since there's no foo, check that /etc/bar is set up right"); |
611 | |
612 | which would produce: |
613 | |
614 | not ok 42 - There's a foo user |
615 | # Failed test (foo.t at line 52) |
616 | # Since there's no foo, check that /etc/bar is set up right. |
617 | |
618 | You might remember C<ok() or diag()> with the mnemonic C<open() or |
619 | die()>. |
620 | |
621 | B<NOTE> The exact formatting of the diagnostic output is still |
622 | changing, but it is guaranteed that whatever you throw at it it won't |
623 | interfere with the test. |
624 | |
625 | =cut |
626 | |
627 | sub diag { |
628 | $Test->diag(@_); |
629 | } |
630 | |
631 | |
632 | =back |
633 | |
3f2ec160 |
634 | =head2 Module tests |
635 | |
636 | You usually want to test if the module you're testing loads ok, rather |
637 | than just vomiting if its load fails. For such purposes we have |
638 | C<use_ok> and C<require_ok>. |
639 | |
640 | =over 4 |
641 | |
642 | =item B<use_ok> |
643 | |
3f2ec160 |
644 | BEGIN { use_ok($module); } |
d020a79a |
645 | BEGIN { use_ok($module, @imports); } |
646 | |
647 | These simply use the given $module and test to make sure the load |
648 | happened ok. Its recommended that you run use_ok() inside a BEGIN |
649 | block so its functions are exported at compile-time and prototypes are |
650 | properly honored. |
651 | |
652 | If @imports are given, they are passed through to the use. So this: |
653 | |
654 | BEGIN { use_ok('Some::Module', qw(foo bar)) } |
655 | |
656 | is like doing this: |
657 | |
658 | use Some::Module qw(foo bar); |
3f2ec160 |
659 | |
3f2ec160 |
660 | |
661 | =cut |
662 | |
d020a79a |
663 | sub use_ok ($;@) { |
664 | my($module, @imports) = @_; |
665 | @imports = () unless @imports; |
3f2ec160 |
666 | |
667 | my $pack = caller; |
668 | |
a9153838 |
669 | local($@,$!); # eval sometimes interferes with $! |
3f2ec160 |
670 | eval <<USE; |
671 | package $pack; |
672 | require $module; |
d020a79a |
673 | $module->import(\@imports); |
3f2ec160 |
674 | USE |
675 | |
33459055 |
676 | my $ok = $Test->ok( !$@, "use $module;" ); |
3f2ec160 |
677 | |
678 | unless( $ok ) { |
0cd946aa |
679 | chomp $@; |
33459055 |
680 | $Test->diag(<<DIAGNOSTIC); |
a9153838 |
681 | Tried to use '$module'. |
682 | Error: $@ |
3f2ec160 |
683 | DIAGNOSTIC |
684 | |
685 | } |
686 | |
687 | return $ok; |
688 | } |
689 | |
d020a79a |
690 | =item B<require_ok> |
691 | |
692 | require_ok($module); |
693 | |
694 | Like use_ok(), except it requires the $module. |
695 | |
696 | =cut |
3f2ec160 |
697 | |
698 | sub require_ok ($) { |
699 | my($module) = shift; |
700 | |
701 | my $pack = caller; |
702 | |
a9153838 |
703 | local($!, $@); # eval sometimes interferes with $! |
3f2ec160 |
704 | eval <<REQUIRE; |
705 | package $pack; |
706 | require $module; |
707 | REQUIRE |
708 | |
33459055 |
709 | my $ok = $Test->ok( !$@, "require $module;" ); |
3f2ec160 |
710 | |
711 | unless( $ok ) { |
0cd946aa |
712 | chomp $@; |
33459055 |
713 | $Test->diag(<<DIAGNOSTIC); |
a9153838 |
714 | Tried to require '$module'. |
715 | Error: $@ |
3f2ec160 |
716 | DIAGNOSTIC |
717 | |
718 | } |
719 | |
720 | return $ok; |
721 | } |
722 | |
d020a79a |
723 | =back |
3f2ec160 |
724 | |
725 | =head2 Conditional tests |
726 | |
727 | Sometimes running a test under certain conditions will cause the |
728 | test script to die. A certain function or method isn't implemented |
729 | (such as fork() on MacOS), some resource isn't available (like a |
d020a79a |
730 | net connection) or a module isn't available. In these cases it's |
731 | necessary to skip tests, or declare that they are supposed to fail |
3f2ec160 |
732 | but will work in the future (a todo test). |
733 | |
a9153838 |
734 | For more details on the mechanics of skip and todo tests see |
735 | L<Test::Harness>. |
d020a79a |
736 | |
737 | The way Test::More handles this is with a named block. Basically, a |
738 | block of tests which can be skipped over or made todo. It's best if I |
739 | just show you... |
3f2ec160 |
740 | |
741 | =over 4 |
742 | |
d020a79a |
743 | =item B<SKIP: BLOCK> |
744 | |
745 | SKIP: { |
746 | skip $why, $how_many if $condition; |
3f2ec160 |
747 | |
d020a79a |
748 | ...normal testing code goes here... |
749 | } |
3f2ec160 |
750 | |
d020a79a |
751 | This declares a block of tests to skip, $how_many tests there are, |
752 | $why and under what $condition to skip them. An example is the |
753 | easiest way to illustrate: |
3f2ec160 |
754 | |
d020a79a |
755 | SKIP: { |
756 | skip "Pigs don't fly here", 2 unless Pigs->can('fly'); |
3f2ec160 |
757 | |
d020a79a |
758 | my $pig = Pigs->new; |
759 | $pig->takeoff; |
760 | |
761 | ok( $pig->altitude > 0, 'Pig is airborne' ); |
762 | ok( $pig->airspeed > 0, ' and moving' ); |
763 | } |
3f2ec160 |
764 | |
d020a79a |
765 | If pigs cannot fly, the whole block of tests will be skipped |
766 | completely. Test::More will output special ok's which Test::Harness |
767 | interprets as skipped tests. Its important to include $how_many tests |
768 | are in the block so the total number of tests comes out right (unless |
33459055 |
769 | you're using C<no_plan>, in which case you can leave $how_many off if |
770 | you like). |
d020a79a |
771 | |
a9153838 |
772 | Its perfectly safe to nest SKIP blocks. |
773 | |
774 | Tests are skipped when you B<never> expect them to B<ever> pass. Like |
775 | an optional module is not installed or the operating system doesn't |
776 | have some feature (like fork() or symlinks) or maybe you need an |
777 | Internet connection and one isn't available. |
778 | |
779 | You don't skip tests which are failing because there's a bug in your |
780 | program. For that you use TODO. Read on. |
781 | |
d020a79a |
782 | |
783 | =for _Future |
784 | See L</Why are skip and todo so weird?> |
3f2ec160 |
785 | |
786 | =cut |
787 | |
d020a79a |
788 | #'# |
1af51bd3 |
789 | sub skip { |
d020a79a |
790 | my($why, $how_many) = @_; |
33459055 |
791 | |
792 | unless( defined $how_many ) { |
d020a79a |
793 | # $how_many can only be avoided when no_plan is in use. |
33459055 |
794 | _carp "skip() needs to know \$how_many tests are in the block" |
795 | unless $Test::Builder::No_Plan; |
d020a79a |
796 | $how_many = 1; |
797 | } |
798 | |
799 | for( 1..$how_many ) { |
33459055 |
800 | $Test->skip($why); |
d020a79a |
801 | } |
802 | |
803 | local $^W = 0; |
804 | last SKIP; |
3f2ec160 |
805 | } |
806 | |
3f2ec160 |
807 | |
d020a79a |
808 | =item B<TODO: BLOCK> |
3f2ec160 |
809 | |
d020a79a |
810 | TODO: { |
a9153838 |
811 | local $TODO = $why if $condition; |
3f2ec160 |
812 | |
d020a79a |
813 | ...normal testing code goes here... |
814 | } |
3f2ec160 |
815 | |
d020a79a |
816 | Declares a block of tests you expect to fail and $why. Perhaps it's |
817 | because you haven't fixed a bug or haven't finished a new feature: |
3f2ec160 |
818 | |
d020a79a |
819 | TODO: { |
820 | local $TODO = "URI::Geller not finished"; |
3f2ec160 |
821 | |
d020a79a |
822 | my $card = "Eight of clubs"; |
823 | is( URI::Geller->your_card, $card, 'Is THIS your card?' ); |
3f2ec160 |
824 | |
d020a79a |
825 | my $spoon; |
826 | URI::Geller->bend_spoon; |
827 | is( $spoon, 'bent', "Spoon bending, that's original" ); |
828 | } |
829 | |
830 | With a todo block, the tests inside are expected to fail. Test::More |
831 | will run the tests normally, but print out special flags indicating |
832 | they are "todo". Test::Harness will interpret failures as being ok. |
833 | Should anything succeed, it will report it as an unexpected success. |
834 | |
835 | The nice part about todo tests, as opposed to simply commenting out a |
4bd4e70a |
836 | block of tests, is it's like having a programmatic todo list. You know |
d020a79a |
837 | how much work is left to be done, you're aware of what bugs there are, |
838 | and you'll know immediately when they're fixed. |
839 | |
840 | Once a todo test starts succeeding, simply move it outside the block. |
841 | When the block is empty, delete it. |
842 | |
843 | |
a9153838 |
844 | =item B<todo_skip> |
845 | |
846 | TODO: { |
847 | todo_skip $why, $how_many if $condition; |
848 | |
849 | ...normal testing code... |
850 | } |
851 | |
852 | With todo tests, its best to have the tests actually run. That way |
853 | you'll know when they start passing. Sometimes this isn't possible. |
854 | Often a failing test will cause the whole program to die or hang, even |
855 | inside an C<eval BLOCK> with and using C<alarm>. In these extreme |
856 | cases you have no choice but to skip over the broken tests entirely. |
857 | |
858 | The syntax and behavior is similar to a C<SKIP: BLOCK> except the |
859 | tests will be marked as failing but todo. Test::Harness will |
860 | interpret them as passing. |
861 | |
862 | =cut |
863 | |
864 | sub todo_skip { |
865 | my($why, $how_many) = @_; |
866 | |
867 | unless( defined $how_many ) { |
868 | # $how_many can only be avoided when no_plan is in use. |
869 | _carp "todo_skip() needs to know \$how_many tests are in the block" |
870 | unless $Test::Builder::No_Plan; |
871 | $how_many = 1; |
872 | } |
873 | |
874 | for( 1..$how_many ) { |
875 | $Test->todo_skip($why); |
876 | } |
877 | |
878 | local $^W = 0; |
879 | last TODO; |
880 | } |
881 | |
882 | |
d020a79a |
883 | =back |
3f2ec160 |
884 | |
4bd4e70a |
885 | =head2 Comparison functions |
3f2ec160 |
886 | |
887 | Not everything is a simple eq check or regex. There are times you |
888 | need to see if two arrays are equivalent, for instance. For these |
889 | instances, Test::More provides a handful of useful functions. |
890 | |
891 | B<NOTE> These are NOT well-tested on circular references. Nor am I |
892 | quite sure what will happen with filehandles. |
893 | |
894 | =over 4 |
895 | |
33459055 |
896 | =item B<is_deeply> |
897 | |
898 | is_deeply( $this, $that, $test_name ); |
899 | |
900 | Similar to is(), except that if $this and $that are hash or array |
901 | references, it does a deep comparison walking each data structure to |
902 | see if they are equivalent. If the two structures are different, it |
903 | will display the place where they start differing. |
904 | |
a9153838 |
905 | Barrie Slaymaker's Test::Differences module provides more in-depth |
906 | functionality along these lines, and it plays well with Test::More. |
907 | |
33459055 |
908 | B<NOTE> Display of scalar refs is not quite 100% |
909 | |
910 | =cut |
911 | |
912 | use vars qw(@Data_Stack); |
913 | my $DNE = bless [], 'Does::Not::Exist'; |
914 | sub is_deeply { |
915 | my($this, $that, $name) = @_; |
916 | |
917 | my $ok; |
918 | if( !ref $this || !ref $that ) { |
919 | $ok = $Test->is_eq($this, $that, $name); |
920 | } |
921 | else { |
922 | local @Data_Stack = (); |
923 | if( _deep_check($this, $that) ) { |
924 | $ok = $Test->ok(1, $name); |
925 | } |
926 | else { |
927 | $ok = $Test->ok(0, $name); |
928 | $ok = $Test->diag(_format_stack(@Data_Stack)); |
929 | } |
930 | } |
931 | |
932 | return $ok; |
933 | } |
934 | |
935 | sub _format_stack { |
936 | my(@Stack) = @_; |
937 | |
938 | my $var = '$FOO'; |
939 | my $did_arrow = 0; |
940 | foreach my $entry (@Stack) { |
941 | my $type = $entry->{type} || ''; |
942 | my $idx = $entry->{'idx'}; |
943 | if( $type eq 'HASH' ) { |
944 | $var .= "->" unless $did_arrow++; |
945 | $var .= "{$idx}"; |
946 | } |
947 | elsif( $type eq 'ARRAY' ) { |
948 | $var .= "->" unless $did_arrow++; |
949 | $var .= "[$idx]"; |
950 | } |
951 | elsif( $type eq 'REF' ) { |
952 | $var = "\${$var}"; |
953 | } |
954 | } |
955 | |
956 | my @vals = @{$Stack[-1]{vals}}[0,1]; |
957 | my @vars = (); |
958 | ($vars[0] = $var) =~ s/\$FOO/ \$got/; |
959 | ($vars[1] = $var) =~ s/\$FOO/\$expected/; |
960 | |
961 | my $out = "Structures begin differing at:\n"; |
962 | foreach my $idx (0..$#vals) { |
963 | my $val = $vals[$idx]; |
964 | $vals[$idx] = !defined $val ? 'undef' : |
965 | $val eq $DNE ? "Does not exist" |
966 | : "'$val'"; |
967 | } |
968 | |
969 | $out .= "$vars[0] = $vals[0]\n"; |
970 | $out .= "$vars[1] = $vals[1]\n"; |
971 | |
a9153838 |
972 | $out =~ s/^/ /msg; |
33459055 |
973 | return $out; |
974 | } |
975 | |
976 | |
3f2ec160 |
977 | =item B<eq_array> |
978 | |
979 | eq_array(\@this, \@that); |
980 | |
981 | Checks if two arrays are equivalent. This is a deep check, so |
982 | multi-level structures are handled correctly. |
983 | |
984 | =cut |
985 | |
986 | #'# |
987 | sub eq_array { |
988 | my($a1, $a2) = @_; |
3f2ec160 |
989 | return 1 if $a1 eq $a2; |
990 | |
991 | my $ok = 1; |
33459055 |
992 | my $max = $#$a1 > $#$a2 ? $#$a1 : $#$a2; |
993 | for (0..$max) { |
994 | my $e1 = $_ > $#$a1 ? $DNE : $a1->[$_]; |
995 | my $e2 = $_ > $#$a2 ? $DNE : $a2->[$_]; |
996 | |
997 | push @Data_Stack, { type => 'ARRAY', idx => $_, vals => [$e1, $e2] }; |
3f2ec160 |
998 | $ok = _deep_check($e1,$e2); |
33459055 |
999 | pop @Data_Stack if $ok; |
1000 | |
3f2ec160 |
1001 | last unless $ok; |
1002 | } |
1003 | return $ok; |
1004 | } |
1005 | |
1006 | sub _deep_check { |
1007 | my($e1, $e2) = @_; |
1008 | my $ok = 0; |
1009 | |
d020a79a |
1010 | my $eq; |
1011 | { |
4bd4e70a |
1012 | # Quiet uninitialized value warnings when comparing undefs. |
d020a79a |
1013 | local $^W = 0; |
1014 | |
1015 | if( $e1 eq $e2 ) { |
1016 | $ok = 1; |
3f2ec160 |
1017 | } |
1018 | else { |
d020a79a |
1019 | if( UNIVERSAL::isa($e1, 'ARRAY') and |
1020 | UNIVERSAL::isa($e2, 'ARRAY') ) |
1021 | { |
1022 | $ok = eq_array($e1, $e2); |
1023 | } |
1024 | elsif( UNIVERSAL::isa($e1, 'HASH') and |
1025 | UNIVERSAL::isa($e2, 'HASH') ) |
1026 | { |
1027 | $ok = eq_hash($e1, $e2); |
1028 | } |
33459055 |
1029 | elsif( UNIVERSAL::isa($e1, 'REF') and |
1030 | UNIVERSAL::isa($e2, 'REF') ) |
1031 | { |
1032 | push @Data_Stack, { type => 'REF', vals => [$e1, $e2] }; |
1033 | $ok = _deep_check($$e1, $$e2); |
1034 | pop @Data_Stack if $ok; |
1035 | } |
1036 | elsif( UNIVERSAL::isa($e1, 'SCALAR') and |
1037 | UNIVERSAL::isa($e2, 'SCALAR') ) |
1038 | { |
1039 | push @Data_Stack, { type => 'REF', vals => [$e1, $e2] }; |
1040 | $ok = _deep_check($$e1, $$e2); |
1041 | } |
d020a79a |
1042 | else { |
33459055 |
1043 | push @Data_Stack, { vals => [$e1, $e2] }; |
d020a79a |
1044 | $ok = 0; |
1045 | } |
3f2ec160 |
1046 | } |
1047 | } |
d020a79a |
1048 | |
3f2ec160 |
1049 | return $ok; |
1050 | } |
1051 | |
1052 | |
1053 | =item B<eq_hash> |
1054 | |
1055 | eq_hash(\%this, \%that); |
1056 | |
1057 | Determines if the two hashes contain the same keys and values. This |
1058 | is a deep check. |
1059 | |
1060 | =cut |
1061 | |
1062 | sub eq_hash { |
1063 | my($a1, $a2) = @_; |
3f2ec160 |
1064 | return 1 if $a1 eq $a2; |
1065 | |
1066 | my $ok = 1; |
33459055 |
1067 | my $bigger = keys %$a1 > keys %$a2 ? $a1 : $a2; |
1068 | foreach my $k (keys %$bigger) { |
1069 | my $e1 = exists $a1->{$k} ? $a1->{$k} : $DNE; |
1070 | my $e2 = exists $a2->{$k} ? $a2->{$k} : $DNE; |
1071 | |
1072 | push @Data_Stack, { type => 'HASH', idx => $k, vals => [$e1, $e2] }; |
3f2ec160 |
1073 | $ok = _deep_check($e1, $e2); |
33459055 |
1074 | pop @Data_Stack if $ok; |
1075 | |
3f2ec160 |
1076 | last unless $ok; |
1077 | } |
1078 | |
1079 | return $ok; |
1080 | } |
1081 | |
1082 | =item B<eq_set> |
1083 | |
1084 | eq_set(\@this, \@that); |
1085 | |
1086 | Similar to eq_array(), except the order of the elements is B<not> |
1087 | important. This is a deep check, but the irrelevancy of order only |
1088 | applies to the top level. |
1089 | |
1090 | =cut |
1091 | |
1092 | # We must make sure that references are treated neutrally. It really |
1093 | # doesn't matter how we sort them, as long as both arrays are sorted |
1094 | # with the same algorithm. |
d020a79a |
1095 | sub _bogus_sort { local $^W = 0; ref $a ? 0 : $a cmp $b } |
3f2ec160 |
1096 | |
1097 | sub eq_set { |
1098 | my($a1, $a2) = @_; |
1099 | return 0 unless @$a1 == @$a2; |
1100 | |
1101 | # There's faster ways to do this, but this is easiest. |
1102 | return eq_array( [sort _bogus_sort @$a1], [sort _bogus_sort @$a2] ); |
1103 | } |
1104 | |
3f2ec160 |
1105 | =back |
1106 | |
d020a79a |
1107 | |
a9153838 |
1108 | =head2 Extending and Embedding Test::More |
d020a79a |
1109 | |
a9153838 |
1110 | Sometimes the Test::More interface isn't quite enough. Fortunately, |
1111 | Test::More is built on top of Test::Builder which provides a single, |
1112 | unified backend for any test library to use. This means two test |
1113 | libraries which both use Test::Builder B<can be used together in the |
1114 | same program>. |
1115 | |
1116 | If you simply want to do a little tweaking of how the tests behave, |
1117 | you can access the underlying Test::Builder object like so: |
3f2ec160 |
1118 | |
d020a79a |
1119 | =over 4 |
1120 | |
a9153838 |
1121 | =item B<builder> |
d020a79a |
1122 | |
a9153838 |
1123 | my $test_builder = Test::More->builder; |
d020a79a |
1124 | |
a9153838 |
1125 | Returns the Test::Builder object underlying Test::More for you to play |
1126 | with. |
1127 | |
1128 | =cut |
d020a79a |
1129 | |
a9153838 |
1130 | sub builder { |
1131 | return Test::Builder->new; |
1132 | } |
d020a79a |
1133 | |
a9153838 |
1134 | =back |
3f2ec160 |
1135 | |
d020a79a |
1136 | |
a9153838 |
1137 | =head1 NOTES |
1138 | |
1139 | Test::More is B<explicitly> tested all the way back to perl 5.004. |
d020a79a |
1140 | |
a9153838 |
1141 | =head1 BUGS and CAVEATS |
1142 | |
1143 | =over 4 |
1144 | |
1145 | =item Making your own ok() |
1146 | |
1147 | If you are trying to extend Test::More, don't. Use Test::Builder |
1148 | instead. |
1149 | |
1150 | =item The eq_* family has some caveats. |
d020a79a |
1151 | |
1152 | =item Test::Harness upgrades |
3f2ec160 |
1153 | |
d020a79a |
1154 | no_plan and todo depend on new Test::Harness features and fixes. If |
a9153838 |
1155 | you're going to distribute tests that use no_plan or todo your |
1156 | end-users will have to upgrade Test::Harness to the latest one on |
1157 | CPAN. If you avoid no_plan and TODO tests, the stock Test::Harness |
1158 | will work fine. |
d020a79a |
1159 | |
1160 | If you simply depend on Test::More, it's own dependencies will cause a |
1161 | Test::Harness upgrade. |
1162 | |
1163 | =back |
3f2ec160 |
1164 | |
3f2ec160 |
1165 | |
1166 | =head1 HISTORY |
1167 | |
1168 | This is a case of convergent evolution with Joshua Pritikin's Test |
4bd4e70a |
1169 | module. I was largely unaware of its existence when I'd first |
3f2ec160 |
1170 | written my own ok() routines. This module exists because I can't |
1171 | figure out how to easily wedge test names into Test's interface (along |
1172 | with a few other problems). |
1173 | |
1174 | The goal here is to have a testing utility that's simple to learn, |
1175 | quick to use and difficult to trip yourself up with while still |
1176 | providing more flexibility than the existing Test.pm. As such, the |
1177 | names of the most common routines are kept tiny, special cases and |
1178 | magic side-effects are kept to a minimum. WYSIWYG. |
1179 | |
1180 | |
1181 | =head1 SEE ALSO |
1182 | |
1183 | L<Test::Simple> if all this confuses you and you just want to write |
1184 | some tests. You can upgrade to Test::More later (its forward |
1185 | compatible). |
1186 | |
a9153838 |
1187 | L<Test::Differences> for more ways to test complex data structures. |
1188 | And it plays well with Test::More. |
1189 | |
1190 | L<Test> is the old testing module. Its main benefit is that it has |
1191 | been distributed with Perl since 5.004_05. |
3f2ec160 |
1192 | |
1193 | L<Test::Harness> for details on how your test results are interpreted |
1194 | by Perl. |
1195 | |
1196 | L<Test::Unit> describes a very featureful unit testing interface. |
1197 | |
4bd4e70a |
1198 | L<Test::Inline> shows the idea of embedded testing. |
3f2ec160 |
1199 | |
1200 | L<SelfTest> is another approach to embedded testing. |
1201 | |
4bd4e70a |
1202 | |
1203 | =head1 AUTHORS |
1204 | |
a9153838 |
1205 | Michael G Schwern E<lt>schwern@pobox.comE<gt> with much inspiration |
1206 | from Joshua Pritikin's Test module and lots of help from Barrie |
1207 | Slaymaker, Tony Bowden, chromatic and the perl-qa gang. |
4bd4e70a |
1208 | |
1209 | |
1210 | =head1 COPYRIGHT |
1211 | |
1212 | Copyright 2001 by Michael G Schwern E<lt>schwern@pobox.comE<gt>. |
1213 | |
1214 | This program is free software; you can redistribute it and/or |
1215 | modify it under the same terms as Perl itself. |
1216 | |
a9153838 |
1217 | See F<http://www.perl.com/perl/misc/Artistic.html> |
4bd4e70a |
1218 | |
3f2ec160 |
1219 | =cut |
1220 | |
1221 | 1; |