1 # (X)Emacs mode: -*- cperl -*-
6 =head1 Unit Test Package for Term::ProgressBar
8 This package tests the basic functionality of Term::ProgressBar.
12 use Test::More tests => 11;
15 use Capture::Tiny qw(capture_stderr);
17 my $MESSAGE1 = 'Walking on the Milky Way';
19 =head2 Test 1: compilation
21 This test confirms that the test script and the modules it calls compiled
26 use_ok 'Term::ProgressBar';
28 Term::ProgressBar->__force_term (50);
30 # -------------------------------------
32 =head2 Tests 2--8: Count 1-10
34 Create a progress bar with 10 things.
35 Update it it from 1 to 10. Output a message halfway through.
37 (1) Check no exception thrown on creation
38 (2) Check no exception thrown on update (1..5)
39 (3) Check no exception thrown on message send
40 (4) Check no exception thrown on update (6..10)
41 (5) Check message was issued.
42 (6) Check bar is complete
43 (7) Check bar number is 100%
47 my $err = capture_stderr {
49 lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)';
50 lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)';
51 lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)';
52 lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)';
56 diag "ERR:\n$err\nlength: " . length($err)
59 my @lines = split /\n/, $err;
61 is $lines[0], $MESSAGE1;
62 like $lines[-1], qr/\[=+\]/, 'Count 1-10 (5)';
63 like $lines[-1], qr/^\s*100%/, 'Count 1-10 (6)';
66 # -------------------------------------
68 =head2 Tests 9--11: Message Check
70 Run a progress bar from 0 to 100, each time calling a message after an update.
71 This is to check that message preserves the progress bar value correctly.
73 ( 1) Check no exception thrown on creation
74 ( 2) Check no exception thrown on update, message (0..100).
75 ( 3) Check last progress is 100%
80 my $err = capture_stderr {
82 lives_ok { $p = Term::ProgressBar->new(100); } 'Message Check ( 1)';
83 lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } } 'Message Check ( 2)';
86 my @err_lines = split /\n/, $err;
87 (my $last_line = $err_lines[-1]) =~ tr/\r//d;
88 is substr($last_line, 0, 4), '100%', 'Message Check ( 3)';
91 # ----------------------------------------------------------------------------