From: Jarkko Hietaniemi Date: Wed, 21 Nov 2001 03:09:46 +0000 (+0000) Subject: Upgrade to Test::Simple 0.33. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4bd4e70a66918d2504898c13d13f6efaa155cbb3;p=p5sagit%2Fp5-mst-13.2.git Upgrade to Test::Simple 0.33. p4raw-id: //depot/perl@13152 --- diff --git a/lib/Test/Builder.pm b/lib/Test/Builder.pm index 99ec519..7749585 100644 --- a/lib/Test/Builder.pm +++ b/lib/Test/Builder.pm @@ -8,7 +8,7 @@ $^C ||= 0; use strict; use vars qw($VERSION $CLASS); -$VERSION = 0.03; +$VERSION = 0.05; $CLASS = __PACKAGE__; my $IsVMS = $^O eq 'VMS'; @@ -544,6 +544,9 @@ sub no_ending { Controlling where the test output goes. +It's ok for your test to change where STDOUT and STDERR point to, +Test::Builder's default output settings will not be effected. + =over 4 =item B @@ -554,7 +557,7 @@ Prints out the given $message. Normally, it uses the failure_output() handle, but if this is for a TODO test, the todo_output() handle is used. -Output will be indented and prepended with a # as not to interfere +Output will be indented and marked with a # as not to interfere with test output. We encourage using this rather than calling print directly. @@ -564,7 +567,7 @@ We encourage using this rather than calling print directly. sub diag { my($self, @msgs) = @_; - # Prevent printing headers when compiling (ie. -c) + # Prevent printing headers when compiling (i.e. -c) return if $^C; # Escape each line with a #. @@ -951,14 +954,24 @@ END { At this point, Test::Simple and Test::More are your best examples. -=head1 AUTHOR +=head1 SEE ALSO + +Test::Simple, Test::More, Test::Harness + +=head1 AUTHORS Original code by chromatic, maintained by Michael G Schwern Eschwern@pobox.comE -=head1 SEE ALSO +=head1 COPYRIGHT -Test::Simple, Test::More, Test::Harness +Copyright 2001 by chromatic Echromatic@wgz.orgE, + Michael G Schwern Eschwern@pobox.comE. + +This program is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. + +See L =cut diff --git a/lib/Test/More.pm b/lib/Test/More.pm index 038122a..617455f 100644 --- a/lib/Test/More.pm +++ b/lib/Test/More.pm @@ -18,7 +18,7 @@ sub _carp { require Exporter; use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS $TODO); -$VERSION = '0.32'; +$VERSION = '0.33'; @ISA = qw(Exporter); @EXPORT = qw(ok use_ok require_ok is isnt like is_deeply @@ -117,7 +117,7 @@ Before anything else, you need a testing plan. This basically declares how many tests your script is going to run to protect against premature failure. -The prefered way to do this is to declare a plan when you C. +The preferred way to do this is to declare a plan when you C. use Test::More tests => $Num_Tests; @@ -151,7 +151,7 @@ or for deciding between running the tests at all: use Test::More; if( $^O eq 'MacOS' ) { - plan skip_all => 'Test irrelevent on MacOS'; + plan skip_all => 'Test irrelevant on MacOS'; } else { plan tests => 42; @@ -363,7 +363,7 @@ is similar to: (Mnemonic "This is like that".) The second argument is a regular expression. It may be given as a -regex reference (ie. C) or (for better compatibility with older +regex reference (i.e. C) or (for better compatibility with older perls) as a string that looks like a regex (alternative delimiters are currently not supported): @@ -699,7 +699,7 @@ they are "todo". Test::Harness will interpret failures as being ok. Should anything succeed, it will report it as an unexpected success. The nice part about todo tests, as opposed to simply commenting out a -block of tests, is it's like having a programatic todo list. You know +block of tests, is it's like having a programmatic todo list. You know how much work is left to be done, you're aware of what bugs there are, and you'll know immediately when they're fixed. @@ -709,7 +709,7 @@ When the block is empty, delete it. =back -=head2 Comparision functions +=head2 Comparison functions Not everything is a simple eq check or regex. There are times you need to see if two arrays are equivalent, for instance. For these @@ -832,7 +832,7 @@ sub _deep_check { my $eq; { - # Quiet unintialized value warnings when comparing undefs. + # Quiet uninitialized value warnings when comparing undefs. local $^W = 0; if( $e1 eq $e2 ) { @@ -953,7 +953,7 @@ since ok() takes it's arguments as scalars, it will see the length of ok( $_[0], $_[1] ); } -The other functions act similiarly. +The other functions act similarly. =item The eq_* family have some caveats. @@ -968,17 +968,11 @@ Test::Harness upgrade. =back -=head1 AUTHOR - -Michael G Schwern Eschwern@pobox.comE with much inspiration from -Joshua Pritikin's Test module and lots of discussion with Barrie -Slaymaker and the perl-qa gang. - =head1 HISTORY This is a case of convergent evolution with Joshua Pritikin's Test -module. I was largely unware of its existence when I'd first +module. I was largely unaware of its existence when I'd first written my own ok() routines. This module exists because I can't figure out how to easily wedge test names into Test's interface (along with a few other problems). @@ -1003,10 +997,27 @@ by Perl. L describes a very featureful unit testing interface. -L shows the idea of embedded testing. +L shows the idea of embedded testing. L is another approach to embedded testing. + +=head1 AUTHORS + +Michael G Schwern Eschwern@pobox.comE with much inspiration from +Joshua Pritikin's Test module and lots of discussion with Barrie +Slaymaker and the perl-qa gang. + + +=head1 COPYRIGHT + +Copyright 2001 by Michael G Schwern Eschwern@pobox.comE. + +This program is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. + +See L + =cut 1; diff --git a/lib/Test/Simple/Changes b/lib/Test/Simple/Changes index 2a6ee40..8aebf59 100644 --- a/lib/Test/Simple/Changes +++ b/lib/Test/Simple/Changes @@ -1,5 +1,12 @@ Revision history for Perl extension Test::Simple +0.33 Mon Oct 22 21:05:47 EDT 2001 + * It's now officially safe to redirect STDOUT and STDERR without + affecting test output. + - License and POD cleanup by Autrijus Tang + - Synched up Test::Tutorial with the wiki version + - Minor VMS test nit. + 0.32 Tue Oct 16 16:52:02 EDT 2001 * Finally added a seperate plan() function * Adding a name field to isa_ok() @@ -18,6 +25,7 @@ Revision history for Perl extension Test::Simple 0.30 Thu Sep 27 22:10:04 EDT 2001 * Added Test::Builder + (Thanks muchly to chromatic for getting this off the ground!) * Diagnostics are back to using STDERR *unless* it's from a todo test. Those go to STDOUT. - Fixed it so nothing is printed if a test is run with a -c flag. diff --git a/lib/Test/Simple/t/filehandles.t b/lib/Test/Simple/t/filehandles.t index 3b3c553..8bd0abe 100644 --- a/lib/Test/Simple/t/filehandles.t +++ b/lib/Test/Simple/t/filehandles.t @@ -12,3 +12,17 @@ package Dev::Null; sub TIEHANDLE { bless {} } sub PRINT { 1 } +#!perl -w + +use Test::More tests => 1; + +tie *STDOUT, "Dev::Null" or die $!; + +print "not ok 1\n"; # this should not print. +pass 'STDOUT can be mucked with'; + + +package Dev::Null; + +sub TIEHANDLE { bless {} } +sub PRINT { 1 } diff --git a/lib/Test/Simple/t/no_header.t b/lib/Test/Simple/t/no_header.t index b0a8d49..b788ef5 100644 --- a/lib/Test/Simple/t/no_header.t +++ b/lib/Test/Simple/t/no_header.t @@ -9,6 +9,10 @@ $| = 1; use Test::Builder; +# STDOUT must be unbuffered else our prints might come out after +# Test::More's. +$| = 1; + BEGIN { Test::Builder->new->no_header(1); } diff --git a/lib/Test/Simple/t/output.t b/lib/Test/Simple/t/output.t index 69682e4..eca01bc 100644 --- a/lib/Test/Simple/t/output.t +++ b/lib/Test/Simple/t/output.t @@ -36,6 +36,7 @@ undef $out; open(IN, 'foo') or die $!; chomp(my $line = ); close IN; + ok($line eq 'hi!'); open(FOO, ">>foo") or die $!; diff --git a/lib/Test/Tutorial.pod b/lib/Test/Tutorial.pod index fa1cd2b..bd5b91d 100644 --- a/lib/Test/Tutorial.pod +++ b/lib/Test/Tutorial.pod @@ -5,13 +5,13 @@ Test::Tutorial - A tutorial about writing really basic tests =head1 DESCRIPTION -B -B<*sob*> +I<*sob*> -B +I Is this you? Is writing tests right up there with writing @@ -43,12 +43,12 @@ since 1 + 1 is 2, it prints: What this says is: C<1..1> "I'm going to run one test." [1] C "The first test passed". And that's about all magic there is to -testing. Your basic unit of testing is the 'ok'. For each thing you -test, an 'ok' is printed. Simple. Test::Harness interprets your test +testing. Your basic unit of testing is the I. For each thing you +test, an C is printed. Simple. B interprets your test results to determine if you succeeded or failed (more on that later). Writing all these print statements rapidly gets tedious. Fortunately, -there's Test::Simple. It has one function, ok(). +there's B. It has one function, C. #!/usr/bin/perl -w @@ -56,9 +56,9 @@ there's Test::Simple. It has one function, ok(). ok( 1 + 1 == 2 ); -and that does the same thing as the code above. ok() is the backbone +and that does the same thing as the code above. C is the backbone of Perl testing, and we'll be using it instead of roll-your-own from -here on. If ok() gets a true value, the test passes. False, it +here on. If C gets a true value, the test passes. False, it fails. #!/usr/bin/perl -w @@ -78,12 +78,12 @@ from that comes C<1..2> "I'm going to run two tests." This number is used to ensure your test program ran all the way through and didn't die or skip some tests. C "The first test passed." C "The second test -failed". Test::Simple helpfuly prints out some extra commentary about +failed". Test::Simple helpfully prints out some extra commentary about your tests. It's not scary. Come, hold my hand. We're going to give an example of testing a module. For our example, we'll be testing a date -library, Date::ICal. It's on CPAN, so download a copy and follow +library, B. It's on CPAN, so download a copy and follow along. [2] @@ -93,7 +93,7 @@ This is the hardest part of testing, where do you start? People often get overwhelmed at the apparent enormity of the task of testing a whole module. Best place to start is at the beginning. Date::ICal is an object-oriented module, and that means you start by making an -object. So we test new(). +object. So we test C. #!/usr/bin/perl -w @@ -120,7 +120,7 @@ That output isn't terribly descriptive, is it? When you have two tests you can figure out which one is #2, but what if you have 102? Each test can be given a little descriptive name as the second -argument to ok(). +argument to C. use Test::Simple tests => 2; @@ -138,7 +138,7 @@ So now you'd see... Simplest way to build up a decent testing suite is to just test what the manual says it does. [3] Let's pull something out of the -Date::ICal SYNOPSIS and test that all it's bits work. +L and test that all it's bits work. #!/usr/bin/perl -w @@ -178,15 +178,18 @@ the failure occured, but not much else. We were supposed to get 17, but we didn't. What did we get?? Dunno. We'll have to re-run the test in the debugger or throw in some print statements to find out. -Instead, we'll switch from Test::Simple to Test::More. Test::More -does everything Test::Simple does, and more! In fact, Test::More does +Instead, we'll switch from B to B. B +does everything B does, and more! In fact, Test::More does things I the way Test::Simple does. You can literally swap Test::Simple out and put Test::More in its place. That's just what we're going to do. -Test::More provides more informative ways to say 'ok'. ok() is nice -and generic, you can write almost any test with it, but it can't tell -you what went wrong. For that, we use the is() function. +Test::More does more than Test::Simple. The most important difference +at this point is it provides more informative ways to say "ok". +Although you can write almost any test with a generic C, it +can't tell you what went wrong. Instead, we'll use the C +function, which lets us declare that something is supposed to be the +same as something else: #!/usr/bin/perl -w @@ -207,7 +210,7 @@ you what went wrong. For that, we use the is() function. is( $ical->month, 10, ' month()' ); is( $ical->year, 1964, ' year()' ); -"Is C<$ical->sec> 47?" "Is C<$ical->min> 12?" With is() in place, +"Is C<$ical-Esec> 47?" "Is C<$ical-Emin> 12?" With C in place, you get some more information 1..8 @@ -224,7 +227,7 @@ you get some more information ok 8 - year() # Looks like you failed 1 tests of 8. -letting us know that $ical->day returned 16, but we expected 17. A +letting us know that C<$ical-Eday> returned 16, but we expected 17. A quick check shows that the code is working fine, we made a mistake when writing up the tests. Just change it to: @@ -232,7 +235,7 @@ when writing up the tests. Just change it to: and everything works. -So any time you're doing a "this equals that" sort of test, use is(). +So any time you're doing a "this equals that" sort of test, use C. It even works on arrays. The test is always in scalar context, so you can test how many elements are in a list this way. [5] @@ -291,11 +294,11 @@ or we could set up a little try/expect loop. } So now we can test bunches of dates by just adding them to -%ICal_Dates. Now that it's less work to test with more dates, you'll +C<%ICal_Dates>. Now that it's less work to test with more dates, you'll be inclined to just throw more in as you think of them. Only problem is, every time we add to that we have to keep adjusting -the C ##> line. That can rapidly get -annoying. Instead we use 'no_plan'. This means we're just running +the C ##> line. That can rapidly get +annoying. Instead we use C. This means we're just running some tests, don't know how many. [6] use Test::More 'no_plan'; # instead of tests => 32 @@ -326,11 +329,14 @@ if something in there fails, you'll know which one it was and that will make tracking down the problem easier. So try to put a bit of debugging information into the test names. +Describe what the tests test, to make debugging a failed test easier +for you or for the next person who runs your test. + =head2 Skipping tests Poking around in the existing Date::ICal tests, I found this in -t/01sanity.t [7] +F [7] #!/usr/bin/perl -w @@ -386,7 +392,7 @@ going to work and skip the test. } A little bit of magic happens here. When running on anything but -MacOS, all the tests run normally. But when on MacOS, skip() causes +MacOS, all the tests run normally. But when on MacOS, C causes the entire contents of the SKIP block to be jumped over. It's never run. Instead, it prints special output that tells Test::Harness that the tests have been skipped. @@ -403,10 +409,10 @@ the tests have been skipped. This means your tests won't fail on MacOS. This means less emails from MacPerl users telling you about failing tests that you know will never work. You've got to be careful with skip tests. These are for -tests which don't work and B. It is not for skipping +tests which don't work and I. It is not for skipping genuine bugs (we'll get to that in a moment). -The tests are wholely and completely skipped. [10] This will work. +The tests are wholly and completely skipped. [10] This will work. SKIP: { skip("I don't wanna die!"); @@ -426,7 +432,7 @@ Thumbing through the Date::ICal man page, I came across this: Retrieves, or sets, the date on the object, using any valid ICal date/time string. -"Retrieves or sets". Hmmm, didn't see a test for using ical() to set +"Retrieves or sets". Hmmm, didn't see a test for using C to set the date in the Date::ICal test suite. So I'll write one. use Test::More tests => 1; @@ -447,7 +453,7 @@ run that and I get Whoops! Looks like it's unimplemented. Let's assume we don't have the time to fix this. [11] Normally, you'd just comment out the test and put a note in a todo list somewhere. Instead, we're going to -explicitly state "this test will fail" by wraping it in a TODO block. +explicitly state "this test will fail" by wrapping it in a C block. use Test::More tests => 1; @@ -473,22 +479,22 @@ failure as a successful test. So you can write tests even before you've fixed the underlying code. If a TODO test passes, Test::Harness will report it "UNEXPECTEDLY -SUCCEEDED". When that happens, you simply remove the TODO block and +SUCCEEDED". When that happens, you simply remove the TODO block with C and turn it into a real test. =head2 Testing with taint mode. Taint mode is a funny thing. It's the globalest of all global -features. Once you turn it on it effects B code in your program -and B modules used (and all the modules they use). If a single +features. Once you turn it on it effects I code in your program +and I modules used (and all the modules they use). If a single piece of code isn't taint clean, the whole thing explodes. With that in mind, it's very important to ensure your module works under taint mode. It's very simple to have your tests run under taint mode. Just throw -a -T into the #! line. Test::Harness will read the switches in #! and -use them to run your tests. +a C<-T> into the C<#!> line. Test::Harness will read the switches +in C<#!> and use them to run your tests. #!/usr/bin/perl -Tw @@ -496,7 +502,7 @@ use them to run your tests. ...test normally here... -So when you say "make test" it will be run with taint mode and +So when you say C it will be run with taint mode and warnings on. @@ -517,7 +523,7 @@ some bugs, which is good -- we'll uncover them with our tests. =item 3 You can actually take this one step further and test the manual -itself. Have a look at Pod::Tests (soon to be Test::Inline). +itself. Have a look at B (formerly B). =item 4 @@ -562,3 +568,22 @@ Do NOT be tempted to use TODO tests as a way to avoid fixing simple bugs! =back + +=head1 AUTHORS + +Michael G Schwern Eschwern@pobox.comE and the perl-qa dancers! + +=head1 COPYRIGHT + +Copyright 2001 by Michael G Schwern Eschwern@pobox.comE. + +This documentation is free; you can redistribute it and/or modify it +under the same terms as Perl itself. + +Irrespective of its distribution, all code examples in these files +are hereby placed into the public domain. You are permitted and +encouraged to use this code in your own programs for fun +or for profit as you see fit. A simple comment in the code giving +credit would be courteous but is not required. + +=cut