]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-message.t
38868db29b522aee31b02da2d61cdd4639b32423
[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( evcheck restore_output save_output );
17
18 use constant MESSAGE1 => 'Walking on the Milky Way';
19
20 BEGIN {
21   # 1 for compilation test,
22   plan tests  => 11,
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.
44 Update it it from 1 to 10.  Output a message halfway through.
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 was issued.
51 (6) Check bar is complete
52 (7) 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-10 (1)' ),
60       1, 'Count 1-10 (1)');
61   ok (evcheck(sub { $p->update($_) for 1..5  }, 'Count 1-10 (2)' ),
62       1, 'Count 1-10 (2)');
63   ok (evcheck(sub { $p->message(MESSAGE1)    }, 'Count 1-10 (3)' ),
64       1, 'Count 1-10 (3)');
65   ok (evcheck(sub { $p->update($_) for 6..10 }, 'Count 1-10 (4)' ),
66       1, 'Count 1-10 (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[0], MESSAGE1;
76   ok $lines[-1], qr/\[=+\]/,            'Count 1-10 (5)';
77   ok $lines[-1], qr/^\s*100%/,          'Count 1-10 (6)';
78 }
79
80 # -------------------------------------
81
82 =head2 Tests 9--11: Message Check
83
84 Run a progress bar from 0 to 100, each time calling a message after an update.
85 This is to check that message preserves the progress bar value correctly.
86
87 ( 1) Check no exception thrown on creation
88 ( 2) Check no exception thrown on update, message (0..100).
89 ( 3) Check last progress is 100%
90
91 =cut
92
93 {
94   my $p;
95   save_output('stderr', *STDERR{IO});
96   ok (evcheck(sub { $p = Term::ProgressBar->new(100); }, 'Message Check ( 1)'),
97       1,                                                 'Message Check ( 1)');
98   ok (evcheck(sub { for (0..100) { $p->update($_); $p->message("Hello") } },
99               'Message Check ( 2)',), 
100       1,                                                 'Message Check ( 2)');
101   my $err = restore_output('stderr');
102
103   my @err_lines = split /\n/, $err;
104   (my $last_line = $err_lines[-1]) =~ tr/\r//d;
105   ok substr($last_line, 0, 4), '100%',               'Message Check ( 3)';
106 }
107
108 # ----------------------------------------------------------------------------