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 );
16 use test qw( evcheck restore_output save_output );
19 # 1 for compilation test,
24 =head2 Test 1: compilation
26 This test confirms that the test script and the modules it calls compiled
31 use Term::ProgressBar;
33 ok 1, 1, 'compilation';
35 Term::ProgressBar->__force_term (50);
37 # -------------------------------------
39 =head2 Tests 2--16: Count 1-10
41 Create a progress bar with 10 things.
42 Update it it from 1 to 10.
44 (1) Check no exception thrown on creation
45 (2) Check no exception thrown on update
46 (3) Check bar is complete
47 (4) Check bar number is 100%
48 (5--15) Check bar has no minor characters at any point
54 save_output('stderr', *STDERR{IO});
55 ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-10 (1)' ),
57 ok (evcheck(sub { $p->update($_) for 1..10 }, 'Count 1-10 (2)' ),
59 my $err = restore_output('stderr');
60 my @lines = grep $_ ne '', split /\r/, $err;
63 ok $lines[-1], qr/\[=+\]/, 'Count 1-10 (3)';
64 ok $lines[-1], qr/^\s*100%/, 'Count 1-10 (4)';
65 ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-10 (%d)', 5+$_)
69 # -------------------------------------
71 =head2 Tests 17--30: Count 1-9
73 Create a progress bar with 10 things.
74 Update it it from 1 to 9.
76 (1) Check no exception thrown on creation
77 (2) Check no exception thrown on update
78 (3) Check bar is incomplete
79 (4) Check bar number is 90%
80 (5--14) Check bar has no minor characters at any point
86 save_output('stderr', *STDERR{IO});
87 ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-9 (1)' ),
89 ok (evcheck(sub { $p->update($_) for 1..9 }, 'Count 1-9 (2)' ),
91 my $err = restore_output('stderr');
92 my @lines = grep $_ ne '', split /\r/, $err;
95 ok $lines[-1], qr/\[=+ +\]/, 'Count 1-9 (3)';
96 ok $lines[-1], qr/^\s*90%/, 'Count 1-9 (4)';
97 ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-9 (%d)', 5+$_)
101 # -------------------------------------
105 Make sure the same progress bar text is not printed twice to the
106 terminal (in the case of an update that is too little to affect the
107 percentage or displayed bar).
111 save_output('stderr', *STDERR{IO});
112 my $b = Term::ProgressBar->new(1000000);
113 $b->update($_) foreach (0, 1);
114 my $err = restore_output('stderr');
115 my @lines = grep $_ ne '', split /\r/, $err;