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