| 1 | =head1 NAME |
|---|
| 2 | |
|---|
| 3 | Test::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 | |
|---|
| 42 | If you don't know anything about automated testing in Perl then you should |
|---|
| 43 | probably read about Test::Simple and Test::More before preceding. |
|---|
| 44 | Test::Deep uses the Test::Builder framework. |
|---|
| 45 | |
|---|
| 46 | Test::Deep gives you very flexible ways to check that the result you got is |
|---|
| 47 | the result you were expecting. At it's simplest it compares two structures |
|---|
| 48 | by going through each level, ensuring that the values match, that arrays and |
|---|
| 49 | hashes have the same elements and that references are blessed into the |
|---|
| 50 | correct class. It also handles circular data structures without getting |
|---|
| 51 | caught in an infinite loop. |
|---|
| 52 | |
|---|
| 53 | Where it becomes more interesting is in allowing you to do something besides |
|---|
| 54 | simple exact comparisons. With strings, the C<eq> operator checks that 2 |
|---|
| 55 | strings are exactly equal but sometimes that's not what you want. When you |
|---|
| 56 | don't know exactly what the string should be but you do know some things |
|---|
| 57 | about how it should look, C<eq> is no good and you must use pattern matching |
|---|
| 58 | instead. Test::Deep provides pattern matching for complex data structures |
|---|
| 59 | |
|---|
| 60 | =head1 EXAMPLES |
|---|
| 61 | |
|---|
| 62 | How Test::Deep works is much easier to understand by seeing some examples. |
|---|
| 63 | |
|---|
| 64 | =head2 Without Test::Deep |
|---|
| 65 | |
|---|
| 66 | Say you want to test a function which returns a string. You know that your |
|---|
| 67 | string should be a 7 digit number beginning with 0, C<eq> is no good in this |
|---|
| 68 | situation, you need a regular expression. So you could use Test::More's |
|---|
| 69 | C<like()> function: |
|---|
| 70 | |
|---|
| 71 | like($string, '/^0d{6}$/', "number looks good"); |
|---|
| 72 | |
|---|
| 73 | Similarly, 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 | |
|---|
| 78 | Now imagine your function produces a hash with some personal details in it. |
|---|
| 79 | You want to make sure that there are 2 keys, Name and Phone and that the |
|---|
| 80 | name looks like a name and the phone number looks like a phone number. You |
|---|
| 81 | could 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 | |
|---|
| 88 | But that's not quite right, what if make_person has a serious problem and |
|---|
| 89 | didn'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 | |
|---|
| 104 | Already this is getting messy, now imagine another entry in the hash, an |
|---|
| 105 | array 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 | |
|---|
| 130 | This is a horrible mess and because we don't know in advance how many |
|---|
| 131 | children'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 | |
|---|
| 134 | Test::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 | |
|---|
| 149 | This will do everything that the messy code above does and it will give a |
|---|
| 150 | sensible 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 |
|---|
| 152 | special function imported from Test::Deep. They create a marker that tells |
|---|
| 153 | Test::Deep that something different is happening here. Instead of just doing |
|---|
| 154 | a simple comparison and checking are two things exactly equal, it should do |
|---|
| 155 | something else. |
|---|
| 156 | |
|---|
| 157 | If a person was asked to check that 2 structures are equal, they could print |
|---|
| 158 | them both out and compare them line by line. The markers above are similar |
|---|
| 159 | to writing a note in red pen on one of the printouts telling the person that |
|---|
| 160 | for this piece of the structure, they should stop doing simple line by line |
|---|
| 161 | comparison and do something else. |
|---|
| 162 | |
|---|
| 163 | C<re($regex)> means that Test::Deep should check that the current piece of |
|---|
| 164 | data matches the regex in C<$regex>. C<array_each($struct)> means that |
|---|
| 165 | Test::Deep should expect the current piece of data to be an array and it |
|---|
| 166 | should check that every element of that array matches C<$struct>. |
|---|
| 167 | In this case, every element of C<$person->{ChildNames}> should look like a |
|---|
| 168 | name. If say the 3rd one didn't you would get an error message something |
|---|
| 169 | like |
|---|
| 170 | |
|---|
| 171 | Using Regexp on $data->{ChildNames}[3] |
|---|
| 172 | got : 'Queen John Paul Sartre' |
|---|
| 173 | expect : /^(Mr|Mrs|Miss) \w+ \w+$/ |
|---|
| 174 | |
|---|
| 175 | There are lots of other special comparisons available, see |
|---|
| 176 | L<SPECIAL COMPARISONS PROVIDED> below for the full list. |
|---|
| 177 | |
|---|
| 178 | =head2 Reusing structures |
|---|
| 179 | |
|---|
| 180 | Test::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 | |
|---|
| 193 | You can even put $person_cmp in a module and let other people use it when |
|---|
| 194 | they are writing test scripts for modules that use your modules. |
|---|
| 195 | |
|---|
| 196 | To make things a little more difficult, lets change the person data |
|---|
| 197 | structure so that instead of a list of ChildNames, it contains a list of |
|---|
| 198 | hashes, one for each child. So in fact our person structure will contain |
|---|
| 199 | other person structures which may contain other person structures and so on. |
|---|
| 200 | This is easy to handle with Test::Deep because Test::Deep structures can |
|---|
| 201 | include 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 | |
|---|
| 214 | This will now check that $person->{Children} is an array and that every |
|---|
| 215 | element of that array also matches C<$person_cmp>, this includes checking |
|---|
| 216 | that it's children also match the same pattern and so on. |
|---|
| 217 | |
|---|
| 218 | =head2 Circular data structures |
|---|
| 219 | |
|---|
| 220 | A circular data structure is one which loops back on itself, you can make |
|---|
| 221 | 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. This |
|---|
| 228 | causes problems if you have a program that wants to look inside @a and keep |
|---|
| 229 | looking deeper and deeper at every level, it could get caught in an infinite |
|---|
| 230 | 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 C<$child> in C<$person->{Children}> |
|---|
| 248 | that the C<$child->{Parents} matches C<$person_cmp> however it is smart |
|---|
| 249 | enough not to get caught in an infinite loop where it keeps bouncing between |
|---|
| 250 | the same Parent and Child. |
|---|
| 251 | |
|---|
| 252 | =head1 TERMINOLOGY |
|---|
| 253 | |
|---|
| 254 | C<cmp_deeply($got, $expected, $name)> takes 3 arguments. C<$got> is the |
|---|
| 255 | structure that you are checking, you must not include any special |
|---|
| 256 | comparisons in this structure or you will get a fatal error. C<$expected> |
|---|
| 257 | describes what Test::Deep will be looking for in $got. You can put special |
|---|
| 258 | comparisons in $expected if you want to. |
|---|
| 259 | |
|---|
| 260 | As Test::Deep descends through the 2 structures, it compares them one piece |
|---|
| 261 | at a time, so at any point in the process, Test::Deep is thinking about 2 |
|---|
| 262 | things - the current value from C<$got> and the current value from |
|---|
| 263 | C<$expected>. In the documentation, I call them C<$got_v> and C<exp_v> |
|---|
| 264 | respectively. |
|---|
| 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 | |
|---|
| 273 | This is the main comparison function, the others are just wrappers around |
|---|
| 274 | this. Without any special comparisons, it will descend into $expected, |
|---|
| 275 | following every reference and comparing C<$expected_v> to C<$got_v> (using |
|---|
| 276 | C<eq>) at the same position. If at any stage C<$expected_v> is a special |
|---|
| 277 | comparison then Test::Deep may do something else besides a simple string |
|---|
| 278 | comparison, exactly what it does depends on which special comparison it is. |
|---|
| 279 | |
|---|
| 280 | =head3 $ok = cmp_bag(\@got, \@bag, $name) |
|---|
| 281 | |
|---|
| 282 | Is shorthand for cmp_deeply(\@got, bag(@bag), $name) |
|---|
| 283 | |
|---|
| 284 | =head3 $ok = cmp_set(\@got, \@set, $name) |
|---|
| 285 | |
|---|
| 286 | Is shorthand for cmp_deeply(\@got, set(@set), $name) |
|---|
| 287 | |
|---|
| 288 | =head3 $ok = cmp_methods(\@got, \@methods, $name) |
|---|
| 289 | |
|---|
| 290 | Is shorthand for cmp_deeply(\@got, methods(@methods), $name) |
|---|
| 291 | |
|---|
| 292 | =head3 $ok = eq_deeply($got, $expected) |
|---|
| 293 | |
|---|
| 294 | This is the same as cmp_deeply() except it just returns true or |
|---|
| 295 | false. It does not create diagnostics or talk to L<Test::Builder>, but |
|---|
| 296 | if you want to use it in a non-testing environment then you should |
|---|
| 297 | import 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 | |
|---|
| 302 | otherwise the L<Test::Builder> framework will be loaded and testing messages |
|---|
| 303 | will be output when your program ends. |
|---|
| 304 | |
|---|
| 305 | =head1 SPECIAL COMPARISONS PROVIDED |
|---|
| 306 | |
|---|
| 307 | =head3 ignore() |
|---|
| 308 | |
|---|
| 309 | This makes Test::Deep skip tests on $got_v. No matter what value C<$got_v> |
|---|
| 310 | has, Test::Deep will think it's correct. This is useful if some part of the |
|---|
| 311 | structure you are testing is very complicated and already tested elsewhere, |
|---|
| 312 | or is unpredictable. |
|---|
| 313 | |
|---|
| 314 | cmp_deeply($got, { name => 'John', random => ignore(), address => ['5 A |
|---|
| 315 | street', 'a town', 'a country'], |
|---|
| 316 | }) |
|---|
| 317 | |
|---|
| 318 | is 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 | |
|---|
| 329 | This lets you call methods on an object and check the result of each call. |
|---|
| 330 | The methods will be called in the order supplied. If you want to pass |
|---|
| 331 | arguments to the method you should wrap the method name and arguments in an |
|---|
| 332 | array reference. |
|---|
| 333 | |
|---|
| 334 | cmp_deeply( |
|---|
| 335 | $obj, |
|---|
| 336 | methods(name => "John", ["favourite", "food"] => "taco") |
|---|
| 337 | ); |
|---|
| 338 | |
|---|
| 339 | is roughly the equivalent of checking that |
|---|
| 340 | |
|---|
| 341 | $obj->name eq "John" |
|---|
| 342 | $obj->favourite("food") eq "taco" |
|---|
| 343 | |
|---|
| 344 | The methods will be called in the order you supply them and will be called |
|---|
| 345 | in scalar context. If you need to test methods called in list context then |
|---|
| 346 | you should use listmethods(). |
|---|
| 347 | |
|---|
| 348 | B<NOTE> Just as in a normal test script, you need to be careful if the |
|---|
| 349 | methods you call have side effects like changing the object or other objects |
|---|
| 350 | in the structure. Although the order of the methods is fixed, the order of |
|---|
| 351 | some other tests is not so if $expected is |
|---|
| 352 | |
|---|
| 353 | { |
|---|
| 354 | manager => methods(@manager_methods), |
|---|
| 355 | coder => methods(@coder_methods) |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | there is no way to know which if manager and coder will be tested first. If |
|---|
| 359 | the methods you are testing depend on and alter global variables or if |
|---|
| 360 | manager 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 | |
|---|
| 366 | This is almost identical to methods() except the methods are called in list |
|---|
| 367 | context instead of scalar context. This means that the expected values |
|---|
| 368 | supplied 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 | |
|---|
| 378 | is the equivalent of checking that |
|---|
| 379 | |
|---|
| 380 | $obj->name eq "John" |
|---|
| 381 | cmp_deeply([$obj->favourites("food")], ["Mapo tofu", "Gongbao chicken"]); |
|---|
| 382 | |
|---|
| 383 | The methods will be called in the order you supply them. |
|---|
| 384 | |
|---|
| 385 | B<NOTE> The same caveats apply as for methods(). |
|---|
| 386 | |
|---|
| 387 | =head3 shallow($thing) |
|---|
| 388 | |
|---|
| 389 | $thing is a ref. |
|---|
| 390 | |
|---|
| 391 | This prevents Test::Deep from looking inside $thing. It allows you to |
|---|
| 392 | check 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 | |
|---|
| 397 | will pass because @a and @b have the same elements however |
|---|
| 398 | |
|---|
| 399 | cmp_deeply(\@a, shallow(\@b)) |
|---|
| 400 | |
|---|
| 401 | will fail because although \@a and \@b both contain C<1, 2, 3> they are |
|---|
| 402 | references to different arrays. |
|---|
| 403 | |
|---|
| 404 | =head3 noclass($thing) |
|---|
| 405 | |
|---|
| 406 | $thing is a structure to be compared against. |
|---|
| 407 | |
|---|
| 408 | This makes Test::Deep ignore the class of objects, so it just looks at the |
|---|
| 409 | data they contain. Class checking will be turned off until Test::Deep is |
|---|
| 410 | finished comparing C<$got_v> against C<$thing>. Once Test::Deep comes out of |
|---|
| 411 | C<$thing> it will go back to it's previous setting for checking class. |
|---|
| 412 | |
|---|
| 413 | This can be useful when you want to check that objects have been |
|---|
| 414 | constructed correctly but you don't want to write lots of |
|---|
| 415 | C<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 | |
|---|
| 422 | can be replaced with |
|---|
| 423 | |
|---|
| 424 | cmp_deeply(\@people, noclass([ |
|---|
| 425 | {name => 'John', phone => '555-5555'}, |
|---|
| 426 | {name => 'Anne', phone => '444-4444'} |
|---|
| 427 | ])); |
|---|
| 428 | |
|---|
| 429 | However, this is testing so you should also check that the objects are |
|---|
| 430 | blessed correctly. You could use a map to bless all those hashes or you |
|---|
| 431 | could do a second test like |
|---|
| 432 | |
|---|
| 433 | cmp_deeply($people, array_each(isa("Person")); |
|---|
| 434 | |
|---|
| 435 | =head3 useclass($thing) |
|---|
| 436 | |
|---|
| 437 | This 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 | |
|---|
| 448 | In this example the class of the array reference in C<$got> is ignored but |
|---|
| 449 | the class of C<$object> is checked, as is the class of everything inside |
|---|
| 450 | C<$object>. |
|---|
| 451 | |
|---|
| 452 | =head3 re($regexp, $capture_data, $flags) |
|---|
| 453 | |
|---|
| 454 | $regexp is either a regular expression reference produced with C<qr/.../> or |
|---|
| 455 | a 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 |
|---|
| 458 | an regex. This should can be an array ref or a Test::Deep comparator |
|---|
| 459 | that works on array refs. |
|---|
| 460 | |
|---|
| 461 | $flags is an optional string which controls whether the regex runs as a |
|---|
| 462 | global match. If $flags is "g" then the regex will run as m/$regexp/g. |
|---|
| 463 | |
|---|
| 464 | Without $capture_data, this simply compares $got_v with the regular expression |
|---|
| 465 | provided. So |
|---|
| 466 | |
|---|
| 467 | cmp_deeply($got, [ re("ferg") ]) |
|---|
| 468 | |
|---|
| 469 | is the equivalent of |
|---|
| 470 | |
|---|
| 471 | $got->[0] =~ /ferg/ |
|---|
| 472 | |
|---|
| 473 | With $capture_data |
|---|
| 474 | |
|---|
| 475 | cmp_deeply($got, [re($regex, $capture_data]) |
|---|
| 476 | |
|---|
| 477 | is the equivalent of |
|---|
| 478 | |
|---|
| 479 | my @data = $got->[0] =~ /$regex/; |
|---|
| 480 | cmp_deeply(\@data, $capture_data); |
|---|
| 481 | |
|---|
| 482 | So you can do something simple like |
|---|
| 483 | |
|---|
| 484 | cmp_deeply($got, re(qr/(\d\d)(\w\w)/, [25, "ab" ]) |
|---|
| 485 | |
|---|
| 486 | to check that (\d\d) was 25 and (\w\w) was "ab" but you can also use |
|---|
| 487 | Test::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 | |
|---|
| 492 | here, the regex will match the string and will capture the animal names and |
|---|
| 493 | check that they match the specified set, in this case it will fail, |
|---|
| 494 | complaining that "goat" is not in the set. |
|---|
| 495 | |
|---|
| 496 | =head3 superhashof(\%hash) |
|---|
| 497 | |
|---|
| 498 | This will check that the hash C<%$got> is a "super-hash" of C<%hash>. That |
|---|
| 499 | is that all the key and value pairs in C<%hash> appear in C<%$got> but |
|---|
| 500 | C<%$got> can have extra ones also. |
|---|
| 501 | |
|---|
| 502 | For example |
|---|
| 503 | |
|---|
| 504 | cmp_deeply({a => 1, b => 2}, superhashof({a => 1})) |
|---|
| 505 | |
|---|
| 506 | will pass but |
|---|
| 507 | |
|---|
| 508 | cmp_deeply({a => 1, b => 2}, superhashof({a => 1, c => 3})) |
|---|
| 509 | |
|---|
| 510 | will fail. |
|---|
| 511 | |
|---|
| 512 | =head3 subhashof(\%hash) |
|---|
| 513 | |
|---|
| 514 | This will check that the hash C<%$got> is a "sub-hash" of C<%hash>. That is |
|---|
| 515 | that all the key and value pairs in C<%$got> also appear in C<%hash>. |
|---|
| 516 | |
|---|
| 517 | For example |
|---|
| 518 | |
|---|
| 519 | cmp_deeply({a => 1}, subhashof({a => 1, b => 2})) |
|---|
| 520 | |
|---|
| 521 | will pass but |
|---|
| 522 | |
|---|
| 523 | cmp_deeply({a => 1, c => 3}, subhashof({a => 1, b => 2})) |
|---|
| 524 | |
|---|
| 525 | will fail. |
|---|
| 526 | |
|---|
| 527 | =head3 bag(@elements) |
|---|
| 528 | |
|---|
| 529 | @elements is an array of elements. |
|---|
| 530 | |
|---|
| 531 | This does a bag comparison, that is, it compares two arrays but ignores the |
|---|
| 532 | order of the elements so |
|---|
| 533 | |
|---|
| 534 | cmp_deeply([1, 2, 2], bag(2, 2, 1)) |
|---|
| 535 | |
|---|
| 536 | will be a pass. |
|---|
| 537 | |
|---|
| 538 | The object returned by bag() has an add() method. |
|---|
| 539 | |
|---|
| 540 | my $bag = bag(1, 2, 3); |
|---|
| 541 | $bag->add(2, 3, 4); |
|---|
| 542 | |
|---|
| 543 | will result in a bag containing 1, 2, 2, 3, 3, 4. |
|---|
| 544 | |
|---|
| 545 | C<NOTE> If you use certain special comparisons within a bag or set |
|---|
| 546 | comparison there is a danger that a test will fail when it should have |
|---|
| 547 | passed. It can only happen if two or more special comparisons in the bag are |
|---|
| 548 | competing to match elements. Consider this comparison |
|---|
| 549 | |
|---|
| 550 | cmp_deeply(['furry', 'furball'], bag(re("^fur"), re("furb"))) |
|---|
| 551 | |
|---|
| 552 | There 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 |
|---|
| 554 | could happen that C<re("^fur")> is paired with "furball" and then C<re("^furb")> |
|---|
| 555 | cannot find a match and so the test fails. Examples of other competing |
|---|
| 556 | comparisons are C<bag(1, 2, 2)> vs C<set(1, 2)> and |
|---|
| 557 | C<methods(m1 => "v1", m2 => "v2")> vs C<methods(m1 => "v1")> |
|---|
| 558 | |
|---|
| 559 | This problem is could be solved by using a slower and more complicated |
|---|
| 560 | algorithm for set and bag matching. Something for the future... |
|---|
| 561 | |
|---|
| 562 | =head3 set(@elements) |
|---|
| 563 | |
|---|
| 564 | @elements is an array of elements. |
|---|
| 565 | |
|---|
| 566 | This does a set comparison, that is, it compares two arrays but ignores the |
|---|
| 567 | order of the elements and it ignores duplicate elements, so |
|---|
| 568 | |
|---|
| 569 | cmp_deeply([1, 2, 2, 3], set(3, 2, 1, 1)) |
|---|
| 570 | |
|---|
| 571 | will be a pass. |
|---|
| 572 | |
|---|
| 573 | The object returned by set() has an add() method. |
|---|
| 574 | |
|---|
| 575 | my $set = set(1, 2, 3); |
|---|
| 576 | $set->add(4, 5, 6); |
|---|
| 577 | |
|---|
| 578 | will result in a set containing 1, 2, 3, 4, 5, 5. |
|---|
| 579 | |
|---|
| 580 | C<NOTE> See the NOTE on the bag() comparison for some dangers in using |
|---|
| 581 | special comparisons inside set() |
|---|
| 582 | |
|---|
| 583 | =head3 superbagof(@elements), subbagof(@elements), supersetof(@elements) and subsetof(@elements) |
|---|
| 584 | |
|---|
| 585 | @elements is an array of elements. |
|---|
| 586 | |
|---|
| 587 | These do exactly what you'd expect them to do, so for example |
|---|
| 588 | |
|---|
| 589 | cmp_deeply($data, subbagof(1, 1, 3, 4)); |
|---|
| 590 | |
|---|
| 591 | checks that @$data contains at most 2 "1"s, 1 "3" and 1 "4" and |
|---|
| 592 | |
|---|
| 593 | cmp_deeply($data, supsersetof(1, 4)); |
|---|
| 594 | |
|---|
| 595 | check that @$data contains at least 1 "1" and 1 "4". |
|---|
| 596 | |
|---|
| 597 | These are just special cases of the Set and Bag comparisons so they also |
|---|
| 598 | give you an add() method and they also have the same limitations when using |
|---|
| 599 | special 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 | |
|---|
| 605 | This allows you to compare data against multiple expected results and make |
|---|
| 606 | sure each of them matches. |
|---|
| 607 | |
|---|
| 608 | cmp_deeply($got, all(isa("Person"), methods(name => 'John'))) |
|---|
| 609 | |
|---|
| 610 | is equivalent to |
|---|
| 611 | |
|---|
| 612 | $got->isa("Person") |
|---|
| 613 | $got->name eq 'John' |
|---|
| 614 | |
|---|
| 615 | If either test fails then the whole thing is considered a fail. This is a |
|---|
| 616 | short-circuit test, the testing is stopped after the first failure, although |
|---|
| 617 | in the future it may complete all tests so that diagnostics can be output |
|---|
| 618 | for all failures. When reporting failure, the parts are counted from 1. |
|---|
| 619 | |
|---|
| 620 | Thanks to the magic of overloading, you can write |
|---|
| 621 | |
|---|
| 622 | all(isa("Person"), methods(name => 'John'), re("^wi")) |
|---|
| 623 | |
|---|
| 624 | as |
|---|
| 625 | |
|---|
| 626 | isa("Person") & methods(name => 'John') | re("^wi") |
|---|
| 627 | |
|---|
| 628 | Note B<single> | not double as || cannot be overloaded. This will only work |
|---|
| 629 | when there is a special comparison involved. If you write |
|---|
| 630 | |
|---|
| 631 | "john" | "anne" | "robert" |
|---|
| 632 | |
|---|
| 633 | Perl will turn this into |
|---|
| 634 | |
|---|
| 635 | "{onort" |
|---|
| 636 | |
|---|
| 637 | which is presumably not what you wanted. This is because Perl |s them |
|---|
| 638 | together as strings before Test::Deep gets a chance to do any overload |
|---|
| 639 | tricks. |
|---|
| 640 | |
|---|
| 641 | =head3 any(@expecteds) |
|---|
| 642 | |
|---|
| 643 | @expecteds is an array of expected structures. |
|---|
| 644 | |
|---|
| 645 | This can be used to compare data against multiple expected results and make |
|---|
| 646 | sure that at least one of them matches. This is a short-circuit test so if |
|---|
| 647 | a test passes then none of the tests after that will be attempted. |
|---|
| 648 | |
|---|
| 649 | You can also use overloading with | similarly to all(). |
|---|
| 650 | |
|---|
| 651 | =head3 isa($class), Isa($class) |
|---|
| 652 | |
|---|
| 653 | $class is a class name. |
|---|
| 654 | |
|---|
| 655 | This uses UNIVERSAL::isa() to check that $got_v is blessed into the class |
|---|
| 656 | $class. |
|---|
| 657 | |
|---|
| 658 | B<NOTE:> Isa() does exactly as documented here, isa() is slightly |
|---|
| 659 | different. If isa() is called with 1 argument it falls through to |
|---|
| 660 | Isa(). If isa() called with 2 arguments, it falls through to |
|---|
| 661 | UNIVERAL::isa. This is to prevent breakage when you import isa() into |
|---|
| 662 | a package that is used as a class. Without this, anyone calling |
|---|
| 663 | C<Class-E<gt>isa($other_class)> would get the wrong answer. This is a |
|---|
| 664 | hack 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 |
|---|
| 672 | objects of a known type and you don't want to have to repeat the same test |
|---|
| 673 | for 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 | |
|---|
| 685 | is similar to |
|---|
| 686 | |
|---|
| 687 | foreach my $got_v (@$got) { |
|---|
| 688 | cmp_deeply($got_v, $common_tests) |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | Except it will not explode is $got is not an array reference. It will check |
|---|
| 692 | that each of the objects in @$got is a MyFile and that each one gives the |
|---|
| 693 | correct results for it's methods. |
|---|
| 694 | |
|---|
| 695 | You could go further, if for example there were 3 files and you knew the |
|---|
| 696 | size 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 | |
|---|
| 715 | This will stringify C<$got_v> and compare it to C<$string> using C<eq>, even |
|---|
| 716 | if $got_v is a ref. It is useful for checking the stringified value of an |
|---|
| 717 | overloaded reference. |
|---|
| 718 | |
|---|
| 719 | =head3 num($number, $tolerance) |
|---|
| 720 | |
|---|
| 721 | $number is a number. |
|---|
| 722 | $tolerance is an optional number. |
|---|
| 723 | |
|---|
| 724 | This will add 0 to C<$got_v> and check if it's numerically equal to |
|---|
| 725 | C<$number>, even if $got_v is a ref. It is useful for checking the numerical |
|---|
| 726 | value of an overloaded reference. If $tolerance is supplied then this will |
|---|
| 727 | check that $got_v and $exp_v are less than $tolerance apart. This is useful |
|---|
| 728 | when comparing floating point numbers as rounding errors can make it hard or |
|---|
| 729 | impossible for $got_v to be exactly equal to $exp_v. When $tolerance is |
|---|
| 730 | supplied, the test passes if C<abs($got_v - $exp_v) <= $tolerance>. |
|---|
| 731 | |
|---|
| 732 | B<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 |
|---|
| 734 | now gone. A "lookslike s number" test will replace it soon. Until then you |
|---|
| 735 | can usually just use the string() comparison to be more strict. This will |
|---|
| 736 | work fine for almost all situations, however it will not work when <$got_v> |
|---|
| 737 | is 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 | |
|---|
| 743 | This will check that C<$got_v> and C<$value> have the same truth value, that |
|---|
| 744 | is they will give the same result when used in boolean context, like in an |
|---|
| 745 | if() statement. |
|---|
| 746 | |
|---|
| 747 | =head3 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 C<$got_v> to the subroutine which returns true or false to |
|---|
| 753 | indicate a pass or fail. Fails can be accompanied by a diagnostic string |
|---|
| 754 | 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", \&check_name); |
|---|
| 770 | |
|---|
| 771 | =head1 ANOTHER EXAMPLE |
|---|
| 772 | |
|---|
| 773 | You've written a module to handle people and their film interests. Say you |
|---|
| 774 | have a function that returns an array of people from a query, each person is |
|---|
| 775 | a 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 | |
|---|
| 786 | Soon after, your query function changes and all the results now have an ID |
|---|
| 787 | field. Now your test is failing again because you left out ID from each of |
|---|
| 788 | the hashes. The problem is that the IDs are generated by the database and |
|---|
| 789 | you have no way of knowing what each person's ID is. With Test::Deep you can |
|---|
| 790 | change 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 | |
|---|
| 801 | But your test still fails. Now, because you're using a database, you no |
|---|
| 802 | longer know what order the people will appear in. You could add a sort into |
|---|
| 803 | the database query but that could slow down your application. Instead you |
|---|
| 804 | can get Test::Deep to ignore the order of the array by doing a bag |
|---|
| 805 | comparison 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 | |
|---|
| 816 | Finally person gets even more complicated and includes a new field called |
|---|
| 817 | Movies, this is a list of movies that the person has seen recently, again |
|---|
| 818 | these movies could also come back in any order so we need a bag inside our |
|---|
| 819 | other 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 | |
|---|
| 832 | Currently any CODE, GLOB or IO refs will be compared using shallow(), which |
|---|
| 833 | means only their memory addresses are compared. |
|---|
| 834 | |
|---|
| 835 | =head1 BUGS |
|---|
| 836 | |
|---|
| 837 | There is a bug in set and bag compare to do with competing SCs. It only |
|---|
| 838 | occurs when you put certain special comparisons inside bag or set |
|---|
| 839 | comparisons you don't need to worry about it. The full details are in the |
|---|
| 840 | bag() docs. It will be fixed in an upcoming version. |
|---|
| 841 | |
|---|
| 842 | =head1 WHAT ARE SPECIAL COMPARISONS? |
|---|
| 843 | |
|---|
| 844 | A special comparison (SC) is simply an object that inherits from |
|---|
| 845 | Test::Deep::Cmp. Whenever C<$expected_v> is an SC then instead of checking |
|---|
| 846 | C<$got_v eq $expected_v>, we pass control over to the SC and let it do it's |
|---|
| 847 | thing. |
|---|
| 848 | |
|---|
| 849 | Test::Deep exports lots of SC constructors, to make it easy for you to use |
|---|
| 850 | them in your test scripts. For example is C<re("hello")> is just a handy way |
|---|
| 851 | of 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 | |
|---|
| 856 | will check C<'a' eq 'a'>, C<'b' eq 'b'> but when it comes to comparing |
|---|
| 857 | C<'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 |
|---|
| 859 | by do something like C<$expected_v->descend($got_v)>. The C<descend()> |
|---|
| 860 | method should just return true or false. |
|---|
| 861 | |
|---|
| 862 | This gives you enough to write your own SCs but I haven't documented how |
|---|
| 863 | diagnostics works because it's about to get an overhaul. |
|---|
| 864 | |
|---|
| 865 | =head1 SEE ALSO |
|---|
| 866 | |
|---|
| 867 | L<Test::More> |
|---|
| 868 | |
|---|
| 869 | =head1 AUTHOR |
|---|
| 870 | |
|---|
| 871 | Fergal Daly E<lt>fergal@esatclear.ieE<gt>, with thanks to Michael G Schwern |
|---|
| 872 | for Test::More's is_deeply function which inspired this. |
|---|
| 873 | |
|---|
| 874 | =head1 COPYRIGHT |
|---|
| 875 | |
|---|
| 876 | Copyright 2003, 2004 by Fergal Daly E<lt>fergal@esatclear.ieE<gt>. |
|---|
| 877 | |
|---|
| 878 | This program is free software; you can redistribute it and/or |
|---|
| 879 | modify it under the same terms as Perl itself. |
|---|
| 880 | |
|---|
| 881 | See F<http://www.perl.com/perl/misc/Artistic.html> |
|---|
| 882 | |
|---|
| 883 | =cut |
|---|