]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-mobile.t
replace the home made capturing of stderr by usage of Capture::Tiny
[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( evcheck );
17
18 use Capture::Tiny qw(capture);
19
20 BEGIN {
21   # 1 for compilation test,
22   plan tests  => 7,
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--7: Count 1-20
42
43 Create a progress bar with 10 things.
44 Update it it from 1 to 5.
45 Change target to 20.
46 Update it from 11 to 20.
47
48 (1) Check no exception thrown on creation
49 (2) Check no exception thrown on update (1..5)
50 (3) Check no exception thrown on target update
51 (4) Check no exception thrown on update (6..10)
52 (5) Check bar is complete
53 (6) Check bar number is 100%
54
55 =cut
56
57 my ($out, $err) = capture {
58   my $p;
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 };
68 print $out;
69
70   $err =~ s!^.*\r!!gm;
71   print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
72     if $ENV{TEST_DEBUG};
73
74   my @lines = split /\n/, $err;
75
76   ok $lines[-1], qr/\[=+\]/,            'Count 1-20 (5)';
77   ok $lines[-1], qr/^\s*100%/,          'Count 1-20 (6)';