/usr/share/perl5/Test/Differences.pm is in libtest-differences-perl 0.64-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | package Test::Differences;
=encoding utf8
=head1 NAME
Test::Differences - Test strings and data structures and show differences if not ok
=head1 VERSION
0.62
=head1 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";
## Passing options:
eq_or_diff $got, $expected, $name, { context => 300 }; ## options
## 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 ...;
=head1 EXPORT
This module exports three test functions and four diff-style functions:
=over 4
=item * Test functions
=over 4
=item * C<eq_or_diff>
=item * C<eq_or_diff_data>
=item * C<eq_or_diff_text>
=back
=item * Diff style functions
=over 4
=item * C<table_diff> (the default)
=item * C<unified_diff>
=item * C<oldstyle_diff>
=item * C<context_diff>
=back
=back
=head1 DESCRIPTION
When the code you're testing returns multiple lines, records or data
structures and they're just plain wrong, an equivalent to the Unix
C<diff> utility may be 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 |Expected |
# +---+----------------+----------------+
# | 1|this is line 1 |this is line 1 |
# * 2|this is line 2 |this is line b *
# | 3|this is line 3 |this is line 3 |
# +---+----------------+----------------+
not ok 2 - differences in whitespace
# Failed test ((eval 2) at line 20)
# +---+------------------+------------------+
# | Ln|Got |Expected |
# +---+------------------+------------------+
# | 1| indented | indented |
# * 2| indented |\tindented *
# | 3| indented | indented |
# +---+------------------+------------------+
not ok 3
# Failed test ((eval 2) at line 22)
# +----+-------------------------------------+----------------------------+
# | Elt|Got |Expected |
# +----+-------------------------------------+----------------------------+
# * 0|bless( [ |[ *
# * 1| 'Move along, nothing to see here' | 'Dry, humorless message' *
# * 2|], 'Test::Builder' ) |] *
# +----+-------------------------------------+----------------------------+
# 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.
=head1 OPTIONS
The options to C<eq_or_diff> give some fine-grained control over the output.
=over 4
=item * C<context>
This allows you to control the amount of context shown:
eq_or_diff $got, $expected, $name, { context => 50000 };
will show you lots and lots of context. Normally, eq_or_diff() uses
some heuristics to determine whether to show 3 lines of context (like
a normal unified diff) or 25 lines.
=item * C<data_type>
C<text> or C<data>. See C<eq_or_diff_text> and C<eq_or_diff_data> to
understand this. You can usually ignore this.
=item * C<Sortkeys>
If passed, whatever value is added is used as the argument for L<Data::Dumper>
Sortkeys option. See the L<Data::Dumper> docs to understand how you can
control the Sortkeys behavior.
=item * C<filename_a> and C<filename_b>
The column headers to use in the output. They default to 'Got' and 'Expected'.
=back
=head1 DIFF STYLES
For extremely long strings, a table diff can wrap on your screen and be hard
to read. If you are comfortable with different diff formats, you can switch
to a format more suitable for your data. These are the four formats supported
by the L<Text::Diff> module and are set with the following functions:
=over 4
=item * C<table_diff> (the default)
=item * C<unified_diff>
=item * C<oldstyle_diff>
=item * C<context_diff>
=back
You can run the following to understand the different diff output styles:
use Test::More 'no_plan';
use Test::Differences;
my $long_string = join '' => 1..40;
TODO: {
local $TODO = 'Testing diff styles';
# this is the default and does not need to explicitly set unless you need
# to reset it back from another diff type
table_diff;
eq_or_diff $long_string, "-$long_string", 'table diff';
unified_diff;
eq_or_diff $long_string, "-$long_string", 'unified diff';
context_diff;
eq_or_diff $long_string, "-$long_string", 'context diff';
oldstyle_diff;
eq_or_diff $long_string, "-$long_string", 'oldstyle diff';
}
=head1 UNICODE
Generally you'll find that the following test output is disappointing.
use Test::Differences;
my $want = { 'Traditional Chinese' => '中國' };
my $have = { 'Traditional Chinese' => '中国' };
eq_or_diff $have, $want, 'Unicode, baby';
The output looks like this:
# Failed test 'Unicode, baby'
# at t/unicode.t line 12.
# +----+----------------------------+----------------------------+
# | Elt|Got |Expected |
# +----+----------------------------+----------------------------+
# | 0|'Traditional Chinese' |'Traditional Chinese' |
# * 1|'\xe4\xb8\xad\xe5\x9b\xbd' |'\xe4\xb8\xad\xe5\x9c\x8b' *
# +----+----------------------------+----------------------------+
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
This is generally not helpful and someone points out that you didn't declare
your test program as being utf8, so you do that:
use Test::Differences;
use utf8;
my $want = { 'Traditional Chinese' => '中國' };
my $have = { 'Traditional Chinese' => '中国' };
eq_or_diff $have, $want, 'Unicode, baby';
Here's what you get:
# Failed test 'Unicode, baby'
# at t/unicode.t line 12.
# +----+-----------------------+-----------------------+
# | Elt|Got |Expected |
# +----+-----------------------+-----------------------+
# | 0|'Traditional Chinese' |'Traditional Chinese' |
# * 1|'\x{4e2d}\x{56fd}' |'\x{4e2d}\x{570b}' *
# +----+-----------------------+-----------------------+
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests
That's better, but still awful. However, if you have C<Text::Diff> 0.40 or
higher installed, you can add this to your code:
BEGIN { $ENV{DIFF_OUTPUT_UNICODE} = 1 }
Make sure you do this I<before> you load L<Text::Diff>. Then this is the output:
# +----+-----------------------+-----------------------+
# | Elt|Got |Expected |
# +----+-----------------------+-----------------------+
# | 0|'Traditional Chinese' |'Traditional Chinese' |
# * 1|'中国' |'中國' *
# +----+-----------------------+-----------------------+
=head1 DEPLOYING
There are several basic ways of deploying Test::Differences requiring more or less
labor by you or your users.
=over
=item *
Fallback to C<is_deeply>.
This is your best option if you want this module to be optional.
use Test::More;
BEGIN {
if (!eval q{ use Test::Differences; 1 }) {
*eq_or_diff = \&is_deeply;
}
}
=item *
eval "use Test::Differences";
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 qw( !ok ); ## get all syms *except* ok
eval "use Test::Differences";
use Data::Dumper;
sub ok {
goto &eq_or_diff if defined &eq_or_diff && @_ > 1;
@_ = map ref $_ ? Dumper( @_ ) : $_, @_;
goto Test::&ok;
}
plan tests => 1;
ok "a", "b";
=item *
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.
=item *
t/lib/Test/Differences.pm, t/lib/Text/Diff.pm, ...
By placing Test::Differences and its 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 C<use lib "t/lib";> in the top of each test suite before the
C<use Test::Differences;>, C<make test> should work well.
You might want to check once in a while for new Test::Differences releases
if you do this.
=back
=cut
our $VERSION = "0.64"; # or "0.001_001" for a dev release
$VERSION = eval $VERSION;
use Exporter;
@ISA = qw( Exporter );
@EXPORT = qw(
eq_or_diff
eq_or_diff_text
eq_or_diff_data
unified_diff
context_diff
oldstyle_diff
table_diff
);
use strict;
use Carp;
use Text::Diff;
use Data::Dumper;
{
my $diff_style = 'Table';
my %allowed_style = map { $_ => 1 } qw/Unified Context OldStyle Table/;
sub _diff_style {
return $diff_style unless @_;
my $requested_style = shift;
unless ( $allowed_style{$requested_style} ) {
Carp::croak("Uknown style ($requested_style) requested for diff");
}
$diff_style = $requested_style;
}
}
sub unified_diff { _diff_style('Unified') }
sub context_diff { _diff_style('Context') }
sub oldstyle_diff { _diff_style('OldStyle') }
sub table_diff { _diff_style('Table') }
sub _identify_callers_test_package_of_choice {
## This is called at each test in case Test::Differences was used before
## the base testing modules.
## First see if %INC tells us much of interest.
my $has_builder_pm = grep $_ eq "Test/Builder.pm", keys %INC;
my $has_test_pm = grep $_ eq "Test.pm", keys %INC;
return "Test" if $has_test_pm && !$has_builder_pm;
return "Test::Builder" if !$has_test_pm && $has_builder_pm;
if ( $has_test_pm && $has_builder_pm ) {
## TODO: Look in caller's namespace for hints. For now, assume Builder.
## This should only ever be an issue if multiple test suites end
## up in memory at once.
return "Test::Builder";
}
}
my $warned_of_unknown_test_lib;
sub eq_or_diff_text { $_[3] = { data_type => "text" }; goto &eq_or_diff; }
sub eq_or_diff_data { $_[3] = { data_type => "data" }; goto &eq_or_diff; }
## This string is a cheat: it's used to see if the two arrays of values
## are identical. The stringified values are joined using this joint
## and compared using eq. This is a deep equality comparison for
## references and a shallow one for scalars.
my $joint = chr(0) . "A" . chr(1);
sub eq_or_diff {
my ( @vals, $name, $options );
$options = pop if @_ > 2 && ref $_[-1];
( $vals[0], $vals[1], $name ) = @_;
my($data_type, $filename_a, $filename_b);
if($options) {
$data_type = $options->{data_type};
$filename_a = $options->{filename_a};
$filename_b = $options->{filename_b};
}
$data_type ||= "text" unless ref $vals[0] || ref $vals[1];
$data_type ||= "data";
$filename_a ||= 'Got';
$filename_b ||= 'Expected';
my @widths;
local $Data::Dumper::Indent = 1;
local $Data::Dumper::Purity = 0;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Deepcopy = 1;
local $Data::Dumper::Quotekeys = 0;
local $Data::Dumper::Useperl = 1;
local $Data::Dumper::Sortkeys =
exists $options->{Sortkeys} ? $options->{Sortkeys} : 1;
my ( $got, $expected ) = map
[ split /^/, Data::Dumper::Dumper($_) ],
@vals;
my $caller = caller;
my $passed
= join( $joint, @$got ) eq join( $joint, @$expected );
my $diff;
unless ($passed) {
my $context;
$context = $options->{context}
if exists $options->{context};
$context = 2**31 unless defined $context;
confess "context must be an integer: '$context'\n"
unless $context =~ /\A\d+\z/;
$diff = diff $got, $expected,
{ CONTEXT => $context,
STYLE => _diff_style(),
FILENAME_A => $filename_a,
FILENAME_B => $filename_b,
OFFSET_A => $data_type eq "text" ? 1 : 0,
OFFSET_B => $data_type eq "text" ? 1 : 0,
INDEX_LABEL => $data_type eq "text" ? "Ln" : "Elt",
};
chomp $diff;
$diff .= "\n";
}
my $which = _identify_callers_test_package_of_choice;
if ( $which eq "Test" ) {
@_
= $passed
? ( "", "", $name )
: ( "\n$diff", "No differences", $name );
goto &Test::ok;
}
elsif ( $which eq "Test::Builder" ) {
my $test = Test::Builder->new;
## TODO: Call exported_to here? May not need to because the caller
## should have imported something based on Test::Builder already.
$test->ok( $passed, $name );
$test->diag($diff) unless $passed;
}
else {
unless ($warned_of_unknown_test_lib) {
Carp::cluck
"Can't identify test lib in use, doesn't seem to be Test.pm or Test::Builder based\n";
$warned_of_unknown_test_lib = 1;
}
## Play dumb and hope nobody notices the fool drooling in the corner
if ($passed) {
print "ok\n";
}
else {
$diff =~ s/^/# /gm;
print "not ok\n", $diff;
}
}
}
=head1 LIMITATIONS
=head2 C<Test> or C<Test::More>
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.
=head2 Exporting
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 C<eval "use Test::Differences qw( is_deeply );"> to get
automatic upgrading to diffing behaviors without the C<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.
=head2 Unicode
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.
=head2 C<Data::Dumper> and older Perls.
Relies on Data::Dumper (for now), which, prior to perl5.8, will not always
report hashes in the same order. C< $Data::Dumper::Sortkeys > I<is> set to 1,
so on more recent versions of Data::Dumper, this should not occur. Check CPAN
to see if it's been peeled out of the main perl distribution and backported.
Reported by Ilya Martynov <ilya@martynov.org>, although the Sortkeys "future
perfect" workaround has been set in anticipation of a new Data::Dumper for a
while. Note that the two hashes should report the same here:
not ok 5
# Failed test (t/ctrl/05-home.t at line 51)
# +----+------------------------+----+------------------------+
# | Elt|Got | Elt|Expected |
# +----+------------------------+----+------------------------+
# | 0|{ | 0|{ |
# | 1| 'password' => '', | 1| 'password' => '', |
# * 2| 'method' => 'login', * | |
# | 3| 'ctrl' => 'home', | 2| 'ctrl' => 'home', |
# | | * 3| 'method' => 'login', *
# | 4| 'email' => 'test' | 4| 'email' => 'test' |
# | 5|} | 5|} |
# +----+------------------------+----+------------------------+
Data::Dumper also overlooks the difference between
$a[0] = \$a[1];
$a[1] = \$a[0]; # $a[0] = \$a[1]
and
$x = \$y;
$y = \$x;
@a = ( $x, $y ); # $a[0] = \$y, not \$a[1]
The former involves two scalars, the latter 4: $x, $y, and @a[0,1].
This was carefully explained to me in words of two syllables or less by
Yves Orton <demerphq@hotmail.com>. The plan to address this is to allow
you to select Data::Denter or some other module of your choice as an
option.
=head1 AUTHORS
Barrie Slaymaker <barries@slaysys.com> - original author
Curtis "Ovid" Poe <ovid@cpan.org>
David Cantrell <david@cantrell.org.uk>
=head1 LICENSE
Copyright 2001-2008 Barrie Slaymaker, All Rights Reserved.
You may use this software under the terms of the GNU public license, any
version, or the Artistic license.
=cut
1;
|