Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Tuesday June 30, 2009
10:16 AM

Calling All Test:: Authors

[ #39193 ]

The latest developer release of Test::More allows subtests. Subtests are great in that they solve a lot of problems in advanced Perl testing, but they have required a change in Test::Builder. Previously you could do stuff like this:

package Test::StringReverse;

use base 'Test::Builder::Module';
our @EXPORT = qw(is_reversed);

my $BUILDER = Test::Builder->new;

sub is_reversed ($$;$) {
    my ( $have, $want, $name ) = @_;

    my $passed = $want eq scalar reverse $name;

    $BUILDER->ok($passed, $name);
    $BUILDER->diag(<<"    END_DIAG") if not $passed;
    have: $have
    want: $want
    END_DIAG

    return $passed;
}

1;

And you've have a simple (untested ;) test for whether or not strings are reversed.

The reason that worked is that Test::Builder->new used to return a singleton. This is no longer true. If someone uses your test library in a subtest, the above code would break. Instead, you want to do this:

sub is_reversed ($$;$) {
    my ( $have, $want, $name ) = @_;

    my $passed  = $want eq scalar reverse $name;
    my $builder = __PACKAGE__->builder;

    $builder->ok($passed, $name);
    $builder->diag(<<"    END_DIAG") if not $passed;
    have: $have
    want: $want
    END_DIAG

    return $passed;
}

It's a minor change, it's completely backwards-compatible and it supports subtests. There's a work-around being planned, but it's not out there yet.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Running visitcpan [cpan.org] against my mini CPAN repo and searching for "Test::Builder->new" in a perl file in the lib directory revealed 182 hits. (Some are certainly false-positives, like Test-Simple itself.)

    List is online here [dagolden.com].

    -- dagolden

    • That looks sweet!

      Could you post the exact incantation you used with visitcpan? I'd like to steal some ideas.

      • It doesn't ignore Test::Bulder->new within subroutines -- this is an ack search so you get anything that uses that code at all. You'd need PPI for something more sophisticated.

        I ran:

        visitcpan -q -a dist ~/git/sandbox/visitcpan/scan-test-builder-new | tee t-b-new

        See also scan-test-builder-new [dagolden.com] for the actual script run in each distribution directory

    • Test::Warn is not in the list, because dist has no lib directory, only Warn.pm.