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