1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
use strict;
use warnings;
use Test::More;
BEGIN {
use Data::Printer::Config;
no warnings 'redefine';
*Data::Printer::Config::load_rc_file = sub { {} };
};
use Data::Printer colored => 0, return_value => 'void';
use Fcntl;
use File::Temp qw( :seekable tempfile );
if (!eval { require Capture::Tiny; 1; }) {
plan skip_all => 'Capture::Tiny not found';
}
else {
plan tests => 13;
}
#=====================
# testing OUTPUT
#=====================
my $item = 42;
my ($stdout, $stderr) = Capture::Tiny::capture( sub {
p $item, output => *STDOUT;
});
is $stdout, $item . $/, 'redirected output to STDOUT';
is $stderr, '', 'redirecting to STDOUT leaves STDERR empty';
#=====================
# testing OUTPUT ref
#=====================
$item++; # just to make sure there won't be any sort of cache
($stdout, $stderr) = Capture::Tiny::capture( sub {
p $item, output => \*STDOUT;
});
is $stdout, $item . $/, 'redirected output to a STDOUT ref';
is $stderr, '', 'redirecting to STDOUT ref leaves STDERR empty';
#=====================
# testing scalar ref
#=====================
$item++;
my $destination;
($stdout, $stderr) = Capture::Tiny::capture( sub {
p $item, output => \$destination;
});
is $destination, $item . $/, 'redirected output to a scalar ref';
is $stdout, '', 'redirecting to scalar ref leaver STDOUT empty';
is $stderr, '', 'redirecting to scalar ref leaves STDERR empty';
#=====================
# testing file handle
#=====================
$item++;
my $fh = tempfile;
($stdout, $stderr) = Capture::Tiny::capture( sub {
p $item, output => $fh;
});
seek( $fh, 0, SEEK_SET );
my $buffer = do { local $/; <$fh> };
is $buffer, $item . $/, 'redirected output to a file handle';
is $stdout, '', 'redirecting to file handle leaves STDOUT empty';
is $stderr, '', 'redirecting to file handle leaves STDERR empty';
#====================
# testing file name
#====================
$item++;
my $filename;
($fh, $filename) = tempfile;
($stdout, $stderr) = Capture::Tiny::capture( sub {
p $item, output => $filename, multiline => 0;
});
seek( $fh, 0, SEEK_SET );
$buffer = do { local $/; <$fh> };
like $buffer, qr{\A$item\s*\z}, 'redirected output to a filename';
is $stdout , '' , 'redirecting to filename leaves STDOUT empty';
is $stderr , '' , 'redirecting to filename leaves STDERR empty';
|