diff options
author | Jarkko Hietaniemi <jhi@iki.fi> | 2001-06-18 04:17:15 +0000 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-06-18 04:17:15 +0000 |
commit | b695f709e8a342e35e482b0437eb6cdacdc58b6b (patch) | |
tree | 2d16192636e6ba806ff7a907f682c74f7705a920 /lib/Test | |
parent | d780cd7a0195e946e636d3ee546f6ef4f21d6acc (diff) | |
download | perl-b695f709e8a342e35e482b0437eb6cdacdc58b6b.tar.gz |
The Grand Trek: move the *.t files from t/ to lib/ and ext/.
No doubt I made some mistakes like missed some files or
misnamed some files. The naming rules were more or less:
(1) if the module is from CPAN, follows its ways, be it
t/*.t or test.pl.
(2) otherwise if there are multiple tests for a module
put them in a t/
(3) otherwise if there's only one test put it in Module.t
(4) helper files go to module/ (locale, strict, warnings)
(5) use longer filenames now that we can (but e.g. the
compat-0.6.t and the Text::Balanced test files still
were renamed to be more civil against the 8.3 people)
installperl was updated appropriately not to install the
*.t files or the help files from under lib.
TODO: some helper files still remain under t/ that could
follow their 'masters'. UPDATE: On second thoughts, why
should they. They can continue to live under t/lib, and
in fact the locale/strict/warnings helpers that were moved
could be moved back. This way the amount of non-installable
stuff under lib/ stays smaller.
p4raw-id: //depot/perl@10676
Diffstat (limited to 'lib/Test')
-rw-r--r-- | lib/Test/Harness.pm | 2 | ||||
-rw-r--r-- | lib/Test/Harness.t | 205 | ||||
-rw-r--r-- | lib/Test/t/fail.t | 93 | ||||
-rw-r--r-- | lib/Test/t/mix.t | 17 | ||||
-rw-r--r-- | lib/Test/t/onfail.t | 31 | ||||
-rw-r--r-- | lib/Test/t/qr.t | 13 | ||||
-rw-r--r-- | lib/Test/t/skip.t | 40 | ||||
-rw-r--r-- | lib/Test/t/success.t | 11 | ||||
-rw-r--r-- | lib/Test/t/todo.t | 13 |
9 files changed, 424 insertions, 1 deletions
diff --git a/lib/Test/Harness.pm b/lib/Test/Harness.pm index 18ee902439..e0c4dbe3f7 100644 --- a/lib/Test/Harness.pm +++ b/lib/Test/Harness.pm @@ -554,7 +554,7 @@ on TTY. The width is the width of the "yada/blah..." string. sub _mk_leader { my ($te, $width) = @_; - chop($te); # XXX chomp? + $te =~ s/\.\w+$/./; if ($^O eq 'VMS') { $te =~ s/^.*\.t\./\[.t./s; } my $blank = (' ' x 77); diff --git a/lib/Test/Harness.t b/lib/Test/Harness.t new file mode 100644 index 0000000000..a4c423ddd3 --- /dev/null +++ b/lib/Test/Harness.t @@ -0,0 +1,205 @@ +#!perl + +BEGIN { + chdir 't' if -d 't'; + @INC = '../lib'; +} + +use strict; + +# For shutting up Test::Harness. +package My::Dev::Null; +use Tie::Handle; +@My::Dev::Null::ISA = qw(Tie::StdHandle); + +sub WRITE { } + + +package main; + +# Utility testing functions. +my $test_num = 1; +sub ok ($;$) { + my($test, $name) = @_; + my $okstring = ''; + $okstring = "not " unless $test; + $okstring .= "ok $test_num"; + $okstring .= " - $name" if defined $name; + print "$okstring\n"; + $test_num++; +} + +sub eqhash { + my($a1, $a2) = @_; + return 0 unless keys %$a1 == keys %$a2; + + my $ok = 1; + foreach my $k (keys %$a1) { + $ok = $a1->{$k} eq $a2->{$k}; + last unless $ok; + } + + return $ok; +} + +use vars qw($Total_tests %samples); + +my $loaded; +BEGIN { $| = 1; $^W = 1; } +END {print "not ok $test_num\n" unless $loaded;} +print "1..$Total_tests\n"; +use Test::Harness; +$loaded = 1; +ok(1, 'compile'); +######################### End of black magic. + +BEGIN { + %samples = ( + simple => { + bonus => 0, + max => 5, + 'ok' => 5, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + simple_fail => { + bonus => 0, + max => 5, + 'ok' => 3, + files => 1, + bad => 1, + good => 0, + tests => 1, + sub_skipped => 0, + skipped => 0, + }, + descriptive => { + bonus => 0, + max => 5, + 'ok' => 5, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + no_nums => { + bonus => 0, + max => 5, + 'ok' => 4, + files => 1, + bad => 1, + good => 0, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + todo => { + bonus => 1, + max => 5, + 'ok' => 5, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + skip => { + bonus => 0, + max => 5, + 'ok' => 5, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 1, + skipped => 0, + }, + bailout => 0, + combined => { + bonus => 1, + max => 10, + 'ok' => 8, + files => 1, + bad => 1, + good => 0, + tests => 1, + sub_skipped=> 1, + skipped => 0 + }, + duplicates => { + bonus => 0, + max => 10, + 'ok' => 11, + files => 1, + bad => 1, + good => 0, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + header_at_end => { + bonus => 0, + max => 4, + 'ok' => 4, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + skip_all => { + bonus => 0, + max => 0, + 'ok' => 0, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 1, + }, + with_comments => { + bonus => 2, + max => 5, + 'ok' => 5, + files => 1, + bad => 0, + good => 1, + tests => 1, + sub_skipped=> 0, + skipped => 0, + }, + ); + + $Total_tests = keys(%samples) + 1; +} + +tie *NULL, 'My::Dev::Null' or die $!; + +while (my($test, $expect) = each %samples) { + # _run_all_tests() runs the tests but skips the formatting. + my($totals, $failed); + eval { + select NULL; # _run_all_tests() isn't as quiet as it should be. + ($totals, $failed) = + Test::Harness::_run_all_tests("lib/sample-tests/$test"); + }; + select STDOUT; + + unless( $@ ) { + ok( eqhash( $expect, {map { $_=>$totals->{$_} } keys %$expect} ), + $test ); + } + else { # special case for bailout + ok( ($test eq 'bailout' and $@ =~ /Further testing stopped: GERONI/i), + $test ); + } +} diff --git a/lib/Test/t/fail.t b/lib/Test/t/fail.t new file mode 100644 index 0000000000..b431502b8a --- /dev/null +++ b/lib/Test/t/fail.t @@ -0,0 +1,93 @@ +# -*-perl-*- +use strict; +use vars qw($Expect); +use Test qw($TESTOUT $ntest ok skip plan); +plan tests => 14; + +open F, ">fails"; +$TESTOUT = *F{IO}; + +my $r=0; +{ + # Shut up deprecated usage warning. + local $^W = 0; + $r |= skip(0,0); +} +$r |= ok(0); +$r |= ok(0,1); +$r |= ok(sub { 1+1 }, 3); +$r |= ok(sub { 1+1 }, sub { 2 * 0}); + +my @list = (0,0); +$r |= ok @list, 1, "\@list=".join(',',@list); +$r |= ok @list, 1, sub { "\@list=".join ',',@list }; +$r |= ok 'segmentation fault', '/bongo/'; + +for (1..2) { $r |= ok(0); } + +$r |= ok(1, undef); +$r |= ok(undef, 1); + +ok($r); # (failure==success :-) + +close F; +$TESTOUT = *STDOUT{IO}; +$ntest = 1; + +open F, "fails"; +my $O; +while (<F>) { $O .= $_; } +close F; +unlink "fails"; + +ok join(' ', map { m/(\d+)/; $1 } grep /^not ok/, split /\n+/, $O), + join(' ', 1..13); + +my @got = split /not ok \d+\n/, $O; +shift @got; + +$Expect =~ s/\n+$//; +my @expect = split /\n\n/, $Expect; + +for (my $x=0; $x < @got; $x++) { + ok $got[$x], $expect[$x]."\n"; +} + + +BEGIN { + $Expect = <<"EXPECT"; +# Failed test 1 in $0 at line 14 + +# Failed test 2 in $0 at line 16 + +# Test 3 got: '0' ($0 at line 17) +# Expected: '1' + +# Test 4 got: '2' ($0 at line 18) +# Expected: '3' + +# Test 5 got: '2' ($0 at line 19) +# Expected: '0' + +# Test 6 got: '2' ($0 at line 22) +# Expected: '1' (\@list=0,0) + +# Test 7 got: '2' ($0 at line 23) +# Expected: '1' (\@list=0,0) + +# Test 8 got: 'segmentation fault' ($0 at line 24) +# Expected: qr{bongo} + +# Failed test 9 in $0 at line 26 + +# Failed test 10 in $0 at line 26 fail #2 + +# Failed test 11 in $0 at line 28 + +# Test 12 got: <UNDEF> ($0 at line 29) +# Expected: '1' + +# Failed test 13 in $0 at line 31 +EXPECT + +} diff --git a/lib/Test/t/mix.t b/lib/Test/t/mix.t new file mode 100644 index 0000000000..d911689845 --- /dev/null +++ b/lib/Test/t/mix.t @@ -0,0 +1,17 @@ +# -*-perl-*- +use strict; +use Test; +BEGIN { plan tests => 4, todo => [2,3] } + +ok(sub { + my $r = 0; + for (my $x=0; $x < 10; $x++) { + $r += $x*($r+1); + } + $r + }, 3628799); + +ok(0); +ok(1); + +skip(1,0); diff --git a/lib/Test/t/onfail.t b/lib/Test/t/onfail.t new file mode 100644 index 0000000000..dce4373401 --- /dev/null +++ b/lib/Test/t/onfail.t @@ -0,0 +1,31 @@ +# -*-perl-*- + +use strict; +use Test qw($ntest plan ok $TESTOUT); +use vars qw($mycnt); + +BEGIN { plan test => 6, onfail => \&myfail } + +$mycnt = 0; + +my $why = "zero != one"; +# sneak in a test that Test::Harness wont see +open J, ">junk"; +$TESTOUT = *J{IO}; +ok(0, 1, $why); +$TESTOUT = *STDOUT{IO}; +close J; +unlink "junk"; +$ntest = 1; + +sub myfail { + my ($f) = @_; + ok(@$f, 1); + + my $t = $$f[0]; + ok($$t{diagnostic}, $why); + ok($$t{'package'}, 'main'); + ok($$t{repetition}, 1); + ok($$t{result}, 0); + ok($$t{expected}, 1); +} diff --git a/lib/Test/t/qr.t b/lib/Test/t/qr.t new file mode 100644 index 0000000000..ea40f87308 --- /dev/null +++ b/lib/Test/t/qr.t @@ -0,0 +1,13 @@ +#!./perl -w + +use strict; +BEGIN { + if ($] < 5.005) { + print "1..0\n"; + print "ok 1 # skipped; this test requires at least perl 5.005\n"; + exit; + } +} +use Test; plan tests => 1; + +ok 'abc', qr/b/; diff --git a/lib/Test/t/skip.t b/lib/Test/t/skip.t new file mode 100644 index 0000000000..7db35e65dc --- /dev/null +++ b/lib/Test/t/skip.t @@ -0,0 +1,40 @@ +# -*-perl-*- +use strict; +use Test qw($TESTOUT $ntest plan ok skip); plan tests => 6; + +open F, ">skips" or die "open skips: $!"; +$TESTOUT = *F{IO}; + +skip(1, 0); #should skip + +my $skipped=1; +skip('hop', sub { $skipped = 0 }); +skip(sub {'jump'}, sub { $skipped = 0 }); +skip('skipping stones is more fun', sub { $skipped = 0 }); + +close F; + +$TESTOUT = *STDOUT{IO}; +$ntest = 1; +open F, "skips" or die "open skips: $!"; + +ok $skipped, 1, 'not skipped?'; + +my @T = <F>; +chop @T; +my @expect = split /\n+/, join('',<DATA>); +ok @T, 4; +for (my $x=0; $x < @T; $x++) { + ok $T[$x], $expect[$x]; +} + +END { close F; unlink "skips" } + +__DATA__ +ok 1 # skip + +ok 2 # skip hop + +ok 3 # skip jump + +ok 4 # skip skipping stones is more fun diff --git a/lib/Test/t/success.t b/lib/Test/t/success.t new file mode 100644 index 0000000000..a580f0a567 --- /dev/null +++ b/lib/Test/t/success.t @@ -0,0 +1,11 @@ +# -*-perl-*- +use strict; +use Test; +BEGIN { plan tests => 11 } + +ok(ok(1)); +ok(ok('fixed', 'fixed')); +ok(skip(1,0)); +ok(undef, undef); +ok(ok 'the brown fox jumped over the lazy dog', '/lazy/'); +ok(ok 'the brown fox jumped over the lazy dog', 'm,fox,'); diff --git a/lib/Test/t/todo.t b/lib/Test/t/todo.t new file mode 100644 index 0000000000..ae02a04f6b --- /dev/null +++ b/lib/Test/t/todo.t @@ -0,0 +1,13 @@ +# -*-perl-*- +use strict; +use Test; +BEGIN { + my $tests = 5; + plan tests => $tests, todo => [1..$tests]; +} + +ok(0); +ok(1); +ok(0,1); +ok(0,1,"need more tuits"); +ok(1,1); |