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