]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-simple.t
import Term-ProgressBar-2.09 from CPAN
[term-progressbar.git] / t / v2-simple.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( DATA_DIR
17              evcheck restore_output save_output );
18
19 BEGIN {
20   # 1 for compilation test,
21   plan tests  => 31,
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--16: Count 1-10
41
42 Create a progress bar with 10 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 is complete
48 (4) Check bar number is 100%
49 (5--15) Check bar has no minor characters at any point
50
51 =cut
52
53 {
54   my $p;
55   save_output('stderr', *STDERR{IO});
56   ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-10 (1)' ),
57       1, 'Count 1-10 (1)');
58   ok (evcheck(sub { $p->update($_) for 1..10 }, 'Count 1-10 (2)' ),
59       1, 'Count 1-10 (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/\[=+\]/,            'Count 1-10 (3)';
65   ok $lines[-1], qr/^\s*100%/,          'Count 1-10 (4)';
66   ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-10 (%d)', 5+$_)
67     for 0..10;
68 }
69
70 # -------------------------------------
71
72 =head2 Tests 17--30: Count 1-9
73
74 Create a progress bar with 10 things.
75 Update it it from 1 to 9.
76
77 (1) Check no exception thrown on creation
78 (2) Check no exception thrown on update
79 (3) Check bar is incomplete
80 (4) Check bar number is 90%
81 (5--14) Check bar has no minor characters at any point
82
83 =cut
84
85 {
86   my $p;
87   save_output('stderr', *STDERR{IO});
88   ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-9 (1)' ),
89       1, 'Count 1-9 (1)');
90   ok (evcheck(sub { $p->update($_) for 1..9 }, 'Count 1-9 (2)' ),
91       1, 'Count 1-9 (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/\[=+ +\]/,          'Count 1-9 (3)';
97   ok $lines[-1], qr/^\s*90%/,           'Count 1-9 (4)';
98   ok $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-9 (%d)', 5+$_)
99     for 0..9;
100 }
101
102 # -------------------------------------
103
104 =head2 Test 31
105
106 Make sure the same progress bar text is not printed twice to the
107 terminal (in the case of an update that is too little to affect the
108 percentage or displayed bar).
109
110 =cut
111 {
112   save_output('stderr', *STDERR{IO});
113   my $b = Term::ProgressBar->new(1000000);
114   $b->update($_) foreach (0, 1);
115   my $err = restore_output('stderr');
116   my @lines = grep $_ ne '', split /\r/, $err;
117   print Dumper \@lines
118     if $ENV{TEST_DEBUG};
119   ok scalar @lines, 1;
120 }