]> git.donarmstrong.com Git - term-progressbar.git/blob - t/eta-linear.t
replace the home made capturing of stderr by usage of Capture::Tiny
[term-progressbar.git] / t / eta-linear.t
1 # (X)Emacs mode: -*- cperl -*-
2
3 use strict;
4
5 =head1 Unit Test Package for Term::ProgressBar
6
7 This package tests the basic functionality of Term::ProgressBar.
8
9 =cut
10
11 use Data::Dumper  qw( Dumper );
12 use FindBin       qw( $Bin );
13 use Test          qw( ok plan );
14
15 use lib $Bin;
16 use test qw( evcheck );
17
18 BEGIN {
19   # 1 for compilation test,
20   plan tests  => 10,
21        todo   => [],
22 }
23
24 =head2 Test 1: compilation
25
26 This test confirms that the test script and the modules it calls compiled
27 successfully.
28
29 =cut
30
31 use Term::ProgressBar;
32
33 ok 1, 1, 'compilation';
34
35 Term::ProgressBar->__force_term (50);
36
37 # -------------------------------------
38
39 =head2 Tests 2--10: Count 1-10
40
41 Create a progress bar with 10 things.  Invoke ETA and name on it.
42 Update it it from 1 to 10.
43
44 (1) Check no exception thrown on creation
45 (2) Check no exception thrown on update 1..5
46 (3) Check no exception thrown on message issued
47 (4) Check no exception thrown on update 6..10
48 (5) Check message seen
49 (6) Check bar is complete
50 (7) Check bar number is 100%
51 (8) Check --DONE-- issued
52 (9) Check estimation done
53
54 =cut
55
56 use Capture::Tiny qw(capture);
57
58 my ($out, $err) = capture {
59   my $p;
60   ok (evcheck(sub {
61                 $p = Term::ProgressBar->new({count => 10, name => 'fred',
62                                              ETA => 'linear'});
63               }, 'Count 1-10 (1)' ),
64       1, 'Count 1-10 (1)');
65   ok (evcheck(sub { for (1..5) { $p->update($_); sleep 1 } },
66               'Count 1-10 (2)' ),
67       1, 'Count 1-10 (2)');
68   ok (evcheck(sub { $p->message('Hello Mum!') },
69               'Count 1-10 (3)' ),
70       1, 'Count 1-10 (3)');
71   ok (evcheck(sub { for (6..10) { $p->update($_); sleep 1 } },
72               'Count 1-10 (4)' ),
73       1, 'Count 1-10 (4)');
74 };
75 print $out;
76   my @lines = grep $_ ne '', split /[\n\r]+/, $err;
77   print Dumper \@lines
78     if $ENV{TEST_DEBUG};
79   ok grep $_ eq 'Hello Mum!', @lines;
80   ok $lines[-1], qr/\[=+\]/,                                  'Count 1-10 (6)';
81   ok $lines[-1], qr/^fred: \s*100%/,                          'Count 1-10 (7)';
82   ok $lines[-1], qr/D[ \d]\dh\d{2}m\d{2}s$/,                  'Count 1-10 (8)';
83   ok $lines[-2], qr/ Left$/,                                  'Count 1-10 (9)';
84
85
86 # ----------------------------------------------------------------------------