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