]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-message.t
import Term-ProgressBar-2.09 from CPAN
[term-progressbar.git] / t / v2-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 2.101 qw( Dumper );
12 use FindBin 1.42 qw( $Bin );
13 use Test 1.122 qw( ok plan );
14
15 use lib $Bin;
16 use test qw( DATA_DIR
17              evcheck restore_output save_output );
18
19 use constant MESSAGE1 => 'Walking on the Milky Way';
20
21 BEGIN {
22   # 1 for compilation test,
23   plan tests  => 11,
24        todo   => [],
25 }
26
27 =head2 Test 1: compilation
28
29 This test confirms that the test script and the modules it calls compiled
30 successfully.
31
32 =cut
33
34 use Term::ProgressBar;
35
36 ok 1, 1, 'compilation';
37
38 Term::ProgressBar->__force_term (50);
39
40 # -------------------------------------
41
42 =head2 Tests 2--8: Count 1-10
43
44 Create a progress bar with 10 things.
45 Update it it from 1 to 10.  Output a message halfway through.
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 message send
50 (4) Check no exception thrown on update (6..10)
51 (5) Check message was issued.
52 (6) Check bar is complete
53 (7) Check bar number is 100%
54
55 =cut
56
57 {
58   my $p;
59   save_output('stderr', *STDERR{IO});
60   ok (evcheck(sub { $p = Term::ProgressBar->new(10); }, '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   my $err = restore_output('stderr');
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[0], MESSAGE1;
77   ok $lines[-1], qr/\[=+\]/,            'Count 1-10 (5)';
78   ok $lines[-1], qr/^\s*100%/,          'Count 1-10 (6)';
79 }
80
81 # -------------------------------------
82
83 =head2 Tests 9--11: Message Check
84
85 Run a progress bar from 0 to 100, each time calling a message after an update.
86 This is to check that message preserves the progress bar value correctly.
87
88 ( 1) Check no exception thrown on creation
89 ( 2) Check no exception thrown on update, message (0..100).
90 ( 3) Check last progress is 100%
91
92 =cut
93
94 {
95   my $p;
96   save_output('stderr', *STDERR{IO});
97   ok (evcheck(sub { $p = Term::ProgressBar->new(100); }, 'Message Check ( 1)'),
98       1,                                                 'Message Check ( 1)');
99   ok (evcheck(sub { for (0..100) { $p->update($_); $p->message("Hello") } },
100               'Message Check ( 2)',), 
101       1,                                                 'Message Check ( 2)');
102   my $err = restore_output('stderr');
103
104   my @err_lines = split /\n/, $err;
105   (my $last_line = $err_lines[-1]) =~ tr/\r//d;
106   ok substr($last_line, 0, 4), '100%',               'Message Check ( 3)';
107 }
108
109 # ----------------------------------------------------------------------------