]> git.donarmstrong.com Git - term-progressbar.git/blob - t/compat.t
replace home made grab subroutine by Capture::Tiny::capture
[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   ok $tp;
33   is $out, '';
34   is $err, "$test_str: ";
35 }
36
37 #    print Data::Dumper->Dump([$b, $out, $err], [qw( b o e)])
38 #      if $ENV{TEST_DEBUG};
39
40 diag 'do half the stuff and check half the bar has printed';
41 my $halfway = floor($count / 2);
42 {
43   my ($out, $err) = capture { update $tp foreach (0 .. $halfway - 1) };
44   is $out, '';
45   is $err, ('#' x floor(50 / 2));
46   
47 #    print Data::Dumper->Dump([$o], [qw( o )])
48 #      if $ENV{TEST_DEBUG};
49 }
50
51 # do the rest of the stuff and check the whole bar has printed
52 {
53    my ($out, $err) = capture { update $tp foreach ($halfway .. $count - 1) };
54    is $out, '';
55    is $err, ('#' x ceil(50 / 2)) . "\n";
56 #    print Data::Dumper->Dump([$o], [qw( o )])
57 #      if $ENV{TEST_DEBUG};
58
59 }
60
61 # try to do another item and check there is an error
62 eval { update $tp };
63 ok defined($@);
64 is substr($@, 0, length(Term::ProgressBar::ALREADY_FINISHED)),
65           Term::ProgressBar::ALREADY_FINISHED;
66 #  print Data::Dumper->Dump([$@], [qw( @ )])
67 #    if $ENV{TEST_DEBUG};