Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Thursday July 24, 2008
03:03 AM

Altering &Test::Most::explain?

[ #37004 ]

In Test::Most, I have an "explain" function. This functions just like &Test::More::diag, except it only triggers in verbose mode. Thus, if you run an individual test to see its full output, you see the "explain" dialogue. When you run a full test suite, you don't. It's very handy to keep from having "messy" test suite output.

Now I'm thinking about rewriting it as follows:

sub explain {
    return unless $ENV{TEST_VERBOSE};
    if ( ref $_[0] ) {
        require Data::Dumper;
        local $Data::Dumper::Indent   = 1;
        local $Data::Dumper::Sortkeys = 1;
        Test::More::diag(Data::Dumper::Dumper(@_));
    }
    else {
        Test::More::diag(@_);
    }
}

In other words, you can forget about using Data::Dumper in your test suites just to diagnose bugs. You can do this:

my $self = Some::Object->new($id);
explain $self;

Since we rarely need to print a reference as a string, this seems right, but I wonder if I'm violating some principle of least surprise.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • .. but maybe you should even try to dump each ref in the argument list (untested):

    sub explain {
      return unless $ENV{TEST_VERBOSE};
      Test::More::diag(
        map{
          ref $_ ? do {
            require Data::Dumper;
            local $Data::Dumper::Indent   = 1;
         
  • I think it is such a good idea that I would plead for it to be moved into Test::More::diag. But as that is not likely, your idea is quite nifty og even nice.
  • After something like ten years of Perl, I still expect diag to dumper for me, so you won't be surprising me at all.

    --
    rjbs