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         cmp_deeply([$obj->name], ["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, true, false
732
733         cmp_deeply( $got, bool($value) );
734         cmp_deeply( $got, true );
735         cmp_deeply( $got, false );
736
737       $value is anything you like but it's probably best to use 0 or 1
738
739       This will check that $got_v and $value have the same truth value, that
740       is they will give the same result when used in boolean context, like in
741       an "if()" statement.
742
743       Note: "true" and "false" are only imported by special request.
744
745       code
746
747         cmp_deeply( $got, code(\&subref) );
748
749       "\&subref" is a reference to a subroutine which will be passed a single
750       argument, it then should return a true or false and possibly a string
751
752       This will pass $got_v to the subroutine which returns true or false to
753       indicate a pass or fail. Fails can be accompanied by a diagnostic
754       string which gives an explanation of why it's a fail.
755
756         sub check_name
757         {
758           my $name = shift;
759           if ($boss->likes($name))
760           {
761             return 1;
762           }
763           else
764           {
765             return (0, "the boss doesn't like your name");
766           }
767         }
768
769         cmp_deeply("Brian", code(\&check_name));
770
771   SET COMPARISONS
772       Set comparisons give special semantics to array comparisons:
773
774       •   The order of items in a set is irrelevant
775
776       •   The presence of duplicate items in a set is ignored.
777
778       As such, in any set comparison, the following arrays are equal:
779
780         [ 1, 2 ]
781         [ 1, 1, 2 ]
782         [ 1, 2, 1 ]
783         [ 2, 1, 1 ]
784         [ 1, 1, 2 ]
785
786       All are interpreted by "set" semantics as if the set was only specified
787       as:
788
789         [ 1, 2 ]
790
791       All "set" functions return an object which can have additional items
792       added to it:
793
794         my $set = set( 1, 2 );
795         $set->add(1, 3, 1 );  # Set is now ( 1, 2, 3 )
796
797       Special care must be taken when using special comparisons within sets.
798       See "SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS" for
799       details.
800
801       set
802
803         cmp_deeply( \@got, set(@elements) );
804
805       This does a set comparison, that is, it compares two arrays but ignores
806       the order of the elements and it ignores duplicate elements, but
807       ensures that all items in @elements will be in $got and all items in
808       $got will be in @elements.
809
810       So the following tests will be passes, and will be equivalent:
811
812         cmp_deeply([1, 2, 2, 3], set(3, 2, 1, 1));
813         cmp_deeply([1, 2, 3],    set(3, 2, 1));
814
815       supersetof
816
817         cmp_deeply( \@got, supersetof(@elements) );
818
819       This function works much like "set", and performs a set comparison of
820       $got_v with the elements of @elements.
821
822       "supersetof" is however slightly relaxed, such that $got may contain
823       things not in @elements, but must at least contain all @elements.
824
825       These two statements are equivalent, and will be passes:
826
827         cmp_deeply([1,2,3,3,4,5], supersetof(2,2,3));
828         cmp_deeply([1,2,3,4,5],   supersetof(2,3));
829
830       But these will be failures:
831
832         cmp_deeply([1,2,3,4,5],   supersetof(2,3,6)); # 6 not in superset
833         cmp_deeply([1],           supersetof(1,2));   # 2 not in superset
834
835       subsetof
836
837         cmp_deeply( \@got, subsetof(@elements) );
838
839       This function works much like "set", and performs a set comparison of
840       $got_v with the elements of @elements.
841
842       This is the inverse of "supersetof", which expects all unique elements
843       found in $got_v must be in @elements.
844
845         cmp_deeply([1,2,4,5], subsetof(2,3,3)    ) # Fail: 1,4 & 5 extra
846         cmp_deeply([2,3,3],   subsetof(1,2,4,5)  ) # Fail: 3 extra
847         cmp_deeply([2,3,3],   subsetof(1,2,4,5,3)) # Pass
848
849       none
850
851         cmp_deeply( $got, none(@elements) );
852
853       @elements is an array of elements, wherein no elements in @elements may
854       be equal to $got_v.
855
856       noneof
857
858         cmp_deeply( \@got, noneof(@elements) );
859
860       @elements is an array of elements, wherein no elements in @elements may
861       be found in $got_v.
862
863       For example:
864
865         # Got has no 1, no 2, and no 3
866         cmp_deeply( [1], noneof( 1, 2, 3 ) ); # fail
867         cmp_deeply( [5], noneof( 1, 2, 3 ) ); # pass
868
869   BAG COMPARISONS
870       Bag comparisons give special semantics to array comparisons, that are
871       similar to set comparisons, but slightly different.
872
873       •   The order of items in a bag is irrelevant
874
875       •   The presence of duplicate items in a bag is PRESERVED
876
877       As such, in any bag comparison, the following arrays are equal:
878
879         [ 1, 1, 2 ]
880         [ 1, 2, 1 ]
881         [ 2, 1, 1 ]
882         [ 1, 1, 2 ]
883
884       However, they are NOT equal to any of the following:
885
886         [ 1, 2 ]
887         [ 1, 2, 2 ]
888         [ 1, 1, 1, 2 ]
889
890       All "bag" functions return an object which can have additional items
891       added to it:
892
893         my $bag = bag( 1, 2 );
894         $bag->add(1, 3, 1 );  # Bag is now ( 1, 1, 1, 2, 3 )
895
896       Special care must be taken when using special comparisons within bags.
897       See "SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS" for
898       details.
899
900       bag
901
902         cmp_deeply( \@got, bag(@elements) );
903
904       This does an order-insensitive bag comparison between $got and
905       @elements, ensuring that:
906
907       each item in @elements is found in $got
908       the number of times a $expected_v is found in @elements is reflected in
909       $got
910       no items are found in $got other than those in @elements.
911
912       As such, the following are passes, and are equivalent to each other:
913
914         cmp_deeply([1, 2, 2], bag(2, 2, 1))
915         cmp_deeply([2, 1, 2], bag(2, 2, 1))
916         cmp_deeply([2, 2, 1], bag(2, 2, 1))
917
918       But the following are failures:
919
920         cmp_deeply([1, 2, 2],     bag(2, 2, 1, 1)) # Not enough 1's in Got
921         cmp_deeply([1, 2, 2, 1],  bag(2, 2, 1)   ) # Too many   1's in Got
922
923       superbagof
924
925         cmp_deeply( \@got, superbagof( @elements ) );
926
927       This function works much like "bag", and performs a bag comparison of
928       $got_v with the elements of @elements.
929
930       "superbagof" is however slightly relaxed, such that $got may contain
931       things not in @elements, but must at least contain all @elements.
932
933       So:
934
935         # pass
936         cmp_deeply( [1, 1, 2], superbagof( 1 )      );
937
938         # fail: not enough 1's in superbag
939         cmp_deeply( [1, 1, 2], superbagof( 1, 1, 1 ));
940
941       subbagof
942
943         cmp_deeply( \@got, subbagof(@elements) );
944
945       This function works much like "bag", and performs a bag comparison of
946       $got_v with the elements of @elements.
947
948       This is the inverse of "superbagof", and expects all elements in $got
949       to be in @elements, while allowing items to exist in @elements that are
950       not in $got
951
952         # pass
953         cmp_deeply( [1],        subbagof( 1, 1, 2 ) );
954
955         # fail: too many 1's in subbag
956         cmp_deeply( [1, 1, 1],  subbagof( 1, 1, 2 ) );
957
958   HASH COMPARISONS
959       Typically, if you're doing simple hash comparisons,
960
961         cmp_deeply( \%got, \%expected )
962
963       is sufficient. "cmp_deeply" will ensure %got and %hash have identical
964       keys, and each key from either has the same corresponding value.
965
966       superhashof
967
968         cmp_deeply( \%got, superhashof(\%hash) );
969
970       This will check that the hash %$got is a "super-hash" of %hash. That is
971       that all the key and value pairs in %hash appear in %$got but %$got can
972       have extra ones also.
973
974       For example
975
976         cmp_deeply({a => 1, b => 2}, superhashof({a => 1}))
977
978       will pass but
979
980         cmp_deeply({a => 1, b => 2}, superhashof({a => 1, c => 3}))
981
982       will fail.
983
984       subhashof
985
986         cmp_deeply( \%got, subhashof(\%hash) );
987
988       This will check that the hash %$got is a "sub-hash" of %hash. That is
989       that all the key and value pairs in %$got also appear in %hash.
990
991       For example
992
993         cmp_deeply({a => 1}, subhashof({a => 1, b => 2}))
994
995       will pass but
996
997         cmp_deeply({a => 1, c => 3}, subhashof({a => 1, b => 2}))
998
999       will fail.
1000

DIAGNOSTIC FUNCTIONS

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

ANOTHER EXAMPLE

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

USING TEST::DEEP WITH TEST::BUILDER

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

LIMITATIONS

1096       Currently any CODE, GLOB or IO refs will be compared using shallow(),
1097       which means only their memory addresses are compared.
1098

BUGS

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

CAVEATS

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

WHAT ARE SPECIAL COMPARISONS?

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

EXPORTS

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

SEE ALSO

1172       Test::More
1173

MAINTAINER

1175         Ricardo Signes <rjbs@cpan.org>
1176

AUTHOR

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