# (X)Emacs mode: -*- cperl -*-
use strict;
+use warnings;
=head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
# 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 Capture::Tiny qw(capture);
$| = 1;
my $count = 100;
-# Test 2: create a bar
+diag 'create a bar';
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); };
+ isa_ok $tp, 'Term::ProgressBar';
+ is $out, '', 'empty stdout';
+ is $err, "$test_str: ";
}
-ok 1;
-# Test 3: do half the stuff and check half the bar has printed
+diag 'do half the stuff and check half the bar has printed';
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 { $tp->update foreach (0 .. $halfway - 1) };
+ is $out, '', 'empty stdout';
+ is $err, ('#' x floor(50 / 2));
}
-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 ';
+ my ($out, $err) = capture { $tp->update foreach ($halfway .. $count - 1) };
+ is $out, '', 'empty stdout';
+ is $err, ('#' x ceil(50 / 2)) . "\n";
}
-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 ';
-}
-ok 1;
+# try to do another item and check there is an error
+eval { $tp->update };
+my $err = $@;
+ok defined($err);
+is substr($err, 0, length(Term::ProgressBar::ALREADY_FINISHED)),
+ Term::ProgressBar::ALREADY_FINISHED;