]> git.donarmstrong.com Git - term-progressbar.git/commitdiff
replace home made grab subroutine by Capture::Tiny::capture
authorGabor Szabo <gabor@szabgab.com>
Tue, 29 Nov 2011 21:46:00 +0000 (23:46 +0200)
committerGabor Szabo <gabor@szabgab.com>
Tue, 29 Nov 2011 21:46:00 +0000 (23:46 +0200)
t/compat.t

index 3698e57aa88747db68e22b82455cdda21ca3df76..19855a0b74b26f36a5e8790b00f2ba08afe5c17b 100644 (file)
@@ -1,6 +1,7 @@
 # (X)Emacs mode: -*- cperl -*-
 
 use strict;
 # (X)Emacs mode: -*- cperl -*-
 
 use strict;
+use warnings;
 
 =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
 
 
 =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
 
@@ -12,104 +13,55 @@ and is intended to test compatibility with that version.
 # Utility -----------------------------
 
 use Data::Dumper qw( );
 # Utility -----------------------------
 
 use Data::Dumper qw( );
-use Test::More tests => 4;
-
-# -------------------------------------
-
-# grab_output()
-#
-# Eval some code and return what was printed to stdout and stderr.
-#
-# Parameters: string of code to eval
-#
-# Returns: listref of [ stdout text, stderr text ]
-#
-sub grab_output($) {
-    die 'usage: grab_stderr(string to eval)' if @_ != 1;
-    my $code = shift;
-       use File::Temp qw(tempdir);
-       my $dir = tempdir( CLEANUP => 1 );
-    my $tmp_o = "$dir/out"; my $tmp_e = "$dir/err";
-    local (*OLDOUT, *OLDERR);
-
-    # Try to get a message to the outside world if we die
-    local $SIG{__DIE__} = sub { print $_[0]; die $_[0] };
-
-    open(OLDOUT, ">&STDOUT") or die "can't dup stdout: $!";
-    open(OLDERR, ">&STDERR") or die "can't dup stderr: $!";
-    open(STDOUT, ">$tmp_o")  or die "can't open stdout to $tmp_o: $!";
-    open(STDERR, ">$tmp_e")  or die "can't open stderr to $tmp_e: $!";
-    eval $code;
-    # Doubtful whether most of these messages will ever be seen!
-    close(STDOUT)            or die "cannot close stdout opened to $tmp_o: $!";
-    close(STDERR)            or die "cannot close stderr opened to $tmp_e: $!";
-    open(STDOUT, ">&OLDOUT") or die "can't dup stdout back again: $!";
-    open(STDERR, ">&OLDERR") or die "can't dup stderr back again: $!";
-
-    die $@ if $@;
-
-    local $/ = undef;
-    open (TMP_O, $tmp_o) or die "cannot open $tmp_o: $!";
-    open (TMP_E, $tmp_e) or die "cannot open $tmp_e: $!";
-    my $o = <TMP_O>; my $e = <TMP_E>;
-    close TMP_O   or die "cannot close filehandle opened to $tmp_o: $!";
-    close TMP_E   or die "cannot close filehandle opened to $tmp_e: $!";
-    unlink $tmp_o or die "cannot unlink $tmp_o: $!";
-    unlink $tmp_e or die "cannot unlink $tmp_e: $!";
-
-    return [ $o, $e ];
-}
+use Test::More tests => 9;
 
 use Term::ProgressBar;
 use POSIX qw<floor ceil>;
 
 use Term::ProgressBar;
 use POSIX qw<floor ceil>;
+use Capture::Tiny qw(capture);
 
 $| = 1;
 
 my $count = 100;
 
 
 $| = 1;
 
 my $count = 100;
 
-# Test 2: create a bar
+diag 'create a bar';
 my $test_str = 'test';
 
 my $test_str = 'test';
 
-use vars '$b';
-my $o = grab_output("\$b = new Term::ProgressBar '$test_str', $count");
-if (not $b or $o->[0] ne '' or $o->[1] ne "$test_str: ") {
-    print Data::Dumper->Dump([$b, $o], [qw( b o )])
-      if $ENV{TEST_DEBUG};
-    print 'not ';
+my $tp;
+{
+  my ($out, $err) = capture { $tp = Term::ProgressBar->new($test_str, $count); };
+  ok $tp;
+  is $out, '';
+  is $err, "$test_str: ";
 }
 }
-ok 1;
 
 
-# Test 3: do half the stuff and check half the bar has printed
+#    print Data::Dumper->Dump([$b, $out, $err], [qw( b o e)])
+#      if $ENV{TEST_DEBUG};
+
+diag 'do half the stuff and check half the bar has printed';
 my $halfway = floor($count / 2);
 my $halfway = floor($count / 2);
-$o = grab_output("update \$b foreach (0 .. $halfway - 1)");
-if ($o->[0] ne ''
-    or $o->[1] ne ('#' x floor(50 / 2)) )
 {
 {
-    print Data::Dumper->Dump([$o], [qw( o )])
-      if $ENV{TEST_DEBUG};
-    print 'not ';
+  my ($out, $err) = capture { update $tp foreach (0 .. $halfway - 1) };
+  is $out, '';
+  is $err, ('#' x floor(50 / 2));
+  
+#    print Data::Dumper->Dump([$o], [qw( o )])
+#      if $ENV{TEST_DEBUG};
 }
 }
-ok 1;
 
 
-# Test 4: do the rest of the stuff and check the whole bar has printed
-$o = grab_output("update \$b foreach ($halfway .. $count - 1)");
-if ($o->[0] ne ''
-    or $o->[1] ne ('#' x ceil(50 / 2)) . "\n" )
+# do the rest of the stuff and check the whole bar has printed
 {
 {
-    print Data::Dumper->Dump([$o], [qw( o )])
-      if $ENV{TEST_DEBUG};
-    print 'not ';
-}
-ok 1;
-
-# Test 5: try to do another item and check there is an error
-eval { update $b };
-unless ( defined($@)
-         and
-         (substr($@, 0, length(Term::ProgressBar::ALREADY_FINISHED))
-          eq Term::ProgressBar::ALREADY_FINISHED) ) {
-  print Data::Dumper->Dump([$@], [qw( @ )])
-    if $ENV{TEST_DEBUG};
-  print 'not ';
+   my ($out, $err) = capture { update $tp foreach ($halfway .. $count - 1) };
+   is $out, '';
+   is $err, ('#' x ceil(50 / 2)) . "\n";
+#    print Data::Dumper->Dump([$o], [qw( o )])
+#      if $ENV{TEST_DEBUG};
+
 }
 }
-ok 1;
+
+# try to do another item and check there is an error
+eval { update $tp };
+ok defined($@);
+is substr($@, 0, length(Term::ProgressBar::ALREADY_FINISHED)),
+          Term::ProgressBar::ALREADY_FINISHED;
+#  print Data::Dumper->Dump([$@], [qw( @ )])
+#    if $ENV{TEST_DEBUG};