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