]> git.donarmstrong.com Git - term-progressbar.git/blob - t/v2-message.t
6b687344f7edb18f6045ab6ccd1bbcdf60ac93fb
[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 Test::More tests => 11;
13 use Test::Exception;
14
15 use constant MESSAGE1 => 'Walking on the Milky Way';
16
17 use Capture::Tiny qw(capture);
18
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 Term::ProgressBar->__force_term (50);
30
31 # -------------------------------------
32
33 =head2 Tests 2--8: Count 1-10
34
35 Create a progress bar with 10 things.
36 Update it it from 1 to 10.  Output a message halfway through.
37
38 (1) Check no exception thrown on creation
39 (2) Check no exception thrown on update (1..5)
40 (3) Check no exception thrown on message send
41 (4) Check no exception thrown on update (6..10)
42 (5) Check message was issued.
43 (6) Check bar is complete
44 (7) Check bar number is 100%
45
46 =cut
47
48 my ($out, $err) = capture {
49   my $p;
50   lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)';
51   lives_ok { $p->update($_) for 1..5  }         'Count 1-10 (2)';
52   lives_ok { $p->message(MESSAGE1)    }         'Count 1-10 (3)';
53   lives_ok { $p->update($_) for 6..10 }         'Count 1-10 (4)';
54 };
55 print $out;
56
57   $err =~ s!^.*\r!!gm;
58   print STDERR "ERR:\n$err\nlength: ", length($err), "\n"
59     if $ENV{TEST_DEBUG};
60
61   my @lines = split /\n/, $err;
62
63   is $lines[0], MESSAGE1;
64   like $lines[-1], qr/\[=+\]/,            'Count 1-10 (5)';
65   like $lines[-1], qr/^\s*100%/,          'Count 1-10 (6)';
66
67 # -------------------------------------
68
69 =head2 Tests 9--11: Message Check
70
71 Run a progress bar from 0 to 100, each time calling a message after an update.
72 This is to check that message preserves the progress bar value correctly.
73
74 ( 1) Check no exception thrown on creation
75 ( 2) Check no exception thrown on update, message (0..100).
76 ( 3) Check last progress is 100%
77
78 =cut
79
80 ($out, $err) = capture {
81   my $p;
82   lives_ok { $p = Term::ProgressBar->new(100); } 'Message Check ( 1)';
83   lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } }  'Message Check ( 2)';
84 };
85 print $out;
86
87   my @err_lines = split /\n/, $err;
88   (my $last_line = $err_lines[-1]) =~ tr/\r//d;
89   is substr($last_line, 0, 4), '100%',               'Message Check ( 3)';
90
91 # ----------------------------------------------------------------------------