Test::Simple/More/Builder/Tutorial 0.41
[p5sagit/p5-mst-13.2.git] / lib / Test / Builder.pm
index 1378242..2d8eddd 100644 (file)
@@ -8,7 +8,7 @@ $^C ||= 0;
 
 use strict;
 use vars qw($VERSION $CLASS);
-$VERSION = 0.05;
+$VERSION = '0.11';
 $CLASS = __PACKAGE__;
 
 my $IsVMS = $^O eq 'VMS';
@@ -55,11 +55,13 @@ Test::Builder - Backend for building test libraries
 
 =head1 DESCRIPTION
 
-I<THIS IS ALPHA GRADE SOFTWARE>  The interface will change.
+I<THIS IS ALPHA GRADE SOFTWARE>  Meaning the underlying code is well
+tested, yet the interface is subject to change.
 
 Test::Simple and Test::More have proven to be popular testing modules,
-but they're not always flexible enough.  Test::Builder provides the
-a building block upon which to write your own test libraries.
+but they're not always flexible enough.  Test::Builder provides the a
+building block upon which to write your own test libraries I<which can
+work together>.
 
 =head2 Construction
 
@@ -243,8 +245,8 @@ sub ok {
     $Curr_Test++;
     
     $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/;
-You named your test '$name'.  You shouldn't use numbers for your test names.
-Very confusing.
+    You named your test '$name'.  You shouldn't use numbers for your test names.
+    Very confusing.
 ERR
 
     my($pack, $file, $line) = $self->caller;
@@ -279,7 +281,7 @@ ERR
 
     unless( $test ) {
         my $msg = $todo ? "Failed (TODO)" : "Failed";
-        $self->diag("$msg test ($file at line $line)\n");
+        $self->diag("    $msg test ($file at line $line)\n");
     } 
 
     return $test ? 1 : 0;
@@ -294,7 +296,7 @@ string version.
 
 =item B<is_num>
 
-  $Test->is_num($get, $expected, $name);
+  $Test->is_num($got, $expected, $name);
 
 Like Test::More's is().  Checks if $got == $expected.  This is the
 numeric version.
@@ -302,41 +304,112 @@ numeric version.
 =cut
 
 sub is_eq {
-    my $self = shift;
+    my($self, $got, $expect, $name) = @_;
     local $Level = $Level + 1;
-    return $self->_is('eq', @_);
+
+    if( !defined $got || !defined $expect ) {
+        # undef only matches undef and nothing else
+        my $test = !defined $got && !defined $expect;
+
+        $self->ok($test, $name);
+        $self->_is_diag($got, 'eq', $expect) unless $test;
+        return $test;
+    }
+
+    return $self->cmp_ok($got, 'eq', $expect, $name);
 }
 
 sub is_num {
-    my $self = shift;
+    my($self, $got, $expect, $name) = @_;
     local $Level = $Level + 1;
-    return $self->_is('==', @_);
+
+    if( !defined $got || !defined $expect ) {
+        # undef only matches undef and nothing else
+        my $test = !defined $got && !defined $expect;
+
+        $self->ok($test, $name);
+        $self->_is_diag($got, '==', $expect) unless $test;
+        return $test;
+    }
+
+    return $self->cmp_ok($got, '==', $expect, $name);
 }
 
-sub _is {
-    my($self, $type, $got, $expect, $name) = @_;
+sub _is_diag {
+    my($self, $got, $type, $expect) = @_;
+
+    foreach my $val (\$got, \$expect) {
+        if( defined $$val ) {
+            if( $type eq 'eq' ) {
+                # quote and force string context
+                $$val = "'$$val'"
+            }
+            else {
+                # force numeric context
+                $$val = $$val+0;
+            }
+        }
+        else {
+            $$val = 'undef';
+        }
+    }
 
-    my $test;
-    {
-        local $^W = 0;      # so we can compare undef quietly
-        $test = $type eq 'eq' ? $got eq $expect
-                              : $got == $expect;
+    $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
+         got: %s
+    expected: %s
+DIAGNOSTIC
+
+}    
+
+=item B<isnt_eq>
+
+  $Test->isnt_eq($got, $dont_expect, $name);
+
+Like Test::More's isnt().  Checks if $got ne $dont_expect.  This is
+the string version.
+
+=item B<isnt_num>
+
+  $Test->is_num($got, $dont_expect, $name);
+
+Like Test::More's isnt().  Checks if $got ne $dont_expect.  This is
+the numeric version.
+
+=cut
+
+sub isnt_eq {
+    my($self, $got, $dont_expect, $name) = @_;
+    local $Level = $Level + 1;
+
+    if( !defined $got || !defined $dont_expect ) {
+        # undef only matches undef and nothing else
+        my $test = defined $got || defined $dont_expect;
+
+        $self->ok($test, $name);
+        $self->_cmp_diag('ne', $got, $dont_expect) unless $test;
+        return $test;
     }
+
+    return $self->cmp_ok($got, 'ne', $dont_expect, $name);
+}
+
+sub isnt_num {
+    my($self, $got, $dont_expect, $name) = @_;
     local $Level = $Level + 1;
-    my $ok = $self->ok($test, $name);
 
-    unless( $ok ) {
-        $got    = defined $got    ? "'$got'"    : 'undef';
-        $expect = defined $expect ? "'$expect'" : 'undef';
-        $self->diag(sprintf <<DIAGNOSTIC, $got, $expect);
-     got: %s
-expected: %s
-DIAGNOSTIC
-    }        
+    if( !defined $got || !defined $dont_expect ) {
+        # undef only matches undef and nothing else
+        my $test = defined $got || defined $dont_expect;
 
-    return $ok;
+        $self->ok($test, $name);
+        $self->_cmp_diag('!=', $got, $dont_expect) unless $test;
+        return $test;
+    }
+
+    return $self->cmp_ok($got, '!=', $dont_expect, $name);
 }
 
+
 =item B<like>
 
   $Test->like($this, qr/$regex/, $name);
@@ -346,36 +419,65 @@ Like Test::More's like().  Checks if $this matches the given $regex.
 
 You'll want to avoid qr// if you want your tests to work before 5.005.
 
+=item B<unlike>
+
+  $Test->unlike($this, qr/$regex/, $name);
+  $Test->unlike($this, '/$regex/', $name);
+
+Like Test::More's unlike().  Checks if $this B<does not match> the
+given $regex.
+
 =cut
 
 sub like {
     my($self, $this, $regex, $name) = @_;
 
     local $Level = $Level + 1;
+    $self->_regex_ok($this, $regex, '=~', $name);
+}
+
+sub unlike {
+    my($self, $this, $regex, $name) = @_;
+
+    local $Level = $Level + 1;
+    $self->_regex_ok($this, $regex, '!~', $name);
+}
+
+sub _regex_ok {
+    my($self, $this, $regex, $cmp, $name) = @_;
+
+    local $Level = $Level + 1;
 
     my $ok = 0;
+    my $usable_regex;
     if( ref $regex eq 'Regexp' ) {
-        local $^W = 0;
-        $ok = $self->ok( $this =~ $regex ? 1 : 0, $name );
+        $usable_regex = $regex;
     }
     # Check if it looks like '/foo/'
     elsif( my($re, $opts) = $regex =~ m{^ /(.*)/ (\w*) $ }sx ) {
-        local $^W = 0;
-        $ok = $self->ok( $this =~ /(?$opts)$re/ ? 1 : 0, $name );
+        $usable_regex = "(?$opts)$re";
     }
     else {
         $ok = $self->ok( 0, $name );
 
-        $self->diag("'$regex' doesn't look much like a regex to me.");
+        $self->diag("    '$regex' doesn't look much like a regex to me.");
 
         return $ok;
     }
 
+    {
+        local $^W = 0;
+        my $test = $this =~ /$usable_regex/ ? 1 : 0;
+        $test = !$test if $cmp eq '!~';
+        $ok = $self->ok( $test, $name );
+    }
+
     unless( $ok ) {
         $this = defined $this ? "'$this'" : 'undef';
-        $self->diag(sprintf <<DIAGNOSTIC, $this);
-              %s
-doesn't match '$regex'
+        my $match = $cmp eq '=~' ? "doesn't match" : "matches";
+        $self->diag(sprintf <<DIAGNOSTIC, $this, $match, $regex);
+                  %s
+    %13s '%s'
 DIAGNOSTIC
 
     }
@@ -383,6 +485,71 @@ DIAGNOSTIC
     return $ok;
 }
 
+=item B<cmp_ok>
+
+  $Test->cmp_ok($this, $type, $that, $name);
+
+Works just like Test::More's cmp_ok().
+
+    $Test->cmp_ok($big_num, '!=', $other_big_num);
+
+=cut
+
+sub cmp_ok {
+    my($self, $got, $type, $expect, $name) = @_;
+
+    my $test;
+    {
+        local $^W = 0;
+        local($@,$!);   # don't interfere with $@
+                        # eval() sometimes resets $!
+        $test = eval "\$got $type \$expect";
+    }
+    local $Level = $Level + 1;
+    my $ok = $self->ok($test, $name);
+
+    unless( $ok ) {
+        if( $type =~ /^(eq|==)$/ ) {
+            $self->_is_diag($got, $type, $expect);
+        }
+        else {
+            $self->_cmp_diag($got, $type, $expect);
+        }
+    }
+    return $ok;
+}
+
+sub _cmp_diag {
+    my($self, $got, $type, $expect) = @_;
+    
+    $got    = defined $got    ? "'$got'"    : 'undef';
+    $expect = defined $expect ? "'$expect'" : 'undef';
+    $self->diag(sprintf <<DIAGNOSTIC, $got, $type, $expect);
+    %s
+        %s
+    %s
+DIAGNOSTIC
+}
+
+=item B<BAILOUT>
+
+    $Test->BAILOUT($reason);
+
+Indicates to the Test::Harness that things are going so badly all
+testing should terminate.  This includes running any additional test
+scripts.
+
+It will exit with 255.
+
+=cut
+
+sub BAILOUT {
+    my($self, $reason) = @_;
+
+    $self->_print("Bail out!  $reason");
+    exit 255;
+}
+
 =item B<skip>
 
     $Test->skip;
@@ -413,6 +580,41 @@ sub skip {
     return 1;
 }
 
+
+=item B<todo_skip>
+
+  $Test->todo_skip;
+  $Test->todo_skip($why);
+
+Like skip(), only it will declare the test as failing and TODO.  Similar
+to
+
+    print "not ok $tnum # TODO $why\n";
+
+=cut
+
+sub todo_skip {
+    my($self, $why) = @_;
+    $why ||= '';
+
+    unless( $Have_Plan ) {
+        die "You tried to run tests without a plan!  Gotta have a plan.\n";
+    }
+
+    $Curr_Test++;
+
+    $Test_Results[$Curr_Test-1] = 1;
+
+    my $out = "not ok";
+    $out   .= " $Curr_Test" if $self->use_numbers;
+    $out   .= " # TODO $why\n";
+
+    $Test->_print($out);
+
+    return 1;
+}
+
+
 =begin _unimplemented
 
 =item B<skip_rest>
@@ -558,7 +760,8 @@ handle, but if this is for a TODO test, the todo_output() handle is
 used.
 
 Output will be indented and marked with a # so as not to interfere
-with test output.
+with test output.  A newline will be put on the end if there isn't one
+already.
 
 We encourage using this rather than calling print directly.
 
@@ -566,16 +769,18 @@ We encourage using this rather than calling print directly.
 
 sub diag {
     my($self, @msgs) = @_;
+    return unless @msgs;
 
     # Prevent printing headers when compiling (i.e. -c)
     return if $^C;
 
     # Escape each line with a #.
     foreach (@msgs) {
-        s/^([^#])/#     $1/;
-        s/\n([^#])/\n#     $1/g;
+        s/^/# /gms;
     }
 
+    push @msgs, "\n" unless $msgs[-1] =~ /\n\Z/;
+
     local $Level = $Level + 1;
     my $fh = $self->todo ? $self->todo_output : $self->failure_output;
     local($\, $", $,) = (undef, ' ', '');
@@ -685,8 +890,14 @@ unless( $^C ) {
     # test suites while still getting normal test output.
     open(TESTOUT, ">&STDOUT") or die "Can't dup STDOUT:  $!";
     open(TESTERR, ">&STDERR") or die "Can't dup STDERR:  $!";
+
+    # Set everything to unbuffered else plain prints to STDOUT will
+    # come out in the wrong order from our own prints.
     _autoflush(\*TESTOUT);
+    _autoflush(\*STDOUT);
     _autoflush(\*TESTERR);
+    _autoflush(\*STDERR);
+
     $CLASS->output(\*TESTOUT);
     $CLASS->failure_output(\*TESTERR);
     $CLASS->todo_output(\*TESTOUT);
@@ -912,24 +1123,24 @@ sub _ending {
 
         if( $Curr_Test < $Expected_Tests ) {
             $self->diag(<<"FAIL");
-# Looks like you planned $Expected_Tests tests but only ran $Curr_Test.
+Looks like you planned $Expected_Tests tests but only ran $Curr_Test.
 FAIL
         }
         elsif( $Curr_Test > $Expected_Tests ) {
             my $num_extra = $Curr_Test - $Expected_Tests;
             $self->diag(<<"FAIL");
-# Looks like you planned $Expected_Tests tests but ran $num_extra extra.
+Looks like you planned $Expected_Tests tests but ran $num_extra extra.
 FAIL
         }
         elsif ( $num_failed ) {
             $self->diag(<<"FAIL");
-# Looks like you failed $num_failed tests of $Expected_Tests.
+Looks like you failed $num_failed tests of $Expected_Tests.
 FAIL
         }
 
         if( $Test_Died ) {
             $self->diag(<<"FAIL");
-# Looks like your test died just after $Curr_Test.
+Looks like your test died just after $Curr_Test.
 FAIL
 
             _my_exit( 255 ) && return;
@@ -941,7 +1152,7 @@ FAIL
         _my_exit( 0 ) && return;
     }
     else {
-        $self->diag("# No tests run!\n");
+        $self->diag("No tests run!\n");
         _my_exit( 255 ) && return;
     }
 }
@@ -971,7 +1182,7 @@ Copyright 2001 by chromatic E<lt>chromatic@wgz.orgE<gt>,
 This program is free software; you can redistribute it and/or 
 modify it under the same terms as Perl itself.
 
-See L<http://www.perl.com/perl/misc/Artistic.html>
+See F<http://www.perl.com/perl/misc/Artistic.html>
 
 =cut