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