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 Test::More tests => 11;
15 use constant MESSAGE1 => 'Walking on the Milky Way';
17 use Capture::Tiny qw(capture);
20 =head2 Test 1: compilation
22 This test confirms that the test script and the modules it calls compiled
27 use_ok 'Term::ProgressBar';
29 Term::ProgressBar->__force_term (50);
31 # -------------------------------------
33 =head2 Tests 2--8: Count 1-10
35 Create a progress bar with 10 things.
36 Update it it from 1 to 10. Output a message halfway through.
38 (1) Check no exception thrown on creation
39 (2) Check no exception thrown on update (1..5)
40 (3) Check no exception thrown on message send
41 (4) Check no exception thrown on update (6..10)
42 (5) Check message was issued.
43 (6) Check bar is complete
44 (7) Check bar number is 100%
48 my ($out, $err) = capture {
50 lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)';
51 lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)';
52 lives_ok { $p->message(MESSAGE1) } 'Count 1-10 (3)';
53 lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)';
58 print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
61 my @lines = split /\n/, $err;
63 is $lines[0], MESSAGE1;
64 like $lines[-1], qr/\[=+\]/, 'Count 1-10 (5)';
65 like $lines[-1], qr/^\s*100%/, 'Count 1-10 (6)';
67 # -------------------------------------
69 =head2 Tests 9--11: Message Check
71 Run a progress bar from 0 to 100, each time calling a message after an update.
72 This is to check that message preserves the progress bar value correctly.
74 ( 1) Check no exception thrown on creation
75 ( 2) Check no exception thrown on update, message (0..100).
76 ( 3) Check last progress is 100%
80 ($out, $err) = capture {
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)';
87 my @err_lines = split /\n/, $err;
88 (my $last_line = $err_lines[-1]) =~ tr/\r//d;
89 is substr($last_line, 0, 4), '100%', 'Message Check ( 3)';
91 # ----------------------------------------------------------------------------