Gestionnaire de fichiers - Editer - /usr/local/share/man/man3/Test::Deep.3pm
Arrière
.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Test::Deep 3" .TH Test::Deep 3 "2018-04-19" "perl v5.16.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::Deep \- Extremely flexible deep comparison .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Test::More tests => $Num_Tests; \& use Test::Deep; \& \& cmp_deeply( \& $actual_horrible_nested_data_structure, \& $expected_horrible_nested_data_structure, \& "got the right horrible nested data structure" \& ); \& \& cmp_deeply( \& $object, \& methods(name => "John", phone => "55378008"), \& "object methods ok" \& ); \& \& cmp_deeply( \& \e@array, \& [$hash1, $hash2, ignore()], \& "first 2 elements are as expected, ignoring 3" \& ); \& \& cmp_deeply( \& $object, \& noclass({value => 5}), \& "object looks ok, not checking its class" \& ); \& \& cmp_deeply( \& \e@result, \& bag(\*(Aqa\*(Aq, \*(Aqb\*(Aq, {key => [1, 2]}), \& "array has the 3 things we wanted in some order" \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" If you don't know anything about automated testing in Perl then you should probably read about Test::Simple and Test::More before preceding. Test::Deep uses the Test::Builder framework. .PP Test::Deep gives you very flexible ways to check that the result you got is the result you were expecting. At its simplest it compares two structures by going through each level, ensuring that the values match, that arrays and hashes have the same elements and that references are blessed into the correct class. It also handles circular data structures without getting caught in an infinite loop. .PP Where it becomes more interesting is in allowing you to do something besides simple exact comparisons. With strings, the \f(CW\*(C`eq\*(C'\fR operator checks that 2 strings are exactly equal but sometimes that's not what you want. When you don't know exactly what the string should be but you do know some things about how it should look, \f(CW\*(C`eq\*(C'\fR is no good and you must use pattern matching instead. Test::Deep provides pattern matching for complex data structures .PP Test::Deep has \fB\f(BIa lot\fB\fR of exports. See \*(L"\s-1EXPORTS\*(R"\s0 below. .SH "EXAMPLES" .IX Header "EXAMPLES" How Test::Deep works is much easier to understand by seeing some examples. .SS "Without Test::Deep" .IX Subsection "Without Test::Deep" Say you want to test a function which returns a string. You know that your string should be a 7 digit number beginning with 0, \f(CW\*(C`eq\*(C'\fR is no good in this situation, you need a regular expression. So you could use Test::More's \&\f(CW\*(C`like()\*(C'\fR function: .PP .Vb 1 \& like($string, qr/^0[0\-9]{6}$/, "number looks good"); .Ve .PP Similarly, to check that a string looks like a name, you could do: .PP .Vb 2 \& like($string, qr/^(Mr|Mrs|Miss) \ew+ \ew+$/, \& "got title, first and last name"); .Ve .PP Now imagine your function produces a hash with some personal details in it. You want to make sure that there are 2 keys, Name and Phone and that the name looks like a name and the phone number looks like a phone number. You could do: .PP .Vb 4 \& $hash = make_person(); \& like($hash\->{Name}, qr/^(Mr|Mrs|Miss) \ew+ \ew+$/, "name ok"); \& like($hash\->{Phone}, qr/^0[0\-9]{6}$/, "phone ok"); \& is(scalar keys %$hash, 2, "correct number of keys"); .Ve .PP But that's not quite right, what if make_person has a serious problem and didn't even return a hash? We really need to write .PP .Vb 12 \& if (ref($hash) eq "HASH") \& { \& like($hash\->{Name}, qr/^(Mr|Mrs|Miss) \ew+ \ew+$/, "name ok"); \& like($hash\->{Phone}, qr/^0[0\-9]{6}$/, "phone ok"); \& is(scalar keys %$hash, 2, "correct number of keys"); \& } \& else \& { \& fail("person not a hash"); \& fail("person not a hash"); \& fail("person not a hash"); # need 3 to keep the plan correct \& } .Ve .PP Already this is getting messy, now imagine another entry in the hash, an array of children's names. This would require .PP .Vb 10 \& if (ref($hash) eq "HASH") \& { \& like($hash\->{Name}, $name_pat, "name ok"); \& like($hash\->{Phone}, \*(Aq/^0d{6}$/\*(Aq, "phone ok"); \& my $cn = $hash\->{ChildNames}; \& if (ref($cn) eq "ARRAY") \& { \& foreach my $child (@$cn) \& { \& like($child, $name_pat); \& } \& } \& else \& { \& fail("child names not an array") \& } \& } \& else \& { \& fail("person not a hash"); \& } .Ve .PP This is a horrible mess and because we don't know in advance how many children's names there will be, we can't make a plan for our test anymore (actually, we could but it would make things even more complicated). .PP Test::Deep to the rescue. .SS "With Test::Deep" .IX Subsection "With Test::Deep" .Vb 10 \& my $name_re = re(\*(Aq^(Mr|Mrs|Miss) \ew+ \ew+$\*(Aq); \& cmp_deeply( \& $person, \& { \& Name => $name_re, \& Phone => re(\*(Aq^0d{6}$\*(Aq), \& ChildNames => array_each($name_re) \& }, \& "person ok" \& ); .Ve .PP This will do everything that the messy code above does and it will give a sensible message telling you exactly what went wrong if it finds a part of \&\f(CW$person\fR that doesn't match the pattern. \f(CW\*(C`re()\*(C'\fR and \f(CW\*(C`array_each()\*(C'\fR are special function imported from Test::Deep. They create a marker that tells Test::Deep that something different is happening here. Instead of just doing a simple comparison and checking are two things exactly equal, it should do something else. .PP If a person was asked to check that 2 structures are equal, they could print them both out and compare them line by line. The markers above are similar to writing a note in red pen on one of the printouts telling the person that for this piece of the structure, they should stop doing simple line by line comparison and do something else. .PP \&\f(CW\*(C`re($regex)\*(C'\fR means that Test::Deep should check that the current piece of data matches the regex in \f(CW$regex\fR. \f(CW\*(C`array_each($struct)\*(C'\fR means that Test::Deep should expect the current piece of data to be an array and it should check that every element of that array matches \f(CW$struct\fR. In this case, every element of \f(CW\*(C`$person\->{ChildNames}\*(C'\fR should look like a name. If say the 3rd one didn't you would get an error message something like .PP .Vb 3 \& Using Regexp on $data\->{ChildNames}[3] \& got : \*(AqQueen John Paul Sartre\*(Aq \& expect : /^(Mr|Mrs|Miss) \ew+ \ew+$/ .Ve .PP There are lots of other special comparisons available, see \&\*(L"\s-1SPECIAL COMPARISONS PROVIDED\*(R"\s0 below for the full list. .SS "Reusing structures" .IX Subsection "Reusing structures" Test::Deep is good for reusing test structures so you can do this .PP .Vb 6 \& my $name_re = re(\*(Aq^(Mr|Mrs|Miss) \ew+ \ew+$\*(Aq); \& my $person_cmp = { \& Name => $name_re, \& Phone => re(\*(Aq^0d{6}$\*(Aq), \& ChildNames => array_each($name_re) \& }; \& \& cmp_deeply($person1, $person_cmp, "person ok"); \& cmp_deeply($person2, $person_cmp, "person ok"); \& cmp_deeply($person3, $person_cmp, "person ok"); .Ve .PP You can even put \f(CW$person_cmp\fR in a module and let other people use it when they are writing test scripts for modules that use your modules. .PP To make things a little more difficult, lets change the person data structure so that instead of a list of ChildNames, it contains a list of hashes, one for each child. So in fact our person structure will contain other person structures which may contain other person structures and so on. This is easy to handle with Test::Deep because Test::Deep structures can include themselves. Simply do .PP .Vb 6 \& my $name_re = re(\*(Aq^(Mr|Mrs|Miss) \ew+ \ew+$\*(Aq); \& my $person_cmp = { \& Name => $name_re, \& Phone => re(\*(Aq^0d{6}$\*(Aq), \& # note no mention of Children here \& }; \& \& $person_cmp\->{Children} = array_each($person_cmp); \& \& cmp_deeply($person, $person_cmp, "person ok"); .Ve .PP This will now check that \f(CW$person\fR\->{Children} is an array and that every element of that array also matches \f(CW$person_cmp\fR, this includes checking that its children also match the same pattern and so on. .SS "Circular data structures" .IX Subsection "Circular data structures" A circular data structure is one which loops back on itself, you can make one easily by doing .PP .Vb 3 \& my @b; \& my @a = (1, 2, 3, \e@b); \& push(@b, \e@a); .Ve .PP now \f(CW@a\fR contains a reference to be \f(CW@b\fR and \f(CW@b\fR contains a reference to \&\f(CW@a\fR. This causes problems if you have a program that wants to look inside \&\f(CW@a\fR and keep looking deeper and deeper at every level, it could get caught in an infinite loop looking into \f(CW@a\fR then \f(CW@b\fR then \f(CW@a\fR then \f(CW@b\fR and so on. .PP Test::Deep avoids this problem so we can extend our example further by saying that a person should also list their parents. .PP .Vb 6 \& my $name_re = re(\*(Aq^(Mr|Mrs|Miss) \ew+ \ew+$\*(Aq); \& my $person_cmp = { \& Name => $name_re, \& Phone => re(\*(Aq^0d{6}$\*(Aq), \& # note no mention of Children here \& }; \& \& $person_cmp\->{Children} = each_array($person_cmp); \& $person_cmp\->{Parents} = each_array($person_cmp); \& \& cmp_deeply($person, $person_cmp, "person ok"); .Ve .PP So this will check that for each child \f(CW$child\fR in \f(CW\*(C`$person\->{Children}\*(C'\fR that the \f(CW\*(C`$child\->{Parents}\*(C'\fR matches \f(CW$person_cmp\fR however it is smart enough not to get caught in an infinite loop where it keeps bouncing between the same Parent and Child. .SH "TERMINOLOGY" .IX Header "TERMINOLOGY" \&\f(CW\*(C`cmp_deeply($got, $expected, $name)\*(C'\fR takes 3 arguments. \f(CW$got\fR is the structure that you are checking, you must not include any special comparisons in this structure or you will get a fatal error. \f(CW$expected\fR describes what Test::Deep will be looking for in \f(CW$got\fR. You can put special comparisons in \f(CW$expected\fR if you want to. .PP As Test::Deep descends through the 2 structures, it compares them one piece at a time, so at any point in the process, Test::Deep is thinking about 2 things \- the current value from \f(CW$got\fR and the current value from \&\f(CW$expected\fR. In the documentation, I call them \f(CW$got_v\fR and \f(CW\*(C`exp_v\*(C'\fR respectively. .SH "COMPARISON FUNCTIONS" .IX Header "COMPARISON FUNCTIONS" \fIcmp_deeply\fR .IX Subsection "cmp_deeply" .PP .Vb 1 \& my $ok = cmp_deeply($got, $expected, $name) .Ve .PP \&\f(CW$got\fR is the result to be checked. \f(CW$expected\fR is the structure against which \f(CW$got\fR will be check. \f(CW$name\fR is the test name. .PP This is the main comparison function, the others are just wrappers around this. \f(CW$got\fR and \f(CW$expected\fR are compared recursively. Each value in \&\f(CW$expected\fR defines what's expected at the corresponding location in \f(CW$got\fR. Simple scalars are compared with \f(CW\*(C`eq\*(C'\fR. References to structures like hashes and arrays are compared recursively. .PP Items in \f(CW$expected\fR, though, can also represent complex tests that check for numbers in a given range, hashes with at least a certain set of keys, a string matching a regex, or many other things. .PP See \*(L"\s-1WHAT ARE SPECIAL COMPARISONS\*(R"\s0 for details. .PP \fIcmp_bag\fR .IX Subsection "cmp_bag" .PP .Vb 1 \& my $ok = cmp_bag(\e@got, \e@bag, $name) .Ve .PP Is shorthand for cmp_deeply(\e@got, bag(@bag), \f(CW$name\fR) .PP \&\fIn.b.\fR: Both arguments must be array refs. If they aren't an exception will be thrown. .PP \fIcmp_set\fR .IX Subsection "cmp_set" .PP .Vb 1 \& my $ok = cmp_set(\e@got, \e@set, $name) .Ve .PP Is shorthand for cmp_deeply(\e@got, set(@set), \f(CW$name\fR) .PP \fIcmp_methods\fR .IX Subsection "cmp_methods" .PP .Vb 1 \& my $ok = cmp_methods(\e@got, \e@methods, $name) .Ve .PP Is shorthand for cmp_deeply(\e@got, methods(@methods), \f(CW$name\fR) .PP \fIeq_deeply\fR .IX Subsection "eq_deeply" .PP .Vb 1 \& my $ok = eq_deeply($got, $expected) .Ve .PP This is the same as \fIcmp_deeply()\fR except it just returns true or false. It does not create diagnostics or talk to Test::Builder, but if you want to use it in a non-testing environment then you should import it through Test::Deep::NoTest. For example .PP .Vb 2 \& use Test::Deep::NoTest; \& print "a equals b" unless eq_deeply($a, $b); .Ve .PP otherwise the Test::Builder framework will be loaded and testing messages will be output when your program ends. .PP \fIcmp_details\fR .IX Subsection "cmp_details" .PP .Vb 1 \& ($ok, $stack) = cmp_details($got, $expected) .Ve .PP This behaves much like eq_deeply, but it additionally allows you to produce diagnostics in case of failure by passing the value in \f(CW$stack\fR to \f(CW\*(C`deep_diag\*(C'\fR. .PP Do not make assumptions about the structure or content of \f(CW$stack\fR and do not use it if \f(CW$ok\fR contains a true value. .PP See \*(L"\s-1USING TEST::DEEP WITH TEST::BUILDER\*(R"\s0 for example uses. .SH "SPECIAL COMPARISONS PROVIDED" .IX Header "SPECIAL COMPARISONS PROVIDED" In the documentation below, \f(CW$got_v\fR is used to indicate any given value within the \f(CW$got\fR structure. .PP \fIignore\fR .IX Subsection "ignore" .PP .Vb 1 \& cmp_deeply( $got, ignore() ); .Ve .PP This makes Test::Deep skip tests on \f(CW$got_v\fR. No matter what value \f(CW$got_v\fR has, Test::Deep will think it's correct. This is useful if some part of the structure you are testing is very complicated and already tested elsewhere, or if it is unpredictable. .PP .Vb 8 \& cmp_deeply( \& $got, \& { \& name => \*(AqJohn\*(Aq, \& random => ignore(), \& address => [ \*(Aq5 A street\*(Aq, \*(Aqa town\*(Aq, \*(Aqa country\*(Aq ], \& } \& ); .Ve .PP is the equivalent of checking .PP .Vb 3 \& $got\->{name} eq \*(AqJohn\*(Aq; \& exists $got\->{random}; \& cmp_deeply($got\->{address}, [\*(Aq5 A street\*(Aq, \*(Aqa town\*(Aq, \*(Aqa country\*(Aq]); .Ve .PP \fImethods\fR .IX Subsection "methods" .PP .Vb 1 \& cmp_deeply( $got, methods(%hash) ); .Ve .PP \&\f(CW%hash\fR is a hash of method call => expected value pairs. .PP This lets you call methods on an object and check the result of each call. The methods will be called in the order supplied. If you want to pass arguments to the method you should wrap the method name and arguments in an array reference. .PP .Vb 4 \& cmp_deeply( \& $obj, \& methods(name => "John", ["favourite", "food"] => "taco") \& ); .Ve .PP is roughly the equivalent of checking that .PP .Vb 2 \& $obj\->name eq "John" \& $obj\->favourite("food") eq "taco" .Ve .PP The methods will be called in the order you supply them and will be called in scalar context. If you need to test methods called in list context then you should use \f(CW\*(C`listmethods()\*(C'\fR. .PP \&\fB\s-1NOTE\s0\fR Just as in a normal test script, you need to be careful if the methods you call have side effects like changing the object or other objects in the structure. Although the order of the methods is fixed, the order of some other tests is not so if \f(CW$expected\fR is .PP .Vb 4 \& { \& manager => methods(@manager_methods), \& coder => methods(@coder_methods) \& } .Ve .PP there is no way to know which if manager and coder will be tested first. If the methods you are testing depend on and alter global variables or if manager and coder are the same object then you may run into problems. .PP \fIlistmethods\fR .IX Subsection "listmethods" .PP .Vb 1 \& cmp_deeply( $got, listmethods(%hash) ); .Ve .PP \&\f(CW%hash\fR is a hash of pairs mapping method names to expected return values. .PP This is almost identical to \fImethods()\fR except the methods are called in list context instead of scalar context. This means that the expected return values supplied must be in array references. .PP .Vb 7 \& cmp_deeply( \& $obj, \& listmethods( \& name => "John", \& ["favourites", "food"] => ["Mapo tofu", "Gongbao chicken"] \& ) \& ); .Ve .PP is the equivalent of checking that .PP .Vb 2 \& $obj\->name eq "John" \& cmp_deeply([$obj\->favourites("food")], ["Mapo tofu", "Gongbao chicken"]); .Ve .PP The methods will be called in the order you supply them. .PP \&\fB\s-1NOTE\s0\fR The same caveats apply as for \fImethods()\fR. .PP \fIshallow\fR .IX Subsection "shallow" .PP .Vb 1 \& cmp_deeply( $got, shallow($thing) ); .Ve .PP \&\f(CW$thing\fR is a ref. .PP This prevents Test::Deep from looking inside \f(CW$thing\fR. It allows you to check that \f(CW$got_v\fR and \f(CW$thing\fR are references to the same variable. So .PP .Vb 2 \& my @a = @b = (1, 2, 3); \& cmp_deeply(\e@a, \e@b); .Ve .PP will pass because \f(CW@a\fR and \f(CW@b\fR have the same elements however .PP .Vb 1 \& cmp_deeply(\e@a, shallow(\e@b)) .Ve .PP will fail because although \f(CW\*(C`\e@a\*(C'\fR and \f(CW\*(C`\e@b\*(C'\fR both contain \f(CW\*(C`1, 2, 3\*(C'\fR they are references to different arrays. .PP \fInoclass\fR .IX Subsection "noclass" .PP .Vb 1 \& cmp_deeply( $got, noclass($thing) ); .Ve .PP \&\f(CW$thing\fR is a structure to be compared against. .PP This makes Test::Deep ignore the class of objects, so it just looks at the data they contain. Class checking will be turned off until Test::Deep is finished comparing \f(CW$got_v\fR against \f(CW$thing\fR. Once Test::Deep comes out of \&\f(CW$thing\fR it will go back to its previous setting for checking class. .PP This can be useful when you want to check that objects have been constructed correctly but you don't want to write lots of \&\f(CW\*(C`bless\*(C'\fRes. If \f(CW@people\fR is an array of Person objects then .PP .Vb 4 \& cmp_deeply(\e@people, [ \& bless {name => \*(AqJohn\*(Aq, phone => \*(Aq555\-5555\*(Aq}, "Person", \& bless {name => \*(AqAnne\*(Aq, phone => \*(Aq444\-4444\*(Aq}, "Person", \& ]); .Ve .PP can be replaced with .PP .Vb 4 \& cmp_deeply(\e@people, noclass([ \& {name => \*(AqJohn\*(Aq, phone => \*(Aq555\-5555\*(Aq}, \& {name => \*(AqAnne\*(Aq, phone => \*(Aq444\-4444\*(Aq} \& ])); .Ve .PP However, this is testing so you should also check that the objects are blessed correctly. You could use a map to bless all those hashes or you could do a second test like .PP .Vb 1 \& cmp_deeply(\e@people, array_each(isa("Person")); .Ve .PP \fIuseclass\fR .IX Subsection "useclass" .PP .Vb 1 \& cmp_deeply( $got, useclass($thing) ); .Ve .PP This turns back on the class comparison while inside a \f(CW\*(C`noclass()\*(C'\fR. .PP .Vb 8 \& cmp_deeply( \& $got, \& noclass( \& [ \& useclass( $object ) \& ] \& ) \& ) .Ve .PP In this example the class of the array reference in \f(CW$got\fR is ignored but the class of \f(CW$object\fR is checked, as is the class of everything inside \&\f(CW$object\fR. .PP \fIre\fR .IX Subsection "re" .PP .Vb 1 \& cmp_deeply( $got, re($regexp, $capture_data, $flags) ); .Ve .PP \&\f(CW$regexp\fR is either a regular expression reference produced with \f(CW\*(C`qr/.../\*(C'\fR or a string which will be used to construct a regular expression. .PP \&\f(CW$capture_data\fR is optional and is used to check the strings captured by an regex. This should can be an array ref or a Test::Deep comparator that works on array refs. .PP \&\f(CW$flags\fR is an optional string which controls whether the regex runs as a global match. If \f(CW$flags\fR is \*(L"g\*(R" then the regex will run as \f(CW\*(C`m/$regexp/g\*(C'\fR. .PP Without \f(CW$capture_data\fR, this simply compares \f(CW$got_v\fR with the regular expression provided. So .PP .Vb 1 \& cmp_deeply($got, [ re("ferg") ]) .Ve .PP is the equivalent of .PP .Vb 1 \& $got\->[0] =~ /ferg/ .Ve .PP With \f(CW$capture_data\fR, .PP .Vb 1 \& cmp_deeply($got, [re($regex, $capture_data)]) .Ve .PP is the equivalent of .PP .Vb 2 \& my @data = $got\->[0] =~ /$regex/; \& cmp_deeply(\e@data, $capture_data); .Ve .PP So you can do something simple like .PP .Vb 1 \& cmp_deeply($got, re(qr/(\ed\ed)(\ew\ew)/, [25, "ab" ])) .Ve .PP to check that \f(CW\*(C`(\ed\ed)\*(C'\fR was 25 and \f(CW\*(C`(\ew\ew)\*(C'\fR was \*(L"ab\*(R" but you can also use Test::Deep objects to do more complex testing of the captured values .PP .Vb 8 \& cmp_deeply( \& "cat=2,dog=67,sheep=3,goat=2,dog=5", \& re( \& qr/(\eD+)=\ed+,?/, \& set(qw( cat sheep dog )), \& "g" \& ), \& ); .Ve .PP here, the regex will match the string and will capture the animal names and check that they match the specified set, in this case it will fail, complaining that \*(L"goat\*(R" is not in the set. .PP \fIall\fR .IX Subsection "all" .PP .Vb 1 \& cmp_deeply( $got, all(@expecteds) ); .Ve .PP \&\f(CW@expecteds\fR is an array of expected structures. .PP This allows you to compare data against multiple expected results and make sure each of them matches. .PP .Vb 1 \& cmp_deeply($got, all(isa("Person"), methods(name => \*(AqJohn\*(Aq))) .Ve .PP is equivalent to .PP .Vb 2 \& $got\->isa("Person") \& $got\->name eq \*(AqJohn\*(Aq .Ve .PP If either test fails then the whole thing is considered a fail. This is a short-circuit test, the testing is stopped after the first failure, although in the future it may complete all tests so that diagnostics can be output for all failures. When reporting failure, the parts are counted from 1. .PP Thanks to the magic of overloading, you can write .PP .Vb 1 \& any( re("^wi"), all(isa("Person"), methods(name => \*(AqJohn\*(Aq)) ) .Ve .PP as .PP .Vb 1 \& re("^wi") | isa("Person") & methods(name => \*(AqJohn\*(Aq) .Ve .PP Note \fBsingle\fR \f(CW\*(C`|\*(C'\fR not double, as \f(CW\*(C`||\*(C'\fR cannot be overloaded. This will only work when there is a special comparison involved. If you write .PP .Vb 1 \& "john" | "anne" | "robert" .Ve .PP Perl will turn this into .PP .Vb 1 \& "{onort" .Ve .PP which is presumably not what you wanted. This is because perl ors them together as strings before Test::Deep gets a chance to do any overload tricks. .PP \fIany\fR .IX Subsection "any" .PP .Vb 1 \& cmp_deeply( $got, any(@expecteds) ); .Ve .PP \&\f(CW@expecteds\fR is an array of expected structures. .PP This can be used to compare data against multiple expected results and make sure that at least one of them matches. This is a short-circuit test so if a test passes then none of the tests after that will be attempted. .PP You can also use overloading with \f(CW\*(C`|\*(C'\fR similarly to \fIall()\fR. .PP \fIIsa\fR .IX Subsection "Isa" .PP .Vb 1 \& cmp_deeply( $got, Isa($class) ); .Ve .PP \fIisa\fR .IX Subsection "isa" .PP .Vb 1 \& cmp_deeply( $got, isa($class) ); .Ve .PP \&\f(CW$class\fR is a class name. .PP This uses \f(CW\*(C`UNIVERSAL::isa()\*(C'\fR to check that \f(CW$got_v\fR is blessed into the class \f(CW$class\fR. .PP \&\fB\s-1NOTE:\s0\fR \f(CW\*(C`Isa()\*(C'\fR does exactly as documented here, but \f(CW\*(C`isa()\*(C'\fR is slightly different. If \f(CW\*(C`isa()\*(C'\fR is called with 1 argument it falls through to \&\f(CW\*(C`Isa()\*(C'\fR. If \f(CW\*(C`isa()\*(C'\fR called with 2 arguments, it falls through to \&\f(CW\*(C`UNIVERSAL::isa\*(C'\fR. This is to prevent breakage when you import \f(CW\*(C`isa()\*(C'\fR into a package that is used as a class. Without this, anyone calling \&\f(CW\*(C`Class\->isa($other_class)\*(C'\fR would get the wrong answer. This is a hack to patch over the fact that \f(CW\*(C`isa\*(C'\fR is exported by default. .PP \fIobj_isa\fR .IX Subsection "obj_isa" .PP .Vb 1 \& cmp_deeply( $got, obj_isa($class) ); .Ve .PP This test accepts only objects that are instances of \f(CW$class\fR or a subclass. Unlike the \f(CW\*(C`Isa\*(C'\fR test, this test will never accept class names. .PP \fIarray_each\fR .IX Subsection "array_each" .PP .Vb 1 \& cmp_deeply( \e@got, array_each($thing) ); .Ve .PP \&\f(CW$thing\fR is a structure to be compared against. .PP <$got_v> must be an array reference. Each element of it will be compared to \&\f(CW$thing\fR. This is useful when you have an array of similar things, for example objects of a known type and you don't want to have to repeat the same test for each one. .PP .Vb 7 \& my $common_tests = all( \& isa("MyFile"), \& methods( \& handle => isa("IO::Handle") \& filename => re("^/home/ted/tmp"), \& ) \& ); \& \& cmp_deeply($got, array_each($common_tests)); .Ve .PP is similar to .PP .Vb 3 \& foreach my $got_v (@$got) { \& cmp_deeply($got_v, $common_tests) \& } .Ve .PP Except it will not explode if \f(CW$got\fR is not an array reference. It will check that each of the objects in \f(CW@$got\fR is a MyFile and that each one gives the correct results for its methods. .PP You could go further, if for example there were 3 files and you knew the size of each one you could do this .PP .Vb 12 \& cmp_deeply( \& $got, \& all( \& array_each($common_tests), \& [ \& methods(size => 1000), \& methods(size => 200), \& methods(size => 20) \& ] \& ) \& ) \& cmp_deeply($got, array_each($structure)); .Ve .PP \fIhash_each\fR .IX Subsection "hash_each" .PP .Vb 1 \& cmp_deeply( \e%got, hash_each($thing) ); .Ve .PP This test behaves like \f(CW\*(C`array_each\*(C'\fR (see above) but tests that each hash value passes its tests. .PP \fIstr\fR .IX Subsection "str" .PP .Vb 1 \& cmp_deeply( $got, str($string) ); .Ve .PP \&\f(CW$string\fR is a string. .PP This will stringify \f(CW$got_v\fR and compare it to \f(CW$string\fR using \f(CW\*(C`eq\*(C'\fR, even if \f(CW$got_v\fR is a ref. It is useful for checking the stringified value of an overloaded reference. .PP \fInum\fR .IX Subsection "num" .PP .Vb 1 \& cmp_deeply( $got, num($number, $tolerance) ); .Ve .PP \&\f(CW$number\fR is a number. .PP \&\f(CW$tolerance\fR is an optional number. .PP This will add 0 to \f(CW$got_v\fR and check if it's numerically equal to \&\f(CW$number\fR, even if \f(CW$got_v\fR is a ref. It is useful for checking the numerical value of an overloaded reference. If \f(CW$tolerance\fR is supplied then this will check that \f(CW$got_v\fR and \f(CW$exp_v\fR are less than \&\f(CW$tolerance\fR apart. This is useful when comparing floating point numbers as rounding errors can make it hard or impossible for \f(CW$got_v\fR to be exactly equal to \f(CW$exp_v\fR. When \f(CW$tolerance\fR is supplied, the test passes if \&\f(CW\*(C`abs($got_v \- $exp_v) <= $tolerance\*(C'\fR. .PP \&\fBNote\fR in Perl, \f(CW\*(C`"12blah" == 12\*(C'\fR because Perl will be smart and convert \&\*(L"12blah\*(R" into 12. You may not want this. There was a strict mode but that is now gone. A \*(L"looks like a number\*(R" test will replace it soon. Until then you can usually just use the \fIstring()\fR comparison to be more strict. This will work fine for almost all situations, however it will not work when <$got_v> is an overloaded value who's string and numerical values differ. .PP \fIbool\fR .IX Subsection "bool" .PP .Vb 1 \& cmp_deeply( $got, bool($value) ); .Ve .PP \&\f(CW$value\fR is anything you like but it's probably best to use 0 or 1 .PP This will check that \f(CW$got_v\fR and \f(CW$value\fR have the same truth value, that is they will give the same result when used in boolean context, like in an \&\f(CW\*(C`if()\*(C'\fR statement. .PP \fIcode\fR .IX Subsection "code" .PP .Vb 1 \& cmp_deeply( $got, code(\e&subref) ); .Ve .PP \&\f(CW\*(C`\e&subref\*(C'\fR is a reference to a subroutine which will be passed a single argument, it then should return a true or false and possibly a string .PP This will pass \f(CW$got_v\fR to the subroutine which returns true or false to indicate a pass or fail. Fails can be accompanied by a diagnostic string which gives an explanation of why it's a fail. .PP .Vb 12 \& sub check_name \& { \& my $name = shift; \& if ($boss\->likes($name)) \& { \& return 1; \& } \& else \& { \& return (0, "the boss doesn\*(Aqt like your name"); \& } \& } \& \& cmp_deeply("Brian", code(\e&check_name)); .Ve .SS "\s-1SET COMPARISONS\s0" .IX Subsection "SET COMPARISONS" Set comparisons give special semantics to array comparisons: .IP "\(bu" 4 The order of items in a set is irrelevant .IP "\(bu" 4 The presence of duplicate items in a set is ignored. .PP As such, in any set comparison, the following arrays are equal: .PP .Vb 5 \& [ 1, 2 ] \& [ 1, 1, 2 ] \& [ 1, 2, 1 ] \& [ 2, 1, 1 ] \& [ 1, 1, 2 ] .Ve .PP All are interpreted by \f(CW\*(C`set\*(C'\fR semantics as if the set was only specified as: .PP .Vb 1 \& [ 1, 2 ] .Ve .PP All \f(CW\*(C`set\*(C'\fR functions return an object which can have additional items added to it: .PP .Vb 2 \& my $set = set( 1, 2 ); \& $set\->add(1, 3, 1 ); # Set is now ( 1, 2, 3 ) .Ve .PP Special care must be taken when using special comparisons within sets. See \&\*(L"\s-1SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS\*(R"\s0 for details. .PP \fIset\fR .IX Subsection "set" .PP .Vb 1 \& cmp_deeply( \e@got, set(@elements) ); .Ve .PP This does a set comparison, that is, it compares two arrays but ignores the order of the elements and it ignores duplicate elements, but ensures that all items in in \f(CW@elements\fR will be in \f(CW$got\fR and all items in \f(CW$got\fR will be in \f(CW@elements\fR. .PP So the following tests will be passes, and will be equivalent: .PP .Vb 2 \& cmp_deeply([1, 2, 2, 3], set(3, 2, 1, 1)); \& cmp_deeply([1, 2, 3], set(3, 2, 1)); .Ve .PP \fIsupersetof\fR .IX Subsection "supersetof" .PP .Vb 1 \& cmp_deeply( \e@got, supersetof(@elements) ); .Ve .PP This function works much like \f(CW\*(C`set\*(C'\fR, and performs a set comparison of \f(CW$got_v\fR with the elements of \f(CW@elements\fR. .PP \&\f(CW\*(C`supersetof\*(C'\fR is however slightly relaxed, such that \f(CW$got\fR may contain things not in \f(CW@elements\fR, but must at least contain all \f(CW@elements\fR. .PP These two statements are equivalent, and will be passes: .PP .Vb 2 \& cmp_deeply([1,2,3,3,4,5], supersetof(2,2,3)); \& cmp_deeply([1,2,3,4,5], supersetof(2,3)); .Ve .PP But these will be failures: .PP .Vb 2 \& cmp_deeply([1,2,3,4,5], supersetof(2,3,6)); # 6 not in superset \& cmp_deeply([1], supersetof(1,2)); # 2 not in superset .Ve .PP \fIsubsetof\fR .IX Subsection "subsetof" .PP .Vb 1 \& cmp_deeply( \e@got, subsetof(@elements) ); .Ve .PP This function works much like \f(CW\*(C`set\*(C'\fR, and performs a set comparison of \f(CW$got_v\fR with the elements of \f(CW@elements\fR. .PP This is the inverse of \f(CW\*(C`supersetof\*(C'\fR, which expects all unique elements found in \f(CW$got_v\fR must be in \f(CW@elements\fR. .PP .Vb 3 \& cmp_deeply([1,2,4,5], subsetof(2,3,3) ) # Fail: 1,4 & 5 extra \& cmp_deeply([2,3,3], subsetof(1,2,4,5) ) # Fail: 3 extra \& cmp_deeply([2,3,3], subsetof(1,2,4,5,3)) # Pass .Ve .PP \fInone\fR .IX Subsection "none" .PP .Vb 1 \& cmp_deeply( $got, none(@elements) ); .Ve .PP \&\f(CW@elements\fR is an array of elements, wherein no elements in \f(CW@elements\fR may be equal to \f(CW$got_v\fR. .PP \fInoneof\fR .IX Subsection "noneof" .PP .Vb 1 \& cmp_deeply( \e@got, noneof(@elements) ); .Ve .PP \&\f(CW@elements\fR is an array of elements, wherein no elements in \f(CW@elements\fR may be found in \f(CW$got_v\fR. .PP For example: .PP .Vb 3 \& # Got has no 1, no 2, and no 3 \& cmp_deeply( [1], noneof( 1, 2, 3 ) ); # fail \& cmp_deeply( [5], noneof( 1, 2, 3 ) ); # pass .Ve .SS "\s-1BAG COMPARISONS\s0" .IX Subsection "BAG COMPARISONS" Bag comparisons give special semantics to array comparisons, that are similar to set comparisons, but slightly different. .IP "\(bu" 4 The order of items in a bag is irrelevant .IP "\(bu" 4 The presence of duplicate items in a bag is \fB\s-1PRESERVED\s0\fR .PP As such, in any bag comparison, the following arrays are equal: .PP .Vb 4 \& [ 1, 1, 2 ] \& [ 1, 2, 1 ] \& [ 2, 1, 1 ] \& [ 1, 1, 2 ] .Ve .PP However, they are \fB\s-1NOT\s0\fR equal to any of the following: .PP .Vb 3 \& [ 1, 2 ] \& [ 1, 2, 2 ] \& [ 1, 1, 1, 2 ] .Ve .PP All \f(CW\*(C`bag\*(C'\fR functions return an object which can have additional items added to it: .PP .Vb 2 \& my $bag = bag( 1, 2 ); \& $bag\->add(1, 3, 1 ); # Bag is now ( 1, 1, 1, 2, 3 ) .Ve .PP Special care must be taken when using special comparisons within bags. See \&\*(L"\s-1SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS\*(R"\s0 for details. .PP \fIbag\fR .IX Subsection "bag" .PP .Vb 1 \& cmp_deeply( \e@got, bag(@elements) ); .Ve .PP This does an order-insensitive bag comparison between \f(CW$got\fR and \&\f(CW@elements\fR, ensuring that: .ie n .IP "each item in @elements is found in $got" 4 .el .IP "each item in \f(CW@elements\fR is found in \f(CW$got\fR" 4 .IX Item "each item in @elements is found in $got" .PD 0 .ie n .IP "the number of times a $expected_v is found in @elements is reflected in $got" 4 .el .IP "the number of times a \f(CW$expected_v\fR is found in \f(CW@elements\fR is reflected in \f(CW$got\fR" 4 .IX Item "the number of times a $expected_v is found in @elements is reflected in $got" .ie n .IP "no items are found in $got other than those in @elements." 4 .el .IP "no items are found in \f(CW$got\fR other than those in \f(CW@elements\fR." 4 .IX Item "no items are found in $got other than those in @elements." .PD .PP As such, the following are passes, and are equivalent to each other: .PP .Vb 3 \& cmp_deeply([1, 2, 2], bag(2, 2, 1)) \& cmp_deeply([2, 1, 2], bag(2, 2, 1)) \& cmp_deeply([2, 2, 1], bag(2, 2, 1)) .Ve .PP But the following are failures: .PP .Vb 2 \& cmp_deeply([1, 2, 2], bag(2, 2, 1, 1)) # Not enough 1\*(Aqs in Got \& cmp_deeply([1, 2, 2, 1], bag(2, 2, 1) ) # Too many 1\*(Aqs in Got .Ve .PP \fIsuperbagof\fR .IX Subsection "superbagof" .PP .Vb 1 \& cmp_deeply( \e@got, superbagof( @elements ) ); .Ve .PP This function works much like \f(CW\*(C`bag\*(C'\fR, and performs a bag comparison of \f(CW$got_v\fR with the elements of \f(CW@elements\fR. .PP \&\f(CW\*(C`superbagof\*(C'\fR is however slightly relaxed, such that \f(CW$got\fR may contain things not in \f(CW@elements\fR, but must at least contain all \f(CW@elements\fR. .PP So: .PP .Vb 2 \& # pass \& cmp_deeply( [1, 1, 2], superbagof( 1 ) ); \& \& # fail: not enough 1\*(Aqs in superbag \& cmp_deeply( [1, 1, 2], superbagof( 1, 1, 1 )); .Ve .PP \fIsubbagof\fR .IX Subsection "subbagof" .PP .Vb 1 \& cmp_deeply( \e@got, subbagof(@elements) ); .Ve .PP This function works much like \f(CW\*(C`bag\*(C'\fR, and performs a bag comparison of \f(CW$got_v\fR with the elements of \f(CW@elements\fR. .PP This is the inverse of \f(CW\*(C`superbagof\*(C'\fR, and expects all elements in \f(CW$got\fR to be in \f(CW@elements\fR, while allowing items to exist in \f(CW@elements\fR that are not in \f(CW$got\fR .PP .Vb 2 \& # pass \& cmp_deeply( [1], subbagof( 1, 1, 2 ) ); \& \& # fail: too many 1\*(Aqs in subbag \& cmp_deeply( [1, 1, 1], subbagof( 1, 1, 2 ) ); .Ve .SS "\s-1HASH COMPARISONS\s0" .IX Subsection "HASH COMPARISONS" Typically, if you're doing simple hash comparisons, .PP .Vb 1 \& cmp_deeply( \e%got, \e%expected ) .Ve .PP is sufficient. \f(CW\*(C`cmp_deeply\*(C'\fR will ensure \f(CW%got\fR and \f(CW%hash\fR have identical keys, and each key from either has the same corresponding value. .PP \fIsuperhashof\fR .IX Subsection "superhashof" .PP .Vb 1 \& cmp_deeply( \e%got, superhashof(\e%hash) ); .Ve .PP This will check that the hash \f(CW%$got\fR is a \*(L"super-hash\*(R" of \f(CW%hash\fR. That is that all the key and value pairs in \f(CW%hash\fR appear in \f(CW%$got\fR but \&\f(CW%$got\fR can have extra ones also. .PP For example .PP .Vb 1 \& cmp_deeply({a => 1, b => 2}, superhashof({a => 1})) .Ve .PP will pass but .PP .Vb 1 \& cmp_deeply({a => 1, b => 2}, superhashof({a => 1, c => 3})) .Ve .PP will fail. .PP \fIsubhashof\fR .IX Subsection "subhashof" .PP .Vb 1 \& cmp_deeply( \e%got, subhashof(\e%hash) ); .Ve .PP This will check that the hash \f(CW%$got\fR is a \*(L"sub-hash\*(R" of \f(CW%hash\fR. That is that all the key and value pairs in \f(CW%$got\fR also appear in \f(CW%hash\fR. .PP For example .PP .Vb 1 \& cmp_deeply({a => 1}, subhashof({a => 1, b => 2})) .Ve .PP will pass but .PP .Vb 1 \& cmp_deeply({a => 1, c => 3}, subhashof({a => 1, b => 2})) .Ve .PP will fail. .SH "DIAGNOSTIC FUNCTIONS" .IX Header "DIAGNOSTIC FUNCTIONS" \fIdeep_diag\fR .IX Subsection "deep_diag" .PP .Vb 1 \& my $reason = deep_diag($stack); .Ve .PP \&\f(CW$stack\fR is a value returned by cmp_details. Do not call this function if cmp_details returned a true value for \f(CW$ok\fR. .PP \&\f(CW\*(C`deep_diag()\*(C'\fR returns a human readable string describing how the comparison failed. .SH "ANOTHER EXAMPLE" .IX Header "ANOTHER EXAMPLE" You've written a module to handle people and their film interests. Say you have a function that returns an array of people from a query, each person is a hash with 2 keys: Name and Age and the array is sorted by Name. You can do .PP .Vb 8 \& cmp_deeply( \& $result, \& [ \& {Name => \*(AqAnne\*(Aq, Age => 26}, \& {Name => "Bill", Age => 47} \& {Name => \*(AqJohn\*(Aq, Age => 25}, \& ] \& ); .Ve .PP Soon after, your query function changes and all the results now have an \s-1ID\s0 field. Now your test is failing again because you left out \s-1ID\s0 from each of the hashes. The problem is that the IDs are generated by the database and you have no way of knowing what each person's \s-1ID\s0 is. With Test::Deep you can change your query to .PP .Vb 8 \& cmp_deeply( \& $result, \& [ \& {Name => \*(AqJohn\*(Aq, Age => 25, ID => ignore()}, \& {Name => \*(AqAnne\*(Aq, Age => 26, ID => ignore()}, \& {Name => "Bill", Age => 47, ID => ignore()} \& ] \& ); .Ve .PP But your test still fails. Now, because you're using a database, you no longer know what order the people will appear in. You could add a sort into the database query but that could slow down your application. Instead you can get Test::Deep to ignore the order of the array by doing a bag comparison instead. .PP .Vb 8 \& cmp_deeply( \& $result, \& bag( \& {Name => \*(AqJohn\*(Aq, Age => 25, ID => ignore()}, \& {Name => \*(AqAnne\*(Aq, Age => 26, ID => ignore()}, \& {Name => "Bill", Age => 47, ID => ignore()} \& ) \& ); .Ve .PP Finally person gets even more complicated and includes a new field called Movies, this is a list of movies that the person has seen recently, again these movies could also come back in any order so we need a bag inside our other bag comparison, giving us something like .PP .Vb 8 \& cmp_deeply( \& $result, \& bag( \& {Name => \*(AqJohn\*(Aq, Age => 25, ID => ignore(), Movies => bag(...)}, \& {Name => \*(AqAnne\*(Aq, Age => 26, ID => ignore(), Movies => bag(...)}, \& {Name => "Bill", Age => 47, ID => ignore(), Movies => bag(...)} \& ) \& ); .Ve .SH "USING TEST::DEEP WITH TEST::BUILDER" .IX Header "USING TEST::DEEP WITH TEST::BUILDER" Combining \f(CW\*(C`cmp_details\*(C'\fR and \f(CW\*(C`deep_diag\*(C'\fR makes it possible to use Test::Deep in your own test classes. .PP In a Test::Builder subclass, create a test method in the following form: .PP .Vb 4 \& sub behaves_ok { \& my $self = shift; \& my $expected = shift; \& my $test_name = shift; \& \& my $got = do_the_important_work_here(); \& \& my ($ok, $stack) = cmp_details($got, $expected); \& unless ($Test\->ok($ok, $test_name)) { \& my $diag = deep_diag($stack); \& $Test\->diag($diag); \& } \& } .Ve .PP As the subclass defines a test class, not tests themselves, make sure it uses Test::Deep::NoTest, not \f(CW\*(C`Test::Deep\*(C'\fR itself. .SH "LIMITATIONS" .IX Header "LIMITATIONS" Currently any \s-1CODE, GLOB\s0 or \s-1IO\s0 refs will be compared using \fIshallow()\fR, which means only their memory addresses are compared. .SH "BUGS" .IX Header "BUGS" There is a bug in set and bag compare to do with competing SCs. It only occurs when you put certain special comparisons inside bag or set comparisons you don't need to worry about it. The full details are in the \&\f(CW\*(C`bag()\*(C'\fR docs. It will be fixed in an upcoming version. .SH "CAVEATS" .IX Header "CAVEATS" .SS "\s-1SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS\s0" .IX Subsection "SPECIAL CARE WITH SPECIAL COMPARISONS IN SETS AND BAGS" If you use certain special comparisons within a bag or set comparison there is a danger that a test will fail when it should have passed. It can only happen if two or more special comparisons in the bag are competing to match elements. Consider this comparison .PP .Vb 1 \& cmp_deeply([\*(Aqfurry\*(Aq, \*(Aqfurball\*(Aq], bag(re("^fur"), re("furb"))) .Ve .PP There are two things that could happen, hopefully \f(CW\*(C`re("^fur")\*(C'\fR is paired with \&\*(L"furry\*(R" and \f(CW\*(C`re("^furb")\*(C'\fR is paired with \*(L"furb\*(R" and everything is fine but it could happen that \f(CW\*(C`re("^fur")\*(C'\fR is paired with \*(L"furball\*(R" and then \f(CW\*(C`re("^furb")\*(C'\fR cannot find a match and so the test fails. Examples of other competing comparisons are \f(CW\*(C`bag(1, 2, 2)\*(C'\fR vs \f(CW\*(C`set(1, 2)\*(C'\fR and \&\f(CW\*(C`methods(m1 => "v1", m2 => "v2")\*(C'\fR vs \f(CW\*(C`methods(m1 => "v1")\*(C'\fR .PP This problem is could be solved by using a slower and more complicated algorithm for set and bag matching. Something for the future... .SH "WHAT ARE SPECIAL COMPARISONS?" .IX Header "WHAT ARE SPECIAL COMPARISONS?" A special comparison (\s-1SC\s0) is simply an object that inherits from Test::Deep::Cmp. Whenever \f(CW$expected_v\fR is an \s-1SC\s0 then instead of checking \&\f(CW\*(C`$got_v eq $expected_v\*(C'\fR, we pass control over to the \s-1SC\s0 and let it do its thing. .PP Test::Deep exports lots of \s-1SC\s0 constructors, to make it easy for you to use them in your test scripts. For example is \f(CW\*(C`re("hello")\*(C'\fR is just a handy way of creating a Test::Deep::Regexp object that will match any string containing \&\*(L"hello\*(R". So .PP .Vb 1 \& cmp_deeply([ \*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqhello world\*(Aq], [\*(Aqa\*(Aq, \*(Aqb\*(Aq, re("^hello")]); .Ve .PP will check \f(CW\*(Aqa\*(Aq eq \*(Aqa\*(Aq\fR, \f(CW\*(Aqb\*(Aq eq \*(Aqb\*(Aq\fR but when it comes to comparing \&\f(CW\*(Aqhello world\*(Aq\fR and \f(CW\*(C`re("^hello")\*(C'\fR it will see that \&\f(CW$expected_v\fR is an \s-1SC\s0 and so will pass control to the Test::Deep::Regexp class by do something like \f(CW\*(C`$expected_v\->descend($got_v)\*(C'\fR. The \f(CW\*(C`descend()\*(C'\fR method should just return true or false. .PP This gives you enough to write your own SCs but I haven't documented how diagnostics works because it's about to get an overhaul (theoretically). .SH "EXPORTS" .IX Header "EXPORTS" By default, Test::Deep will export everything in its \f(CW\*(C`v0\*(C'\fR tag, as if you had written: .PP .Vb 1 \& use Test::Deep \*(Aq:v0\*(Aq; .Ve .PP Those things are: .PP .Vb 6 \& all any array array_each arrayelementsonly arraylength arraylengthonly bag \& blessed bool cmp_bag cmp_deeply cmp_methods cmp_set code eq_deeply hash \& hash_each hashkeys hashkeysonly ignore Isa isa listmethods methods noclass \& none noneof num obj_isa re reftype regexpmatches regexponly regexpref \& regexprefonly scalarrefonly scalref set shallow str subbagof subhashof \& subsetof superbagof superhashof supersetof useclass .Ve .PP A slightly better set of exports is the \f(CW\*(C`v1\*(C'\fR set. It's all the same things, with the exception of \f(CW\*(C`Isa\*(C'\fR and \f(CW\*(C`blessed\*(C'\fR. If you want to import \&\*(L"everything\*(R", you probably want to \f(CW\*(C`use Test::Deep \*(Aq:V1\*(Aq;\*(C'\fR. .PP There's another magic export group: \f(CW\*(C`:preload\*(C'\fR. If that is specified, all of the Test::Deep plugins will be loaded immediately instead of lazily. .SH "SEE ALSO" .IX Header "SEE ALSO" Test::More .SH "MAINTAINER" .IX Header "MAINTAINER" .Vb 1 \& Ricardo Signes <rjbs@cpan.org> .Ve .SH "AUTHOR" .IX Header "AUTHOR" Fergal Daly <fergal@esatclear.ie>, with thanks to Michael G Schwern for Test::More's is_deeply function which inspired this. .PP \&\fBPlease\fR do not bother Fergal Daly with bug reports. Send them to the maintainer (above) or submit them at the issue tracker <https://github.com/rjbs/Test-Deep/issues>. .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2003, 2004 by Fergal Daly <fergal@esatclear.ie>. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP See \fIhttp://www.perl.com/perl/misc/Artistic.html\fR
| ver. 1.4 |
Github
|
.
| PHP 8.0.30 | Génération de la page: 0 |
proxy
|
phpinfo
|
Réglages