]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-mobile.t
import Term-ProgressBar-2.09 from CPAN
[term-progressbar.git] / t / v2-mobile.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 moving target 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( DATA_DIR
17              evcheck restore_output save_output );
18
19 BEGIN {
20   # 1 for compilation test,
21   plan tests  => 7,
22        todo   => [],
23 }
24
25 =head2 Test 1: compilation
26
27 This test confirms that the test script and the modules it calls compiled
28 successfully.
29
30 =cut
31
32 use Term::ProgressBar;
33
34 ok 1, 1, 'compilation';
35
36 Term::ProgressBar->__force_term (50);
37
38 # -------------------------------------
39
40 =head2 Tests 2--7: Count 1-20
41
42 Create a progress bar with 10 things.
43 Update it it from 1 to 5.
44 Change target to 20.
45 Update it from 11 to 20.
46
47 (1) Check no exception thrown on creation
48 (2) Check no exception thrown on update (1..5)
49 (3) Check no exception thrown on target update
50 (4) Check no exception thrown on update (6..10)
51 (5) Check bar is complete
52 (6) Check bar number is 100%
53
54 =cut
55
56 {
57   my $p;
58   save_output('stderr', *STDERR{IO});
59   ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, 'Count 1-20 (1)' ),
60       1, 'Count 1-20 (1)');
61   ok (evcheck(sub { $p->update($_) for 1..5  },  'Count 1-20 (2)' ),
62       1, 'Count 1-20 (2)');
63   ok (evcheck(sub { $p->target(20)    },         'Count 1-20 (3)' ),
64       1, 'Count 1-20 (3)');
65   ok (evcheck(sub { $p->update($_) for 11..20 }, 'Count 1-20 (4)' ),
66       1, 'Count 1-20 (4)');
67   my $err = restore_output('stderr');
68
69   $err =~ s!^.*\r!!gm;
70   print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
71     if $ENV{TEST_DEBUG};
72
73   my @lines = split /\n/, $err;
74
75   ok $lines[-1], qr/\[=+\]/,            'Count 1-20 (5)';
76   ok $lines[-1], qr/^\s*100%/,          'Count 1-20 (6)';
77 }