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