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?
Consider the following:
use Test::More tests => 1;
is(get_data_count([1..39]), 39,
"should be 39 items");
sub get_data_count {return @{+shift};}
This will print out:
1..1
ok 1 - should be 39 items
Now add one subroutine and one test to the foregoing:
use Test::More tests => 2;
is(get_data_count([1..39]), 39,
"should be 39 items");
ok(print_data_count([1..39]),
"print_data_count() printed successfully");
sub get_data_count {return @{+shift};}
sub print_data_count {
print 'Current data count: ' .
@{+shift} . "\n";
}
This will print out:
1..2
ok 1 - should be 39 items
Current data count: 39
ok 2 - print_data_count() printed successfully
The comment on the second test is, however, somewhat
misleading. The test reports that *something* was
printed -- because the return value of 'print' was
true -- but it doesn't test *what* was printed. The
only *completely* meaningful test would be one that
captures the list of items printed by
print_data_count().
Another way of putting this: Could a Test::More
function 'didprint' be invented which would have an
interface like this?
didprint( (print_data_count([1..39]),
'Current data count: 39')
"print_data_count() printed ideally");
I've Googled comp.lang.perl.misc and this list's
archive and have looked thru Perlmonks as well, but
haven't come up with a *simple* solution ('simple'
being defined as one that uses just Perl built-in
functions or modules distributed with 5.8 core).
Any suggestions? TIA.
Jim Keenan
=====
Affiliations: Perl Seminar NY / New York Perlmongers / Toronto Perlmongers
__________________________________
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo