5
use Capture::Tiny ':all';
13
my $have_diff = eval {
14
require Test::Differences;
15
Test::Differences->import;
21
if ( $have_diff ) { eq_or_diff( $g, $e, $l ); }
22
else { is( $g, $e, $l ); }
27
return $text eq 'unicode' ? 'binmode(STDOUT,q{:utf8}); binmode(STDERR,q{:utf8});' : '';
32
return unless $t eq 'unicode';
34
my @orig_layers = grep {$_ ne 'unix' and $_ ne 'perlio' and $seen{$_}++} PerlIO::get_layers(\*STDOUT);
35
binmode(STDOUT, ":utf8") if fileno(STDOUT);
36
binmode(STDERR, ":utf8") if fileno(STDERR);
41
my ($t, @orig_layers) = @_;
42
return unless $t eq 'unicode';
43
binmode(STDOUT, join( ":", "", "raw", @orig_layers)) if fileno(STDOUT);
44
binmode(STDERR, join( ":", "", "raw", @orig_layers)) if fileno(STDERR);
47
#--------------------------------------------------------------------------#
50
short => 'Hello World',
51
multiline => 'First line\nSecond line\n',
52
( $] < 5.008 ? () : ( unicode => 'Hi! \x{263a}\n') ),
55
#--------------------------------------------------------------------------#
56
# fcn($perl_code_string) => execute the perl in current process or subprocess
57
#--------------------------------------------------------------------------#
60
perl => sub { eval $_[0] },
61
sys => sub { system($^X, '-e', $_[0]) },
64
#--------------------------------------------------------------------------#
68
output => sub { _binmode($_[0]) . "print STDOUT qq{STDOUT:$texts{$_[0]}}" },
69
expect => sub { eval "qq{STDOUT:$texts{$_[0]}}", "" },
72
output => sub { _binmode($_[0]) . "print STDERR qq{STDERR:$texts{$_[0]}}" },
73
expect => sub { "", eval "qq{STDERR:$texts{$_[0]}}" },
76
output => sub { _binmode($_[0]) . "print STDOUT qq{STDOUT:$texts{$_[0]}}; print STDERR qq{STDERR:$texts{$_[0]}}" },
77
expect => sub { eval "qq{STDOUT:$texts{$_[0]}}", eval "qq{STDERR:$texts{$_[0]}}" },
81
#--------------------------------------------------------------------------#
87
my ($m, $c, $t, $l) = @_;
88
my ($got_out, $got_err) = capture {
89
$methods{$m}->( $channels{$c}{output}->($t) );
91
my @expected = $channels{$c}{expect}->($t);
92
_is_or_diff( $got_out, $expected[0], "$l|$m|$c|$t - got STDOUT" );
93
_is_or_diff( $got_err, $expected[1], "$l|$m|$c|$t - got STDERR" );
99
my ($m, $c, $t, $l) = @_;
100
my $got_out = capture {
101
$methods{$m}->( $channels{$c}{output}->($t) );
103
my @expected = $channels{$c}{expect}->($t);
104
_is_or_diff( $got_out, $expected[0], "$l|$m|$c|$t - got STDOUT" );
110
my ($m, $c, $t, $l) = @_;
111
my $got_out = capture_merged {
112
$methods{$m}->( $channels{$c}{output}->($t) );
114
my @expected = $channels{$c}{expect}->($t);
115
like( $got_out, qr/\Q$expected[0]\E/, "$l|$m|$c|$t - got STDOUT" );
116
like( $got_out, qr/\Q$expected[1]\E/, "$l|$m|$c|$t - got STDERR" );
122
my ($m, $c, $t, $l) = @_;
123
my ($got_out, $got_err);
124
my ($tee_out, $tee_err) = capture {
125
($got_out, $got_err) = tee {
126
$methods{$m}->( $channels{$c}{output}->($t) );
129
my @expected = $channels{$c}{expect}->($t);
130
_is_or_diff( $got_out, $expected[0], "$l|$m|$c|$t - got STDOUT" );
131
_is_or_diff( $tee_out, $expected[0], "$l|$m|$c|$t - tee STDOUT" );
132
_is_or_diff( $got_err, $expected[1], "$l|$m|$c|$t - got STDERR" );
133
_is_or_diff( $tee_err, $expected[1], "$l|$m|$c|$t - tee STDERR" );
139
my ($m, $c, $t, $l) = @_;
140
my ($got_out, $got_err);
141
my ($tee_out, $tee_err) = capture {
143
$methods{$m}->( $channels{$c}{output}->($t) );
146
my @expected = $channels{$c}{expect}->($t);
147
_is_or_diff( $got_out, $expected[0], "$l|$m|$c|$t - got STDOUT" );
148
_is_or_diff( $tee_out, $expected[0], "$l|$m|$c|$t - tee STDOUT" );
149
_is_or_diff( $tee_err, $expected[1], "$l|$m|$c|$t - tee STDERR" );
155
my ($m, $c, $t, $l) = @_;
156
my ($got_out, $got_err);
157
my ($tee_out, $tee_err) = capture {
158
$got_out = tee_merged {
159
$methods{$m}->( $channels{$c}{output}->($t) );
162
my @expected = $channels{$c}{expect}->($t);
163
like( $got_out, qr/\Q$expected[0]\E/, "$l|$m|$c|$t - got STDOUT" );
164
like( $got_out, qr/\Q$expected[1]\E/, "$l|$m|$c|$t - got STDERR" );
165
like( $tee_out, qr/\Q$expected[0]\E/, "$l|$m|$c|$t - tee STDOUT (STDOUT)" );
166
like( $tee_out, qr/\Q$expected[1]\E/, "$l|$m|$c|$t - tee STDOUT (STDERR)" );
167
_is_or_diff( $tee_err, '', "$l|$m|$c|$t - tee STDERR" );
172
#--------------------------------------------------------------------------#
173
# What I want to be able to do:
177
# channels => 'both',
182
my $test_type = shift or return;
183
my $todo = shift || '';
184
for my $m ( keys %methods ) {
185
for my $c ( keys %channels ) {
186
for my $t ( keys %texts ) {
187
my @orig_layers = _set_utf8($t);
188
local $TODO = "not yet supported"
190
$tests{$test_type}{test}->($m, $c, $t, $test_type);
191
_restore_layers($t, @orig_layers);