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