Dueling code wizardry is one of the things I love most about Perl
At least week's Perl Conference, Damian Conway talked about some new magical awesomeness he created, as he so frequently does. It's Test::Expr, and it makes it easier to write tests:
# Write this ... ... instead of this. ok $got eq $expected; is $got, $expected; ok $got ne $unexpected; isnt $got, $unexpected; ok $got == $expected; is_deeply $got, $expected; ok $got ~~ $expected; unlike $got, $pattern; ok $got =~ $pattern; like $got, $pattern; ok $got !~ $pattern; unlike $got, $pattern; ok $obj->isa($classname); is_ok $got, $classname; ok $obj->can($methodname); can_ok $obj, $methodname;
It also improves the diagnostics by showing the expression that failed.
# Failed test '$got eq $expected' # at t/synopsis.t line 13. # because: # $got --> "1.0" # $expected --> 1
Chad Granum, the maintainer of much of Perl's testing infrastructure took that last part as a challenge and overnight created his own magic in response: Test2::Plugin::SourceDiag.
use Test2::V0; use Test2::Plugin::SourceDiag; ok(0, "fail"); done_testing;
Produces the output:
not ok 1 - fail Failure source code: # ------------ # 4: ok(0, "fail"); # ------------ # Failed test 'fail' # at test.pl line 4.instead of:
not ok 1 - fail # Failed test 'fail' # at foo.t line 4.
This kind of dueling wizardry is one of the things that I love so much about Perl and its community.
Watch Chad's lightning talk: