And here are the fruits of my application of IO::Capture: a module with three subroutines which have proven useful in the project I'm working on for my day job.Jim Keenan wrote:
Using the standard Test::More framework, is it possible to test whether what was printed to a filehandle matches a predetermined string or list of strings?
Would IO::Capture be of help here?
The full module is here: http://mysite.verizon.net/jkeen/perl/modules/misc/TestAuxiliary-0.01.tar.gz
Here is the SYNOPSIS, which includes simple examples of each function:
use Test::More qw(no_plan);
use IO::Capture::Stdout;
use TestAuxiliary qw(
verify_number_lines
verify_number_matches
get_matches
); $capture = IO::Capture::Stdout->new();
$capture->start();
print_greek();
$capture->stop();
is(verify_number_lines($capture), 4,
"number of screen lines printed is correct"); my @week = (
[ qw| Monday Lundi Lunes | ],
[ qw| Tuesday Mardi Martes | ],
[ qw| Wednesday Mercredi Miercoles | ],
[ qw| Thursday Jeudi Jueves | ],
[ qw| Friday Vendredi Viernes | ],
[ qw| Saturday Samedi Sabado | ],
[ qw| Sunday Dimanche Domingo | ],
);
$capture->start();
print_week([EMAIL PROTECTED]);
$capture->stop();
my $regex = qr/English:.*?French:.*?Spanish:/s;
is(verify_number_matches($capture, $regex), 7,
"correct number of forms printed to screen"); $regex = qr/French:\s+(.*?)\n/s;
my @predicted = qw| Lundi Mardi Mercredi Jeudi
Vendredi Samedi Dimanche |;
ok(eq_array([EMAIL PROTECTED], get_matches($capture, $regex)),
"all predicted matches found"); sub print_greek {
local $_;
print "$_\n" for (qw| alpha beta gamma delta |);
return 1;
} sub print_week {
my $weekref = shift;
my @week = @{$weekref};
for (my $day=0; $day<=$#week; $day++) {
print "English: $week[$day][0]\n";
print "French: $week[$day][1]\n";
print "Spanish: $week[$day][2]\n";
print "\n";
}
return 1;
}If you like these, or if you have similar subroutines which can extend the functionality of IO::Capture::Stdout, please let me know. Perhaps we can work this up into a full-scale CPAN module or subclass of IO::Capture.
Thank you very much.
jimk
