March 2013 Archives

What does this PHP print?

Via this blog entry which I'm sure some of you have seen before, the following PHP prints "horse".

<?php
  $arg = 'T';                                                                                                                
  $vehicle = ( ( $arg == 'B' ) ? 'bus' :
               ( $arg == 'A' ) ? 'airplane' :
               ( $arg == 'T' ) ? 'train' :
               ( $arg == 'C' ) ? 'car' :
               ( $arg == 'H' ) ? 'horse' :
               'feet' );
  echo $vehicle;
?>

No fun shooting fish (or a horse) in a barrel, but wow. Just wow.

Better reporting for your test suite

It's very poorly documented, but Test::Class::Moose also has reporting built in.

Here's how diag() all test classes from slowest to fastest, with time information:

use Test::Most;
use Test::Class::Moose::Load 't/lib';    # assumes your T:C:M tests are here

my $test_suite = Test::Class::Moose->new;
$test_suite->runtests;

my $report = $test_suite->test_reporting;

# sort from slowest to fastest
my @classes =
  sort { $b->time->real <=> $a->time->real } $report->all_test_classes;
foreach my $class (@classes) {
    my $name = $class->name;
    my $duration = $class->time->duration; # a human-readable version
    diag "$name runtime: $duration";                                                                                                       
}

You can do that at a test method level, too. Plus, the time is broken down into system, user, and real time.

Using Roles with Test::Class::Moose

Note: this post assumes you have Test::Class::Moose version 0.06 or higher (on its way to the CPAN now).

By now you may have heard of Test::Class::Moose. I wrote this to solve a need that many people have: they want the awesomeness of Test::Class, but the modern OO facilities of Moose.

Test::Class::Moose isn't for testing Moose classes, it's for testing anything you would have previously used Test::Class for, except that now you get Moosy (Moosee? Moosey?) goodness to go with it. I'll be attending YAPC::NA 2013 in Austin and I've pitched a Test::Class::Moose talk and, even if it doesn't get accepted, I figured I should at least write the slides. One of my most common uses cases (and and itch I always rescratch whenever I use Test::Class, Test::Class::Most or, now, Test::Class::Moose) caused one of my slides to have too much code.

So now it's fixed, released to the CPAN, and available for everyone to tell me it's "too magical" (a complaint I've heard in the past). Here's the problem and how I solved it.

Discoverable tests and creating testing standards

I like writing code. I like writing tests. I don't like:

  • Trying to figure out where the tests are
  • Writing boilerplate
  • Finding yet another package without tests

That last one is particularly vexing when you discover that your code is failing because another package doesn't load, but it doesn't have tests. So I fixed that.

Some of what follows is a repeat of things written in previous posts, but it's important enough that it bears repeating.

About Ovid

user-pic Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.