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
|
package Perl6::Say::Auxiliary;
#$Id: Auxiliary.pm 1344 2009-03-16 03:27:56Z jimk $
# Contains test subroutines for distribution with Perl6::Say
# As of: March 29, 2009
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
_validate
capture_say
capture_say_file
capture_say_scalar
$capture_fail_message
);
use Carp;
*is = *Test::More::is;
our $capture_fail_message;
eval qq{ require IO::CaptureOutput; };
$capture_fail_message="tests require IO::CaptureOutput" if $@;
sub _validate {
my $pred = shift;
croak "2nd argument to capture_STDOUT must be integer"
unless $pred =~ m/^\d+$/;
return ($pred == 1) ? q{line} : q{lines};
}
sub capture_say {
my $argsref = shift;
my ($str, @list);
if (ref($argsref->{data}) eq q{ARRAY}) {
@list = @{$argsref->{data}};
} else {
$str = $argsref->{data};
}
my $pred = $argsref->{pred};
my $l = _validate($pred);
my $cat;
#IO::CaptureOutput->import('capture');
IO::CaptureOutput::capture(sub { &{$argsref->{eval}}; },\$cat);
#my $capture = IO::Capture::Stdout->new();
#$capture->start;
#&{$argsref->{eval}};
#$capture->stop;
#my $cat = join q{}, $capture->read();
unless ($str) {
$str = join q{}, @list;
}
is($cat, "$str\n", "$pred $l $argsref->{msg}");
}
sub capture_say_file {
my $argsref = shift;
my $pred = $argsref->{pred};
my $l = _validate($pred);
{
my $tmp = File::Temp->new( DIR => "./t" );
my $tmpfile = $tmp->filename;
&{$argsref->{eval}}($tmpfile, $argsref->{data});
my $slurp;
open READ, $tmpfile or croak "Cannot open tempfile for reading";
{
local $/ = undef;
$slurp = <READ>;
}
close READ or croak "Cannot close tempfile after reading";
my $str;
(ref($argsref->{data}) eq q{ARRAY})
? $str = join q{}, @{$argsref->{data}}
: $str = $argsref->{data};
is($slurp, "$str\n", "$pred $l $argsref->{msg}");
}
}
sub capture_say_scalar {
my $argsref = shift;
my $pred = $argsref->{pred};
my $l = _validate($pred);
my $got = &{$argsref->{eval}}($argsref->{data});
my $str;
(ref($argsref->{data}) eq q{ARRAY})
? $str = join q{}, @{$argsref->{data}}
: $str = $argsref->{data};
is($got, "$str\n", "$pred $l $argsref->{msg}");
}
1;
|