Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Friday April 09, 2004
03:50 PM

Today's handy testing trick

[ #18253 ]
Say you have a test that wants to compare two big multi-line text strings, like web pages. You can do this:

is( $wanted, $actual, "Pages match" );

but if they don't match, then you have mile-long "expected X, got Y" output. That's no fun.

So do this:

is_deeply( [split /\n/, $wanted], [split /\n/, $actual], "Pages match" );

Now, it'll show what the first different line is. Much easier to debug, because the output is like this:

not ok 13 - Pages match
#     Failed test (t/cached.t at line 54)
#     Structures begin differing at:
#          $got->[48] = '<td align="center"><b>15:46:02<br>'
#     $expected->[48] = '<td align="center"><b>15:45:58<br>'

In fact, I think I may have to submit a is_multiline function to Test::More that is a wrapper around it.

sub is_multiline {
    my $got = shift;
    my $expected = shift;
    my $msg = shift;

    return is_deeply( [split /\n/, $got], [split /\n/, $expected], $msg );

Schwern, whaddya think?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I had run into that problem myself when testing the contents of dynamically generated PDF files (before I turned on compression thus removing the plain text from the file). The failed is() spaghetti is even worse w/ PDF content. Ask me how I know. :-)
    "Perl users are the Greatful Dead fans of computer science." --slashdot comment
  • Would be what I'd use :-)