My $0.02:

Very nice integration of IO::Capture.

I think this is very promising, but all the start(), stop() calls seem overly repetitive to me. What about refactoring it into a set of test functions that handle it for the user automatically? Just quickly off the cuff, what about a test module (Test::Output? Test::Print? ) that provided functions like this:

stdout_is      { fcn() } $string, "comment"; # exact
stdout_like    { fcn() } qr/regex/, "comment"; # regex match
stdout_count   { fcn() } qr/regex/, $count, "comment";  # number of matches
stdout_found   { fcn() } qr/regex/, [EMAIL PROTECTED], "comment"; # list of 
matches

# repeat for stderr...

If you prototype it to take a code reference, then you can capture the results of dereferencing that code reference behind the scenes. (Using stdout_count, if the regex is "\n", you get the line count.)

You can provide a utility function that returns a string with the output for people to write custom tests against:

stdout_capture  { fcn() };

Regards,
David Golden

James E Keenan wrote:
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.

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

Reply via email to