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 2.101 qw( Dumper );
12 use FindBin 1.42 qw( $Bin );
13 use Test::More tests => 11;
16 use test qw( evcheck );
18 use constant MESSAGE1 => 'Walking on the Milky Way';
20 use Capture::Tiny qw(capture);
23 =head2 Test 1: compilation
25 This test confirms that the test script and the modules it calls compiled
30 use_ok 'Term::ProgressBar';
32 Term::ProgressBar->__force_term (50);
34 # -------------------------------------
36 =head2 Tests 2--8: Count 1-10
38 Create a progress bar with 10 things.
39 Update it it from 1 to 10. Output a message halfway through.
41 (1) Check no exception thrown on creation
42 (2) Check no exception thrown on update (1..5)
43 (3) Check no exception thrown on message send
44 (4) Check no exception thrown on update (6..10)
45 (5) Check message was issued.
46 (6) Check bar is complete
47 (7) Check bar number is 100%
51 my ($out, $err) = capture {
53 ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-10 (1)' ),
55 ok (evcheck(sub { $p->update($_) for 1..5 }, 'Count 1-10 (2)' ),
57 ok (evcheck(sub { $p->message(MESSAGE1) }, 'Count 1-10 (3)' ),
59 ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 (4)' ),
65 print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
68 my @lines = split /\n/, $err;
70 is $lines[0], MESSAGE1;
71 like $lines[-1], qr/\[=+\]/, 'Count 1-10 (5)';
72 like $lines[-1], qr/^\s*100%/, 'Count 1-10 (6)';
74 # -------------------------------------
76 =head2 Tests 9--11: Message Check
78 Run a progress bar from 0 to 100, each time calling a message after an update.
79 This is to check that message preserves the progress bar value correctly.
81 ( 1) Check no exception thrown on creation
82 ( 2) Check no exception thrown on update, message (0..100).
83 ( 3) Check last progress is 100%
87 ($out, $err) = capture {
89 ok (evcheck(sub { $p = Term::ProgressBar->new(100); }, 'Message Check ( 1)'),
90 'Message Check ( 1)');
91 ok (evcheck(sub { for (0..100) { $p->update($_); $p->message("Hello") } },
92 'Message Check ( 2)',),
93 'Message Check ( 2)');
97 my @err_lines = split /\n/, $err;
98 (my $last_line = $err_lines[-1]) =~ tr/\r//d;
99 is substr($last_line, 0, 4), '100%', 'Message Check ( 3)';
101 # ----------------------------------------------------------------------------