1 # (X)Emacs mode: -*- cperl -*-
6 =head1 Unit Test Package for Term::ProgressBar
8 This package tests the name functionality of Term::ProgressBar.
12 use Test::More tests => 20;
15 use Capture::Tiny qw(capture);
17 my $MESSAGE1 = 'The Gospel of St. Jude';
18 my $NAME1 = 'Algenon';
21 use_ok 'Term::ProgressBar';
23 Term::ProgressBar->__force_term (50);
25 # -------------------------------------
27 =head2 Tests 2--10: Count 1-10
29 Create a progress bar with 10 things, and a name 'Algenon'.
30 Update it it from 1 to 10.
32 (1) Check no exception thrown on creation
33 (2) Check no exception thrown on update (1..3)
34 (3) Check bar number is 30%
35 (4) Check bar is 30% along
36 (5) Check no exception thrown on message send
37 (6) Check no exception thrown on update (6..10)
38 (7) Check message seen
39 (8) Check bar is complete
40 (9) Check bar number is 100%
46 my ($out, $err) = capture {
48 $p = Term::ProgressBar->new({count => 10, name => $NAME1});
50 lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)';
55 diag "ERR (1) :\n$err\nlength: " . length($err)
57 my @lines = split /\n/, $err;
59 like $lines[-1], qr/^@{[$NAME1]}: \s*\b30%/, 'Count 1-10 ( 3)';
60 my ($bar, $space) = $lines[-1] =~ /\[(=*)(\s*)\]/;
61 my $length = length($bar) + length($space);
63 ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n")
65 my $barexpect = $length * 0.3;
66 cmp_ok length($bar), '>', $barexpect -1;
67 cmp_ok length($bar), '<', $barexpect+1;
70 ($out, $err) = capture {
71 lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)';
72 lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)';
77 diag "ERR (2) :\n$err\nlength: " . length($err)
80 @lines = split /\n/, $err;
82 is $lines[0], $MESSAGE1, 'Count 1-10 ( 7)';
83 like $lines[-1], qr/\[=+\]/, 'Count 1-10 ( 8)';
84 like $lines[-1], qr/^@{[$NAME1]}: \s*100%/, 'Count 1-10 ( 9)';
87 # -------------------------------------
89 =head2 Tests 11--20: Count 1-20
91 Create a progress bar with 20 things, and a name 'Smegma'.
92 Update it it from 1 to 20.
95 (1) Check no exception thrown on creation
96 (2) Check no exception thrown on update (1..12)
97 (3) Check bar number is 60%
98 (4) Check bar is 60% along
99 (5) Check no exception thrown on message send
100 (6) Check no exception thrown on update (13..20)
101 (7) Check message seen
102 (8) Check bar is complete
103 (9) Check bar number is 100%
109 my ($out, $err) = capture {
110 lives_ok { $p = Term::ProgressBar->new($NAME2, 10); } 'Count 1-10 ( 1)';
111 lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)';
116 diag "ERR (1) :\n$err\nlength: " . length($err)
118 my @lines = split /\n/, $err;
120 like $lines[-1], qr/^@{[$NAME2]}: \s*\b30%/, 'Count 1-10 ( 3)';
121 my ($bar, $space) = $lines[-1] =~ /(\#*)(\s*)/;
122 my $length = length($bar) + length($space);
124 ("LENGTHS (1) :BAR:" . length($bar) . ":SPACE:" . length($space))
126 my $barexpect = $length * 0.3;
127 cmp_ok length($bar), '>', $barexpect -1;
128 cmp_ok length($bar), '<', $barexpect+1;
130 ($out, $err) = capture {
131 lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)';
132 lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)';
137 diag "ERR (2) :\n$err\nlength: " . length($err)
140 @lines = split /\n/, $err;
142 like $lines[-1], qr/^@{[$NAME2]}: \s*\d+% \#*$/, 'Count 1-10 ( 8)';
143 like $lines[-1], qr/^@{[$NAME2]}: \s*100%/, 'Count 1-10 ( 9)';
146 # -------------------------------------