.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::Differences 3" .TH Test::Differences 3 2024-09-01 "perl v5.40.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Test::Differences \- Test strings and data structures and show differences if not ok .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Test; ## Or use Test::More \& use Test::Differences; \& \& eq_or_diff $got, "a\enb\enc\en", "testing strings"; \& eq_or_diff \e@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, \e@expected_arrays "testing DBI arrays"; \& eq_or_diff $sth\->fetchall_hashref, \e@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 ...; .Ve .SH EXPORT .IX Header "EXPORT" This module exports three test functions and four diff-style functions: .IP \(bu 4 Test functions .RS 4 .IP \(bu 4 \&\f(CW\*(C`eq_or_diff\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`eq_or_diff_data\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`eq_or_diff_text\*(C'\fR .RE .RS 4 .RE .IP \(bu 4 Diff style functions .RS 4 .IP \(bu 4 \&\f(CW\*(C`table_diff\*(C'\fR (the default) .IP \(bu 4 \&\f(CW\*(C`unified_diff\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`oldstyle_diff\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`context_diff\*(C'\fR .RE .RS 4 .RE .SH DESCRIPTION .IX Header "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 \&\f(CW\*(C`diff\*(C'\fR 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: .PP .Vb 10 \& 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 |\etindented * \& # | 3| indented | indented | \& # +\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& not ok 3 \& # Failed test ((eval 2) at line 22) \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # | Elt|Got |Expected | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # * 0|bless( [ |[ * \& # * 1| \*(AqMove along, nothing to see here\*(Aq | \*(AqDry, humorless message\*(Aq * \& # * 2|], \*(AqTest::Builder\*(Aq ) |] * \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # Looks like you failed 3 tests of 3. .Ve .PP 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. .SH OPTIONS .IX Header "OPTIONS" The options to \f(CW\*(C`eq_or_diff\*(C'\fR give some fine-grained control over the output. .IP \(bu 4 \&\f(CW\*(C`context\*(C'\fR .Sp This allows you to control the amount of context shown: .Sp .Vb 1 \& eq_or_diff $got, $expected, $name, { context => 50000 }; .Ve .Sp will show you lots and lots of context. Normally, \fBeq_or_diff()\fR uses some heuristics to determine whether to show 3 lines of context (like a normal unified diff) or 25 lines. .IP \(bu 4 \&\f(CW\*(C`data_type\*(C'\fR .Sp \&\f(CW\*(C`text\*(C'\fR or \f(CW\*(C`data\*(C'\fR. This normally defaults to \f(CW\*(C`data\*(C'\fR. If, however, neither of \&\f(CW$got\fR or \f(CW$expected\fR is a reference then it defaults to \f(CW\*(C`text\*(C'\fR. You can also force one or the other by calling \f(CW\*(C`eq_or_diff_text\*(C'\fR or \&\f(CW\*(C`eq_or_diff_data\*(C'\fR. .Sp The difference is that in text mode lines are numbered from 1, but in data mode from 0 (and are refered to as 'elements' (Elt) instead of lines): .Sp .Vb 5 \& # +\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ \& # | Ln|Got |Expected | \& # +\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ \& # * 1|\*(Aqfoo\*(Aq |\*(Aqbar\*(Aq * \& # +\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ \& \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ \& # | Elt|Got | Elt|Expected | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ \& # * 0|[ * 0|\*(Aqbar\*(Aq * \& # * 1| \*(Aqfoo\*(Aq * | | \& # * 2|] * | | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-+ .Ve .Sp The difference is purely cosmetic, it makes no difference to how comparisons are performed. .IP \(bu 4 \&\f(CW\*(C`Sortkeys\*(C'\fR .Sp If passed, whatever value is added is used as the argument for Data::Dumper Sortkeys option. See the Data::Dumper docs to understand how you can control the Sortkeys behavior. .IP \(bu 4 \&\f(CW\*(C`filename_a\*(C'\fR and \f(CW\*(C`filename_b\*(C'\fR .Sp The column headers to use in the output. They default to 'Got' and 'Expected'. .SH "DIFF STYLES" .IX Header "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 Text::Diff module and are set with the following functions: .IP \(bu 4 \&\f(CW\*(C`table_diff\*(C'\fR (the default) .IP \(bu 4 \&\f(CW\*(C`unified_diff\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`oldstyle_diff\*(C'\fR .IP \(bu 4 \&\f(CW\*(C`context_diff\*(C'\fR .PP You can run the following to understand the different diff output styles: .PP .Vb 2 \& use Test::More \*(Aqno_plan\*(Aq; \& use Test::Differences; \& \& my $long_string = join \*(Aq\*(Aq => 1..40; \& \& TODO: { \& local $TODO = \*(AqTesting diff styles\*(Aq; \& \& # 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", \*(Aqtable diff\*(Aq; \& \& unified_diff; \& eq_or_diff $long_string, "\-$long_string", \*(Aqunified diff\*(Aq; \& \& context_diff; \& eq_or_diff $long_string, "\-$long_string", \*(Aqcontext diff\*(Aq; \& \& oldstyle_diff; \& eq_or_diff $long_string, "\-$long_string", \*(Aqoldstyle diff\*(Aq; \& } .Ve .SH UNICODE .IX Header "UNICODE" Generally you'll find that the following test output is disappointing. .PP .Vb 2 \& use Test::Differences; \& use utf8; \& \& my $want = { \*(AqTraditional Chinese\*(Aq => \*(Aq中國\*(Aq }; \& my $have = { \*(AqTraditional Chinese\*(Aq => \*(Aq中国\*(Aq }; \& \& eq_or_diff $have, $want, \*(AqUnicode, baby\*(Aq; .Ve .PP Here's what you get: .PP .Vb 11 \& # Failed test \*(AqUnicode, baby\*(Aq \& # at t/unicode.t line 12. \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # | Elt|Got |Expected | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # | 0|\*(AqTraditional Chinese\*(Aq |\*(AqTraditional Chinese\*(Aq | \& # * 1|\*(Aq\ex{4e2d}\ex{56fd}\*(Aq |\*(Aq\ex{4e2d}\ex{570b}\*(Aq * \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # Looks like you failed 1 test of 1. \& Dubious, test returned 1 (wstat 256, 0x100) \& Failed 1/1 subtests .Ve .PP A patch to fix this would be *most* welcome. .SH Unknown::Values .IX Header "Unknown::Values" Unknown::Values is a module which provides values which will never compare as being the same as anything else, not even the same as itself. .PP If code looks too hard at one of these values (and Test::Differences looks very hard indeed) that is a fatal error. This means that while we can detect the presence of these beasties, and tell you that they compare different, for Complicated Internals Reasons we can't show you much context. Sorry. .PP NB that the support for these is experimental and relies on an undocumented unstable interface in Unknown::Values. If that fails then Test::Differences will \fIprobably\fR just die when it sees them instead of telling you that the comparison failed. .SH LIMITATIONS .IX Header "LIMITATIONS" .ie n .SS """Test"" or ""Test::More""" .el .SS "\f(CWTest\fP or \f(CWTest::More\fP" .IX Subsection "Test or 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 \f(CW%INC\fR, 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. .SS Exporting .IX Subsection "Exporting" Exports all 3 functions by default (and by design). Use .PP .Vb 1 \& use Test::Differences (); .Ve .PP to suppress this behavior if you don't like the namespace pollution. .PP This module will not override functions like \fBok()\fR, \fBis()\fR, \fBis_deeply()\fR, etc. If it did, then you could \f(CW\*(C`eval "use Test::Differences qw( is_deeply );"\*(C'\fR to get automatic upgrading to diffing behaviors without the \f(CW\*(C`sub my_ok\*(C'\fR 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. .SS Booleans .IX Subsection "Booleans" Historically, true and false (whether resulting from logical-not \f(CW\*(C`!\*(C'\fR or from equality checks like \f(CW\*(C`==\*(C'\fR) were represented in perl as numeric \f(CW1\fR for true and the empty string \f(CW\*(Aq\*(Aq\fR for false. In perl 5.36.0 they became real Boolean values which for backward compatibility also have their old numeric/string values. In 5.38 (and in dev releases from 5.37.2 onwards) Data::Dumper belatedly learned about this, and so Test::Differences (which uses Data::Dumper under the bonnet) can also tell the difference. .PP This means that `eq_or_diff(1, !!1)` will behave differently depending on whether you are using perl 5.38 or later (in which case it will say they are different) or 5.36 or earlier (which will say they are the same). .SS Unicode .IX Subsection "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. .ie n .SS """Data::Dumper"" and older Perls." .el .SS "\f(CWData::Dumper\fP and older Perls." .IX Subsection "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. \f(CW $Data::Dumper::Sortkeys \fR \fIis\fR 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 , 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: .PP .Vb 10 \& not ok 5 \& # Failed test (t/ctrl/05\-home.t at line 51) \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # | Elt|Got | Elt|Expected | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ \& # | 0|{ | 0|{ | \& # | 1| \*(Aqpassword\*(Aq => \*(Aq\*(Aq, | 1| \*(Aqpassword\*(Aq => \*(Aq\*(Aq, | \& # * 2| \*(Aqmethod\*(Aq => \*(Aqlogin\*(Aq, * | | \& # | 3| \*(Aqctrl\*(Aq => \*(Aqhome\*(Aq, | 2| \*(Aqctrl\*(Aq => \*(Aqhome\*(Aq, | \& # | | * 3| \*(Aqmethod\*(Aq => \*(Aqlogin\*(Aq, * \& # | 4| \*(Aqemail\*(Aq => \*(Aqtest\*(Aq | 4| \*(Aqemail\*(Aq => \*(Aqtest\*(Aq | \& # | 5|} | 5|} | \& # +\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+ .Ve .PP Data::Dumper also overlooks the difference between .PP .Vb 2 \& $a[0] = \e$a[1]; \& $a[1] = \e$a[0]; # $a[0] = \e$a[1] .Ve .PP and .PP .Vb 3 \& $x = \e$y; \& $y = \e$x; \& @a = ( $x, $y ); # $a[0] = \e$y, not \e$a[1] .Ve .PP The former involves two scalars, the latter 4: \f(CW$x\fR, \f(CW$y\fR, and \f(CW@a\fR[0,1]. This was carefully explained to me in words of two syllables or less by Yves Orton . The plan to address this is to allow you to select Data::Denter or some other module of your choice as an option. .SS Code-refs .IX Subsection "Code-refs" Test::Differences turns on \f(CW$Data::Dumper::Deparse\fR, so any code-refs in your data structures will be turned into text before they are examined, using B::Deparse. The precise text generated for a sub-ref might not be what you expect as it is generated from the compiled version of the code, but it should at least be consistent and spot differences correctly. .PP You can turn this behaviour off by setting \f(CW$Test::Differences::NoDeparse\fR. .SH AUTHORS .IX Header "AUTHORS" .Vb 1 \& Barrie Slaymaker \- original author \& \& Curtis "Ovid" Poe \& \& David Cantrell .Ve .SH LICENSE .IX Header "LICENSE" Copyright Barrie Slaymaker, Curtis "Ovid" Poe, and David Cantrell. .PP All Rights Reserved. .PP You may use, distribute and modify this software under the terms of the GNU public license, any version, or the Artistic license.