]> git.donarmstrong.com Git - term-progressbar.git/blob - t/compat.t
daba215626f1c3c3a78abc918112ec4bb9539954
[term-progressbar.git] / t / compat.t
1 # (X)Emacs mode: -*- cperl -*-
2
3 use strict;
4
5 =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
6
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.
9
10 =cut
11
12 # Utility -----------------------------
13
14 use Data::Dumper qw( );
15 use Test qw( ok plan );
16
17 # Test Setup --------------------------
18
19 BEGIN {
20   plan tests => 5,
21        todo  => [],
22        ;
23 }
24
25 # -------------------------------------
26
27 # grab_output()
28 #
29 # Eval some code and return what was printed to stdout and stderr.
30 #
31 # Parameters: string of code to eval
32 #
33 # Returns: listref of [ stdout text, stderr text ]
34 #
35 sub grab_output($) {
36     die 'usage: grab_stderr(string to eval)' if @_ != 1;
37     my $code = shift;
38         use File::Temp qw(tempdir);
39         my $dir = tempdir( CLEANUP => 1 );
40     my $tmp_o = "$dir/out"; my $tmp_e = "$dir/err";
41     local (*OLDOUT, *OLDERR);
42
43     # Try to get a message to the outside world if we die
44     local $SIG{__DIE__} = sub { print $_[0]; die $_[0] };
45
46     open(OLDOUT, ">&STDOUT") or die "can't dup stdout: $!";
47     open(OLDERR, ">&STDERR") or die "can't dup stderr: $!";
48     open(STDOUT, ">$tmp_o")  or die "can't open stdout to $tmp_o: $!";
49     open(STDERR, ">$tmp_e")  or die "can't open stderr to $tmp_e: $!";
50     eval $code;
51     # Doubtful whether most of these messages will ever be seen!
52     close(STDOUT)            or die "cannot close stdout opened to $tmp_o: $!";
53     close(STDERR)            or die "cannot close stderr opened to $tmp_e: $!";
54     open(STDOUT, ">&OLDOUT") or die "can't dup stdout back again: $!";
55     open(STDERR, ">&OLDERR") or die "can't dup stderr back again: $!";
56
57     die $@ if $@;
58
59     local $/ = undef;
60     open (TMP_O, $tmp_o) or die "cannot open $tmp_o: $!";
61     open (TMP_E, $tmp_e) or die "cannot open $tmp_e: $!";
62     my $o = <TMP_O>; my $e = <TMP_E>;
63     close TMP_O   or die "cannot close filehandle opened to $tmp_o: $!";
64     close TMP_E   or die "cannot close filehandle opened to $tmp_e: $!";
65     unlink $tmp_o or die "cannot unlink $tmp_o: $!";
66     unlink $tmp_e or die "cannot unlink $tmp_e: $!";
67
68     return [ $o, $e ];
69 }
70
71 # Change 1..1 below to 1..last_test_to_print .
72 # (It may become useful if the test is moved to ./t subdirectory.)
73
74 use Term::ProgressBar;
75 use POSIX qw<floor ceil>;
76
77 =head2 Test 1: compilation
78
79 This test confirms that the test script and the modules it calls compiled
80 successfully.
81
82 =cut
83
84 ok 1, 1, 'compilation';
85
86 # -------------------------------------
87
88 $| = 1;
89
90 my $count = 100;
91
92 # Test 2: create a bar
93 my $test_str = 'test';
94
95 use vars '$b';
96 my $o = grab_output("\$b = new Term::ProgressBar '$test_str', $count");
97 if (not $b or $o->[0] ne '' or $o->[1] ne "$test_str: ") {
98     print Data::Dumper->Dump([$b, $o], [qw( b o )])
99       if $ENV{TEST_DEBUG};
100     print 'not ';
101 }
102 print "ok 2\n";
103
104 # Test 3: do half the stuff and check half the bar has printed
105 my $halfway = floor($count / 2);
106 $o = grab_output("update \$b foreach (0 .. $halfway - 1)");
107 if ($o->[0] ne ''
108     or $o->[1] ne ('#' x floor(50 / 2)) )
109 {
110     print Data::Dumper->Dump([$o], [qw( o )])
111       if $ENV{TEST_DEBUG};
112     print 'not ';
113 }
114 print "ok 3\n";
115
116 # Test 4: do the rest of the stuff and check the whole bar has printed
117 $o = grab_output("update \$b foreach ($halfway .. $count - 1)");
118 if ($o->[0] ne ''
119     or $o->[1] ne ('#' x ceil(50 / 2)) . "\n" )
120 {
121     print Data::Dumper->Dump([$o], [qw( o )])
122       if $ENV{TEST_DEBUG};
123     print 'not ';
124 }
125 print "ok 4\n";
126
127 # Test 5: try to do another item and check there is an error
128 eval { update $b };
129 unless ( defined($@)
130          and
131          (substr($@, 0, length(Term::ProgressBar::ALREADY_FINISHED))
132           eq Term::ProgressBar::ALREADY_FINISHED) ) {
133   print Data::Dumper->Dump([$@], [qw( @ )])
134     if $ENV{TEST_DEBUG};
135   print 'not ';
136 }
137 print "ok 5\n";