1Test::Deep(3)         User Contributed Perl Documentation        Test::Deep(3)
2
3
4

NAME

6       Test::Deep - Extremely flexible deep comparison
7

VERSION

9       version 1.204
10

SYNOPSIS

12         use Test::More tests => $Num_Tests;
13         use Test::Deep;
14
15         cmp_deeply(
16           $actual_horrible_nested_data_structure,
17           $expected_horrible_nested_data_structure,
18           "got the right horrible nested data structure"
19         );
20
21         cmp_deeply(
22           $object,
23           methods(name => "John", phone => "55378008"),
24           "object methods ok"
25         );
26
27         cmp_deeply(
28           \@array,
29           [$hash1, $hash2, ignore()],
30           "first 2 elements are as expected, ignoring 3"
31         );
32
33         cmp_deeply(
34           $object,
35           noclass({value => 5}),
36           "object looks ok, not checking its class"
37         );
38
39         cmp_deeply(
40           \@result,
41           bag('a', 'b', {key => [1, 2]}),
42           "array has the 3 things we wanted in some order"
43         );
44

DESCRIPTION

46       If you don't know anything about automated testing in Perl then you
47       should probably read about Test::Simple and Test::More before
48       preceding.  Test::Deep uses the Test::Builder framework.
49
50       Test::Deep gives you very flexible ways to check that the result you
51       got is the result you were expecting. At its simplest it compares two
52       structures by going through each level, ensuring that the values match,
53       that arrays and hashes have the same elements and that references are
54       blessed into the correct class. It also handles circular data
55       structures without getting caught in an infinite loop.
56
57       Where it becomes more interesting is in allowing you to do something
58       besides simple exact comparisons. With strings, the "eq" operator
59       checks that 2 strings are exactly equal but sometimes that's not what
60       you want. When you don't know exactly what the string should be but you
61       do know some things about how it should look, "eq" is no good and you
62       must use pattern matching instead. Test::Deep provides pattern matching
63       for complex data structures
64
65       Test::Deep has a lot of exports.  See "EXPORTS" below.
66

PERL VERSION

68       This library should run on perls released even a long time ago.  It
69       should work on any version of perl released in the last five years.
70
71       Although it may work on older versions of perl, no guarantee is made
72       that the minimum required version will not be increased.  The version
73       may be increased for any reason, and there is no promise that patches
74       will be accepted to lower the minimum required perl.
75

EXAMPLES

77       How Test::Deep works is much easier to understand by seeing some
78       examples.
79
80   Without Test::Deep
81       Say you want to test a function which returns a string. You know that
82       your string should be a 7 digit number beginning with 0, "eq" is no
83       good in this situation, you need a regular expression. So you could use
84       Test::More's like() function:
85
86         like($string, qr/^0[0-9]{6}$/, "number looks good");
87
88       Similarly, to check that a string looks like a name, you could do:
89
90         like($string, qr/^(Mr|Mrs|Miss) \w+ \w+$/,
91           "got title, first and last name");
92
93       Now imagine your function produces a hash with some personal details in
94       it.  You want to make sure that there are 2 keys, Name and Phone and
95       that the name looks like a name and the phone number looks like a phone
96       number. You could do:
97
98         $hash = make_person();
99         like($hash->{Name}, qr/^(Mr|Mrs|Miss) \w+ \w+$/, "name ok");
100         like($hash->{Phone}, qr/^0[0-9]{6}$/, "phone ok");
101         is(scalar keys %$hash, 2, "correct number of keys");
102
103       But that's not quite right, what if make_person has a serious problem
104       and didn't even return a hash? We really need to write
105
106         if (ref($hash) eq "HASH")
107         {
108           like($hash->{Name}, qr/^(Mr|Mrs|Miss) \w+ \w+$/, "name ok");
109           like($hash->{Phone}, qr/^0[0-9]{6}$/, "phone ok");
110           is(scalar keys %$hash, 2, "correct number of keys");
111         }
112         else
113         {
114           fail("person not a hash");
115           fail("person not a hash");
116           fail("person not a hash"); # need 3 to keep the plan correct
117         }
118
119       Already this is getting messy, now imagine another entry in the hash,
120       an array of children's names. This would require
121
122         if (ref($hash) eq "HASH")
123         {
124           like($hash->{Name}, $name_pat, "name ok");
125           like($hash->{Phone}, '/^0d{6}$/', "phone ok");
126           my $cn = $hash->{ChildNames};
127           if (ref($cn) eq "ARRAY")
128           {
129             foreach my $child (@$cn)
130             {
131               like($child, $name_pat);
132             }
133           }
134           else
135           {
136               fail("child names not an array")
137           }
138         }
139         else
140         {
141           fail("person not a hash");
142         }
143
144       This is a horrible mess and because we don't know in advance how many
145       children's names there will be, we can't make a plan for our test
146       anymore (actually, we could but it would make things even more
147       complicated).
148
149       Test::Deep to the rescue.
150
151   With Test::Deep
152         my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
153         cmp_deeply(
154           $person,
155           {
156             Name => $name_re,
157             Phone => re('^0d{6}$'),
158             ChildNames => array_each($name_re)
159           },
160           "person ok"
161         );
162
163       This will do everything that the messy code above does and it will give
164       a sensible message telling you exactly what went wrong if it finds a
165       part of $person that doesn't match the pattern. re() and array_each()
166       are special function imported from Test::Deep. They create a marker
167       that tells Test::Deep that something different is happening here.
168       Instead of just doing a simple comparison and checking are two things
169       exactly equal, it should do something else.
170
171       If a person was asked to check that 2 structures are equal, they could
172       print them both out and compare them line by line. The markers above
173       are similar to writing a note in red pen on one of the printouts
174       telling the person that for this piece of the structure, they should
175       stop doing simple line by line comparison and do something else.
176
177       re($regex) means that Test::Deep should check that the current piece of
178       data matches the regex in $regex. array_each($struct) means that
179       Test::Deep should expect the current piece of data to be an array and
180       it should check that every element of that array matches $struct.  In
181       this case, every element of "$person->{ChildNames}" should look like a
182       name. If say the 3rd one didn't you would get an error message
183       something like
184
185         Using Regexp on $data->{ChildNames}[3]
186            got    : 'Queen John Paul Sartre'
187            expect : /^(Mr|Mrs|Miss) \w+ \w+$/
188
189       There are lots of other special comparisons available, see "SPECIAL
190       COMPARISONS PROVIDED" below for the full list.
191
192   Reusing structures
193       Test::Deep is good for reusing test structures so you can do this
194
195         my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
196         my $person_cmp = {
197           Name => $name_re,
198           Phone => re('^0d{6}$'),
199           ChildNames => array_each($name_re)
200         };
201
202         cmp_deeply($person1, $person_cmp, "person ok");
203         cmp_deeply($person2, $person_cmp, "person ok");
204         cmp_deeply($person3, $person_cmp, "person ok");
205
206       You can even put $person_cmp in a module and let other people use it
207       when they are writing test scripts for modules that use your modules.
208
209       To make things a little more difficult, lets change the person data
210       structure so that instead of a list of ChildNames, it contains a list
211       of hashes, one for each child. So in fact our person structure will
212       contain other person structures which may contain other person
213       structures and so on.  This is easy to handle with Test::Deep because
214       Test::Deep structures can include themselves. Simply do
215
216         my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
217         my $person_cmp = {
218           Name => $name_re,
219           Phone => re('^0d{6}$'),
220           # note no mention of Children here
221         };
222
223         $person_cmp->{Children} = array_each($person_cmp);
224
225         cmp_deeply($person, $person_cmp, "person ok");
226
227       This will now check that $person->{Children} is an array and that every
228       element of that array also matches $person_cmp, this includes checking
229       that its children also match the same pattern and so on.
230
231   Circular data structures
232       A circular data structure is one which loops back on itself, you can
233       make one easily by doing
234
235         my @b;
236         my @a = (1, 2, 3, \@b);
237         push(@b, \@a);
238
239       now @a contains a reference to be @b and @b contains a reference to @a.
240       This causes problems if you have a program that wants to look inside @a
241       and keep looking deeper and deeper at every level, it could get caught
242       in an infinite loop looking into @a then @b then @a then @b and so on.
243
244       Test::Deep avoids this problem so we can extend our example further by
245       saying that a person should also list their parents.
246
247         my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
248         my $person_cmp = {
249           Name => $name_re,
250           Phone => re('^0d{6}$'),
251           # note no mention of Children here
252         };
253
254         $person_cmp->{Children} = each_array($person_cmp);
255         $person_cmp->{Parents} = each_array($person_cmp);
256
257         cmp_deeply($person, $person_cmp, "person ok");
258
259       So this will check that for each child $child in "$person->{Children}"
260       that the "$child->{Parents}" matches $person_cmp however it is smart
261       enough not to get caught in an infinite loop where it keeps bouncing
262       between the same Parent and Child.
263

TERMINOLOGY

265       "cmp_deeply($got, $expected, $name)" takes 3 arguments. $got is the
266       structure that you are checking, you must not include any special
267       comparisons in this structure or you will get a fatal error. $expected
268       describes what Test::Deep will be looking for in $got. You can put
269       special comparisons in $expected if you want to.
270
271       As Test::Deep descends through the 2 structures, it compares them one
272       piece at a time, so at any point in the process, Test::Deep is thinking
273       about 2 things - the current value from $got and the current value from
274       $expected. In the documentation, I call them $got_v and "exp_v"
275       respectively.
276

COMPARISON FUNCTIONS

278       cmp_deeply
279
280         my $ok = cmp_deeply($got, $expected, $name)
281
282       $got is the result to be checked. $expected is the structure against
283       which $got will be check. $name is the test name.
284
285       This is the main comparison function, the others are just wrappers
286       around this.  $got and $expected are compared recursively.  Each value
287       in $expected defines what's expected at the corresponding location in
288       $got.  Simple scalars are compared with "eq".  References to structures
289       like hashes and arrays are compared recursively.
290
291       Items in $expected, though, can also represent complex tests that check
292       for numbers in a given range, hashes with at least a certain set of
293       keys, a string matching a regex, or many other things.
294
295       See "WHAT ARE SPECIAL COMPARISONS" for details.
296
297       cmp_bag
298
299         my $ok = cmp_bag(\@got, \@bag, $name)
300
301       Is shorthand for cmp_deeply(\@got, bag(@bag), $name)
302
303       n.b.: Both arguments must be array refs. If they aren't an exception
304       will be thrown.
305
306       cmp_set
307
308         my $ok = cmp_set(\@got, \@set, $name)
309
310       Is shorthand for cmp_deeply(\@got, set(@set), $name)
311
312       cmp_methods
313
314         my $ok = cmp_methods(\@got, \@methods, $name)
315
316       Is shorthand for cmp_deeply(\@got, methods(@methods), $name)
317
318       eq_deeply
319
320         my $ok = eq_deeply($got, $expected)
321
322       This is the same as cmp_deeply() except it just returns true or false.
323       It does not create diagnostics or talk to Test::Builder, but if you
324       want to use it in a non-testing environment then you should import it
325       through Test::Deep::NoTest. For example
326
327         use Test::Deep::NoTest;
328         print "a equals b" unless eq_deeply($a, $b);
329
330       otherwise the Test::Builder framework will be loaded and testing
331       messages will be output when your program ends.
332
333       cmp_details
334
335         ($ok, $stack) = cmp_details($got, $expected)
336
337       This behaves much like eq_deeply, but it additionally allows you to
338       produce diagnostics in case of failure by passing the value in $stack
339       to "deep_diag".
340
341       Do not make assumptions about the structure or content of $stack and do
342       not use it if $ok contains a true value.
343
344       See "USING TEST::DEEP WITH TEST::BUILDER" for example uses.
345

SPECIAL COMPARISONS PROVIDED

347       In the documentation below, $got_v is used to indicate any given value
348       within the $got structure.
349
350       ignore
351
352         cmp_deeply( $got, ignore() );
353
354       This makes Test::Deep skip tests on $got_v. No matter what value $got_v
355       has, Test::Deep will think it's correct. This is useful if some part of
356       the structure you are testing is very complicated and already tested
357       elsewhere, or if it is unpredictable.
358
359         cmp_deeply(
360           $got,
361           {
362             name    => 'John',
363             random  => ignore(),
364             address => [ '5 A street', 'a town', 'a country' ],
365           }
366         );
367
368       is the equivalent of checking
369
370         $got->{name} eq 'John';
371         exists $got->{random};
372         cmp_deeply($got->{address}, ['5 A street', 'a town', 'a country']);
373
374       methods
375
376         cmp_deeply( $got, methods(%hash) );
377
378       %hash is a hash of method call => expected value pairs.
379
380       This lets you call methods on an object and check the result of each
381       call.  The methods will be called in the order supplied. If you want to
382       pass arguments to the method you should wrap the method name and
383       arguments in an array reference.
384
385         cmp_deeply(
386           $obj,
387           methods(name => "John", ["favourite", "food"] => "taco")
388         );
389
390       is roughly the equivalent of checking that
391
392         $obj->name eq "John"
393         $obj->favourite("food") eq "taco"
394
395       The methods will be called in the order you supply them and will be
396       called in scalar context. If you need to test methods called in list
397       context then you should use listmethods().
398
399       NOTE Just as in a normal test script, you need to be careful if the
400       methods you call have side effects like changing the object or other
401       objects in the structure. Although the order of the methods is fixed,
402       the order of some other tests is not so if $expected is
403
404         {
405           manager => methods(@manager_methods),
406           coder => methods(@coder_methods)
407         }
408
409       there is no way to know which if manager and coder will be tested
410       first. If the methods you are testing depend on and alter global
411       variables or if manager and coder are the same object then you may run
412       into problems.
413
414       listmethods
415
416         cmp_deeply( $got, listmethods(%hash) );
417
418       %hash is a hash of pairs mapping method names to expected return
419       values.
420
421       This is almost identical to methods() except the methods are called in
422       list context instead of scalar context. This means that the expected
423       return values supplied must be in array references.
424
425         cmp_deeply(
426           $obj,
427           listmethods(
428             name => [ "John" ],
429             ["favourites", "food"] => ["Mapo tofu", "Gongbao chicken"]
430           )
431         );
432
433       is the equivalent of checking that
434
435         cmp_deeply([$obj->name], ["John"]);
436         cmp_deeply([$obj->favourites("food")], ["Mapo tofu", "Gongbao chicken"]);
437
438       The methods will be called in the order you supply them.
439
440       NOTE The same caveats apply as for methods().
441
442       shallow
443
444         cmp_deeply( $got, shallow($thing) );
445
446       $thing is a ref.
447
448       This prevents Test::Deep from looking inside $thing. It allows you to
449       check that $got_v and $thing are references to the same variable. So
450
451         my @a = @b = (1, 2, 3);
452         cmp_deeply(\@a, \@b);
453
454       will pass because @a and @b have the same elements however
455
456         cmp_deeply(\@a, shallow(\@b))
457
458       will fail because although "\@a" and "\@b" both contain "1, 2, 3" they
459       are references to different arrays.
460
461       noclass
462
463         cmp_deeply( $got, noclass($thing) );
464
465       $thing is a structure to be compared against.
466
467       This makes Test::Deep ignore the class of objects, so it just looks at
468       the data they contain. Class checking will be turned off until
469       Test::Deep is finished comparing $got_v against $thing. Once Test::Deep
470       comes out of $thing it will go back to its previous setting for
471       checking class.
472
473       This can be useful when you want to check that objects have been
474       constructed correctly but you don't want to write lots of "bless"es. If
475       @people is an array of Person objects then
476
477         cmp_deeply(\@people, [
478           bless {name => 'John', phone => '555-5555'}, "Person",
479           bless {name => 'Anne', phone => '444-4444'}, "Person",
480         ]);
481
482       can be replaced with
483
484         cmp_deeply(\@people, noclass([
485           {name => 'John', phone => '555-5555'},
486           {name => 'Anne', phone => '444-4444'}
487         ]));
488
489       However, this is testing so you should also check that the objects are
490       blessed correctly. You could use a map to bless all those hashes or you
491       could do a second test like
492
493         cmp_deeply(\@people, array_each(isa("Person"));
494
495       useclass
496
497         cmp_deeply( $got, useclass($thing) );
498
499       This turns back on the class comparison while inside a noclass().
500
501         cmp_deeply(
502           $got,
503           noclass(
504             [
505               useclass( $object )
506             ]
507           )
508         )
509
510       In this example the class of the array reference in $got is ignored but
511       the class of $object is checked, as is the class of everything inside
512       $object.
513
514       re
515
516         cmp_deeply( $got, re($regexp, $capture_data, $flags) );
517
518       $regexp is either a regular expression reference produced with
519       "qr/.../" or a string which will be used to construct a regular
520       expression.
521
522       $capture_data is optional and is used to check the strings captured by
523       an regex. This should can be an array ref or a Test::Deep comparator
524       that works on array refs.
525
526       $flags is an optional string which controls whether the regex runs as a
527       global match. If $flags is "g" then the regex will run as
528       "m/$regexp/g".
529
530       Without $capture_data, this simply compares $got_v with the regular
531       expression provided. So
532
533         cmp_deeply($got, [ re("ferg") ])
534
535       is the equivalent of
536
537         $got->[0] =~ /ferg/
538
539       With $capture_data,
540
541         cmp_deeply($got, [re($regex, $capture_data)])
542
543       is the equivalent of
544
545         my @data = $got->[0] =~ /$regex/;
546         cmp_deeply(\@data, $capture_data);
547
548       So you can do something simple like
549
550         cmp_deeply($got, re(qr/(\d\d)(\w\w)/, [25, "ab" ]))
551
552       to check that "(\d\d)" was 25 and "(\w\w)" was "ab" but you can also
553       use Test::Deep objects to do more complex testing of the captured
554       values
555
556         cmp_deeply(
557           "cat=2,dog=67,sheep=3,goat=2,dog=5",
558           re(
559             qr/(\D+)=\d+,?/,
560             set(qw( cat sheep dog )),
561             "g"
562           ),
563         );
564
565       here, the regex will match the string and will capture the animal names
566       and check that they match the specified set, in this case it will fail,
567       complaining that "goat" is not in the set.
568
569       all
570
571         cmp_deeply( $got, all(@expecteds) );
572
573       @expecteds is an array of expected structures.
574
575       This allows you to compare data against multiple expected results and
576       make sure each of them matches.
577
578         cmp_deeply($got, all(isa("Person"), methods(name => 'John')))
579
580       is equivalent to
581
582         $got->isa("Person")
583         $got->name eq 'John'
584
585       If either test fails then the whole thing is considered a fail. This is
586       a short-circuit test, the testing is stopped after the first failure,
587       although in the future it may complete all tests so that diagnostics
588       can be output for all failures. When reporting failure, the parts are
589       counted from 1.
590
591       Thanks to the magic of overloading, you can write
592
593         any( re("^wi"), all(isa("Person"), methods(name => 'John')) )
594
595       as
596
597          re("^wi") | isa("Person") & methods(name => 'John')
598
599       Note single "|" not double, as "||" cannot be overloaded. This will
600       only work when there is a special comparison involved. If you write
601
602         "john" | "anne" | "robert"
603
604       Perl will turn this into
605
606         "{onort"
607
608       which is presumably not what you wanted. This is because perl ors them
609       together as strings before Test::Deep gets a chance to do any overload
610       tricks.
611
612       any
613
614         cmp_deeply( $got, any(@expecteds) );
615
616       @expecteds is an array of expected structures.
617
618       This can be used to compare data against multiple expected results and
619       make sure that at least one of them matches. This is a short-circuit
620       test so if a test passes then none of the tests after that will be
621       attempted.
622
623       You can also use overloading with "|" similarly to all().
624
625       Isa
626
627         cmp_deeply( $got, Isa($class) );
628
629       isa
630
631         cmp_deeply( $got, isa($class) );
632
633       $class is a class name.
634
635       This uses UNIVERSAL::isa() to check that $got_v is blessed into the
636       class $class.
637
638       NOTE: Isa() does exactly as documented here, but isa() is slightly
639       different. If isa() is called with 1 argument it falls through to
640       Isa(). If isa() called with 2 arguments, it falls through to
641       "UNIVERSAL::isa". This is to prevent breakage when you import isa()
642       into a package that is used as a class. Without this, anyone calling
643       "Class->isa($other_class)" would get the wrong answer. This is a hack
644       to patch over the fact that "isa" is exported by default.
645
646       obj_isa
647
648         cmp_deeply( $got, obj_isa($class) );
649
650       This test accepts only objects that are instances of $class or a
651       subclass.  Unlike the "Isa" test, this test will never accept class
652       names.
653
654       array_each
655
656         cmp_deeply( \@got, array_each($thing) );
657
658       $thing is a structure to be compared against.
659
660       <$got_v> must be an array reference. Each element of it will be
661       compared to $thing. This is useful when you have an array of similar
662       things, for example objects of a known type and you don't want to have
663       to repeat the same test for each one.
664
665         my $common_tests = all(
666            isa("MyFile"),
667            methods(
668              handle => isa("IO::Handle")
669              filename => re("^/home/ted/tmp"),
670           )
671         );
672
673         cmp_deeply($got, array_each($common_tests));
674
675       is similar to
676
677         foreach my $got_v (@$got) {
678           cmp_deeply($got_v, $common_tests)
679         }
680
681       Except it will not explode if $got is not an array reference. It will
682       check that each of the objects in @$got is a MyFile and that each one
683       gives the correct results for its methods.
684
685       You could go further, if for example there were 3 files and you knew
686       the size of each one you could do this
687
688         cmp_deeply(
689           $got,
690           all(
691             array_each($common_tests),
692             [
693               methods(size => 1000),
694               methods(size => 200),
695               methods(size => 20)
696             ]
697           )
698         )
699         cmp_deeply($got, array_each($structure));
700
701       hash_each
702
703         cmp_deeply( \%got, hash_each($thing) );
704
705       This test behaves like "array_each" (see above) but tests that each
706       hash value passes its tests.
707
708       str
709
710         cmp_deeply( $got, str($string) );
711
712       $string is a string.
713
714       This will stringify $got_v and compare it to $string using "eq", even
715       if $got_v is a ref. It is useful for checking the stringified value of
716       an overloaded reference.
717
718       num
719
720         cmp_deeply( $got, num($number, $tolerance) );
721
722       $number is a number.
723
724       $tolerance is an optional number.
725
726       This will add 0 to $got_v and check if it's numerically equal to
727       $number, even if $got_v is a ref. It is useful for checking the
728       numerical value of an overloaded reference. If $tolerance is supplied
729       then this will check that $got_v and $exp_v are less than $tolerance
730       apart. This is useful when comparing floating point numbers as rounding
731       errors can make it hard or impossible for $got_v to be exactly equal to
732       $exp_v. When $tolerance is supplied, the test passes if "abs($got_v -
733       $exp_v) <= $tolerance".
734
735       Note in Perl, ""12blah" == 12" because Perl will be smart and convert
736       "12blah" into 12. You may not want this. There was a strict mode but
737       that is now gone. A "looks like a number" test will replace it soon.
738       Until then you can usually just use the string() comparison to be more
739       strict. This will work fine for almost all situations, however it will
740       not work when <$got_v> is an overloaded value who's string and
741       numerical values differ.
742
743       bool, true, false
744
745         cmp_deeply( $got, bool($value) );
746         cmp_deeply( $got, true );
747         cmp_deeply( $got, false );
748
749       $value is anything you like but it's probably best to use 0 or 1
750
751       This will check that $got_v and $value have the same truth value, that
752       is they will give the same result when used in boolean context, like in
753       an if() statement.
754
755       Note: "true" and "false" are only imported by special request.
756
757       code
758
759         cmp_deeply( $got, code(\&subref) );
760
761       "\&subref" is a reference to a subroutine which will be passed a single
762       argument, it then should return a true or false and possibly a string
763
764       This will pass $got_v to the subroutine which returns true or false to
765       indicate a pass or fail. Fails can be accompanied by a diagnostic
766       string which gives an explanation of why it's a fail.
767
768         sub check_name
769         {
770           my $name = shift;
771           if ($boss->likes($name))
772           {
773             return 1;
774           }
775           else
776           {
777             return (0, "the boss doesn't like your name");
778           }
779         }
780
781         cmp_deeply("Brian", code(\&check_name));
782
783   SET COMPARISONS
784       Set comparisons give special semantics to array comparisons:
785
786       •   The order of items in a set is irrelevant
787
788       •   The presence of duplicate items in a set is ignored.
789
790       As such, in any set comparison, the following arrays are equal:
791
792         [ 1, 2 ]
793         [ 1, 1, 2 ]
794         [ 1, 2, 1 ]
795         [ 2, 1, 1 ]
796         [ 1, 1, 2 ]
797
798       All are interpreted by "set" semantics as if the set was only specified
799       as:
800
801         [ 1, 2 ]
802
803       All "set" functions return an object which can have additional items
804       added to it:
805
806         my $set = set( 1, 2 );
807         $set->add(1, 3, 1 );  # Set is now ( 1, 2, 3 )
808
809       Special care must be taken when using special comparisons within sets.
810       See "SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS" for
811       details.
812
813       set
814
815         cmp_deeply( \@got, set(@elements) );
816
817       This does a set comparison, that is, it compares two arrays but ignores
818       the order of the elements and it ignores duplicate elements, but
819       ensures that all items in @elements will be in $got and all items in
820       $got will be in @elements.
821
822       So the following tests will be passes, and will be equivalent:
823
824         cmp_deeply([1, 2, 2, 3], set(3, 2, 1, 1));
825         cmp_deeply([1, 2, 3],    set(3, 2, 1));
826
827       supersetof
828
829         cmp_deeply( \@got, supersetof(@elements) );
830
831       This function works much like "set", and performs a set comparison of
832       $got_v with the elements of @elements.
833
834       "supersetof" is however slightly relaxed, such that $got may contain
835       things not in @elements, but must at least contain all @elements.
836
837       These two statements are equivalent, and will be passes:
838
839         cmp_deeply([1,2,3,3,4,5], supersetof(2,2,3));
840         cmp_deeply([1,2,3,4,5],   supersetof(2,3));
841
842       But these will be failures:
843
844         cmp_deeply([1,2,3,4,5],   supersetof(2,3,6)); # 6 not in superset
845         cmp_deeply([1],           supersetof(1,2));   # 2 not in superset
846
847       subsetof
848
849         cmp_deeply( \@got, subsetof(@elements) );
850
851       This function works much like "set", and performs a set comparison of
852       $got_v with the elements of @elements.
853
854       This is the inverse of "supersetof", which expects all unique elements
855       found in $got_v must be in @elements.
856
857         cmp_deeply([1,2,4,5], subsetof(2,3,3)    ) # Fail: 1,4 & 5 extra
858         cmp_deeply([2,3,3],   subsetof(1,2,4,5)  ) # Fail: 3 extra
859         cmp_deeply([2,3,3],   subsetof(1,2,4,5,3)) # Pass
860
861       none
862
863         cmp_deeply( $got, none(@elements) );
864
865       @elements is an array of elements, wherein no elements in @elements may
866       be equal to $got_v.
867
868       noneof
869
870         cmp_deeply( \@got, noneof(@elements) );
871
872       @elements is an array of elements, wherein no elements in @elements may
873       be found in $got_v.
874
875       For example:
876
877         # Got has no 1, no 2, and no 3
878         cmp_deeply( [1], noneof( 1, 2, 3 ) ); # fail
879         cmp_deeply( [5], noneof( 1, 2, 3 ) ); # pass
880
881   BAG COMPARISONS
882       Bag comparisons give special semantics to array comparisons, that are
883       similar to set comparisons, but slightly different.
884
885       •   The order of items in a bag is irrelevant
886
887       •   The presence of duplicate items in a bag is PRESERVED
888
889       As such, in any bag comparison, the following arrays are equal:
890
891         [ 1, 1, 2 ]
892         [ 1, 2, 1 ]
893         [ 2, 1, 1 ]
894         [ 1, 1, 2 ]
895
896       However, they are NOT equal to any of the following:
897
898         [ 1, 2 ]
899         [ 1, 2, 2 ]
900         [ 1, 1, 1, 2 ]
901
902       All "bag" functions return an object which can have additional items
903       added to it:
904
905         my $bag = bag( 1, 2 );
906         $bag->add(1, 3, 1 );  # Bag is now ( 1, 1, 1, 2, 3 )
907
908       Special care must be taken when using special comparisons within bags.
909       See "SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS" for
910       details.
911
912       bag
913
914         cmp_deeply( \@got, bag(@elements) );
915
916       This does an order-insensitive bag comparison between $got and
917       @elements, ensuring that:
918
919       each item in @elements is found in $got
920       the number of times a $expected_v is found in @elements is reflected in
921       $got
922       no items are found in $got other than those in @elements.
923
924       As such, the following are passes, and are equivalent to each other:
925
926         cmp_deeply([1, 2, 2], bag(2, 2, 1))
927         cmp_deeply([2, 1, 2], bag(2, 2, 1))
928         cmp_deeply([2, 2, 1], bag(2, 2, 1))
929
930       But the following are failures:
931
932         cmp_deeply([1, 2, 2],     bag(2, 2, 1, 1)) # Not enough 1's in Got
933         cmp_deeply([1, 2, 2, 1],  bag(2, 2, 1)   ) # Too many   1's in Got
934
935       superbagof
936
937         cmp_deeply( \@got, superbagof( @elements ) );
938
939       This function works much like "bag", and performs a bag comparison of
940       $got_v with the elements of @elements.
941
942       "superbagof" is however slightly relaxed, such that $got may contain
943       things not in @elements, but must at least contain all @elements.
944
945       So:
946
947         # pass
948         cmp_deeply( [1, 1, 2], superbagof( 1 )      );
949
950         # fail: not enough 1's in superbag
951         cmp_deeply( [1, 1, 2], superbagof( 1, 1, 1 ));
952
953       subbagof
954
955         cmp_deeply( \@got, subbagof(@elements) );
956
957       This function works much like "bag", and performs a bag comparison of
958       $got_v with the elements of @elements.
959
960       This is the inverse of "superbagof", and expects all elements in $got
961       to be in @elements, while allowing items to exist in @elements that are
962       not in $got
963
964         # pass
965         cmp_deeply( [1],        subbagof( 1, 1, 2 ) );
966
967         # fail: too many 1's in subbag
968         cmp_deeply( [1, 1, 1],  subbagof( 1, 1, 2 ) );
969
970   HASH COMPARISONS
971       Typically, if you're doing simple hash comparisons,
972
973         cmp_deeply( \%got, \%expected )
974
975       is sufficient. "cmp_deeply" will ensure %got and %hash have identical
976       keys, and each key from either has the same corresponding value.
977
978       superhashof
979
980         cmp_deeply( \%got, superhashof(\%hash) );
981
982       This will check that the hash %$got is a "super-hash" of %hash. That is
983       that all the key and value pairs in %hash appear in %$got but %$got can
984       have extra ones also.
985
986       For example
987
988         cmp_deeply({a => 1, b => 2}, superhashof({a => 1}))
989
990       will pass but
991
992         cmp_deeply({a => 1, b => 2}, superhashof({a => 1, c => 3}))
993
994       will fail.
995
996       subhashof
997
998         cmp_deeply( \%got, subhashof(\%hash) );
999
1000       This will check that the hash %$got is a "sub-hash" of %hash. That is
1001       that all the key and value pairs in %$got also appear in %hash.
1002
1003       For example
1004
1005         cmp_deeply({a => 1}, subhashof({a => 1, b => 2}))
1006
1007       will pass but
1008
1009         cmp_deeply({a => 1, c => 3}, subhashof({a => 1, b => 2}))
1010
1011       will fail.
1012

DIAGNOSTIC FUNCTIONS

1014       deep_diag
1015
1016         my $reason = deep_diag($stack);
1017
1018       $stack is a value returned by cmp_details.  Do not call this function
1019       if cmp_details returned a true value for $ok.
1020
1021       deep_diag() returns a human readable string describing how the
1022       comparison failed.
1023

ANOTHER EXAMPLE

1025       You've written a module to handle people and their film interests. Say
1026       you have a function that returns an array of people from a query, each
1027       person is a hash with 2 keys: Name and Age and the array is sorted by
1028       Name. You can do
1029
1030         cmp_deeply(
1031           $result,
1032           [
1033             {Name => 'Anne', Age => 26},
1034             {Name => "Bill", Age => 47}
1035             {Name => 'John', Age => 25},
1036           ]
1037         );
1038
1039       Soon after, your query function changes and all the results now have an
1040       ID field. Now your test is failing again because you left out ID from
1041       each of the hashes. The problem is that the IDs are generated by the
1042       database and you have no way of knowing what each person's ID is. With
1043       Test::Deep you can change your query to
1044
1045         cmp_deeply(
1046           $result,
1047           [
1048             {Name => 'John', Age => 25, ID => ignore()},
1049             {Name => 'Anne', Age => 26, ID => ignore()},
1050             {Name => "Bill", Age => 47, ID => ignore()}
1051           ]
1052         );
1053
1054       But your test still fails. Now, because you're using a database, you no
1055       longer know what order the people will appear in. You could add a sort
1056       into the database query but that could slow down your application.
1057       Instead you can get Test::Deep to ignore the order of the array by
1058       doing a bag comparison instead.
1059
1060         cmp_deeply(
1061           $result,
1062           bag(
1063             {Name => 'John', Age => 25, ID => ignore()},
1064             {Name => 'Anne', Age => 26, ID => ignore()},
1065             {Name => "Bill", Age => 47, ID => ignore()}
1066           )
1067         );
1068
1069       Finally person gets even more complicated and includes a new field
1070       called Movies, this is a list of movies that the person has seen
1071       recently, again these movies could also come back in any order so we
1072       need a bag inside our other bag comparison, giving us something like
1073
1074         cmp_deeply(
1075         $result,
1076           bag(
1077             {Name => 'John', Age => 25, ID => ignore(), Movies => bag(...)},
1078             {Name => 'Anne', Age => 26, ID => ignore(), Movies => bag(...)},
1079             {Name => "Bill", Age => 47, ID => ignore(), Movies => bag(...)}
1080           )
1081         );
1082

USING TEST::DEEP WITH TEST::BUILDER

1084       Combining "cmp_details" and "deep_diag" makes it possible to use
1085       Test::Deep in your own test classes.
1086
1087       In a Test::Builder subclass, create a test method in the following
1088       form:
1089
1090         sub behaves_ok {
1091           my $self = shift;
1092           my $expected = shift;
1093           my $test_name = shift;
1094
1095           my $got = do_the_important_work_here();
1096
1097           my ($ok, $stack) = cmp_details($got, $expected);
1098           unless ($Test->ok($ok, $test_name)) {
1099             my $diag = deep_diag($stack);
1100             $Test->diag($diag);
1101           }
1102         }
1103
1104       As the subclass defines a test class, not tests themselves, make sure
1105       it uses Test::Deep::NoTest, not "Test::Deep" itself.
1106

LIMITATIONS

1108       Currently any CODE, GLOB or IO refs will be compared using shallow(),
1109       which means only their memory addresses are compared.
1110

BUGS

1112       There is a bug in set and bag compare to do with competing SCs. It only
1113       occurs when you put certain special comparisons inside bag or set
1114       comparisons you don't need to worry about it. The full details are in
1115       the bag() docs. It will be fixed in an upcoming version.
1116

CAVEATS

1118   SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS
1119       If you use certain special comparisons within a bag or set comparison
1120       there is a danger that a test will fail when it should have passed. It
1121       can only happen if two or more special comparisons in the bag are
1122       competing to match elements.  Consider this comparison
1123
1124         cmp_deeply(['furry', 'furball'], bag(re("^fur"), re("furb")))
1125
1126       There are two things that could happen, hopefully re("^fur") is paired
1127       with "furry" and re("^furb") is paired with "furb" and everything is
1128       fine but it could happen that re("^fur") is paired with "furball" and
1129       then re("^furb") cannot find a match and so the test fails. Examples of
1130       other competing comparisons are "bag(1, 2, 2)" vs "set(1, 2)" and
1131       "methods(m1 => "v1", m2 => "v2")" vs "methods(m1 => "v1")"
1132
1133       This problem is could be solved by using a slower and more complicated
1134       algorithm for set and bag matching. Something for the future...
1135

WHAT ARE SPECIAL COMPARISONS?

1137       A special comparison (SC) is simply an object that inherits from
1138       Test::Deep::Cmp. Whenever $expected_v is an SC then instead of checking
1139       "$got_v eq $expected_v", we pass control over to the SC and let it do
1140       its thing.
1141
1142       Test::Deep exports lots of SC constructors, to make it easy for you to
1143       use them in your test scripts. For example is re("hello") is just a
1144       handy way of creating a Test::Deep::Regexp object that will match any
1145       string containing "hello". So
1146
1147         cmp_deeply([ 'a', 'b', 'hello world'], ['a', 'b', re("^hello")]);
1148
1149       will check 'a' eq 'a', 'b' eq 'b' but when it comes to comparing 'hello
1150       world' and re("^hello") it will see that $expected_v is an SC and so
1151       will pass control to the Test::Deep::Regexp class by do something like
1152       "$expected_v->descend($got_v)". The descend() method should just return
1153       true or false.
1154
1155       This gives you enough to write your own SCs but I haven't documented
1156       how diagnostics works because it's about to get an overhaul
1157       (theoretically).
1158

EXPORTS

1160       By default, Test::Deep will export everything in its "v0" tag, as if
1161       you had written:
1162
1163         use Test::Deep ':v0';
1164
1165       Those things are:
1166
1167         all any array array_each arrayelementsonly arraylength arraylengthonly bag
1168         blessed bool cmp_bag cmp_deeply cmp_methods cmp_set code eq_deeply hash
1169         hash_each hashkeys hashkeysonly ignore Isa isa listmethods methods noclass
1170         none noneof num obj_isa re reftype regexpmatches regexponly regexpref
1171         regexprefonly scalarrefonly scalref set shallow str subbagof subhashof
1172         subsetof superbagof superhashof supersetof useclass
1173
1174       A slightly better set of exports is the "v1" set.  It's all the same
1175       things, with the exception of "Isa" and "blessed".  If you want to
1176       import "everything", you probably want to "use Test::Deep ':V1';".
1177
1178       There's another magic export group:  ":preload".  If that is specified,
1179       all of the Test::Deep plugins will be loaded immediately instead of
1180       lazily.
1181

SEE ALSO

1183       Test::More
1184

THANKS

1186       Thanks to Michael G Schwern for Test::More's is_deeply function which
1187       inspired this library.
1188

AUTHORS

1190       •   Fergal Daly
1191
1192       •   Ricardo SIGNES <cpan@semiotic.systems>
1193

CONTRIBUTORS

1195       •   Alexander Karelas <karjala@karjala.org>
1196
1197       •   Belden Lyman <blyman@shutterstock.com>
1198
1199       •   Daniel Böhmer <dboehmer@cpan.org>
1200
1201       •   David Steinbrunner <dsteinbrunner@pobox.com>
1202
1203       •   Denis Ibaev <dionys@gmail.com>
1204
1205       •   Ed Adjei <edmund@cpan.org>
1206
1207       •   Fabrice Gabolde <fabrice.gabolde@gmail.com>
1208
1209       •   Felipe Gasper <felipe@felipegasper.com>
1210
1211       •   Fergal Daly <fergal@esatclear.ie>
1212
1213       •   George Hartzell <hartzell@alerce.com>
1214
1215       •   Graham Knop <haarg@haarg.org>
1216
1217       •   Ivan Bessarabov <ivan@bessarabov.ru>
1218
1219       •   José Joaquín Atria <jjatria@cpan.org>
1220
1221       •   Karen Etheridge <ether@cpan.org>
1222
1223       •   Kent Fredric <kentfredric@gmail.com>
1224
1225       •   Lance Wicks <lancew@cpan.org>
1226
1227       •   Matthew Horsfall <wolfsage@gmail.com>
1228
1229       •   Michael Hamlin <myrrhlin@gmail.com>
1230
1231       •   Mohammad S Anwar <mohammad.anwar@yahoo.com>
1232
1233       •   Peter Haworth <peter.haworth@headforwards.com>
1234
1235       •   Philip J. Ludlam <p.ludlam@cv-library.co.uk>
1236
1237       •   Ricardo Signes <rjbs@semiotic.systems>
1238
1239       •   Zoffix Znet <cpan@zoffix.com>
1240
1242       This software is copyright (c) 2003 by Fergal Daly.
1243
1244       This is free software; you can redistribute it and/or modify it under
1245       the same terms as the Perl 5 programming language system itself.
1246
1247
1248
1249perl v5.36.0                      2023-01-20                     Test::Deep(3)
Impressum