]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v1-message.t
replace the home made capturing of stderr by usage of Capture::Tiny
[term-progressbar.git] / t / v1-message.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         qw( ok plan );
14
15 use lib $Bin;
16 use test qw( evcheck );
17
18 use constant MESSAGE1 => 'Walking on the Milky Way';
19
20 BEGIN {
21   # 1 for compilation test,
22   plan tests  => 8,
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--8: Count 1-10
42
43 Create a progress bar with 10 things, and a name 'bob'.
44 Update it it from 1 to 10.
45
46 (1) Check no exception thrown on creation
47 (2) Check no exception thrown on update (1..5)
48 (3) Check no exception thrown on message send
49 (4) Check no exception thrown on update (6..10)
50 (5) Check message output.
51 (5) Check bar is complete
52 (6) Check bar number is 100%
53
54 =cut
55 use Capture::Tiny qw(capture);
56
57 my ($out, $err) = capture {
58   my $p;
59   ok (evcheck(sub { $p = Term::ProgressBar->new('bob', 10); },
60               'Count 1-10 (1)' ),
61       1, 'Count 1-10 (1)');
62   ok (evcheck(sub { $p->update($_) for 1..5  }, 'Count 1-10 (2)' ),
63       1, 'Count 1-10 (2)');
64   ok (evcheck(sub { $p->message(MESSAGE1)    }, 'Count 1-10 (3)' ),
65       1, 'Count 1-10 (3)');
66   ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 (4)' ),
67       1, 'Count 1-10 (4)');
68 };
69 print $out;
70
71   $err =~ s!^.*\r!!gm;
72   print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
73     if $ENV{TEST_DEBUG};
74
75   my @lines = split /\n/, $err;
76
77   ok $lines[0], MESSAGE1;
78   ok $lines[-1], qr/bob:\s+\d+% \#+/,            'Count 1-10 (6)';
79   ok $lines[-1], qr/^bob:\s+100%/,               'Count 1-10 (7)';