]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-simple.t
upgrade Capture::Tiny and use the new capture_stderr
[term-progressbar.git] / t / v2-simple.t
1 # (X)Emacs mode: -*- cperl -*-
2
3 use strict;
4 use warnings;
5
6 =head1 Unit Test Package for Term::ProgressBar
7
8 This package tests the basic functionality of Term::ProgressBar.
9
10 =cut
11
12 use Test::More tests => 31;
13 use Test::Exception;
14
15 use Capture::Tiny qw(capture_stderr);
16
17 use_ok 'Term::ProgressBar';
18
19 Term::ProgressBar->__force_term (50);
20
21 # -------------------------------------
22
23 =head2 Tests 2--16: Count 1-10
24
25 Create a progress bar with 10 things.
26 Update it it from 1 to 10.
27
28 (1) Check no exception thrown on creation
29 (2) Check no exception thrown on update
30 (3) Check bar is complete
31 (4) Check bar number is 100%
32 (5--15) Check bar has no minor characters at any point
33
34 =cut
35 {
36   my $err = capture_stderr {
37     my $p;
38     lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)';
39     lives_ok { $p->update($_) for 1..10 } 'Count 1-10 (2)';
40   };
41
42   my @lines = grep {$_ ne ''} split /\r/, $err;
43   diag explain \@lines
44     if $ENV{TEST_DEBUG};
45   like $lines[-1], qr/\[=+\]/,            'Count 1-10 (3)';
46   like $lines[-1], qr/^\s*100%/,          'Count 1-10 (4)';
47   like $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-10 (%d)', 5+$_)
48     for 0..10;
49 }
50 # -------------------------------------
51
52 =head2 Tests 17--30: Count 1-9
53
54 Create a progress bar with 10 things.
55 Update it it from 1 to 9.
56
57 (1) Check no exception thrown on creation
58 (2) Check no exception thrown on update
59 (3) Check bar is incomplete
60 (4) Check bar number is 90%
61 (5--14) Check bar has no minor characters at any point
62
63 =cut
64
65 {
66   my $err = capture_stderr {
67     my $p;
68     lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-9 (1)';
69     lives_ok { $p->update($_) for 1..9 } 'Count 1-9 (2)';
70   };
71
72   my @lines = grep $_ ne '', split /\r/, $err;
73   diag explain \@lines
74     if $ENV{TEST_DEBUG};
75   like $lines[-1], qr/\[=+ +\]/,          'Count 1-9 (3)';
76   like $lines[-1], qr/^\s*90%/,           'Count 1-9 (4)';
77   like $lines[$_], qr/\[[= ]+\]/, sprintf('Count 1-9 (%d)', 5+$_)
78     for 0..9;
79 }
80 # -------------------------------------
81
82 =head2 Test 31
83
84 Make sure the same progress bar text is not printed twice to the
85 terminal (in the case of an update that is too little to affect the
86 percentage or displayed bar).
87
88 =cut
89 {
90   my $err = capture_stderr {
91     my $tp = Term::ProgressBar->new(1000000);
92     $tp->update($_) foreach (0, 1);
93   };
94
95   my @lines = grep {$_ ne ''} split /\r/, $err;
96   diag explain \@lines
97     if $ENV{TEST_DEBUG};
98   is scalar @lines, 1;
99 }