]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-message.t
upgrade Capture::Tiny and use the new capture_stderr
[term-progressbar.git] / t / v2-message.t
1 # (X)Emacs mode: -*- cperl -*-
2
3 use strict;
4 use warnings;
5
6 =head1 Unit Test Package for Term::ProgressBar
7
8 This package tests the basic functionality of Term::ProgressBar.
9
10 =cut
11
12 use Test::More tests => 11;
13 use Test::Exception;
14
15 use Capture::Tiny qw(capture_stderr);
16
17 my $MESSAGE1 = 'Walking on the Milky Way';
18
19 =head2 Test 1: compilation
20
21 This test confirms that the test script and the modules it calls compiled
22 successfully.
23
24 =cut
25
26 use_ok 'Term::ProgressBar';
27
28 Term::ProgressBar->__force_term (50);
29
30 # -------------------------------------
31
32 =head2 Tests 2--8: Count 1-10
33
34 Create a progress bar with 10 things.
35 Update it it from 1 to 10.  Output a message halfway through.
36
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%
44
45 =cut
46 {
47   my $err = capture_stderr {
48     my $p;
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)';
53   };
54
55   $err =~ s!^.*\r!!gm;
56   diag "ERR:\n$err\nlength: " . length($err)
57     if $ENV{TEST_DEBUG};
58
59   my @lines = split /\n/, $err;
60
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)';
64 }
65
66 # -------------------------------------
67
68 =head2 Tests 9--11: Message Check
69
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.
72
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%
76
77 =cut
78
79 {
80   my $err = capture_stderr {
81     my $p;
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)';
84   };
85
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)';
89 }
90
91 # ----------------------------------------------------------------------------