]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v1-message.t
0b43108094d8f0fca03969025bcf91beb65525ec
[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::More tests => 8;
14
15 use lib $Bin;
16 use test qw( evcheck );
17
18 use constant MESSAGE1 => 'Walking on the Milky Way';
19
20 =head2 Test 1: compilation
21
22 This test confirms that the test script and the modules it calls compiled
23 successfully.
24
25 =cut
26
27 use_ok 'Term::ProgressBar';
28
29
30 Term::ProgressBar->__force_term (50);
31
32 # -------------------------------------
33
34 =head2 Tests 2--8: Count 1-10
35
36 Create a progress bar with 10 things, and a name 'bob'.
37 Update it it from 1 to 10.
38
39 (1) Check no exception thrown on creation
40 (2) Check no exception thrown on update (1..5)
41 (3) Check no exception thrown on message send
42 (4) Check no exception thrown on update (6..10)
43 (5) Check message output.
44 (5) Check bar is complete
45 (6) Check bar number is 100%
46
47 =cut
48 use Capture::Tiny qw(capture);
49
50 my ($out, $err) = capture {
51   my $p;
52   ok (evcheck(sub { $p = Term::ProgressBar->new('bob', 10); },
53               'Count 1-10 (1)' ),
54       'Count 1-10 (1)');
55   ok (evcheck(sub { $p->update($_) for 1..5  }, 'Count 1-10 (2)' ),
56       'Count 1-10 (2)');
57   ok (evcheck(sub { $p->message(MESSAGE1)    }, 'Count 1-10 (3)' ),
58       'Count 1-10 (3)');
59   ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 (4)' ),
60       'Count 1-10 (4)');
61 };
62 print $out;
63
64   $err =~ s!^.*\r!!gm;
65   print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
66     if $ENV{TEST_DEBUG};
67
68   my @lines = split /\n/, $err;
69
70   is $lines[0], MESSAGE1;
71   like $lines[-1], qr/bob:\s+\d+% \#+/,            'Count 1-10 (6)';
72   like $lines[-1], qr/^bob:\s+100%/,               'Count 1-10 (7)';