]> git.donarmstrong.com Git - term-progressbar.git/blob - t/eta-linear.t
02189d55882ff1c13bcf1c1dcd79ca7e12f263b7
[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( 
17              evcheck restore_output save_output );
18
19 BEGIN {
20   # 1 for compilation test,
21   plan tests  => 10,
22        todo   => [],
23 }
24
25 =head2 Test 1: compilation
26
27 This test confirms that the test script and the modules it calls compiled
28 successfully.
29
30 =cut
31
32 use Term::ProgressBar;
33
34 ok 1, 1, 'compilation';
35
36 Term::ProgressBar->__force_term (50);
37
38 # -------------------------------------
39
40 =head2 Tests 2--10: Count 1-10
41
42 Create a progress bar with 10 things.  Invoke ETA and name on it.
43 Update it it from 1 to 10.
44
45 (1) Check no exception thrown on creation
46 (2) Check no exception thrown on update 1..5
47 (3) Check no exception thrown on message issued
48 (4) Check no exception thrown on update 6..10
49 (5) Check message seen
50 (6) Check bar is complete
51 (7) Check bar number is 100%
52 (8) Check --DONE-- issued
53 (9) Check estimation done
54
55 =cut
56
57 {
58   my $p;
59   save_output('stderr', *STDERR{IO});
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   my $err = restore_output('stderr');
75 #  $err =~ s!^.*\r!!gm;
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 # ----------------------------------------------------------------------------