]> git.donarmstrong.com Git - term-progressbar.git/blob - t/compat.t
cleanup of the test code
[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 Test::More tests => 9;
16
17 use Term::ProgressBar;
18 use POSIX qw<floor ceil>;
19 use Capture::Tiny qw(capture);
20
21 $| = 1;
22
23 my $count = 100;
24
25 diag 'create a bar';
26 my $test_str = 'test';
27
28 my $tp;
29 {
30   my ($out, $err) = capture { $tp = Term::ProgressBar->new($test_str, $count); };
31   isa_ok $tp, 'Term::ProgressBar';
32   is $out, '', 'empty stdout';
33   is $err, "$test_str: ";
34 }
35
36 diag 'do half the stuff and check half the bar has printed';
37 my $halfway = floor($count / 2);
38 {
39   my ($out, $err) = capture { $tp->update foreach (0 .. $halfway - 1) };
40   is $out, '', 'empty stdout';
41   is $err, ('#' x floor(50 / 2));
42 }
43
44 # do the rest of the stuff and check the whole bar has printed
45 {
46    my ($out, $err) = capture { $tp->update foreach ($halfway .. $count - 1) };
47    is $out, '', 'empty stdout';
48    is $err, ('#' x ceil(50 / 2)) . "\n";
49 }
50
51 # try to do another item and check there is an error
52 eval { $tp->update };
53 my $err = $@;
54 ok defined($err);
55 is substr($err, 0, length(Term::ProgressBar::ALREADY_FINISHED)),
56           Term::ProgressBar::ALREADY_FINISHED;