Use cases

BooK on 2005-03-23T00:22:40

I discovered a nice testing technique while coding my ever useless module (Acme::MetaSyntactic). I had trouble defining the API, so I finally wrote down 5 or 6 sample scripts. They all did the same thing, using the API in a different way.

What's nice is that it's quite easy to test that all the use cases behave in the same way:

use Test::More;
use File::Spec::Functions;

my @cases = glob catfile qw( t cases case* );

plan tests 2 * @cases;

for my $case (@cases) {
    # you can add more command-line options
    my $result = `$^X -Mblib -Mstrict -w $case`; 
    is( $? >> 8, 0, "$_ ran successfully" );
    # and the tests for $result can be fancier
    is( $result, "whatever's expected", "result" );
}

As a bonus, there is no need to update the tests when adding a new testcase!


Portability nitpick

schwern on 2005-03-23T02:45:41

my $result = `$^X -Mblib -Mstrict -w $case`;

To be 100% portable, that's...

        my $result = `$^X "-Mblib" "-Mstrict" -w $case`;

Why? VMS downcases anything not in quotes.