|
Packit Service |
44c065 |
# Copyright (c) 2009 by David Golden. All rights reserved.
|
|
Packit Service |
44c065 |
# Licensed under Apache License, Version 2.0 (the "License").
|
|
Packit Service |
44c065 |
# You may not use this file except in compliance with the License.
|
|
Packit Service |
44c065 |
# A copy of the License was distributed with this file or you may obtain a
|
|
Packit Service |
44c065 |
# copy of the License from http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
use strict;
|
|
Packit Service |
44c065 |
use warnings;
|
|
Packit Service |
44c065 |
use Test::More;
|
|
Packit Service |
44c065 |
use lib 't/lib';
|
|
Packit Service |
44c065 |
use IO::Handle;
|
|
Packit Service |
44c065 |
use Utils qw/next_fd sig_num/;
|
|
Packit Service |
44c065 |
use Capture::Tiny ':all';
|
|
Packit Service |
44c065 |
use Config;
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
plan tests => 12;
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
local $ENV{PERL_CAPTURE_TINY_TIMEOUT} = 0; # no timeouts
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
my $builder = Test::More->builder;
|
|
Packit Service |
44c065 |
binmode($builder->failure_output, ':utf8') if $] >= 5.008;
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
my $fd = next_fd;
|
|
Packit Service |
44c065 |
my ($out, $err, $res, @res);
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
# capture to array
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
($out, $err, @res) = capture {
|
|
Packit Service |
44c065 |
print STDOUT "foo\n";
|
|
Packit Service |
44c065 |
print STDERR "bar\n";
|
|
Packit Service |
44c065 |
return qw/one two three/;
|
|
Packit Service |
44c065 |
};
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
is( $out, "foo\n", "capture -> STDOUT captured" );
|
|
Packit Service |
44c065 |
is( $err, "bar\n", "capture -> STDERR captured" );
|
|
Packit Service |
44c065 |
is_deeply( \@res, [qw/one two three/], "return values -> array" );
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
# capture to scalar
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
($out, $err, $res) = capture {
|
|
Packit Service |
44c065 |
print STDOUT "baz\n";
|
|
Packit Service |
44c065 |
print STDERR "bam\n";
|
|
Packit Service |
44c065 |
return qw/one two three/;
|
|
Packit Service |
44c065 |
};
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
is( $out, "baz\n", "capture -> STDOUT captured" );
|
|
Packit Service |
44c065 |
is( $err, "bam\n", "capture -> STDERR captured" );
|
|
Packit Service |
44c065 |
is( $res, "one", "return value -> scalar" );
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
# capture_stdout to array
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
($out, @res) = capture_stdout {
|
|
Packit Service |
44c065 |
print STDOUT "foo\n";
|
|
Packit Service |
44c065 |
return qw/one two three/;
|
|
Packit Service |
44c065 |
};
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
is( $out, "foo\n", "capture_stdout -> STDOUT captured" );
|
|
Packit Service |
44c065 |
is_deeply( \@res, [qw/one two three/], "return values -> array" );
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
# capture_merged to array
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
($out, $res) = capture_merged {
|
|
Packit Service |
44c065 |
print STDOUT "baz\n";
|
|
Packit Service |
44c065 |
print STDERR "bam\n";
|
|
Packit Service |
44c065 |
return qw/one two three/;
|
|
Packit Service |
44c065 |
};
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
like( $out, qr/baz/, "capture_merged -> STDOUT captured" );
|
|
Packit Service |
44c065 |
like( $out, qr/bam/, "capture_merged -> STDERR captured" );
|
|
Packit Service |
44c065 |
is( $res, "one", "return value -> scalar" );
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
# finish
|
|
Packit Service |
44c065 |
#--------------------------------------------------------------------------#
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
is( next_fd, $fd, "no file descriptors leaked" );
|
|
Packit Service |
44c065 |
|
|
Packit Service |
44c065 |
exit 0;
|
|
Packit Service |
44c065 |
|