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

NAME

6       Test::Deep - Extremely flexible deep comparison
7

SYNOPSIS

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

DESCRIPTION

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

EXAMPLES

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

TERMINOLOGY

253       "cmp_deeply($got, $expected, $name)" takes 3 arguments. $got is the
254       structure that you are checking, you must not include any special
255       comparisons in this structure or you will get a fatal error. $expected
256       describes what Test::Deep will be looking for in $got. You can put
257       special comparisons in $expected if you want to.
258
259       As Test::Deep descends through the 2 structures, it compares them one
260       piece at a time, so at any point in the process, Test::Deep is thinking
261       about 2 things - the current value from $got and the current value from
262       $expected. In the documentation, I call them $got_v and "exp_v"
263       respectively.
264

COMPARISON FUNCTIONS

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

SPECIAL COMPARISONS PROVIDED

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

DIAGNOSTIC FUNCTIONS

998       deep_diag
999
1000         my $reason = deep_diag($stack);
1001
1002       $stack is a value returned by cmp_details.  Do not call this function
1003       if cmp_details returned a true value for $ok.
1004
1005       "deep_diag()" returns a human readable string describing how the
1006       comparison failed.
1007

ANOTHER EXAMPLE

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

USING TEST::DEEP WITH TEST::BUILDER

1068       Combining "cmp_details" and "deep_diag" makes it possible to use
1069       Test::Deep in your own test classes.
1070
1071       In a Test::Builder subclass, create a test method in the following
1072       form:
1073
1074         sub behaves_ok {
1075           my $self = shift;
1076           my $expected = shift;
1077           my $test_name = shift;
1078
1079           my $got = do_the_important_work_here();
1080
1081           my ($ok, $stack) = cmp_details($got, $expected);
1082           unless ($Test->ok($ok, $test_name)) {
1083             my $diag = deep_diag($stack);
1084             $Test->diag($diag);
1085           }
1086         }
1087
1088       As the subclass defines a test class, not tests themselves, make sure
1089       it uses Test::Deep::NoTest, not "Test::Deep" itself.
1090

LIMITATIONS

1092       Currently any CODE, GLOB or IO refs will be compared using shallow(),
1093       which means only their memory addresses are compared.
1094

BUGS

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

CAVEATS

1102   SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS
1103       If you use certain special comparisons within a bag or set comparison
1104       there is a danger that a test will fail when it should have passed. It
1105       can only happen if two or more special comparisons in the bag are
1106       competing to match elements.  Consider this comparison
1107
1108         cmp_deeply(['furry', 'furball'], bag(re("^fur"), re("furb")))
1109
1110       There are two things that could happen, hopefully "re("^fur")" is
1111       paired with "furry" and "re("^furb")" is paired with "furb" and
1112       everything is fine but it could happen that "re("^fur")" is paired with
1113       "furball" and then "re("^furb")" cannot find a match and so the test
1114       fails. Examples of other competing comparisons are "bag(1, 2, 2)" vs
1115       "set(1, 2)" and "methods(m1 => "v1", m2 => "v2")" vs "methods(m1 =>
1116       "v1")"
1117
1118       This problem is could be solved by using a slower and more complicated
1119       algorithm for set and bag matching. Something for the future...
1120

WHAT ARE SPECIAL COMPARISONS?

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

EXPORTS

1145       By default, Test::Deep will export everything in its "v0" tag, as if
1146       you had written:
1147
1148         use Test::Deep ':v0';
1149
1150       Those things are:
1151
1152         all any array array_each arrayelementsonly arraylength arraylengthonly bag
1153         blessed bool cmp_bag cmp_deeply cmp_methods cmp_set code eq_deeply hash
1154         hash_each hashkeys hashkeysonly ignore Isa isa listmethods methods noclass
1155         none noneof num obj_isa re reftype regexpmatches regexponly regexpref
1156         regexprefonly scalarrefonly scalref set shallow str subbagof subhashof
1157         subsetof superbagof superhashof supersetof useclass
1158
1159       A slightly better set of exports is the "v1" set.  It's all the same
1160       things, with the exception of "Isa" and "blessed".  If you want to
1161       import "everything", you probably want to "use Test::Deep ':V1';".
1162
1163       There's another magic export group:  ":preload".  If that is specified,
1164       all of the Test::Deep plugins will be loaded immediately instead of
1165       lazily.
1166

SEE ALSO

1168       Test::More
1169

MAINTAINER

1171         Ricardo Signes <rjbs@cpan.org>
1172

AUTHOR

1174       Fergal Daly <fergal@esatclear.ie>, with thanks to Michael G Schwern for
1175       Test::More's is_deeply function which inspired this.
1176
1177       Please do not bother Fergal Daly with bug reports.  Send them to the
1178       maintainer (above) or submit them at the issue tracker
1179       <https://github.com/rjbs/Test-Deep/issues>.
1180
1182       Copyright 2003, 2004 by Fergal Daly <fergal@esatclear.ie>.
1183
1184       This program is free software; you can redistribute it and/or modify it
1185       under the same terms as Perl itself.
1186
1187       See http://www.perl.com/perl/misc/Artistic.html
1188
1189
1190
1191perl v5.30.0                      2019-07-26                     Test::Deep(3)
Impressum