There is no Title

It's a blog.

0 notes

At sundown, council workers lock up the seven hills of Edinburgh. This has been done since 1964, after the eigth hill mysteriously disappeared overnight on 4 June.

At sundown, council workers lock up the seven hills of Edinburgh. This has been done since 1964, after the eigth hill mysteriously disappeared overnight on 4 June.

0 notes

Perl: Tweaking a Common Idiom for Data-Driven Tests

A common Perl idiom is to use data-driven tests:

my @tests = (

  {
    build => {
      foo => 1,
      bar => 2,
    },
    check => {
      foo => 1,
    },
  },
  
 
  {
    build => {
      foo => 2,
      bar => 7,
    },
    check => {
      foo => 2,
    },
  },
  
  ... 
);

foreach my $test (@tests) {
  ok(my $thing = Thing->new( $test->{build} ), "new Thing");

  foreach my $param (keys $thing->{check}) {
    is($thing->$param, $thing->{check}->{$param}, "verify ${param}");
  }
}

This makes the test data redundant. A nice little workaround is to do something like this:

my $FROM_BUILD = \0; # A constant that we'll never use

my @tests = (

  {
    build => {
      foo => 1,
      bar => 2,
    },
    check => {
      foo => $FROM_BUILD,
    },
  },
  
 
  {
    build => {
      foo => 2,
      bar => 7,
    },
    check => {
      foo => $FROM_BUILD,
    },
  },
  
  ... 
);

foreach my $test (@tests) {
  ok(my $thing = Thing->new( $test->{build} ), "new Thing");

  foreach my $param (keys $thing->{check}) {
  
    $thing->{check}->{$param} =
      $thing->{build}->{$param}
        if $thing->{check}->{$param} == $FROM_BUILD;
      
    is($thing->$param, $thing->{check}->{$param}, "verify ${param}");
  }
}

This reduces mistakes due to cutting-and-pasting then tweaking similar test cases.

Filed under perl tests