]> git.donarmstrong.com Git - term-progressbar.git/blob - t/zero.t
e857a59514bfd9091bc6680dac46c898f6f4ff1f
[term-progressbar.git] / t / zero.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 zero-progress handling of progress bar.
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 restore_output save_output );
17
18 BEGIN {
19   # 1 for compilation test,
20   plan tests  => 9,
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--5: V1 mode
40
41 Create a progress bar with 0 things.
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
46 (3) Check bar displays name
47 (3) Check bar says nothing to do
48
49 =cut
50
51 {
52   my $p;
53   save_output('stderr', *STDERR{IO});
54   my $name = 'doing nothing';
55   ok (evcheck(sub { $p = Term::ProgressBar->new($name, 0); },
56          'V1 mode ( 1)' ),
57       1,                                                       'V1 mode ( 1)');
58   ok (evcheck(sub { $p->update($_) for 1..10 },'V1 mode ( 2)'),
59       1,                                                       'V1 mode ( 2)');
60   my $err = restore_output('stderr');
61   my @lines = grep $_ ne '', split /\r/, $err;
62   print Dumper \@lines
63     if $ENV{TEST_DEBUG};
64   ok $lines[-1], qr/^$name:/,                                  'V1 mode ( 3)';
65   ok $lines[-1], qr/\(nothing to do\)/,                        'V1 mode ( 4)';
66 }
67
68 # -------------------------------------
69
70 =head2 Tests 6--9: V2 mode
71
72 Create a progress bar with 0 things.
73 Update it it from 1 to 10.
74
75 (1) Check no exception thrown on creation
76 (2) Check no exception thrown on update
77 (3) Check bar displays name
78 (4) Check bar says nothing to do
79
80 =cut
81
82 {
83   my $p;
84   save_output('stderr', *STDERR{IO});
85   my $name = 'zero';
86   ok (evcheck(sub { $p = Term::ProgressBar->new({ count => 0,
87                                                   name => $name }); },
88          'V2 mode ( 1)' ),
89       1,                                                       'V2 mode ( 1)');
90   ok (evcheck(sub { $p->update($_) for 1..10 },'V2 mode ( 2)'),
91       1,                                                       'V2 mode ( 2)');
92   my $err = restore_output('stderr');
93   my @lines = grep $_ ne '', split /\r/, $err;
94   print Dumper \@lines
95     if $ENV{TEST_DEBUG};
96   ok $lines[-1], qr/^$name:/,                                  'V2 mode ( 3)';
97   ok $lines[-1], qr/\(nothing to do\)/,                        'V2 mode ( 4)';
98 }
99
100 # ----------------------------------------------------------------------------