From: Jarkko Hietaniemi Date: Mon, 25 Jun 2001 13:35:41 +0000 (+0000) Subject: Add Test::Simple from Michael G Schwern. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4dd974daccefd178b570ef862ae9d655a6d519cc;p=p5sagit%2Fp5-mst-13.2.git Add Test::Simple from Michael G Schwern. p4raw-id: //depot/perl@10913 --- diff --git a/MANIFEST b/MANIFEST index 4e970ed..72d226c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1090,6 +1090,14 @@ lib/termcap.pl Perl library supporting termcap usage lib/Test.pm A simple framework for writing test scripts lib/Test/Harness.pm A test harness lib/Test/Harness.t See if Test::Harness works +lib/Test/Simple.pm Basic utility for writing tests +lib/Test/Simple/t/exit.t Test::Simple test, exit codes +lib/Test/Simple/t/extra.t Test::Simple test +lib/Test/Simple/t/fail.t Test::Simple test, test failures +lib/Test/Simple/t/missing.t Test::Simple test, missing tests +lib/Test/Simple/t/no_plan.t Test::Simple test, forgot the plan +lib/Test/Simple/t/plan_is_noplan.t Test::Simple test, no_plan +lib/Test/Simple/t/simple.t for exit.t lib/Test/t/fail.t See if Test works lib/Test/t/mix.t See if Test works lib/Test/t/onfail.t See if Test works @@ -1844,6 +1852,17 @@ t/lib/st-dump.pl See if Storable works t/lib/strict/refs Tests of "use strict 'refs'" for strict.t t/lib/strict/subs Tests of "use strict 'subs'" for strict.t t/lib/strict/vars Tests of "use strict 'vars'" for strict.t +t/lib/Test/Simple/Catch.pm Utility module for testing Test::Simple +t/lib/Test/Simple/sample_tests/death.plx for exit.t +t/lib/Test/Simple/sample_tests/death_in_eval.plx for exit.t +t/lib/Test/Simple/sample_tests/extras.plx for exit.t +t/lib/Test/Simple/sample_tests/five_fail.plx for exit.t +t/lib/Test/Simple/sample_tests/last_minute_death.plx for exit.t +t/lib/Test/Simple/sample_tests/one_fail.plx for exit.t +t/lib/Test/Simple/sample_tests/require.plx for exit.t +t/lib/Test/Simple/sample_tests/success.plx for exit.t +t/lib/Test/Simple/sample_tests/too_few.plx for exit.t +t/lib/Test/Simple/sample_tests/two_fail.plx for exit.t t/lib/warnings/1global Tests of global warnings for warnings.t t/lib/warnings/2use Tests for "use warnings" for warnings.t t/lib/warnings/3both Tests for interaction of $^W and "use warnings" diff --git a/lib/Test/Simple.pm b/lib/Test/Simple.pm new file mode 100644 index 0000000..a66f5ce --- /dev/null +++ b/lib/Test/Simple.pm @@ -0,0 +1,444 @@ +package Test::Simple; + +require 5.004; + +$Test::Simple::VERSION = '0.08'; + +my(@Test_Results) = (); +my($Num_Tests, $Planned_Tests, $Test_Died) = (0,0,0); +my($Have_Plan) = 0; + + +# Special print function to guard against $\ and -l munging. +sub _print (*@) { + my($fh, @args) = @_; + + local $\; + print $fh @args; +} + +sub print { die "DON'T USE PRINT! Use _print instead" } + + +# I'd like to have Test::Simple interfere with the program being +# tested as little as possible. This includes using Exporter or +# anything else (including strict). +sub import { + # preserve caller() + if( @_ > 1 ) { + if( $_[1] eq 'no_plan' ) { + goto &no_plan; + } + else { + goto &plan + } + } +} + +sub plan { + my($class, %config) = @_; + + if( !exists $config{tests} ) { + die "You have to tell $class how many tests you plan to run.\n". + " use $class tests => 42; for example.\n"; + } + elsif( !defined $config{tests} ) { + die "Got an undefined number of tests. Looks like you tried to tell ". + "$class how many tests you plan to run but made a mistake.\n"; + } + elsif( !$config{tests} ) { + die "You told $class you plan to run 0 tests! You've got to run ". + "something.\n"; + } + else { + $Planned_Tests = $config{tests}; + } + + $Have_Plan = 1; + + _print *TESTOUT, "1..$Planned_Tests\n"; + + my($caller) = caller; + *{$caller.'::ok'} = \&ok; + +} + + +sub no_plan { + $Have_Plan = 1; + + my($caller) = caller; + *{$caller.'::ok'} = \&ok; +} + + + +$| = 1; +open(*TESTOUT, ">&STDOUT") or _whoa(1, "Can't dup STDOUT!"); +open(*TESTERR, ">&STDERR") or _whoa(1, "Can't dup STDERR!"); +{ + my $orig_fh = select TESTOUT; + $| = 1; + select TESTERR; + $| = 1; + select $orig_fh; +} + +=head1 NAME + +Test::Simple - Basic utilities for writing tests. + +=head1 SYNOPSIS + + use Test::Simple tests => 1; + + ok( $foo eq $bar, 'foo is bar' ); + + +=head1 DESCRIPTION + +This is an extremely simple, extremely basic module for writing tests +suitable for CPAN modules and other pursuits. + +The basic unit of Perl testing is the ok. For each thing you want to +test your program will print out an "ok" or "not ok" to indicate pass +or fail. You do this with the ok() function (see below). + +The only other constraint is you must predeclare how many tests you +plan to run. This is in case something goes horribly wrong during the +test and your test program aborts, or skips a test or whatever. You +do this like so: + + use Test::Simple tests => 23; + +You must have a plan. + + +=over 4 + +=item B + + ok( $foo eq $bar, $name ); + ok( $foo eq $bar ); + +ok() is given an expression (in this case C<$foo eq $bar>). If its +true, the test passed. If its false, it didn't. That's about it. + +ok() prints out either "ok" or "not ok" along with a test number (it +keeps track of that for you). + + # This produces "ok 1 - Hell not yet frozen over" (or not ok) + ok( get_temperature($hell) > 0, 'Hell not yet frozen over' ); + +If you provide a $name, that will be printed along with the "ok/not +ok" to make it easier to find your test when if fails (just search for +the name). It also makes it easier for the next guy to understand +what your test is for. Its highly recommended you use test names. + +All tests are run in scalar context. So this: + + ok( @stuff, 'I have some stuff' ); + +will do what you mean (fail if stuff is empty). + +=cut + +sub ok ($;$) { + my($test, $name) = @_; + + unless( $Have_Plan ) { + die "You tried to use ok() without a plan! Gotta have a plan.\n". + " use Test::Simple tests => 23; for example.\n"; + } + + $Num_Tests++; + + # Make sure the print doesn't get interfered with. + local($\, $,); + + _print *TESTERR, < + + _sanity_check(); + +Runs a bunch of end of test sanity checks to make sure reality came +through ok. If anything is wrong it will die with a fairly friendly +error message. + +=cut + +#'# +sub _sanity_check { + _whoa($Num_Tests < 0, 'Says here you ran a negative number of tests!'); + _whoa(!$Have_Plan and $Num_Tests, + 'Somehow your tests ran without a plan!'); + _whoa($Num_Tests != @Test_Results, + 'Somehow you got a different number of results than tests ran!'); +} + +=item B<_whoa> + + _whoa($check, $description); + +A sanity check, similar to assert(). If the $check is true, something +has gone horribly wrong. It will die with the given $description and +a note to contact the author. + +=cut + +sub _whoa { + my($check, $desc) = @_; + if( $check ) { + die < + + _my_exit($exit_num); + +Perl seems to have some trouble with exiting inside an END block. 5.005_03 +and 5.6.1 both seem to do odd things. Instead, this function edits $? +directly. It should ONLY be called from inside an END block. It +doesn't actually exit, that's your job. + +=cut + +sub _my_exit { + $? = $_[0]; + return 1; +} + + +=back + +=end _private + +=cut + +$SIG{__DIE__} = sub { + # We don't want to muck with death in an eval, but $^S isn't + # totally reliable. 5.005_03 and 5.6.1 both do the wrong thing + # with it. Instead, we use caller. This also means it runs under + # 5.004! + my $in_eval = 0; + for( my $stack = 1; my $sub = (caller($stack))[3]; $stack++ ) { + $in_eval = 1 if $sub =~ /^\(eval\)/; + } + $Test_Died = 1 unless $in_eval; +}; + +END { + _sanity_check(); + + # Bailout if import() was never called. This is so + # "require Test::Simple" doesn't puke. + do{ _my_exit(0) && return } if !$Have_Plan and !$Num_Tests; + + # Figure out if we passed or failed and print helpful messages. + if( $Num_Tests ) { + # The plan? We have no plan. + unless( $Planned_Tests ) { + _print *TESTOUT, "1..$Num_Tests\n"; + $Planned_Tests = $Num_Tests; + } + + my $num_failed = grep !$_, @Test_Results[0..$Planned_Tests-1]; + $num_failed += abs($Planned_Tests - @Test_Results); + + if( $Num_Tests < $Planned_Tests ) { + _print *TESTERR, <<"FAIL"; +# Looks like you planned $Planned_Tests tests but only ran $Num_Tests. +FAIL + } + elsif( $Num_Tests > $Planned_Tests ) { + my $num_extra = $Num_Tests - $Planned_Tests; + _print *TESTERR, <<"FAIL"; +# Looks like you planned $Planned_Tests tests but ran $num_extra extra. +FAIL + } + elsif ( $num_failed ) { + _print *TESTERR, <<"FAIL"; +# Looks like you failed $num_failed tests of $Planned_Tests. +FAIL + } + + if( $Test_Died ) { + _print *TESTERR, <<"FAIL"; +# Looks like your test died just after $Num_Tests. +FAIL + + _my_exit( 255 ) && return; + } + + _my_exit( $num_failed <= 254 ? $num_failed : 254 ) && return; + } + elsif ( $Test::Simple::Skip_All ) { + _my_exit( 0 ) && return; + } + else { + _print *TESTERR, "# No tests run!\n"; + _my_exit( 255 ) && return; + } +} + + +=pod + +This module is by no means trying to be a complete testing system. +Its just to get you started. Once you're off the ground its +recommended you look at L. + + +=head1 EXAMPLE + +Here's an example of a simple .t file for the fictional Film module. + + use Test::Simple tests => 5; + + use Film; # What you're testing. + + my $btaste = Film->new({ Title => 'Bad Taste', + Director => 'Peter Jackson', + Rating => 'R', + NumExplodingSheep => 1 + }); + ok( defined($btaste) and ref $btaste eq 'Film', 'new() works' ); + + ok( $btaste->Title eq 'Bad Taste', 'Title() get' ); + ok( $btsate->Director eq 'Peter Jackson', 'Director() get' ); + ok( $btaste->Rating eq 'R', 'Rating() get' ); + ok( $btaste->NumExplodingSheep == 1, 'NumExplodingSheep() get' ); + +It will produce output like this: + + 1..5 + ok 1 - new() works + ok 2 - Title() get + ok 3 - Director() get + not ok 4 - Rating() get + ok 5 - NumExplodingSheep() get + +Indicating the Film::Rating() method is broken. + + +=head1 CAVEATS + +Test::Simple will only report a maximum of 254 failures in its exit +code. If this is a problem, you probably have a huge test script. +Split it into multiple files. (Otherwise blame the Unix folks for +using an unsigned short integer as the exit status). + + +=head1 HISTORY + +This module was conceived while talking with Tony Bowden in his +kitchen one night about the problems I was having writing some really +complicated feature into the new Testing module. He observed that the +main problem is not dealing with these edge cases but that people hate +to write tests B. What was needed was a dead simple module +that took all the hard work out of testing and was really, really easy +to learn. Paul Johnson simultaneously had this idea (unfortunately, +he wasn't in Tony's kitchen). This is it. + + +=head1 AUTHOR + +Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern +, wardrobe by Calvin Klein. + + +=head1 SEE ALSO + +=over 4 + +=item L + +More testing functions! Once you outgrow Test::Simple, look at +Test::More. Test::Simple is 100% forward compatible with Test::More +(ie. you can just use Test::More instead of Test::Simple in your +programs and things will still work). + +=item L + +The original Perl testing module. + +=item L + +Elaborate unit testing. + +=item L, L + +Embed tests in your code! + +=item L + +Interprets the output of your test program. + +=back + +=cut + +1; diff --git a/lib/Test/Simple/t/exit.t b/lib/Test/Simple/t/exit.t new file mode 100644 index 0000000..369a417 --- /dev/null +++ b/lib/Test/Simple/t/exit.t @@ -0,0 +1,38 @@ +# Can't use Test.pm, that's a 5.005 thing. +package My::Test; + +my $test_num = 1; +# Utility testing functions. +sub ok ($;$) { + my($test, $name) = @_; + print "not " unless $test; + print "ok $test_num"; + print " - $name" if defined $name; + print "\n"; + $test_num++; +} + + +package main; + +my %Tests = ( + 'success.plx' => 0, + 'one_fail.plx' => 1, + 'two_fail.plx' => 2, + 'five_fail.plx' => 5, + 'extras.plx' => 3, + 'too_few.plx' => 4, + 'death.plx' => 255, + 'last_minute_death.plx' => 255, + 'death_in_eval.plx' => 0, + 'require.plx' => 0, + ); + +print "1..".keys(%Tests)."\n"; + +chdir 't' if -d 't'; +while( my($test_name, $exit_code) = each %Tests ) { + my $wait_stat = system("$^X -I../lib -Ilib/Test/Simple/ lib/Test/Simple/sample_tests/$test_name"); + My::Test::ok( $wait_stat >> 8 == $exit_code, + "$test_name exited with $exit_code" ); +} diff --git a/lib/Test/Simple/t/extra.t b/lib/Test/Simple/t/extra.t new file mode 100644 index 0000000..707162a --- /dev/null +++ b/lib/Test/Simple/t/extra.t @@ -0,0 +1,51 @@ +# Can't use Test.pm, that's a 5.005 thing. +package My::Test; + +print "1..2\n"; + +my $test_num = 1; +# Utility testing functions. +sub ok ($;$) { + my($test, $name) = @_; + print "not " unless $test; + print "ok $test_num"; + print " - $name" if defined $name; + print "\n"; + $test_num++; +} + + +package main; + +require Test::Simple; + +push @INC, 'lib/Test/Simple'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 3); + +ok(1, 'Foo'); +ok(0, 'Bar'); +ok(1, 'Yar'); +ok(1, 'Car'); +ok(0, 'Sar'); + +END { + My::Test::ok($$out eq <import(tests => 5); + +ok( 1, 'passing' ); +ok( 2, 'passing still' ); +ok( 3, 'still passing' ); +ok( 0, 'oh no!' ); +ok( 0, 'damnit' ); + + +END { + My::Test::ok($$out eq <import(tests => 5); + +ok(1, 'Foo'); +ok(0, 'Bar'); + +END { + My::Test::ok($$out eq <import; +}; + +My::Test::ok($$out eq ''); +My::Test::ok($$err eq ''); +My::Test::ok($@ eq ''); + +eval { + Test::Simple->import(tests => undef); +}; + +My::Test::ok($$out eq ''); +My::Test::ok($$err eq ''); +My::Test::ok($@ =~ /Got an undefined number of tests/); + +eval { + Test::Simple->import(tests => 0); +}; + +My::Test::ok($$out eq ''); +My::Test::ok($$err eq ''); +My::Test::ok($@ =~ /You told Test::Simple you plan to run 0 tests!/); + +eval { + Test::Simple::ok(1); +}; +My::Test::ok( $@ =~ /You tried to use ok\(\) without a plan!/); + + +END { + My::Test::ok($$out eq ''); + My::Test::ok($$err eq ""); + + # Prevent Test::Simple from exiting with non zero. + exit 0; +} diff --git a/lib/Test/Simple/t/plan_is_noplan.t b/lib/Test/Simple/t/plan_is_noplan.t new file mode 100644 index 0000000..0c2a5cd --- /dev/null +++ b/lib/Test/Simple/t/plan_is_noplan.t @@ -0,0 +1,52 @@ +# Can't use Test.pm, that's a 5.005 thing. +package My::Test; + +# This feature requires a fairly new version of Test::Harness +BEGIN { + require Test::Harness; + if( $Test::Harness::VERSION < 1.20 ) { + print "1..0\n"; + exit(0); + } +} + +print "1..2\n"; + +my $test_num = 1; +# Utility testing functions. +sub ok ($;$) { + my($test, $name) = @_; + print "not " unless $test; + print "ok $test_num"; + print " - $name" if defined $name; + print "\n"; + $test_num++; +} + + +package main; + +require Test::Simple; + +push @INC, 'lib/Test/Simple/'; +require Catch; +my($out, $err) = Catch::caught(); + + +Test::Simple->import('no_plan'); + +ok(1, 'foo'); + + +END { + My::Test::ok($$out eq < 3; + +ok(1, 'compile'); + +ok(1); +ok(1, 'foo'); diff --git a/t/lib/Test/Simple/Catch.pm b/t/lib/Test/Simple/Catch.pm new file mode 100644 index 0000000..2f8c887 --- /dev/null +++ b/t/lib/Test/Simple/Catch.pm @@ -0,0 +1,29 @@ +# For testing Test::Simple; +package Catch; + +my $out = tie *Test::Simple::TESTOUT, 'Catch'; +my $err = tie *Test::Simple::TESTERR, 'Catch'; + +# We have to use them to shut up a "used only once" warning. +() = (*Test::Simple::TESTOUT, *Test::Simple::TESTERR); + +sub caught { return $out, $err } + +# Prevent Test::Simple from exiting in its END block. +*Test::Simple::exit = sub {}; + +sub PRINT { + my $self = shift; + $$self .= join '', @_; +} + +sub TIEHANDLE { + my $class = shift; + my $self = ''; + return bless \$self, $class; +} +sub READ {} +sub READLINE {} +sub GETC {} + +1; diff --git a/t/lib/Test/Simple/sample_tests/death.plx b/t/lib/Test/Simple/sample_tests/death.plx new file mode 100644 index 0000000..8796eb2 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/death.plx @@ -0,0 +1,13 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); +close STDERR; + +ok(1); +ok(1); +ok(1); +die "Knife?"; diff --git a/t/lib/Test/Simple/sample_tests/death_in_eval.plx b/t/lib/Test/Simple/sample_tests/death_in_eval.plx new file mode 100644 index 0000000..969dbb0 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/death_in_eval.plx @@ -0,0 +1,22 @@ +require Test::Simple; +use Carp; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + +ok(1); +ok(1); +ok(1); +eval { + die "Foo"; +}; +ok(1); +eval "die 'Bar'"; +ok(1); + +eval { + croak "Moo"; +}; diff --git a/t/lib/Test/Simple/sample_tests/extras.plx b/t/lib/Test/Simple/sample_tests/extras.plx new file mode 100644 index 0000000..ed2d6ab --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/extras.plx @@ -0,0 +1,16 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + + +ok(1); +ok(1); +ok(1); +ok(1); +ok(0); +ok(1); +ok(0); diff --git a/t/lib/Test/Simple/sample_tests/five_fail.plx b/t/lib/Test/Simple/sample_tests/five_fail.plx new file mode 100644 index 0000000..c95e410 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/five_fail.plx @@ -0,0 +1,13 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + +ok(0); +ok(0); +ok(''); +ok(0); +ok(0); diff --git a/t/lib/Test/Simple/sample_tests/last_minute_death.plx b/t/lib/Test/Simple/sample_tests/last_minute_death.plx new file mode 100644 index 0000000..e1df5b1 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/last_minute_death.plx @@ -0,0 +1,16 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); +close STDERR; + +ok(1); +ok(1); +ok(1); +ok(1); +ok(1); + +die "Almost there..."; diff --git a/t/lib/Test/Simple/sample_tests/one_fail.plx b/t/lib/Test/Simple/sample_tests/one_fail.plx new file mode 100644 index 0000000..1762d65 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/one_fail.plx @@ -0,0 +1,14 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + + +ok(1); +ok(2); +ok(0); +ok(1); +ok(2); diff --git a/t/lib/Test/Simple/sample_tests/require.plx b/t/lib/Test/Simple/sample_tests/require.plx new file mode 100644 index 0000000..1a06690 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/require.plx @@ -0,0 +1 @@ +require Test::Simple; diff --git a/t/lib/Test/Simple/sample_tests/success.plx b/t/lib/Test/Simple/sample_tests/success.plx new file mode 100644 index 0000000..eb40a2d --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/success.plx @@ -0,0 +1,13 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + +ok(1); +ok(5, 'yep'); +ok(3, 'beer'); +ok("wibble", "wibble"); +ok(1); diff --git a/t/lib/Test/Simple/sample_tests/too_few.plx b/t/lib/Test/Simple/sample_tests/too_few.plx new file mode 100644 index 0000000..36acac9 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/too_few.plx @@ -0,0 +1,11 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + + +ok(1); +ok(0); diff --git a/t/lib/Test/Simple/sample_tests/two_fail.plx b/t/lib/Test/Simple/sample_tests/two_fail.plx new file mode 100644 index 0000000..5ddb912 --- /dev/null +++ b/t/lib/Test/Simple/sample_tests/two_fail.plx @@ -0,0 +1,14 @@ +require Test::Simple; + +push @INC, 't', '.'; +require Catch; +my($out, $err) = Catch::caught(); + +Test::Simple->import(tests => 5); + + +ok(0); +ok(1); +ok(1); +ok(0); +ok(1);