]> git.donarmstrong.com Git - term-progressbar.git/blob - t/zero.t
7461163054fb3bf1cf0bf346715d471b63418b72
[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 Capture::Tiny qw(capture);
16
17 use lib $Bin;
18 use test qw( evcheck );
19
20 BEGIN {
21   # 1 for compilation test,
22   plan tests  => 9,
23        todo   => [],
24 }
25
26 =head2 Test 1: compilation
27
28 This test confirms that the test script and the modules it calls compiled
29 successfully.
30
31 =cut
32
33 use Term::ProgressBar;
34
35 ok 1, 1, 'compilation';
36
37 Term::ProgressBar->__force_term (50);
38
39 # -------------------------------------
40
41 =head2 Tests 2--5: V1 mode
42
43 Create a progress bar with 0 things.
44 Update it it from 1 to 10.
45
46 (1) Check no exception thrown on creation
47 (2) Check no exception thrown on update
48 (3) Check bar displays name
49 (3) Check bar says nothing to do
50
51 =cut
52
53 {
54   my $p;
55   my $name;
56 my ($out, $err)  = capture {
57   $name = 'doing nothing';
58   ok (evcheck(sub { $p = Term::ProgressBar->new($name, 0); },
59          'V1 mode ( 1)' ),
60       1,                                                       'V1 mode ( 1)');
61   ok (evcheck(sub { $p->update($_) for 1..10 },'V1 mode ( 2)'),
62       1,                                                       'V1 mode ( 2)');
63 };
64 print $out;
65   my @lines = grep $_ ne '', split /\r/, $err;
66   print Dumper \@lines
67     if $ENV{TEST_DEBUG};
68   ok $lines[-1], qr/^$name:/,                                  'V1 mode ( 3)';
69   ok $lines[-1], qr/\(nothing to do\)/,                        'V1 mode ( 4)';
70 }
71
72 # -------------------------------------
73
74 =head2 Tests 6--9: V2 mode
75
76 Create a progress bar with 0 things.
77 Update it it from 1 to 10.
78
79 (1) Check no exception thrown on creation
80 (2) Check no exception thrown on update
81 (3) Check bar displays name
82 (4) Check bar says nothing to do
83
84 =cut
85
86 {
87   my $p;
88   my $name = 'zero';
89 my ($out, $err) = capture {
90   ok (evcheck(sub { $p = Term::ProgressBar->new({ count => 0,
91                                                   name => $name }); },
92          'V2 mode ( 1)' ),
93       1,                                                       'V2 mode ( 1)');
94   ok (evcheck(sub { $p->update($_) for 1..10 },'V2 mode ( 2)'),
95       1,                                                       'V2 mode ( 2)');
96 };
97 print $out;
98   my @lines = grep $_ ne '', split /\r/, $err;
99   print Dumper \@lines
100     if $ENV{TEST_DEBUG};
101   ok $lines[-1], qr/^$name:/,                                  'V2 mode ( 3)';
102   ok $lines[-1], qr/\(nothing to do\)/,                        'V2 mode ( 4)';
103 }
104
105 # ----------------------------------------------------------------------------