root/branches/release-40/t/lib/Test/Deep.pod @ 2583

Revision 2583, 27.7 kB (checked in by mpaschal, 18 months ago)

Add Test::Deep to t/lib, since we already use it in driver-tests.pl
BugzID: 79953

Line 
1=head1 NAME
2
3Test::Deep - Extremely flexible deep comparison
4
5=head1 SYNOPSIS
6
7  use Test::More tests => $Num_Tests;
8  use Test::Deep;
9
10  cmp_deeply(
11    $actual_horrible_nested_data_structure,
12    $expected_horrible_nested_data_structure,
13    "got the right horrible nested data structure"
14  );
15
16  cmp_deeply(
17    $object,
18    methods(name => "John", phone => "55378008"),
19    "object methods ok"
20  );
21
22  cmp_deeply(
23    \@array,
24    [$hash1, $hash2, ignore(
25    "first 2 elements are as expected, ignoring 3"
26  );
27
28  cmp_deeply(
29    $object,
30    noclass({value => 5}),
31    "object looks ok, not checking it's class"
32  );
33
34  cmp_deeply(
35    \@result,
36    bag('a', 'b', {key => [1, 2]}),
37    "array has the 3 things we wanted in some order"
38  );
39
40=head1 DESCRIPTION
41
42If you don't know anything about automated testing in Perl then you should
43probably read about Test::Simple and Test::More before preceding.
44Test::Deep uses the Test::Builder framework.
45
46Test::Deep gives you very flexible ways to check that the result you got is
47the result you were expecting. At it's simplest it compares two structures
48by going through each level, ensuring that the values match, that arrays and
49hashes have the same elements and that references are blessed into the
50correct class. It also handles circular data structures without getting
51caught in an infinite loop.
52
53Where it becomes more interesting is in allowing you to do something besides
54simple exact comparisons. With strings, the C<eq> operator checks that 2
55strings are exactly equal but sometimes that's not what you want. When you
56don't know exactly what the string should be but you do know some things
57about how it should look, C<eq> is no good and you must use pattern matching
58instead. Test::Deep provides pattern matching for complex data structures
59
60=head1 EXAMPLES
61
62How Test::Deep works is much easier to understand by seeing some examples.
63
64=head2 Without Test::Deep
65
66Say you want to test a function which returns a string. You know that your
67string should be a 7 digit number beginning with 0, C<eq> is no good in this
68situation, you need a regular expression. So you could use Test::More's
69C<like()> function:
70
71  like($string, '/^0d{6}$/', "number looks good");
72
73Similarly, to check that a string looks like a name, you could do:
74
75  like($string, '/^(Mr|Mrs|Miss) \w+ \w+$/',
76    "got title, first and last name");
77
78Now imagine your function produces a hash with some personal details in it.
79You want to make sure that there are 2 keys, Name and Phone and that the
80name looks like a name and the phone number looks like a phone number. You
81could do:
82
83  $hash = make_person();
84  like($hash->{Name}, '/^(Mr|Mrs|Miss) \w+ \w+$/', "name ok");
85  like($hash->{Phone}, '/^0d{6}$/', "phone ok");
86  is(scalar keys %$hash, 2, "correct number of keys");
87
88But that's not quite right, what if make_person has a serious problem and
89didn't even return a hash? We really need to write
90
91  if (ref($hash) eq "HASH")
92  {
93    like($hash->{Name}, '/^(Mr|Mrs|Miss) \w+ \w+$/', "name ok");
94    like($hash->{Phone}, '/^0d{6}$/', "phone ok");   
95    is(scalar keys %$hash, 2, "correct number of keys");
96  }
97  else
98  {
99    fail("person not a hash");
100    fail("person not a hash");
101    fail("person not a hash"); # need 3 to keep the plan correct
102  }
103
104Already this is getting messy, now imagine another entry in the hash, an
105array of children's names. This would require
106
107
108  if (ref($hash) eq "HASH")
109  {
110    like($hash->{Name}, $name_pat, "name ok");
111    like($hash->{Phone}, '/^0d{6}$/', "phone ok");
112    my $cn = $hash->{ChildNames};
113    if (ref($cn) eq "ARRAY")
114    {
115      foreach my $child (@$cn)
116      {
117        like($child, $name_pat);
118      }
119    }
120    else
121    {
122        fail("child names not an array")
123    }
124  }
125  else
126  {
127    fail("person not a hash");
128  }
129
130This is a horrible mess and because we don't know in advance how many
131children's names there will be, we can't make a plan for our test anymore
132(actually, we could but it would make things even more complicated).
133
134Test::Deep to the rescue.
135
136=head2 With Test::Deep
137
138  my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
139  cmp_deeply(
140    $person,
141    {
142      Name => $name_re,
143      Phone => re('^0d{6}$'),
144      ChildNames => array_each($name_re)
145    },
146    "person ok"
147  );
148
149This will do everything that the messy code above does and it will give a
150sensible message telling you exactly what went wrong if it finds a part of
151$person that doesn't match the pattern. C<re()> and C<array_each()> are
152special function imported from Test::Deep. They create a marker that tells
153Test::Deep that something different is happening here. Instead of just doing
154a simple comparison and checking are two things exactly equal, it should do
155something else.
156
157If a person was asked to check that 2 structures are equal, they could print
158them both out and compare them line by line. The markers above are similar
159to writing a note in red pen on one of the printouts telling the person that
160for this piece of the structure, they should stop doing simple line by line
161comparison and do something else.
162
163C<re($regex)> means that Test::Deep should check that the current piece of
164data matches the regex in C<$regex>. C<array_each($struct)> means that
165Test::Deep should expect the current piece of data to be an array and it
166should check that every element of that array matches C<$struct>.
167In this case, every element of C<$person->{ChildNames}> should look like a
168name. If say the 3rd one didn't you would get an error message something
169like
170
171  Using Regexp on $data->{ChildNames}[3]
172     got    : 'Queen John Paul Sartre'
173     expect : /^(Mr|Mrs|Miss) \w+ \w+$/
174
175There are lots of other special comparisons available, see
176L<SPECIAL COMPARISONS PROVIDED> below for the full list.
177
178=head2 Reusing structures
179
180Test::Deep is good for reusing test structures so you can do this
181
182  my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
183  my $person_cmp = {
184    Name => $name_re,
185    Phone => re('^0d{6}$'),
186    ChildNames => array_each($name_re)
187  };
188
189  cmp_deeply($person1, $person_cmp, "person ok");
190  cmp_deeply($person2, $person_cmp, "person ok");
191  cmp_deeply($person3, $person_cmp, "person ok");
192
193You can even put $person_cmp in a module and let other people use it when
194they are writing test scripts for modules that use your modules.
195
196To make things a little more difficult, lets change the person data
197structure so that instead of a list of ChildNames, it contains a list of
198hashes, one for each child. So in fact our person structure will contain
199other person structures which may contain other person structures and so on.
200This is easy to handle with Test::Deep because Test::Deep structures can
201include themselves. Simply do
202
203  my $name_re = re('^(Mr|Mrs|Miss) \w+ \w+$');
204  my $person_cmp = {
205    Name => $name_re,
206    Phone => re('^0d{6}$'),
207    # note no mention of Children here
208  };
209
210  $person_cmp->{Children} = each_array($person_cmp);
211
212  cmp_deeply($person, $person_cmp, "person ok");
213
214This will now check that $person->{Children} is an array and that every
215element of that array also matches C<$person_cmp>, this includes checking
216that it's children also match the same pattern and so on.
217
218=head2 Circular data structures
219
220A circular data structure is one which loops back on itself, you can make
221one easily by doing
222
223  my @b;
224  my @a = (1, 2, 3, \@b);
225  push(@b, \@a);
226
227now @a contains a reference to be @b and @b contains a reference to @a. This
228causes problems if you have a program that wants to look inside @a and keep
229looking deeper and deeper at every level, it could get caught in an infinite
230loop looking into @a then @b then @a then @b and so on.
231
232Test::Deep avoids this problem so we can extend our example further by
233saying 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
247So this will check that for each child C<$child> in C<$person->{Children}>
248that the C<$child->{Parents} matches C<$person_cmp> however it is smart
249enough not to get caught in an infinite loop where it keeps bouncing between
250the same Parent and Child.
251
252=head1 TERMINOLOGY
253
254C<cmp_deeply($got, $expected, $name)> takes 3 arguments. C<$got> is the
255structure that you are checking, you must not include any special
256comparisons in this structure or you will get a fatal error. C<$expected>
257describes what Test::Deep will be looking for in $got. You can put special
258comparisons in $expected if you want to.
259
260As Test::Deep descends through the 2 structures, it compares them one piece
261at a time, so at any point in the process, Test::Deep is thinking about 2
262things - the current value from C<$got> and the current value from
263C<$expected>. In the documentation, I call them C<$got_v> and C<exp_v>
264respectively.
265
266=head1 COMPARISON FUNCTIONS
267
268=head3 $ok = cmp_deeply($got, $expected, $name)
269
270$got is the result to be checked. $expected is the structure against which
271$got will be check. $name is the test name.
272
273This is the main comparison function, the others are just wrappers around
274this. Without any special comparisons, it will descend into $expected,
275following every reference and comparing C<$expected_v> to C<$got_v> (using
276C<eq>) at the same position. If at any stage C<$expected_v> is a special
277comparison then Test::Deep may do something else besides a simple string
278comparison, exactly what it does depends on which special comparison it is.
279
280=head3 $ok = cmp_bag(\@got, \@bag, $name)
281
282Is shorthand for cmp_deeply(\@got, bag(@bag), $name)
283
284=head3 $ok = cmp_set(\@got, \@set, $name)
285
286Is shorthand for cmp_deeply(\@got, set(@set), $name)
287
288=head3 $ok = cmp_methods(\@got, \@methods, $name)
289
290Is shorthand for cmp_deeply(\@got, methods(@methods), $name)
291
292=head3 $ok = eq_deeply($got, $expected)
293
294This is the same as cmp_deeply() except it just returns true or
295false. It does not create diagnostics or talk to L<Test::Builder>, but
296if you want to use it in a non-testing environment then you should
297import it through L<Test::Deep::NoTest>. For example
298
299  use Test::Deep::NoTest;
300  print "a equals b" unless eq_deeply($a, $b);
301
302otherwise the L<Test::Builder> framework will be loaded and testing messages
303will be output when your program ends.
304
305=head1 SPECIAL COMPARISONS PROVIDED
306
307=head3 ignore()
308
309This makes Test::Deep skip tests on $got_v. No matter what value C<$got_v>
310has, Test::Deep will think it's correct. This is useful if some part of the
311structure you are testing is very complicated and already tested elsewhere,
312or is unpredictable.
313
314  cmp_deeply($got, { name => 'John', random => ignore(), address => ['5 A
315    street', 'a town', 'a country'],
316  })
317
318is the equivalent of checking
319
320  $got->{name} eq 'John';
321  exists $got->{random};
322  cmp_deeply($got->{address};
323  ['5 A street', 'a town', 'a country']);
324
325=head3 methods(%hash)
326
327%hash is a hash of method call => expected value pairs.
328
329This lets you call methods on an object and check the result of each call.
330The methods will be called in the order supplied. If you want to pass
331arguments to the method you should wrap the method name and arguments in an
332array reference.
333
334  cmp_deeply(
335    $obj,
336    methods(name => "John", ["favourite", "food"] => "taco")
337  );
338
339is roughly the equivalent of checking that
340
341  $obj->name eq "John"
342  $obj->favourite("food") eq "taco"
343
344The methods will be called in the order you supply them and will be called
345in scalar context. If you need to test methods called in list context then
346you should use listmethods().
347
348B<NOTE> Just as in a normal test script, you need to be careful if the
349methods you call have side effects like changing the object or other objects
350in the structure. Although the order of the methods is fixed, the order of
351some other tests is not so if $expected is
352
353  {
354    manager => methods(@manager_methods),
355    coder => methods(@coder_methods)
356  }
357
358there is no way to know which if manager and coder will be tested first. If
359the methods you are testing depend on and alter global variables or if
360manager and coder are the same object then you may run into problems.
361
362=head3 listmethods(%hash)
363
364%hash is a hash of method call => expected value pairs.
365
366This is almost identical to methods() except the methods are called in list
367context instead of scalar context. This means that the expected values
368supplied must be an array reference.
369
370  cmp_deeply(
371    $obj,
372    listmethods(
373      name => "John",
374      ["favourites", "food"] => ["Mapo tofu", "Gongbao chicken"]
375    )
376  );
377
378is the equivalent of checking that
379
380  $obj->name eq "John"
381  cmp_deeply([$obj->favourites("food")], ["Mapo tofu", "Gongbao chicken"]);
382
383The methods will be called in the order you supply them.
384
385B<NOTE> The same caveats apply as for methods().
386
387=head3 shallow($thing)
388
389$thing is a ref.
390
391This prevents Test::Deep from looking inside $thing. It allows you to
392check that $got_v and $thing are references to the same variable. So
393
394  my @a = @b = (1, 2, 3);
395  cmp_deeply(\@a, \@b);
396
397will pass because @a and @b have the same elements however
398
399  cmp_deeply(\@a, shallow(\@b))
400
401will fail because although \@a and \@b both contain C<1, 2, 3> they are
402references to different arrays.
403
404=head3 noclass($thing)
405
406$thing is a structure to be compared against.
407
408This makes Test::Deep ignore the class of objects, so it just looks at the
409data they contain. Class checking will be turned off until Test::Deep is
410finished comparing C<$got_v> against C<$thing>. Once Test::Deep comes out of
411C<$thing> it will go back to it's previous setting for checking class.
412
413This can be useful when you want to check that objects have been
414constructed correctly but you don't want to write lots of
415C<bless>es. If \@people is an array of Person objects then
416
417  cmp_deeply(\@people, noclass([
418    bless {name => 'John', phone => '555-5555'}, "Person",
419    bless {name => 'Anne', phone => '444-4444'}, "Person",
420  ]));
421
422can be replaced with
423
424  cmp_deeply(\@people, noclass([
425    {name => 'John', phone => '555-5555'},
426    {name => 'Anne', phone => '444-4444'}
427  ]));
428
429However, this is testing so you should also check that the objects are
430blessed correctly. You could use a map to bless all those hashes or you
431could do a second test like
432
433  cmp_deeply($people, array_each(isa("Person"));
434
435=head3 useclass($thing)
436
437This turns back on the class comparison while inside a noclass().
438
439  cmp_deeply(
440    $got,
441    noclass(
442      [
443        useclass( $object )
444      ]
445    )
446  )
447
448In this example the class of the array reference in C<$got> is ignored but
449the class of C<$object> is checked, as is the class of everything inside
450C<$object>.
451
452=head3 re($regexp, $capture_data, $flags)
453
454$regexp is either a regular expression reference produced with C<qr/.../> or
455a string which will be used to construct a regular expression.
456
457$capture_data is optional and is used to check the strings captured by
458an regex. This should can be an array ref or a Test::Deep comparator
459that works on array refs.
460
461$flags is an optional string which controls whether the regex runs as a
462global match. If $flags is "g" then the regex will run as m/$regexp/g.
463
464Without $capture_data, this simply compares $got_v with the regular expression
465provided. So
466
467  cmp_deeply($got, [ re("ferg") ])
468
469is the equivalent of
470
471  $got->[0] =~ /ferg/
472
473With $capture_data
474
475  cmp_deeply($got, [re($regex, $capture_data])
476
477is the equivalent of
478
479  my @data = $got->[0] =~ /$regex/;
480  cmp_deeply(\@data, $capture_data);
481
482So you can do something simple like
483
484  cmp_deeply($got, re(qr/(\d\d)(\w\w)/, [25, "ab" ])
485
486to check that (\d\d) was 25 and (\w\w) was "ab" but you can also use
487Test::Deep objects to do more complex testing of the captured values
488
489  cmp_deeply("cat=2,dog=67,sheep=3,goat=2,dog=5",
490    re(qr/(\D+)=\d+,?/, set(qw( cat sheep dog )), "g"))
491
492here, the regex will match the string and will capture the animal names and
493check that they match the specified set, in this case it will fail,
494complaining that "goat" is not in the set.
495
496=head3 superhashof(\%hash)
497
498This will check that the hash C<%$got> is a "super-hash" of C<%hash>. That
499is that all the key and value pairs in C<%hash> appear in C<%$got> but
500C<%$got> can have extra ones also.
501
502For example
503
504  cmp_deeply({a => 1, b => 2}, superhashof({a => 1}))
505
506will pass but
507
508  cmp_deeply({a => 1, b => 2}, superhashof({a => 1, c => 3}))
509
510will fail.
511
512=head3 subhashof(\%hash)
513
514This will check that the hash C<%$got> is a "sub-hash" of C<%hash>. That is
515that all the key and value pairs in C<%$got> also appear in C<%hash>.
516
517For example
518
519  cmp_deeply({a => 1}, subhashof({a => 1, b => 2}))
520
521will pass but
522
523  cmp_deeply({a => 1, c => 3}, subhashof({a => 1, b => 2}))
524
525will fail.
526
527=head3 bag(@elements)
528
529@elements is an array of elements.
530
531This does a bag comparison, that is, it compares two arrays but ignores the
532order of the elements so
533
534  cmp_deeply([1, 2, 2], bag(2, 2, 1))
535
536will be a pass.
537
538The object returned by bag() has an add() method.
539
540  my $bag = bag(1, 2, 3);
541  $bag->add(2, 3, 4);
542
543will result in a bag containing 1, 2, 2, 3, 3, 4.
544
545C<NOTE> If you use certain special comparisons within a bag or set
546comparison there is a danger that a test will fail when it should have
547passed. It can only happen if two or more special comparisons in the bag are
548competing to match elements. Consider this comparison
549
550  cmp_deeply(['furry', 'furball'], bag(re("^fur"), re("furb")))
551
552There are two things that could happen, hopefully C<re("^fur")> is paired with
553"furry" and C<re("^furb")> is paired with "furb" and everything is fine but it
554could happen that C<re("^fur")> is paired with "furball" and then C<re("^furb")>
555cannot find a match and so the test fails. Examples of other competing
556comparisons are C<bag(1, 2, 2)> vs C<set(1, 2)> and
557C<methods(m1 => "v1", m2 => "v2")> vs C<methods(m1 => "v1")>
558
559This problem is could be solved by using a slower and more complicated
560algorithm for set and bag matching. Something for the future...
561
562=head3 set(@elements)
563
564@elements is an array of elements.
565
566This does a set comparison, that is, it compares two arrays but ignores the
567order of the elements and it ignores duplicate elements, so
568
569  cmp_deeply([1, 2, 2, 3], set(3, 2, 1, 1))
570
571will be a pass.
572
573The object returned by set() has an add() method.
574
575  my $set = set(1, 2, 3);
576  $set->add(4, 5, 6);
577
578will result in a set containing 1, 2, 3, 4, 5, 5.
579
580C<NOTE> See the NOTE on the bag() comparison for some dangers in using
581special comparisons inside set()
582
583=head3 superbagof(@elements), subbagof(@elements), supersetof(@elements) and subsetof(@elements)
584
585@elements is an array of elements.
586
587These do exactly what you'd expect them to do, so for example
588
589  cmp_deeply($data, subbagof(1, 1, 3, 4));
590
591checks that @$data contains at most 2 "1"s, 1 "3" and 1 "4" and
592
593  cmp_deeply($data, supsersetof(1, 4));
594
595check that @$data contains at least 1 "1" and 1 "4".
596
597These are just special cases of the Set and Bag comparisons so they also
598give you an add() method and they also have the same limitations when using
599special comparisons inside them (see the NOTE in the bag() section).
600
601=head3 all(@expecteds)
602
603@expecteds is an array of expected structures.
604
605This allows you to compare data against multiple expected results and make
606sure each of them matches.
607
608  cmp_deeply($got, all(isa("Person"), methods(name => 'John')))
609
610is equivalent to
611
612  $got->isa("Person")
613  $got->name eq 'John'
614
615If either test fails then the whole thing is considered a fail. This is a
616short-circuit test, the testing is stopped after the first failure, although
617in the future it may complete all tests so that diagnostics can be output
618for all failures. When reporting failure, the parts are counted from 1.
619
620Thanks to the magic of overloading, you can write
621
622  all(isa("Person"), methods(name => 'John'), re("^wi"))
623
624as
625
626  isa("Person") & methods(name => 'John') | re("^wi")
627
628Note B<single> | not double as || cannot be overloaded. This will only work
629when there is a special comparison involved. If you write
630
631  "john" | "anne" | "robert"
632
633Perl will turn this into
634
635  "{onort"
636
637which is presumably not what you wanted. This is because Perl |s them
638together as strings before Test::Deep gets a chance to do any overload
639tricks.
640
641=head3 any(@expecteds)
642
643@expecteds is an array of expected structures.
644
645This can be used to compare data against multiple expected results and make
646sure that at least one of them matches. This is a short-circuit test so if
647a test passes then none of the tests after that will be attempted.
648
649You can also use overloading with | similarly to all().
650
651=head3 isa($class), Isa($class)
652
653$class is a class name.
654
655This uses UNIVERSAL::isa() to check that $got_v is blessed into the class
656$class.
657
658B<NOTE:> Isa() does exactly as documented here, isa() is slightly
659different. If isa() is called with 1 argument it falls through to
660Isa(). If isa() called with 2 arguments, it falls through to
661UNIVERAL::isa. This is to prevent breakage when you import isa() into
662a package that is used as a class. Without this, anyone calling
663C<Class-E<gt>isa($other_class)> would get the wrong answer. This is a
664hack to patch over the fact that isa is exported by default.
665
666=head3 array_each($thing)
667
668$thing is a structure to be compared against.
669
670<$got_v> must be an array reference. Each element of it will be compared to
671$thing. This is useful when you have an array of similar things, for example
672objects of a known type and you don't want to have to repeat the same test
673for each one.
674
675  my $common_tests = all(
676     isa("MyFile"),
677     methods(
678       handle => isa("IO::Handle")
679       filename => re("^/home/ted/tmp"),
680    )
681  );
682
683  cmp_deeply($got, array_each($common_tests));
684
685is similar to
686
687  foreach my $got_v (@$got) {
688    cmp_deeply($got_v, $common_tests)
689  }
690
691Except it will not explode is $got is not an array reference. It will check
692that each of the objects in @$got is a MyFile and that each one gives the
693correct results for it's methods.
694
695You could go further, if for example there were 3 files and you knew the
696size of each one you could do this
697
698  cmp_deeply(
699    $got,
700    all(
701      array_each($common_tests),
702      [
703        methods(size => 1000),
704        methods(size => 200),
705        methods(size => 20)
706      ]
707    )
708  )
709  cmp_deeply($got, array_each($structure));
710
711=head3 str($string)
712
713$string is a string.
714
715This will stringify C<$got_v> and compare it to C<$string> using C<eq>, even
716if $got_v is a ref. It is useful for checking the stringified value of an
717overloaded reference.
718
719=head3 num($number, $tolerance)
720
721$number is a number.
722$tolerance is an optional number.
723
724This will add 0 to C<$got_v> and check if it's numerically equal to
725C<$number>, even if $got_v is a ref. It is useful for checking the numerical
726value of an overloaded reference. If $tolerance is supplied then this will
727check that $got_v and $exp_v are less than $tolerance apart. This is useful
728when comparing floating point numbers as rounding errors can make it hard or
729impossible for $got_v to be exactly equal to $exp_v. When $tolerance is
730supplied, the test passes if C<abs($got_v - $exp_v) <= $tolerance>.
731
732B<Note> in Perl, C<"12blah" == 12> because Perl will be smart and convert
733"12blah" into 12. You may not want this. There was a strict mode but that is
734now gone. A "lookslike s number" test will replace it soon. Until then you
735can usually just use the string() comparison to be more strict. This will
736work fine for almost all situations, however it will not work when <$got_v>
737is an overloaded value who's string and numerical values differ.
738
739=head3 bool($value)
740
741$value is anything you like but it's probably best to use 0 or 1
742
743This will check that C<$got_v> and C<$value> have the same truth value, that
744is they will give the same result when used in boolean context, like in an
745if() statement.
746
747=head3 code(\&subref)
748
749\&subref is a reference to a subroutine which will be passed a single
750argument, it then should return a true or false and possibly a string
751
752This will pass C<$got_v> to the subroutine which returns true or false to
753indicate a pass or fail. Fails can be accompanied by a diagnostic string
754which 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", \&check_name);
770
771=head1 ANOTHER EXAMPLE
772
773You've written a module to handle people and their film interests. Say you
774have a function that returns an array of people from a query, each person is
775a hash with 2 keys: Name and Age and the array is sorted by Name. You can do
776
777  cmp_deeply(
778    $result,
779    [
780      {Name => 'Anne', Age => 26},
781      {Name => "Bill", Age => 47}
782      {Name => 'John', Age => 25},
783    ]
784  );
785
786Soon after, your query function changes and all the results now have an ID
787field. Now your test is failing again because you left out ID from each of
788the hashes. The problem is that the IDs are generated by the database and
789you have no way of knowing what each person's ID is. With Test::Deep you can
790change your query to
791
792  cmp_deeply(
793    $result,
794    [
795      {Name => 'John', Age => 25, ID => ignore()},
796      {Name => 'Anne', Age => 26, ID => ignore()},
797      {Name => "Bill", Age => 47, ID => ignore()}
798    ]
799  );
800
801But your test still fails. Now, because you're using a database, you no
802longer know what order the people will appear in. You could add a sort into
803the database query but that could slow down your application. Instead you
804can get Test::Deep to ignore the order of the array by doing a bag
805comparison instead.
806
807  cmp_deeply(
808    $result,
809    bag(
810      {Name => 'John', Age => 25, ID => ignore()},
811      {Name => 'Anne', Age => 26, ID => ignore()},
812      {Name => "Bill", Age => 47, ID => ignore()}
813    )
814  );
815
816Finally person gets even more complicated and includes a new field called
817Movies, this is a list of movies that the person has seen recently, again
818these movies could also come back in any order so we need a bag inside our
819other bag comparison, giving us something like
820
821  cmp_deeply(
822  $result,
823    bag(
824      {Name => 'John', Age => 25, ID => ignore(), Movies => bag(...)},
825      {Name => 'Anne', Age => 26, ID => ignore(), Movies => bag(...)},
826      {Name => "Bill", Age => 47, ID => ignore(), Movies => bag(...)}
827    )
828  );
829
830=head1 LIMITITATIONS
831
832Currently any CODE, GLOB or IO refs will be compared using shallow(), which
833means only their memory addresses are compared.
834
835=head1 BUGS
836
837There is a bug in set and bag compare to do with competing SCs. It only
838occurs when you put certain special comparisons inside bag or set
839comparisons you don't need to worry about it. The full details are in the
840bag() docs. It will be fixed in an upcoming version.
841
842=head1 WHAT ARE SPECIAL COMPARISONS?
843
844A special comparison (SC) is simply an object that inherits from
845Test::Deep::Cmp. Whenever C<$expected_v> is an SC then instead of checking
846C<$got_v eq $expected_v>, we pass control over to the SC and let it do it's
847thing.
848
849Test::Deep exports lots of SC constructors, to make it easy for you to use
850them in your test scripts. For example is C<re("hello")> is just a handy way
851of creating a Test::Deep::Regexp object that will match any string containing
852"hello". So
853
854  cmp_deeply([ 'a', 'b', 'hello world'], ['a', 'b', re("^hello")]);
855
856will check C<'a' eq 'a'>, C<'b' eq 'b'> but when it comes to comparing
857C<'hello world'> and C<re("^hello")> it will see that
858$expected_v is an SC and so will pass control to the Test::Deep::Regexp class
859by do something like C<$expected_v->descend($got_v)>. The C<descend()>
860method should just return true or false.
861
862This gives you enough to write your own SCs but I haven't documented how
863diagnostics works because it's about to get an overhaul.
864
865=head1 SEE ALSO
866
867L<Test::More>
868
869=head1 AUTHOR
870
871Fergal Daly E<lt>fergal@esatclear.ieE<gt>, with thanks to Michael G Schwern
872for Test::More's is_deeply function which inspired this.
873
874=head1 COPYRIGHT
875
876Copyright 2003, 2004 by Fergal Daly E<lt>fergal@esatclear.ieE<gt>.
877
878This program is free software; you can redistribute it and/or
879modify it under the same terms as Perl itself.
880
881See F<http://www.perl.com/perl/misc/Artistic.html>
882
883=cut
Note: See TracBrowser for help on using the browser.