1 # (X)Emacs mode: -*- cperl -*-
5 =head1 Unit Test Package for Term::ProgressBar
7 This package tests the name 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 );
19 use constant MESSAGE1 => 'The Gospel of St. Jude';
20 use constant NAME1 => 'Algenon';
21 use constant NAME2 => 'Smegma';
24 # 1 for compilation test,
29 =head2 Test 1: compilation
31 This test confirms that the test script and the modules it calls compiled
36 use Term::ProgressBar;
38 ok 1, 1, 'compilation';
40 Term::ProgressBar->__force_term (50);
42 # -------------------------------------
44 =head2 Tests 2--10: Count 1-10
46 Create a progress bar with 10 things, and a name 'Algenon'.
47 Update it it from 1 to 10.
49 (1) Check no exception thrown on creation
50 (2) Check no exception thrown on update (1..3)
51 (3) Check bar number is 30%
52 (4) Check bar is 30% along
53 (5) Check no exception thrown on message send
54 (6) Check no exception thrown on update (6..10)
55 (7) Check message seen
56 (8) Check bar is complete
57 (9) Check bar number is 100%
63 save_output('stderr', *STDERR{IO});
65 $p = Term::ProgressBar->new({count => 10, name => NAME1});
66 }, 'Count 1-10 ( 1)'),
67 1, 'Count 1-10 ( 1)');
68 ok (evcheck(sub { $p->update($_) for 1..3 }, 'Count 1-10 ( 2)'),
69 1, 'Count 1-10 ( 2)');
71 my $err = restore_output('stderr');
74 print STDERR "ERR (1) :\n$err\nlength: ", length($err), "\n"
76 my @lines = split /\n/, $err;
77 ok $lines[-1], qr/^@{[NAME1()]}: \s*\b30%/, 'Count 1-10 ( 3)';
78 my ($bar, $space) = $lines[-1] =~ /\[(=*)(\s*)\]/;
79 my $length = length($bar) + length($space);
81 ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n")
83 my $barexpect = $length * 0.3;
84 my $ok = length($bar) > $barexpect -1 && length($bar) < $barexpect+1;
87 save_output('stderr', *STDERR{IO});
89 ok (evcheck(sub { $p->message(MESSAGE1) }, 'Count 1-10 ( 5)'),
90 1, 'Count 1-10 ( 5)');
91 ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 ( 6)'),
92 1, 'Count 1-10 ( 6)');
93 $err = restore_output('stderr');
96 print STDERR "ERR (2) :\n$err\nlength: ", length($err), "\n"
99 @lines = split /\n/, $err;
101 ok $lines[0], MESSAGE1, 'Count 1-10 ( 7)';
102 ok $lines[-1], qr/\[=+\]/, 'Count 1-10 ( 8)';
103 ok $lines[-1], qr/^@{[NAME1()]}: \s*100%/, 'Count 1-10 ( 9)';
106 # -------------------------------------
108 =head2 Tests 11--20: Count 1-20
110 Create a progress bar with 20 things, and a name 'Smegma'.
111 Update it it from 1 to 20.
114 (1) Check no exception thrown on creation
115 (2) Check no exception thrown on update (1..12)
116 (3) Check bar number is 60%
117 (4) Check bar is 60% along
118 (5) Check no exception thrown on message send
119 (6) Check no exception thrown on update (13..20)
120 (7) Check message seen
121 (8) Check bar is complete
122 (9) Check bar number is 100%
128 save_output('stderr', *STDERR{IO});
129 ok (evcheck(sub { $p = Term::ProgressBar->new(NAME2, 10); },
131 1, 'Count 1-10 ( 1)');
132 ok (evcheck(sub { $p->update($_) for 1..3 }, 'Count 1-10 ( 2)'),
133 1, 'Count 1-10 ( 2)');
135 my $err = restore_output('stderr');
138 print STDERR "ERR (1) :\n$err\nlength: ", length($err), "\n"
140 my @lines = split /\n/, $err;
141 ok $lines[-1], qr/^@{[NAME2()]}: \s*\b30%/, 'Count 1-10 ( 3)';
142 my ($bar, $space) = $lines[-1] =~ /(\#*)(\s*)/;
143 my $length = length($bar) + length($space);
145 ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n")
147 my $barexpect = $length * 0.3;
148 my $ok = length($bar) > $barexpect -1 && length($bar) < $barexpect+1;
151 save_output('stderr', *STDERR{IO});
153 ok (evcheck(sub { $p->message(MESSAGE1) }, 'Count 1-10 ( 5)'),
154 1, 'Count 1-10 ( 5)');
155 ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 ( 6)'),
156 1, 'Count 1-10 ( 6)');
157 $err = restore_output('stderr');
160 print STDERR "ERR (2) :\n$err\nlength: ", length($err), "\n"
163 @lines = split /\n/, $err;
165 ok $lines[-1], qr/^@{[NAME2()]}: \s*\d+% \#*$/, 'Count 1-10 ( 8)';
166 ok $lines[-1], qr/^@{[NAME2()]}: \s*100%/, 'Count 1-10 ( 9)';
169 # -------------------------------------