blib/lib/Test/Differences.pm

filter: Statementstmt 84.5% 120 / 142 Branchbran 69.5% 57 / 82 Conditioncond 68.7% 22 / 32 MC/DCmcdc 57.8% 11 / 19 Subroutinesub 95.6% 22 / 23 Podpod 100% 7 / 7 totaltotal 78.3% 239 / 305 CC 103 SCAR 32.8
CC
103
Cov
78%
eq_or_diff
44.5
eq_or_diff
44.5
_identify_callers_test_package_of_choice
18.6
CRAP 26.7
↑ Summary
1 package Test::Differences;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Test::Differences - Test strings and data structures and show differences if not ok
8  
9 =head1 SYNOPSIS
10  
11 use Test; ## Or use Test::More
12 use Test::Differences;
13  
14 eq_or_diff $got, "a\nb\nc\n", "testing strings";
15 eq_or_diff \@got, [qw( a b c )], "testing arrays";
16  
17 ## Passing options:
18 eq_or_diff $got, $expected, $name, { context => 300 }; ## options
19  
20 ## Using with DBI-like data structures
21  
22 use DBI;
23  
24 ... open connection & prepare statement and @expected_... here...
25  
26 eq_or_diff $sth->fetchall_arrayref, \@expected_arrays "testing DBI arrays";
27 eq_or_diff $sth->fetchall_hashref, \@expected_hashes, "testing DBI hashes";
28  
29 ## To force textual or data line numbering (text lines are numbered 1..):
30 eq_or_diff_text ...;
31 eq_or_diff_data ...;
32  
33 =head1 EXPORT
34  
35 This module exports three test functions and four diff-style functions:
36  
37 =over 4
38  
39 =item * Test functions
40  
41 =over 4
42  
43 =item * C<eq_or_diff>
44  
45 =item * C<eq_or_diff_data>
46  
47 =item * C<eq_or_diff_text>
48  
49 =back
50  
51 =item * Diff style functions
52  
53 =over 4
54  
55 =item * C<table_diff> (the default)
56  
57 =item * C<unified_diff>
58  
59 =item * C<oldstyle_diff>
60  
61 =item * C<context_diff>
62  
63 =back
64  
65 =back
66  
67 =head1 DESCRIPTION
68  
69 When the code you're testing returns multiple lines, records or data
70 structures and they're just plain wrong, an equivalent to the Unix
71 C<diff> utility may be just what's needed. Here's output from an
72 example test script that checks two text documents and then two
73 (trivial) data structures:
74  
75 t/99example....1..3
76 not ok 1 - differences in text
77 # Failed test ((eval 2) at line 14)
78 # +---+----------------+----------------+
79 # | Ln|Got |Expected |
80 # +---+----------------+----------------+
81 # | 1|this is line 1 |this is line 1 |
82 # * 2|this is line 2 |this is line b *
83 # | 3|this is line 3 |this is line 3 |
84 # +---+----------------+----------------+
85 not ok 2 - differences in whitespace
86 # Failed test ((eval 2) at line 20)
87 # +---+------------------+------------------+
88 # | Ln|Got |Expected |
89 # +---+------------------+------------------+
90 # | 1| indented | indented |
91 # * 2| indented |\tindented *
92 # | 3| indented | indented |
93 # +---+------------------+------------------+
94 not ok 3
95 # Failed test ((eval 2) at line 22)
96 # +----+-------------------------------------+----------------------------+
97 # | Elt|Got |Expected |
98 # +----+-------------------------------------+----------------------------+
99 # * 0|bless( [ |[ *
100 # * 1| 'Move along, nothing to see here' | 'Dry, humorless message' *
101 # * 2|], 'Test::Builder' ) |] *
102 # +----+-------------------------------------+----------------------------+
103 # Looks like you failed 3 tests of 3.
104  
105 eq_or_diff_...() compares two strings or (limited) data structures and
106 either emits an ok indication or a side-by-side diff. Test::Differences
107 is designed to be used with Test.pm and with Test::Simple, Test::More,
108 and other Test::Builder based testing modules. As the SYNOPSIS shows,
109 another testing module must be used as the basis for your test suite.
110  
111 =head1 OPTIONS
112  
113 The options to C<eq_or_diff> give some fine-grained control over the output.
114  
115 =over 4
116  
117 =item * C<context>
118  
119 This allows you to control the amount of context shown:
120  
121 eq_or_diff $got, $expected, $name, { context => 50000 };
122  
123 will show you lots and lots of context. Normally, eq_or_diff() uses
124 some heuristics to determine whether to show 3 lines of context (like
125 a normal unified diff) or 25 lines.
126  
127 =item * C<data_type>
128  
129 C<text> or C<data>. This normally defaults to C<data>. If, however, neither of
130 C<$got> or C<$expected> is a reference then it defaults to C<text>. You can
131 also force one or the other by calling C<eq_or_diff_text> or
132 C<eq_or_diff_data>.
133  
134 The difference is that in text mode lines are numbered from 1, but in data mode
135 from 0 (and are refered to as 'elements' (Elt) instead of lines):
136  
137 # +---+-------+----------+
138 # | Ln|Got |Expected |
139 # +---+-------+----------+
140 # * 1|'foo' |'bar' *
141 # +---+-------+----------+
142  
143 # +----+---------+----+----------+
144 # | Elt|Got | Elt|Expected |
145 # +----+---------+----+----------+
146 # * 0|[ * 0|'bar' *
147 # * 1| 'foo' * | |
148 # * 2|] * | |
149 # +----+---------+----+----------+
150  
151 The difference is purely cosmetic, it makes no difference to how comparisons
152 are performed.
153  
154 =item * C<Sortkeys>
155  
156 If passed, whatever value is added is used as the argument for L<Data::Dumper>
157 Sortkeys option. See the L<Data::Dumper> docs to understand how you can
158 control the Sortkeys behavior.
159  
160 =item * C<filename_a> and C<filename_b>
161  
162 The column headers to use in the output. They default to 'Got' and 'Expected'.
163  
164 =back
165  
166 =head1 DIFF STYLES
167  
168 For extremely long strings, a table diff can wrap on your screen and be hard
169 to read. If you are comfortable with different diff formats, you can switch
170 to a format more suitable for your data. These are the four formats supported
171 by the L<Text::Diff> module and are set with the following functions:
172  
173 =over 4
174  
175 =item * C<table_diff> (the default)
176  
177 =item * C<unified_diff>
178  
179 =item * C<oldstyle_diff>
180  
181 =item * C<context_diff>
182  
183 =back
184  
185 You can run the following to understand the different diff output styles:
186  
187 use Test::More 'no_plan';
188 use Test::Differences;
189  
190 my $long_string = join '' => 1..40;
191  
192 TODO: {
193 local $TODO = 'Testing diff styles';
194  
195 # this is the default and does not need to explicitly set unless you need
196 # to reset it back from another diff type
197 table_diff;
198 eq_or_diff $long_string, "-$long_string", 'table diff';
199  
200 unified_diff;
201 eq_or_diff $long_string, "-$long_string", 'unified diff';
202  
203 context_diff;
204 eq_or_diff $long_string, "-$long_string", 'context diff';
205  
206 oldstyle_diff;
207 eq_or_diff $long_string, "-$long_string", 'oldstyle diff';
208 }
209  
210 =head1 UNICODE
211  
212 Generally you'll find that the following test output is disappointing.
213  
214 use Test::Differences;
215 use utf8;
216  
217 my $want = { 'Traditional Chinese' => '中國' };
218 my $have = { 'Traditional Chinese' => '中国' };
219  
220 eq_or_diff $have, $want, 'Unicode, baby';
221  
222 Here's what you get:
223  
224 # Failed test 'Unicode, baby'
225 # at t/unicode.t line 12.
226 # +----+-----------------------+-----------------------+
227 # | Elt|Got |Expected |
228 # +----+-----------------------+-----------------------+
229 # | 0|'Traditional Chinese' |'Traditional Chinese' |
230 # * 1|'\x{4e2d}\x{56fd}' |'\x{4e2d}\x{570b}' *
231 # +----+-----------------------+-----------------------+
232 # Looks like you failed 1 test of 1.
233 Dubious, test returned 1 (wstat 256, 0x100)
234 Failed 1/1 subtests
235  
236 A patch to fix this would be *most* welcome.
237  
238 =head1 Unknown::Values
239  
240 L<Unknown::Values> is a module which provides values which will never compare as being
241 the same as anything else, not even the same as itself.
242  
243 If code looks too hard at one of these values (and Test::Differences looks very hard indeed)
244 that is a fatal error. This means that while we can detect the presence of these beasties,
245 and tell you that they compare different, for Complicated Internals Reasons we can't show you
246 much context. Sorry.
247  
248 NB that the support for these is experimental and relies on an undocumented unstable
249 interface in Unknown::Values. If that fails then Test::Differences will I<probably> just die
250 when it sees them instead of telling you that the comparison failed.
251  
252 =cut
253  
254 15 our $VERSION = "0.73"; # or "0.001_001" for a dev release
255 15 $VERSION = eval $VERSION;
256  
257 15 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 15 @ISA = qw( Exporter );
260 15 @EXPORT = qw(
261 eq_or_diff
262 eq_or_diff_text
263 eq_or_diff_data
264 unified_diff
265 context_diff
266 oldstyle_diff
267 table_diff
268 );
269  
270 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 15 ▶use Text::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 {
278 15 my $diff_style = 'Table';
279 15 my %allowed_style = map { $_ => 1 } qw/Unified Context OldStyle Table/;
280 33 ▶ sub _diff_style {
Subroutine _diff_style: called CC=3 SCAR=16.2
281 0 ▶ return $diff_style unless @_;
Subroutine _diff_style: not called CC=3 SCAR=16.2
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 29 4 33
unless @_ 0 0 0
282 0 my $requested_style = shift;
283 0 ▶ unless ( $allowed_style{$requested_style} ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($allowed_style{$requested_style}) 0 4 4
unless ($allowed_style{$requested_style}) 0 0 0
284 0 Carp::croak("Uknown style ($requested_style) requested for diff");
285 }
286 0 $diff_style = $requested_style;
287 }
288 }
289  
290 1 ▶sub unified_diff { _diff_style('Unified') }
Subroutine unified_diff: called CC=1 SCAR=0.0
Pod documented
291 1 ▶sub context_diff { _diff_style('Context') }
Subroutine context_diff: called CC=1 SCAR=0.0
Pod documented
292 1 ▶sub oldstyle_diff { _diff_style('OldStyle') }
Subroutine oldstyle_diff: called CC=1 SCAR=0.0
Pod documented
293 1 ▶sub table_diff { _diff_style('Table') }
Subroutine table_diff: called CC=1 SCAR=0.0
Pod documented
294  
295 37 ▶sub _identify_callers_test_package_of_choice {
Subroutine _identify_callers_test_package_of_choice: called CC=7 SCAR=29.2
296 ## This is called at each test in case Test::Differences was used before
297 ## the base testing modules.
298 ## First see if %INC tells us much of interest.
299 37 my $has_builder_pm = grep $_ eq "Test/Builder.pm", keys %INC;
300 37 my $has_test_pm = grep $_ eq "Test.pm", keys %INC;
301  
302 37 ▶ return "Test" if $has_test_pm && !$has_builder_pm;
Branch: 1 branch
BranchTrueFalseTotal
if $has_test_pm and not $has_builder_pm 0 37 37
Condition33%1/3
$has_test_pm and not $has_builder_pm
!ll&&!rl&&r
3700
MC/DC0%0/2
$has_test_pm and not $has_builder_pm
$has_test_pm not $has_builder_pm
Truth table33%1/3
A and not(B)
A= $has_test_pmB= not $has_builder_pm
ABresult
0X0
100
111
303 37 ▶ return "Test::Builder" if !$has_test_pm && $has_builder_pm;
Branch: 1 branch
BranchTrueFalseTotal
if not $has_test_pm and $has_builder_pm 37 0 37
Condition33%1/3
not $has_test_pm and $has_builder_pm
!ll&&!rl&&r
0037
MC/DC0%0/2
not $has_test_pm and $has_builder_pm
not $has_test_pm $has_builder_pm
Truth table33%1/3
not(A) and B
A= not $has_test_pmB= $has_builder_pm
ABresult
0X0
100
111
304  
305 0 ▶ if ( $has_test_pm && $has_builder_pm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_test_pm and $has_builder_pm) 0 0 0
Condition0%0/3
$has_test_pm and $has_builder_pm
!ll&&!rl&&r
000
MC/DC0%0/2
$has_test_pm and $has_builder_pm
$has_test_pm $has_builder_pm
Truth table0%0/3
A and B
A= $has_test_pmB= $has_builder_pm
ABresult
0X0
100
111
306 ## TODO: Look in caller's namespace for hints. For now, assume Builder.
307 ## This should only ever be an issue if multiple test suites end
308 ## up in memory at once.
309 0 return "Test::Builder";
310 }
311 }
312  
313 15 my $warned_of_unknown_test_lib;
314  
315 1 ▶sub eq_or_diff_text { $_[3] = { data_type => "text" }; goto &eq_or_diff; }
Subroutine eq_or_diff_text: called CC=1 SCAR=0.0
Pod documented
316 1 ▶sub eq_or_diff_data { $_[3] = { data_type => "data" }; goto &eq_or_diff; }
Subroutine eq_or_diff_data: called CC=1 SCAR=0.0
Pod documented
317  
318 ## This string is a cheat: it's used to see if the two arrays of values
319 ## are identical. The stringified values are joined using this joint
320 ## and compared using eq. This is a deep equality comparison for
321 ## references and a shallow one for scalars.
322 15 my $joint = chr(0) . "A" . chr(1);
323  
324 47 ▶sub _isnt_ARRAY_of_scalars {
Subroutine _isnt_ARRAY_of_scalars: called CC=2 SCAR=6.9
325 47 ▶ return 1 if ref ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ ne "ARRAY" 14 33 47
326 33 return scalar grep ref, @$_;
327 }
328  
329 26 ▶sub _isnt_HASH_of_scalars {
Subroutine _isnt_HASH_of_scalars: called CC=2 SCAR=6.9
330 26 ▶ return 1 if ref ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ ne "HASH" 12 14 26
331 14 return scalar grep ref, values %$_;
332 }
333  
334 15 ▶use constant ARRAY_of_scalars => "ARRAY of scalars";
Subroutine BEGIN: called CC=1 SCAR=0.0
335 15 ▶use constant ARRAY_of_ARRAYs_of_scalars => "ARRAY of ARRAYs of scalars";
Subroutine BEGIN: called CC=1 SCAR=0.0
336 15 ▶use constant ARRAY_of_HASHes_of_scalars => "ARRAY of HASHes of scalars";
Subroutine BEGIN: called CC=1 SCAR=0.0
337 15 ▶use constant HASH_of_scalars => "HASH of scalars";
Subroutine BEGIN: called CC=1 SCAR=0.0
338  
339 74 ▶sub _grok_type {
Subroutine _grok_type: called CC=10 SCAR=23.3
340 74 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 74 0 74
341 74 ▶ return "SCALAR" unless ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 37 37 74
342 37 ▶ if ( ref eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 24 13 37
elsif (ref $_ eq 'HASH') { } 10 3 13
343 24 ▶ return undef unless @$_;
Branch: 1 branch
BranchTrueFalseTotal
unless @$_ 1 23 24
344 23 ▶ return ARRAY_of_scalars
Branch: 1 branch
BranchTrueFalseTotal
unless _isnt_ARRAY_of_scalars() 11 12 23
345 unless _isnt_ARRAY_of_scalars;
346 12 ▶ return ARRAY_of_ARRAYs_of_scalars
Branch: 1 branch
BranchTrueFalseTotal
unless grep _isnt_ARRAY_of_scalars(), @$_ 4 8 12
347 unless grep _isnt_ARRAY_of_scalars, @$_;
348 8 ▶ return ARRAY_of_HASHes_of_scalars
Branch: 1 branch
BranchTrueFalseTotal
unless grep _isnt_HASH_of_scalars(), @$_ 0 8 8
349 unless grep _isnt_HASH_of_scalars, @$_;
350 8 return 0;
351 }
352 elsif ( ref eq 'HASH' ) {
353 10 ▶ return HASH_of_scalars
Branch: 1 branch
BranchTrueFalseTotal
unless _isnt_HASH_of_scalars($_) 10 0 10
354 unless _isnt_HASH_of_scalars($_);
355 0 return 0;
356 }
357 }
358  
359 37 ▶sub eq_or_diff {
Subroutine eq_or_diff: called CC=33 SCAR=38.0
Pod documented
360 37 my ( @vals, $name, $options );
361 37 ▶ $options = pop if @_ > 2 && ref $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 and ref $_[-1] 5 32 37
Condition100%3/3
@_ > 2 and ref $_[-1]
!ll&&!rl&&r
10225
MC/DC100%2/2
@_ > 2 and ref $_[-1]
@_ > 2 ref $_[-1]
Truth table100%3/3
A and B
A= @_ > 2B= ref $_[-1]
ABresult
0X0
100
111
362 37 ( $vals[0], $vals[1], $name ) = @_;
363  
364 37 my @types = map { _grok_type($_) } @vals;
365 37 ▶ my $dump_it = !$types[0] || !$types[1];
Condition100%3/3
$types[0] && $types[1]
!ll&&!rl&&r
6130
MC/DC100%2/2
$types[0] && $types[1]
$types[0] $types[1]
Truth table100%3/3
A && B
A= $types[0]B= $types[1]
ABresult
0X0
100
111
366  
367 37 my($data_type, $filename_a, $filename_b);
368 37 ▶ if($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 5 32 37
369 5 $data_type = $options->{data_type};
370 5 $filename_a = $options->{filename_a};
371 5 $filename_b = $options->{filename_b};
372 }
373 37 ▶ $data_type ||= "text" unless ref $vals[0] || ref $vals[1];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vals[0] or ref $vals[1] 18 19 37
Condition100%5/5
ref $vals[0] or ref $vals[1]
l!l&&r!l&&!r
18118
$data_type ||= "text"
l!l
216
MC/DC100%2/2
ref $vals[0] or ref $vals[1]
ref $vals[0] ref $vals[1]
MC/DC100%1/1
$data_type ||= "text"
$data_type
Truth table100%3/3
A or B
A= ref $vals[0]B= ref $vals[1]
ABresult
1X1
011
000
Truth table100%2/2
A
A= $data_type
Aresult
11
00
374 37 ▶ $data_type ||= "data";
Condition100%2/2
$data_type ||= "data"
l!l
1819
MC/DC100%1/1
$data_type ||= "data"
$data_type
Truth table100%2/2
A
A= $data_type
Aresult
11
00
375  
376 37 ▶ $filename_a ||= 'Got';
Condition100%2/2
$filename_a ||= "Got"
l!l
136
MC/DC100%1/1
$filename_a ||= "Got"
$filename_a
Truth table100%2/2
A
A= $filename_a
Aresult
11
00
377 37 ▶ $filename_b ||= 'Expected';
Condition100%2/2
$filename_b ||= "Expected"
l!l
136
MC/DC100%1/1
$filename_b ||= "Expected"
$filename_b
Truth table100%2/2
A
A= $filename_b
Aresult
11
00
378  
379 37 my @widths;
380  
381 37 ▶ local $Data::Dumper::Deparse = 1
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Differences::NoDeparse 36 1 37
382 unless($Test::Differences::NoDeparse);
383 37 local $Data::Dumper::Indent = 1;
384 37 local $Data::Dumper::Purity = 0;
385 37 local $Data::Dumper::Terse = 1;
386 37 local $Data::Dumper::Deepcopy = 1;
387 37 local $Data::Dumper::Quotekeys = 0;
388 37 local $Data::Dumper::Useperl = 1;
389 local $Data::Dumper::Sortkeys =
390 37 ▶ exists $options->{Sortkeys} ? $options->{Sortkeys} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'Sortkeys'} ? : 0 37 37
391  
392 37 my $unknown_value_in_got;
393 my $unknown_value_in_expected;
394 37 my @unknown_flags = (\$unknown_value_in_got, \$unknown_value_in_expected);
395  
396 my($got, $expected) = map {
397 37 my $t = eval { [ split /^/, Data::Dumper::Dumper($_) ] };
398  
399 74 my $unknown_flag = shift(@unknown_flags);
400 74 ▶ if($@ =~ /^Dereferencing cannot be performed on unknown values at .*Unknown.Values.Instance/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Dereferencing cannot be performed on unknown values at .*Unknown.Values.Instance/) 0 74 74
401 0 ${$unknown_flag} = 1;
402 }
403  
404 74 $t;
405 } @vals;
406  
407 37 my $caller = caller;
408  
409 37 ▶ my $passed =
Condition50%3/6
!(defined $unknown_value_in_got || defined $unknown_value_in_expected) && join($joint, @$got) eq join($joint, @$expected)
!ll&&!rl&&r
0298
defined $unknown_value_in_got || defined $unknown_value_in_expected
l!l&&r!l&&!r
0037
MC/DC33%1/3
!(defined $unknown_value_in_got || defined $unknown_value_in_expected) && join($joint, @$got) eq join($joint, @$expected)
defined $unknown_value_in_got defined $unknown_value_in_expected join($joint, @$got) eq join($joint, @$expected)
Truth table50%2/4
not(A || B) && C
A= defined $unknown_value_in_gotB= defined $unknown_value_in_expectedC= join($joint, @$got) eq join($joint, @$expected)
ABCresult
1XX0
01X0
0000
0011
410 !defined($unknown_value_in_got) &&
411 !defined($unknown_value_in_expected) &&
412 join( $joint, @$got ) eq join( $joint, @$expected );
413  
414 37 my $diff;
415 37 ▶ unless ($passed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($passed) 29 8 37
416 0 ▶ if($unknown_value_in_got) { $got = \"got something containing an Unknown::Values::unknown value" };
Branch: 1 branch
BranchTrueFalseTotal
if ($unknown_value_in_got) 0 29 29
417 0 ▶ if($unknown_value_in_expected) { $expected = \"expected something containing an Unknown::Values::unknown value" };
Branch: 1 branch
BranchTrueFalseTotal
if ($unknown_value_in_expected) 0 29 29
418 29 my $context;
419  
420 $context = $options->{context}
421 29 ▶ if exists $options->{context};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"context"} 2 27 29
422  
423 29 ▶ $context = $dump_it ? 2**31 : grep( @$_ > 25, $got, $expected ) ? 3 : 25
Branch: 3 branches
BranchTrueFalseTotal
unless defined $context 27 2 29
$dump_it ? : 3 24 27
grep((@$_ > 25), $got, $expected) ? : 3 21 24
424 unless defined $context;
425  
426 29 ▶ confess "context must be an integer: '$context'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $context =~ /\A\d+\z/ 0 29 29
427 unless $context =~ /\A\d+\z/;
428  
429 29 ▶ $diff = diff $got, $expected,
Branch: 3 branches
BranchTrueFalseTotal
$data_type eq 'text' ? : 17 12 29
$data_type eq 'text' ? : 17 12 29
$data_type eq 'text' ? : 17 12 29
430 { CONTEXT => $context,
431 STYLE => _diff_style(),
432 FILENAME_A => $filename_a,
433 FILENAME_B => $filename_b,
434 OFFSET_A => $data_type eq "text" ? 1 : 0,
435 OFFSET_B => $data_type eq "text" ? 1 : 0,
436 INDEX_LABEL => $data_type eq "text" ? "Ln" : "Elt",
437 };
438 29 chomp $diff;
439 29 $diff .= "\n";
440 }
441  
442 37 my $which = _identify_callers_test_package_of_choice;
443  
444 37 ▶ if ( $which eq "Test" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'Test') { } 0 37 37
elsif ($which eq 'Test::Builder') { } 37 0 37
445 @_
446 0 ▶ = $passed
Branch: 1 branch
BranchTrueFalseTotal
$passed ? : 0 0 0
447 ? ( "", "", $name )
448 : ( "\n$diff", "No differences", $name );
449 0 goto &Test::ok;
450 }
451 elsif ( $which eq "Test::Builder" ) {
452 37 my $test = Test::Builder->new;
453 ## TODO: Call exported_to here? May not need to because the caller
454 ## should have imported something based on Test::Builder already.
455 37 $test->ok( $passed, $name );
456 37 ▶ $test->diag($diff) unless $passed;
Branch: 1 branch
BranchTrueFalseTotal
unless $passed 29 8 37
457 }
458 else {
459 0 ▶ unless ($warned_of_unknown_test_lib) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned_of_unknown_test_lib) 0 0 0
460 0 Carp::cluck
461 "Can't identify test lib in use, doesn't seem to be Test.pm or Test::Builder based\n";
462 0 $warned_of_unknown_test_lib = 1;
463 }
464 ## Play dumb and hope nobody notices the fool drooling in the corner
465 0 ▶ if ($passed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passed) { } 0 0 0
466 0 print "ok\n";
467 }
468 else {
469 0 $diff =~ s/^/# /gm;
470 0 print "not ok\n", $diff;
471 }
472 }
473 }
474  
475 =head1 LIMITATIONS
476  
477 =head2 C<Test> or C<Test::More>
478  
479 This module "mixes in" with Test.pm or any of the test libraries based on
480 Test::Builder (Test::Simple, Test::More, etc). It does this by peeking to see
481 whether Test.pm or Test/Builder.pm is in %INC, so if you are not using one of
482 those, it will print a warning and play dumb by not emitting test numbers (or
483 incrementing them). If you are using one of these, it should interoperate
484 nicely.
485  
486 =head2 Exporting
487  
488 Exports all 3 functions by default (and by design). Use
489  
490 use Test::Differences ();
491  
492 to suppress this behavior if you don't like the namespace pollution.
493  
494 This module will not override functions like ok(), is(), is_deeply(), etc. If
495 it did, then you could C<eval "use Test::Differences qw( is_deeply );"> to get
496 automatic upgrading to diffing behaviors without the C<sub my_ok> shown above.
497 Test::Differences intentionally does not provide this behavior because this
498 would mean that Test::Differences would need to emulate every popular test
499 module out there, which would require far more coding and maintenance that I'm
500 willing to do. Use the eval and my_ok deployment shown above if you want some
501 level of automation.
502  
503 =head2 Booleans
504  
505 Historically, true and false (whether resulting from logical-not C<!> or from
506 equality checks like C<==>) were represented in perl as numeric C<1> for true
507 and the empty string C<''> for false. In perl 5.36.0 they became real Boolean
508 values which for backward compatibility also have their old numeric/string
509 values. In 5.38 (and in dev releases from 5.37.2 onwards) L<Data::Dumper>
510 belatedly learned about this, and so Test::Differences (which uses Data::Dumper
511 under the bonnet) can also tell the difference.
512  
513 This means that `eq_or_diff(1, !!1)` will behave differently depending on
514 whether you are using perl 5.38 or later (in which case it will say they are
515 different) or 5.36 or earlier (which will say they are the same).
516  
517 =head2 Unicode
518  
519 Perls before 5.6.0 don't support characters > 255 at all, and 5.6.0
520 seems broken. This means that you might get odd results using perl5.6.0
521 with unicode strings.
522  
523 =head2 C<Data::Dumper> and older Perls.
524  
525 Relies on Data::Dumper (for now), which, prior to perl5.8, will not always
526 report hashes in the same order. C< $Data::Dumper::Sortkeys > I<is> set to 1,
527 so on more recent versions of Data::Dumper, this should not occur. Check CPAN
528 to see if it's been peeled out of the main perl distribution and backported.
529 Reported by Ilya Martynov <ilya@martynov.org>, although the Sortkeys "future
530 perfect" workaround has been set in anticipation of a new Data::Dumper for a
531 while. Note that the two hashes should report the same here:
532  
533 not ok 5
534 # Failed test (t/ctrl/05-home.t at line 51)
535 # +----+------------------------+----+------------------------+
536 # | Elt|Got | Elt|Expected |
537 # +----+------------------------+----+------------------------+
538 # | 0|{ | 0|{ |
539 # | 1| 'password' => '', | 1| 'password' => '', |
540 # * 2| 'method' => 'login', * | |
541 # | 3| 'ctrl' => 'home', | 2| 'ctrl' => 'home', |
542 # | | * 3| 'method' => 'login', *
543 # | 4| 'email' => 'test' | 4| 'email' => 'test' |
544 # | 5|} | 5|} |
545 # +----+------------------------+----+------------------------+
546  
547 Data::Dumper also overlooks the difference between
548  
549 $a[0] = \$a[1];
550 $a[1] = \$a[0]; # $a[0] = \$a[1]
551  
552 and
553  
554 $x = \$y;
555 $y = \$x;
556 @a = ( $x, $y ); # $a[0] = \$y, not \$a[1]
557  
558 The former involves two scalars, the latter 4: $x, $y, and @a[0,1].
559 This was carefully explained to me in words of two syllables or less by
560 Yves Orton <demerphq@hotmail.com>. The plan to address this is to allow
561 you to select Data::Denter or some other module of your choice as an
562 option.
563  
564 =head2 Code-refs
565  
566 Test::Differences turns on C<$Data::Dumper::Deparse>, so any code-refs in your
567 data structures will be turned into text before they are examined, using
568 L<B::Deparse>. The precise text generated for a sub-ref might not be what you
569 expect as it is generated from the compiled version of the code, but it should
570 at least be consistent and spot differences correctly.
571  
572 You can turn this behaviour off by setting C<$Test::Differences::NoDeparse>.
573  
574 =head1 AUTHORS
575  
576 Barrie Slaymaker <barries@slaysys.com> - original author
577  
578 Curtis "Ovid" Poe <ovid@cpan.org>
579  
580 David Cantrell <david@cantrell.org.uk>
581  
582 =head1 LICENSE
583  
584 Copyright Barrie Slaymaker, Curtis "Ovid" Poe, and David Cantrell.
585  
586 All Rights Reserved.
587  
588 You may use, distribute and modify this software under the terms of the GNU
589 public license, any version, or the Artistic license.
590  
591 =cut
592  
593 15 1;
↑ Summary