NAME

Test::Differences - Test strings and data structures and show differences if not ok

SYNOPSIS

use Test;    ## Or use Test::More
use Test::Differences;

eq_or_diff $got,  "a\nb\nc\n",   "testing strings";
eq_or_diff \@got, [qw( a b c )], "testing arrays";

## Using with DBI-like data structures

use DBI;

... open connection & prepare statement and @expected_... here...

eq_or_diff $sth->fetchall_arrayref, \@expected_arrays  "testing DBI arrays";
eq_or_diff $sth->fetchall_hashref,  \@expected_hashes, "testing DBI hashes";

## To force textual or data line numbering (text lines are numbered 1..):
eq_or_diff_text ...;
eq_or_diff_data ...;

DESCRIPTION

When the code you're testing returns multiple lines or records and they're just plain wrong, sometimes an equivalent to the Unix diff utility is just what's needed. Here's output from an example test script that checks two text documents and then two (trivial) data structures:

t/99example....1..3
not ok 1 - differences in text
#     Failed test ((eval 2) at line 14)
#     +---+----------------+---+----------------+
#     | Ln|Got             | Ln|Expected        |
#     +---+----------------+---+----------------+
#     |  1|this is line 1  |  1|this is line 1  |
#     *  2|this is line 2  *  2|this is line b  *
#     |  3|this is line 3  |  3|this is line 3  |
#     +---+----------------+---+----------------+
not ok 2 - differences in whitespace
#     Failed test ((eval 2) at line 20)
#     +---+------------------+---+------------------+
#     | Ln|Got               | Ln|Expected          |
#     +---+------------------+---+------------------+
#     |  1|        indented  |  1|        indented  |
#     *  2|        indented  *  2|\tindented        *
#     |  3|        indented  |  3|        indented  |
#     +---+------------------+---+------------------+
not ok 3
#     Failed test ((eval 2) at line 22)
#     +----+-------------------------------------+----+----------------------------+
#     | Elt|Got                                  | Elt|Expected                    |
#     +----+-------------------------------------+----+----------------------------+
#     *   0|bless( [                             *   0|[                           *
#     *   1|  'Move along, nothing to see here'  *   1|  'Dry, humorless message'  *
#     *   2|], 'Test::Builder' )                 *   2|]                           *
#     +----+-------------------------------------+----+----------------------------+
# Looks like you failed 3 tests of 3.

eq_or_diff_...() compares two strings or (limited) data structures and either emits an ok indication or a side-by-side diff. Test::Differences is designed to be used with Test.pm and with Test::Simple, Test::More, and other Test::Builder based testing modules. As the SYNOPSIS shows, another testing module must be used as the basis for your test suite.

These functions assume that you are presenting it with "flat" records, looking like:

- scalars composed of record-per-line
- arrays of scalars,
- arrays of arrays of scalars,
- arrays of hashes containing only scalars

All of these are flattened in to single strings which are then compared for differences. Differently data structures can be compared, as long as they flatten identically.

All other data structures are run through Data::Dumper first. This is a bit dangerous, as some version of perl shipped with Data::Dumpers that could do the oddest things with some input. This will be changed to an internal dumper with good backward compatability when this bites somebody or I get some free time.

eq_or_diff() starts counting records at 0 unless you pass it two text strings:

eq_or_diff $a, $b;   ## First line is line number 1
eq_or_diff @a, @b;   ## First element is element 0
eq_or_diff $a, @b;   ## First line/element is element 0

If you want to force a first record number of 0, use eq_or_diff_data. If you want to force a first record number of 1, use eq_or_diff_text. I chose this over passing in an options hash because it's clearer and simpler this way. YMMV.

Deploying Test::Differences

There are three basic ways of deploying Test::Differences requiring more or less labor by you or your users.

  • eval "use Differences";

    This is the easiest option.

    If you want to detect the presence of Test::Differences on the fly, something like the following code might do the trick for you:

    use Test;
    
    eval "use Test::Differences";
    
    sub my_ok {
        goto &eq_or_diff if defined &eq_or_diff;
        goto &ok;
    }
    
    plan tests => 1;
    
    my_ok "a", "b";
  • PREREQ_PM => { .... "Test::Differences" => 0, ... }

    This method will let CPAN and CPANPLUS users download it automatically. It will discomfit those users who choose/have to download all packages manually.

  • t/lib/Test/Differences.pm, t/lib/Text/Diff.pm, ...

    By placing Test::Differences and it's prerequisites in the t/lib directory, you avoid forcing your users to download the Test::Differences manually if they aren't using CPAN or CPANPLUS.

    If you put a use lib "t/lib"; in the top of each test suite before the use Test::Differences;, make test should work well.

    You might want to check once in a while for new Test::Differences releases if you do this.

LIMITATIONS

This module "mixes in" with Test.pm or any of the test libraries based on Test::Builder (Test::Simple, Test::More, etc). It does this by peeking to see whether Test.pm or Test/Builder.pm is in %INC, so if you are not using one of those, it will print a warning and play dumb by not emitting test numbers (or incrementing them). If you are using one of these, it should interoperate nicely.

Uses Data::Dumper for complex data structures (like hashes :), which can lead to some problems on older perls.

Exports all 3 functions by default (and by design). Use

use Test::Differences ();

to suppress this behavior if you don't like the namespace pollution.

This module will not override functions like ok(), is(), is_deeply(), etc. If it did, then you could eval "use Test::Differences qw( is_deeply );" to get automatic upgrading to diffing behaviors without the sub my_ok shown above. Test::Differences intentionally does not provide this behavior because this would mean that Test::Differences would need to emulate every popular test module out there, which would require far more coding and maintenance that I'm willing to do. Use the eval and my_ok deployment shown above if you want some level of automation.

LIMITATIONS

Perls before 5.6.0 don't support characters > 255 at all, and 5.6.0 seems broken. This means that you might get odd results using perl5.6.0 with unicode strings.

AUTHOR

Barrie Slaymaker <barries@slaysys.com>

LICENSE

Copyright 2001 Barrie Slaymaker, All Rights Reserved.

You may use this software under the terms of the GNU public license, any version, or the Artistic license.