Testing with Perl: awesome

Sometimes when I get close to the end of the day and it isn’t feasible for me to start on something new I expand on my current project’s test suite. Recently I worked on one of the (seemingly) more complex ones. Basically it tests one of our autocompleters to ensure that it will search for the name and also the public facing id of a certain field. The id part was easy.

The name part was significantly more complex, but not too bad really:

my @data = $schema->resultset('Customer')->
   autocomplete_search({
      query => 'ame'
   })->all;
cmp_deeply @data, all(
   array_each(
      methods(
         name => re(qr/.*ame.*/i)
      )
   )
), "Name matches query";

So basically what that does is ensure that all of the items in @data have a method that match the regex listed. It doesn’t care how many items are returned or any of the other details. Elegance!

Posted Thu, May 7, 2009

If you're interested in being notified when new posts are published, you can subscribe here; you'll get an email once a week at the most.