]> git.donarmstrong.com Git - term-progressbar.git/blob - t/compat.t
8f71b1891d66dc3ef0cb7bf2615e7315920e768e
[term-progressbar.git] / t / compat.t
1 # (X)Emacs mode: -*- cperl -*-
2
3 use strict;
4 use warnings;
5
6 =head1 Unit Test Package for Term::ProgressBar v1.0 Compatibility
7
8 This script is based on the test script for Term::ProgressBar version 1.0,
9 and is intended to test compatibility with that version.
10
11 =cut
12
13 # Utility -----------------------------
14
15 use Data::Dumper qw( );
16 use Test::More tests => 9;
17
18 use Term::ProgressBar;
19 use POSIX qw<floor ceil>;
20 use Capture::Tiny qw(capture);
21
22 $| = 1;
23
24 my $count = 100;
25
26 diag 'create a bar';
27 my $test_str = 'test';
28
29 my $tp;
30 {
31   my ($out, $err) = capture { $tp = Term::ProgressBar->new($test_str, $count); };
32   isa_ok $tp, 'Term::ProgressBar';
33   is $out, '', 'empty stdout';
34   is $err, "$test_str: ";
35 }
36
37 diag 'do half the stuff and check half the bar has printed';
38 my $halfway = floor($count / 2);
39 {
40   my ($out, $err) = capture { $tp->update foreach (0 .. $halfway - 1) };
41   is $out, '', 'empty stdout';
42   is $err, ('#' x floor(50 / 2));
43 }
44
45 # do the rest of the stuff and check the whole bar has printed
46 {
47    my ($out, $err) = capture { $tp->update foreach ($halfway .. $count - 1) };
48    is $out, '', 'empty stdout';
49    is $err, ('#' x ceil(50 / 2)) . "\n";
50 }
51
52 # try to do another item and check there is an error
53 eval { $tp->update };
54 my $err = $@;
55 ok defined($err);
56 is substr($err, 0, length(Term::ProgressBar::ALREADY_FINISHED)),
57           Term::ProgressBar::ALREADY_FINISHED;