Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

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)

Wednesday August 02, 2006
04:59 AM

Test::Class script

[ #30510 ]

I've probably posted this before, but it's just here so I google it later and find it easily. Note that one difference between this and some other Test::Class driver scripts is that this script assumes that all packages in TEST_DIR are test class packages which should be loaded. Thus, I don't have to have a bunch of "use" lines in this script and forget when I've added another class.

Also, note that the else condition allows me to put the following in my .vimrc:

noremap ,T  :!perl t/test_class_tests.t %<cr>

That lets me run a test class while I'm editing it.

#!/usr/bin/perl

use strict;
use warnings;
use File::Find::Rule;
use UNIVERSAL::require;

use constant TEST_DIR => 't/tests/';
use lib 'lib', 't/lib/', TEST_DIR;

sub file_to_module ($) {
    my $file = shift;
    $file =~ s/^@{[TEST_DIR]}//;
    $file =~ s/\.pm$//;
    $file =~ s{/}{::}g;
    warn $file, $/;
    return $file;
}

BEGIN {
    # tests must be loaded in a begin block or they will not be run.
    unless (@ARGV) {
        foreach
            my $file ( File::Find::Rule->file->name('*.pm')->in(TEST_DIR) )
        {
            ( file_to_module $file )->require or die $@;
        }
    }
    else {
        my $file = shift;
        unless ( -e $file ) {
            die "Could not locate file ($file)";
        }
        ( file_to_module $file )->require or die $@;
    }
}
Test::Class->runtests;

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.
  • First of all: unless ... else ...? What were you thinking? :)

    Secondly: sticking the TEST_DIR removal in file_to_module seems like a violation of separatation of concerns. Yeah, I know this script is small, but it still bugged me [perlmonks.org] when I did it in a small script of my own [perlmonks.org]. My suggestion would be to fix it the same way as I did:

    use File::Spec::Functions qw( abs2rel );

    # ...

    # file_to_module which does not concern itself with TEST_DIR

    # ...

    ( file_to_module abs2rel $file, TEST_DIR )->require or die $@;