develooper Front page | perl.vmsperl | Postings from December 2001

[PATCH t/io/open.t t/test.pl] Cleanup and echo purge

Thread Next
From:
Michael G Schwern
Date:
December 6, 2001 14:38
Subject:
[PATCH t/io/open.t t/test.pl] Cleanup and echo purge
Message ID:
20011206223855.GC22648@blackrider
Here's a cleanup of t/io/open.t and purging of a use of `echo`.  I
think this is the last use of `echo` from a VMS PoV.  A few other
things...

    open |- works on VMS.  -| doesn't, though open() returns true
    it produces some noise which is probably pipe_and_fork magic
    going wrong.  Probably could be made to work.

    Needed to make t/test.pl print to STDOUT directly in case a test
    does a select FH (which open.t does).

    Added curr_test() to test.pl so you can get the number of the
    current test.  Useful for making roll-your-own tests play well with
    others


--- t/test.pl	Tue Dec  4 21:43:05 2001
+++ t/test.pl	Thu Dec  6 17:08:46 2001
@@ -15,22 +15,22 @@
 	my %plan = @_;
 	$n = $plan{tests}; 
     }
-    print "1..$n\n";
+    print STDOUT "1..$n\n";
     $planned = $n;
 }
 
 END {
     my $ran = $test - 1;
     if (defined $planned && $planned != $ran) {
-	print "# Looks like you planned $planned tests but ran $ran.\n";
+	print STDOUT "# Looks like you planned $planned tests but ran $ran.\n";
     }
 }
 
 sub skip_all {
     if (@_) {
-	print "1..0 - @_\n";
+	print STDOUT "1..0 - @_\n";
     } else {
-	print "1..0\n";
+	print STDOUT "1..0\n";
     }
     exit(0);
 }
@@ -47,15 +47,15 @@
     }
 
     $out .= " # TODO $TODO" if $TODO;
-    print "$out\n";
+    print STDOUT "$out\n";
 
     unless ($pass) {
-	print "# Failed $where\n";
+	print STDOUT "# Failed $where\n";
     }
 
     # Ensure that the message is properly escaped.
-    print map { /^#/ ? "$_\n" : "# $_\n" } 
-          map { split /\n/ } @mess if @mess;
+    print STDOUT map { /^#/ ? "$_\n" : "# $_\n" } 
+                 map { split /\n/ } @mess if @mess;
 
     $test++;
 
@@ -127,6 +127,10 @@
     _ok(0, _where(), @_);
 }
 
+sub curr_test {
+    return $test;
+}
+
 sub next_test {
     $test++
 }
@@ -137,7 +141,7 @@
     my $why = shift;
     my $n    = @_ ? shift : 1;
     for (1..$n) {
-	print "ok $test # skip: $why\n";
+        print STDOUT "ok $test # skip: $why\n";
         $test++;
     }
     local $^W = 0;
@@ -245,7 +249,7 @@
     if ($args{verbose}) {
 	my $runperldisplay = $runperl;
 	$runperldisplay =~ s/\n/\n\#/g;
-	print "# $runperldisplay\n";
+	print STDOUT "# $runperldisplay\n";
     }
     my $result = `$runperl`;
     $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
@@ -254,7 +258,7 @@
 
 
 sub BAILOUT {
-    print "Bail out! @_\n";
+    print STDOUT "Bail out! @_\n";
     exit;
 }
 
--- t/io/open.t	Sat Mar 24 11:09:24 2001
+++ t/io/open.t	Thu Dec  6 17:21:30 2001
@@ -3,304 +3,228 @@
 BEGIN {
     chdir 't' if -d 't';
     @INC = '../lib';
+    require './test.pl';
 }
 
-# $RCSfile$
 $|  = 1;
 use warnings;
 $Is_VMS = $^O eq 'VMS';
-$Is_Dos = $^O eq 'dos';
 
-print "1..70\n";
+plan tests => 95;
 
-my $test = 1;
-
-sub ok { print "ok $test\n"; $test++ }
-
-# my $file tests
-
-# 1..9
 {
     unlink("afile") if -f "afile";
-    print "$!\nnot " unless open(my $f,"+>afile");
-    ok;
+
+    $! = 0;  # the -f above will set $! if 'afile' doesn't exist.
+    ok( open(my $f,"+>afile"),  'open(my $f, "+>...")' );
+
     binmode $f;
-    print "not " unless -f "afile";
-    ok;
-    print "not " unless print $f "SomeData\n";
-    ok;
-    print "not " unless tell($f) == 9;
-    ok;
-    print "not " unless seek($f,0,0);
-    ok;
+    ok( -f "afile",             '       its a file');
+    ok( (print $f "SomeData\n"),  '       we can print to it');
+    is( tell($f), 9,            '       tell()' );
+    ok( seek($f,0,0),           '       seek set' );
+
     $b = <$f>;
-    print "not " unless $b eq "SomeData\n";
-    ok;
-    print "not " unless -f $f;
-    ok;
+    is( $b, "SomeData\n",       '       readline' );
+    ok( -f $f,                  '       still a file' );
+
     eval  { die "Message" };
-    # warn $@;
-    print "not " unless $@ =~ /<\$f> line 1/;
-    ok;
-    print "not " unless close($f);
-    ok;
-    unlink("afile");
+    like( $@, qr/<\$f> line 1/, '       die message correct' );
+    
+    ok( close($f),              '       close()' );
+    ok( unlink("afile"),        '       unlink()' );
+}
+
+{
+    ok( open(my $f,'>', 'afile'),       "open(my \$f, '>', 'afile')" );
+    ok( (print $f "a row\n"),           '       print');
+    ok( close($f),                      '       close' );
+    ok( -s 'afile' < 10,                '       -s' );
 }
 
-# 10..12
 {
-    print "# \$!='$!'\nnot " unless open(my $f,'>', 'afile');
-    ok;
-    print $f "a row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' < 10;
-    ok;
-}
-
-# 13..15
-{
-    print "# \$!='$!'\nnot " unless open(my $f,'>>', 'afile');
-    ok;
-    print $f "a row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' > 10;
-    ok;
-}
-
-# 16..18
-{
-    print "# \$!='$!'\nnot " unless open(my $f, '<', 'afile');
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    print "not " unless close($f);
-    ok;
-}
-
-# 19..23
-{
-    print "not " unless -s 'afile' < 20;
-    ok;
-    print "# \$!='$!'\nnot " unless open(my $f, '+<', 'afile');
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    seek $f, 0, 1;
-    print $f "yet another row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' > 20;
-    ok;
+    ok( open(my $f,'>>', 'afile'),      "open(my \$f, '>>', 'afile')" );
+    ok( (print $f "a row\n"),           '       print' );
+    ok( close($f),                      '       close' );
+    ok( -s 'afile' > 10,                '       -s'    );
+}
+
+{
+    ok( open(my $f, '<', 'afile'),      "open(my \$f, '<', 'afile')" );
+    my @rows = <$f>;
+    is( scalar @rows, 2,                '       readline, list context' );
+    is( $rows[0], "a row\n",            '       first line read' );
+    is( $rows[1], "a row\n",            '       second line' );
+    ok( close($f),                      '       close' );
+}
+
+{
+    ok( -s 'afile' < 20,                '-s' );
+
+    ok( open(my $f, '+<', 'afile'),     'open +<' );
+    my @rows = <$f>;
+    is( scalar @rows, 2,                '       readline, list context' );
+    ok( seek($f, 0, 1),                 '       seek cur' );
+    ok( (print $f "yet another row\n"), '       print' );
+    ok( close($f),                      '       close' );
+    ok( -s 'afile' > 20,                '       -s' );
 
     unlink("afile");
 }
 
-# 24..26
-if ($Is_VMS) {
-    for (24..26) { print "ok $_ # skipped: not Unix fork\n"; $test++;}
-}
-else {
-    print "# \$!='$!'\nnot " unless open(my $f, '-|', <<'EOC');
-    ./perl -e "print qq(a row\n); print qq(another row\n)"
+SKIP: {
+    skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
+
+    ok( open(my $f, '-|', <<EOC),     'open -|' );
+    $^X -e "print qq(a row\n); print qq(another row\n)"
 EOC
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    print "not " unless close($f);
-    ok;
+
+    my @rows = <$f>;
+    is( scalar @rows, 2,                '       readline, list context' );
+    ok( close($f),                      '       close' );
 }
 
-# 27..30
-if ($Is_VMS) {
-    for (27..30) { print "ok $_ # skipped: not Unix fork\n"; $test++;}
-}
-else {
-    print "# \$!='$!'\nnot " unless open(my $f, '|-', <<'EOC');
-    ./perl -pe "s/^not //"
+{
+    ok( open(my $f, '|-', <<EOC),     'open |-' );
+    $^X -pe "s/^not //"
 EOC
-    ok;
-    @rows = <$f>;
-    print $f "not ok $test\n"; $test++;
-    print $f "not ok $test\n"; $test++;
-    print "#\nnot " unless close($f);
+
+    my @rows = <$f>;
+    my $test = curr_test;
+    print $f "not ok $test - piped in\n";
+    next_test;
+
+    $test = curr_test;
+    print $f "not ok $test - piped in\n";
+    next_test;
+    ok( close($f),                      '       close' );
     sleep 1;
-    ok;
+    pass('flushing');
 }
 
-# 31..32
-eval <<'EOE' and print "not ";
-open my $f, '<&', 'afile';
-1;
-EOE
-ok;
-$@ =~ /Bad filehandle:\s+afile/ or print "not ";
-ok;
 
-# local $file tests
+ok( !eval { open my $f, '<&', 'afile'; 1; },    '<& on a non-filehandle' );
+like( $@, qr/Bad filehandle:\s+afile/,          '       right error' );
+
 
-# 33..41
+# local $file tests
 {
     unlink("afile") if -f "afile";
-    print "$!\nnot " unless open(local $f,"+>afile");
-    ok;
+
+    ok( open(local $f,"+>afile"),       'open local $f, "+>", ...' );
     binmode $f;
-    print "not " unless -f "afile";
-    ok;
-    print "not " unless print $f "SomeData\n";
-    ok;
-    print "not " unless tell($f) == 9;
-    ok;
-    print "not " unless seek($f,0,0);
-    ok;
+
+    ok( -f "afile",                     '       -f' );
+    ok( (print $f "SomeData\n"),        '       print' );
+    is( tell($f), 9,                    '       tell' );
+    ok( seek($f,0,0),                   '       seek set' );
+
     $b = <$f>;
-    print "not " unless $b eq "SomeData\n";
-    ok;
-    print "not " unless -f $f;
-    ok;
+    is( $b, "SomeData\n",               '       readline' );
+    ok( -f $f,                          '       still a file' );
+
     eval  { die "Message" };
-    # warn $@;
-    print "not " unless $@ =~ /<\$f> line 1/;
-    ok;
-    print "not " unless close($f);
-    ok;
+    like( $@, qr/<\$f> line 1/,         '       proper die message' );
+    ok( close($f),                      '       close' );
+
     unlink("afile");
 }
 
-# 42..44
 {
-    print "# \$!='$!'\nnot " unless open(local $f,'>', 'afile');
-    ok;
-    print $f "a row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' < 10;
-    ok;
-}
-
-# 45..47
-{
-    print "# \$!='$!'\nnot " unless open(local $f,'>>', 'afile');
-    ok;
-    print $f "a row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' > 10;
-    ok;
-}
-
-# 48..50
-{
-    print "# \$!='$!'\nnot " unless open(local $f, '<', 'afile');
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    print "not " unless close($f);
-    ok;
-}
-
-# 51..55
-{
-    print "not " unless -s 'afile' < 20;
-    ok;
-    print "# \$!='$!'\nnot " unless open(local $f, '+<', 'afile');
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    seek $f, 0, 1;
-    print $f "yet another row\n";
-    print "not " unless close($f);
-    ok;
-    print "not " unless -s 'afile' > 20;
-    ok;
+    ok( open(local $f,'>', 'afile'),    'open local $f, ">", ...' );
+    ok( (print $f "a row\n"),           '       print');
+    ok( close($f),                      '       close');
+    ok( -s 'afile' < 10,                '       -s' );
+}
+
+{
+    ok( open(local $f,'>>', 'afile'),   'open local $f, ">>", ...' );
+    ok( (print $f "a row\n"),           '       print');
+    ok( close($f),                      '       close');
+    ok( -s 'afile' > 10,                '       -s' );
+}
+
+{
+    ok( open(local $f, '<', 'afile'),   'open local $f, "<", ...' );
+    my @rows = <$f>;
+    is( scalar @rows, 2,                '       readline list context' );
+    ok( close($f),                      '       close' );
+}
+
+ok( -s 'afile' < 20,                '       -s' );
+
+{
+    ok( open(local $f, '+<', 'afile'),  'open local $f, "+<", ...' );
+    my @rows = <$f>;
+    is( scalar @rows, 2,                '       readline list context' );
+    ok( seek($f, 0, 1),                 '       seek cur' );
+    ok( (print $f "yet another row\n"), '       print' );
+    ok( close($f),                      '       close' );
+    ok( -s 'afile' > 20,                '       -s' );
 
     unlink("afile");
 }
 
-# 56..58
-if ($Is_VMS) {
-    for (56..58) { print "ok $_ # skipped: not Unix fork\n"; $test++;}
-}
-else {
-    print "# \$!='$!'\nnot " unless open(local $f, '-|', <<'EOC');
-    ./perl -e "print qq(a row\n); print qq(another row\n)"
+SKIP: {
+    skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
+
+    ok( open(local $f, '-|', <<EOC),  'open local $f, "-|", ...' );
+    $^X -e "print qq(a row\n); print qq(another row\n)"
 EOC
-    ok;
-    @rows = <$f>;
-    print "not " unless @rows == 2;
-    ok;
-    print "not " unless close($f);
-    ok;
+    my @rows = <$f>;
+
+    is( scalar @rows, 2,                '       readline list context' );
+    ok( close($f),                      '       close' );
 }
 
-# 59..62
-if ($Is_VMS) {
-    for (59..62) { print "ok $_ # skipped: not Unix fork\n"; $test++;}
-}
-else {
-    print "# \$!='$!'\nnot " unless open(local $f, '|-', <<'EOC');
-    ./perl -pe "s/^not //"
+{
+    ok( open(local $f, '|-', <<EOC),  'open local $f, "|-", ...' );
+    $^X -pe "s/^not //"
 EOC
-    ok;
-    @rows = <$f>;
-    print $f "not ok $test\n"; $test++;
-    print $f "not ok $test\n"; $test++;
-    print "#\nnot " unless close($f);
+
+    my @rows = <$f>;
+    my $test = curr_test;
+    print $f "not ok $test - piping\n";
+    next_test;
+
+    $test = curr_test;
+    print $f "not ok $test - piping\n";
+    next_test;
+    ok( close($f),                      '       close' );
     sleep 1;
-    ok;
+    pass("Flush");
 }
 
-# 63..64
-eval <<'EOE' and print "not ";
-open local $f, '<&', 'afile';
-1;
-EOE
-ok;
-$@ =~ /Bad filehandle:\s+afile/ or print "not ";
-ok;
 
-# 65..66
+ok( !eval { open local $f, '<&', 'afile'; 1 },  'local <& on non-filehandle');
+like( $@, qr/Bad filehandle:\s+afile/,          '       right error' );
+
+
 {
     local *F;
     for (1..2) {
-        if ($Is_Dos) {
-        open(F, "echo \\#foo|") or print "not ";
-        } else {
-            open(F, "echo #foo|") or print "not ";
-        }
-	print <F>;
-	close F;
+        ok( open(F, qq{$^X -le "print 'ok'"|}), 'open to pipe' );
+	is(scalar <F>, "ok\n",  '       readline');
+        ok( close F,            '       close' );
     }
-    ok;
+
     for (1..2) {
-        if ($Is_Dos) {
-	open(F, "-|", "echo \\#foo") or print "not ";
-        } else {
-            open(F, "-|", "echo #foo") or print "not ";
-        }
-	print <F>;
-	close F;
+        ok( open(F, "-|", qq{$^X -le "print 'ok'"}), 'open -|');
+        is( scalar <F>, "ok\n", '       readline');
+	ok( close F,            '       close' );
     }
-    ok;
 }
 
-# 67..70 - magic temporary file via 3 arg open with undef
+# magic temporary file via 3 arg open with undef
 {
-    open(my $x,"+<",undef) or print "not ";
-    ok;
-    print "not " unless defined(fileno($x));
-    ok;
+    ok( open(my $x,"+<",undef), 'magic temp file via 3 arg open with undef');
+    ok( defined fileno($x),     '       fileno' );
+
     select $x;
-    ok;   # goes to $x
+    ok( (print "ok\n"),         '       print' );
+
     select STDOUT;
-    seek($x,0,0);
-    print <$x>;
-    print "not " unless tell($x) > 3;
-    ok;
+    ok( seek($x,0,0),           '       seek' );
+    is( scalar <$x>, "ok\n",    '       readline' );
+    ok( tell($x) >= 3,          '       tell' );
 }


-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl Quality Assurance	    <perl-qa@perl.org>	       Kwalitee Is Job One
There is nothing wrong.  We have taken control of this sig file.  We will 
return it to you as soon as you are groovy.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About