1 # (X)Emacs mode: -*- cperl -*-
5 =head1 Unit Test Package for Term::ProgressBar
7 This package tests the basic functionality of Term::ProgressBar.
11 use Data::Dumper qw( Dumper );
12 use FindBin qw( $Bin );
13 use Test qw( ok plan );
17 evcheck restore_output save_output );
20 # 1 for compilation test,
25 =head2 Test 1: compilation
27 This test confirms that the test script and the modules it calls compiled
32 use Term::ProgressBar;
34 ok 1, 1, 'compilation';
36 Term::ProgressBar->__force_term (50);
38 # -------------------------------------
40 =head2 Tests 2--16: Count 1-10
42 Create a progress bar with 10 things.
43 Update it it from 1 to 10.
45 (1) Check no exception thrown on creation
46 (2) Check no exception thrown on update
47 (3) Check bar is complete
48 (4) Check bar number is 100%
49 (5--15) Check bar has no minor characters at any point
55 save_output('stderr', *STDERR{IO});
56 ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-10 (1)' ),
58 ok (evcheck(sub { $p->update($_) for 1..10 }, 'Count 1-10 (2)' ),
60 my $err = restore_output('stderr');
61 my @lines = grep $_ ne '', split /\r/, $err;
64 ok $lines[-1], qr/\[=+\]/, 'Count 1-10 (3)';
65 ok $lines[-1], qr/^\s*100%/, 'Count 1-10 (4)';
66 ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-10 (%d)', 5+$_)
70 # -------------------------------------
72 =head2 Tests 17--30: Count 1-9
74 Create a progress bar with 10 things.
75 Update it it from 1 to 9.
77 (1) Check no exception thrown on creation
78 (2) Check no exception thrown on update
79 (3) Check bar is incomplete
80 (4) Check bar number is 90%
81 (5--14) Check bar has no minor characters at any point
87 save_output('stderr', *STDERR{IO});
88 ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-9 (1)' ),
90 ok (evcheck(sub { $p->update($_) for 1..9 }, 'Count 1-9 (2)' ),
92 my $err = restore_output('stderr');
93 my @lines = grep $_ ne '', split /\r/, $err;
96 ok $lines[-1], qr/\[=+ +\]/, 'Count 1-9 (3)';
97 ok $lines[-1], qr/^\s*90%/, 'Count 1-9 (4)';
98 ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-9 (%d)', 5+$_)
102 # -------------------------------------
106 Make sure the same progress bar text is not printed twice to the
107 terminal (in the case of an update that is too little to affect the
108 percentage or displayed bar).
112 save_output('stderr', *STDERR{IO});
113 my $b = Term::ProgressBar->new(1000000);
114 $b->update($_) foreach (0, 1);
115 my $err = restore_output('stderr');
116 my @lines = grep $_ ne '', split /\r/, $err;