1 # (X)Emacs mode: -*- cperl -*-
5 =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
7 This script is based on the test script for Term::ProgressBar version 1.0,
8 and is intended to test compatibility with that version.
12 # Utility -----------------------------
14 use Data::Dumper qw( );
15 use Test qw( ok plan );
17 # Test Setup --------------------------
25 # -------------------------------------
29 # Eval some code and return what was printed to stdout and stderr.
31 # Parameters: string of code to eval
33 # Returns: listref of [ stdout text, stderr text ]
36 die 'usage: grab_stderr(string to eval)' if @_ != 1;
39 my $tmp_o = POSIX::tmpnam(); my $tmp_e = POSIX::tmpnam();
40 local (*OLDOUT, *OLDERR);
42 # Try to get a message to the outside world if we die
43 local $SIG{__DIE__} = sub { print $_[0]; die $_[0] };
45 open(OLDOUT, ">&STDOUT") or die "can't dup stdout: $!";
46 open(OLDERR, ">&STDERR") or die "can't dup stderr: $!";
47 open(STDOUT, ">$tmp_o") or die "can't open stdout to $tmp_o: $!";
48 open(STDERR, ">$tmp_e") or die "can't open stderr to $tmp_e: $!";
50 # Doubtful whether most of these messages will ever be seen!
51 close(STDOUT) or die "cannot close stdout opened to $tmp_o: $!";
52 close(STDERR) or die "cannot close stderr opened to $tmp_e: $!";
53 open(STDOUT, ">&OLDOUT") or die "can't dup stdout back again: $!";
54 open(STDERR, ">&OLDERR") or die "can't dup stderr back again: $!";
59 open (TMP_O, $tmp_o) or die "cannot open $tmp_o: $!";
60 open (TMP_E, $tmp_e) or die "cannot open $tmp_e: $!";
61 my $o = <TMP_O>; my $e = <TMP_E>;
62 close TMP_O or die "cannot close filehandle opened to $tmp_o: $!";
63 close TMP_E or die "cannot close filehandle opened to $tmp_e: $!";
64 unlink $tmp_o or die "cannot unlink $tmp_o: $!";
65 unlink $tmp_e or die "cannot unlink $tmp_e: $!";
70 # Change 1..1 below to 1..last_test_to_print .
71 # (It may become useful if the test is moved to ./t subdirectory.)
73 use Term::ProgressBar;
74 use POSIX qw<floor ceil>;
76 =head2 Test 1: compilation
78 This test confirms that the test script and the modules it calls compiled
83 ok 1, 1, 'compilation';
85 # -------------------------------------
91 # Test 2: create a bar
92 my $test_str = 'test';
95 my $o = grab_output("\$b = new Term::ProgressBar '$test_str', $count");
96 if (not $b or $o->[0] ne '' or $o->[1] ne "$test_str: ") {
97 print Data::Dumper->Dump([$b, $o], [qw( b o )])
103 # Test 3: do half the stuff and check half the bar has printed
104 my $halfway = floor($count / 2);
105 $o = grab_output("update \$b foreach (0 .. $halfway - 1)");
107 or $o->[1] ne ('#' x floor(50 / 2)) )
109 print Data::Dumper->Dump([$o], [qw( o )])
115 # Test 4: do the rest of the stuff and check the whole bar has printed
116 $o = grab_output("update \$b foreach ($halfway .. $count - 1)");
118 or $o->[1] ne ('#' x ceil(50 / 2)) . "\n" )
120 print Data::Dumper->Dump([$o], [qw( o )])
126 # Test 5: try to do another item and check there is an error
130 (substr($@, 0, length(Term::ProgressBar::ALREADY_FINISHED))
131 eq Term::ProgressBar::ALREADY_FINISHED) ) {
132 print Data::Dumper->Dump([$@], [qw( @ )])