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