1Test2::Tools::Compare(3U)ser Contributed Perl DocumentatiToenst2::Tools::Compare(3)
2
3
4
6 Test2::Tools::Compare - Tools for comparing deep data structures.
7
9 Test::More had "is_deeply()". This library is the Test2 version that
10 can be used to compare data structures, but goes a step further in that
11 it provides tools for building a data structure specification against
12 which you can verify your data. There are both 'strict' and 'relaxed'
13 versions of the tools.
14
16 use Test2::Tools::Compare;
17
18 # Hash for demonstration purposes
19 my $some_hash = {a => 1, b => 2, c => 3};
20
21 # Strict checking, everything must match
22 is(
23 $some_hash,
24 {a => 1, b => 2, c => 3},
25 "The hash we got matches our expectations"
26 );
27
28 # Relaxed Checking, only fields we care about are checked, and we can use a
29 # regex to approximate a field.
30 like(
31 $some_hash,
32 {a => 1, b => qr/[0-9]+/},
33 "'a' is 1, 'b' is an integer, we don't care about 'c'."
34 );
35
36 ADVANCED
37 Declarative hash, array, and objects builders are available that allow
38 you to generate specifications. These are more verbose than simply
39 providing a hash, but have the advantage that every component you
40 specify has a line number associated. This is helpful for debugging as
41 the failure output will tell you not only which fields was incorrect,
42 but also the line on which you declared the field.
43
44 use Test2::Tools::Compare qw{
45 is like isnt unlike
46 match mismatch validator
47 hash array bag object meta number float rounded within string subset bool
48 in_set not_in_set check_set
49 item field call call_list call_hash prop check all_items all_keys all_vals all_values
50 etc end filter_items
51 T F D DNE FDNE E
52 event fail_events
53 exact_ref
54 };
55
56 is(
57 $some_hash,
58 hash {
59 field a => 1;
60 field b => 2;
61 field c => 3;
62 },
63 "Hash matches spec"
64 );
65
67 $bool = is($got, $expect)
68 $bool = is($got, $expect, $name)
69 $bool = is($got, $expect, $name, @diag)
70 $got is the data structure you want to check. $expect is what you
71 want $got to look like. $name is an optional name for the test.
72 @diag is optional diagnostics messages that will be printed to
73 STDERR in event of failure, they will not be displayed when the
74 comparison is successful. The boolean true/false result of the
75 comparison is returned.
76
77 This is the strict checker. The strict checker requires a perfect
78 match between $got and $expect. All hash fields must be specified,
79 all array items must be present, etc. All
80 non-scalar/hash/array/regex references must be identical (same
81 memory address). Scalar, hash and array references will be
82 traversed and compared. Regex references will be compared to see if
83 they have the same pattern.
84
85 is(
86 $some_hash,
87 {a => 1, b => 2, c => 3},
88 "The hash we got matches our expectations"
89 );
90
91 The only exception to strictness is when it is given an $expect
92 object that was built from a specification, in which case the
93 specification determines the strictness. Strictness only applies to
94 literal values/references that are provided and converted to a
95 specification for you.
96
97 is(
98 $some_hash,
99 hash { # Note: the hash function is not exported by default
100 field a => 1;
101 field b => match(qr/[0-9]+/); # Note: The match function is not exported by default
102 # Don't care about other fields.
103 },
104 "The hash comparison is not strict"
105 );
106
107 This works for both deep and shallow structures. For instance you
108 can use this to compare two strings:
109
110 is('foo', 'foo', "strings match");
111
112 Note: This is not the tool to use if you want to check if two
113 references are the same exact reference, use "ref_is()" from the
114 Test2::Tools::Ref plugin instead. Most of the time this will work
115 as well, however there are problems if your reference contains a
116 cycle and refers back to itself at some point. If this happens, an
117 exception will be thrown to break an otherwise infinite recursion.
118
119 Note: Non-reference values will be compared as strings using "eq",
120 so that means '2.0' and '2' will match.
121
122 $bool = isnt($got, $expect)
123 $bool = isnt($got, $expect, $name)
124 $bool = isnt($got, $expect, $name, @diag)
125 Opposite of "is()". Does all the same checks, but passes when there
126 is a mismatch.
127
128 $bool = like($got, $expect)
129 $bool = like($got, $expect, $name)
130 $bool = like($got, $expect, $name, @diag)
131 $got is the data structure you want to check. $expect is what you
132 want $got to look like. $name is an optional name for the test.
133 @diag is optional diagnostics messages that will be printed to
134 STDERR in event of failure, they will not be displayed when the
135 comparison is successful. The boolean true/false result of the
136 comparison is returned.
137
138 This is the relaxed checker. This will ignore hash keys or array
139 indexes that you do not actually specify in your $expect structure.
140 In addition regex and sub references will be used as validators. If
141 you provide a regex using "qr/.../", the regex itself will be used
142 to validate the corresponding value in the $got structure. The same
143 is true for coderefs, the value is passed in as the first argument
144 (and in $_) and the sub should return a boolean value. In this
145 tool regexes will stringify the thing they are checking.
146
147 like(
148 $some_hash,
149 {a => 1, b => qr/[0-9]+/},
150 "'a' is 1, 'b' is an integer, we don't care about other fields"
151 );
152
153 This works for both deep and shallow structures. For instance you
154 can use this to compare two strings:
155
156 like('foo bar', qr/^foo/, "string matches the pattern");
157
158 $bool = unlike($got, $expect)
159 $bool = unlike($got, $expect, $name)
160 $bool = unlike($got, $expect, $name, @diag)
161 Opposite of "like()". Does all the same checks, but passes when
162 there is a mismatch.
163
164 QUICK CHECKS
165 Note: None of these are exported by default. You need to request them.
166
167 Quick checks are a way to quickly generate a common value
168 specification. These can be used in structures passed into "is" and
169 "like" through the $expect argument.
170
171 Example:
172
173 is($foo, T(), '$foo has a true value');
174
175 $check = T()
176 This verifies that the value in the corresponding $got structure is
177 true, any true value will do.
178
179 is($foo, T(), '$foo has a true value');
180
181 is(
182 { a => 'xxx' },
183 { a => T() },
184 "The 'a' key is true"
185 );
186
187 $check = F()
188 This verifies that the value in the corresponding $got structure is
189 false, any false value will do, but the value must exist.
190
191 is($foo, F(), '$foo has a false value');
192
193 is(
194 { a => 0 },
195 { a => F() },
196 "The 'a' key is false"
197 );
198
199 It is important to note that a nonexistent value does not count as
200 false. This check will generate a failing test result:
201
202 is(
203 { a => 1 },
204 { a => 1, b => F() },
205 "The 'b' key is false"
206 );
207
208 This will produce the following output:
209
210 not ok 1 - The b key is false
211 # Failed test "The 'b' key is false"
212 # at some_file.t line 10.
213 # +------+------------------+-------+---------+
214 # | PATH | GOT | OP | CHECK |
215 # +------+------------------+-------+---------+
216 # | {b} | <DOES NOT EXIST> | FALSE | FALSE() |
217 # +------+------------------+-------+---------+
218
219 In Perl, you can have behavior that is different for a missing key
220 vs. a false key, so it was decided not to count a completely absent
221 value as false. See the "DNE()" shortcut below for checking that a
222 field is missing.
223
224 If you want to check for false and/or DNE use the "FDNE()" check.
225
226 $check = D()
227 This is to verify that the value in the $got structure is defined.
228 Any value other than "undef" will pass.
229
230 This will pass:
231
232 is('foo', D(), 'foo is defined');
233
234 This will fail:
235
236 is(undef, D(), 'foo is defined');
237
238 $check = U()
239 This is to verify that the value in the $got structure is
240 undefined.
241
242 This will pass:
243
244 is(undef, U(), 'not defined');
245
246 This will fail:
247
248 is('foo', U(), 'not defined');
249
250 $check = DF()
251 This is to verify that the value in the $got structure is defined
252 but false. Any false value other than "undef" will pass.
253
254 This will pass:
255
256 is(0, DF(), 'foo is defined but false');
257
258 These will fail:
259
260 is(undef, DF(), 'foo is defined but false');
261 is(1, DF(), 'foo is defined but false');
262
263 $check = E()
264 This can be used to check that a value exists. This is useful to
265 check that an array has more values, or to check that a key exists
266 in a hash, even if the value is undefined.
267
268 These pass:
269
270 is(['a', 'b', undef], ['a', 'b', E()], "There is a third item in the array");
271 is({a => 1, b => 2}, {a => 1, b => E()}, "The 'b' key exists in the hash");
272
273 These will fail:
274
275 is(['a', 'b'], ['a', 'b', E()], "Third item exists");
276 is({a => 1}, {a => 1, b => E()}, "'b' key exists");
277
278 $check = DNE()
279 This can be used to check that no value exists. This is useful to
280 check the end bound of an array, or to check that a key does not
281 exist in a hash.
282
283 These pass:
284
285 is(['a', 'b'], ['a', 'b', DNE()], "There is no third item in the array");
286 is({a => 1}, {a => 1, b => DNE()}, "The 'b' key does not exist in the hash");
287
288 These will fail:
289
290 is(['a', 'b', 'c'], ['a', 'b', DNE()], "No third item");
291 is({a => 1, b => 2}, {a => 1, b => DNE()}, "No 'b' key");
292
293 $check = FDNE()
294 This is a combination of "F()" and "DNE()". This will pass for a
295 false value, or a nonexistent value.
296
297 VALUE SPECIFICATIONS
298 Note: None of these are exported by default. You need to request them.
299
300 $check = string "..."
301 Verify that the value matches the given string using the "eq"
302 operator.
303
304 $check = !string "..."
305 Verify that the value does not match the given string using the
306 "ne" operator.
307
308 $check = number ...;
309 Verify that the value matches the given number using the "=="
310 operator.
311
312 $check = !number ...;
313 Verify that the value does not match the given number using the
314 "!=" operator.
315
316 $check = float ...;
317 Verify that the value is approximately equal to the given number.
318
319 If a 'precision' parameter is specified, both operands will be
320 rounded to 'precision' number of fractional decimal digits and
321 compared with "eq".
322
323 is($near_val, float($val, precision => 4), "Near 4 decimal digits");
324
325 Otherwise, the check will be made within a range of +/-
326 'tolerance', with a default 'tolerance' of 1e-08.
327
328 is( $near_val, float($val, tolerance => 0.01), "Almost there...");
329
330 See also "within" and "rounded".
331
332 $check = !float ...;
333 Verify that the value is not approximately equal to the given
334 number.
335
336 If a 'precision' parameter is specified, both operands will be
337 rounded to 'precision' number of fractional decimal digits and
338 compared with "eq".
339
340 Otherwise, the check will be made within a range of +/-
341 'tolerance', with a default 'tolerance' of 1e-08.
342
343 See also "!within" and "!rounded".
344
345 $check = within($num, $tolerance);
346 Verify that the value approximately matches the given number,
347 within a range of +/- $tolerance. Compared using the "=="
348 operator.
349
350 $tolerance is optional and defaults to 1e-08.
351
352 $check = !within($num, $tolerance);
353 Verify that the value does not approximately match the given number
354 within a range of +/- $tolerance. Compared using the "!="
355 operator.
356
357 $tolerance is optional and defaults to 1e-08.
358
359 $check = rounded($num, $precision);
360 Verify that the value approximately matches the given number, when
361 both are rounded to $precision number of fractional digits.
362 Compared using the "eq" operator.
363
364 $check = !rounded($num, $precision);
365 Verify that the value does not approximately match the given
366 number, when both are rounded to $precision number of fractional
367 digits. Compared using the "ne" operator.
368
369 $check = bool ...;
370 Verify the value has the same boolean value as the given argument
371 (XNOR).
372
373 $check = !bool ...;
374 Verify the value has a different boolean value from the given
375 argument (XOR).
376
377 $check = check_isa ...;
378 Verify the value is an instance of the given class name.
379
380 $check = !check_isa ...;
381 Verify the value is not an instance of the given class name.
382
383 $check = match qr/.../
384 $check = !mismatch qr/.../
385 Verify that the value matches the regex pattern. This form of
386 pattern check will NOT stringify references being checked.
387
388 Note: "!mismatch()" is documented for completion, please do not use
389 it.
390
391 $check = !match qr/.../
392 $check = mismatch qr/.../
393 Verify that the value does not match the regex pattern. This form
394 of pattern check will NOT stringify references being checked.
395
396 Note: "mismatch()" was created before overloading of "!" for
397 "match()" was a thing.
398
399 $check = validator(sub{ ... })
400 $check = validator($NAME => sub{ ... })
401 $check = validator($OP, $NAME, sub{ ... })
402 The coderef is the only required argument. The coderef should check
403 that the value is what you expect and return a boolean true or
404 false. Optionally, you can specify a name and operator that are
405 used in diagnostics. They are also provided to the sub itself as
406 named parameters.
407
408 Check the value using this sub. The sub gets the value in $_, and
409 it receives the value and several other items as named parameters.
410
411 my $check = validator(sub {
412 my %params = @_;
413
414 # These both work:
415 my $got = $_;
416 my $got = $params{got};
417
418 # Check if a value exists at all
419 my $exists = $params{exists}
420
421 # What $OP (if any) did we specify when creating the validator
422 my $operator = $params{operator};
423
424 # What name (if any) did we specify when creating the validator
425 my $name = $params{name};
426
427 ...
428
429 return $bool;
430 }
431
432 $check = exact_ref($ref)
433 Check that the value is exactly the same reference as the one
434 provided.
435
436 SET BUILDERS
437 Note: None of these are exported by default. You need to request them.
438
439 my $check = check_set($check1, $check2, ...)
440 Check that the value matches ALL of the specified checks.
441
442 my $check = in_set($check1, $check2, ...)
443 Check that the value matches ONE OR MORE of the specified checks.
444
445 not_in_set($check1, $check2, ...)
446 Check that the value DOES NOT match ANY of the specified checks.
447
448 check $thing
449 Check that the value matches the specified thing.
450
451 HASH BUILDER
452 Note: None of these are exported by default. You need to request them.
453
454 $check = hash {
455 field foo => 1;
456 field bar => 2;
457
458 # Ensure the 'baz' keys does not even exist in the hash.
459 field baz => DNE();
460
461 # Ensure the key exists, but is set to undef
462 field bat => undef;
463
464 # Any check can be used
465 field boo => $check;
466
467 # Set checks that apply to all keys or values. Can be done multiple
468 # times, and each call can define multiple checks, all will be run.
469 all_vals match qr/a/, match qr/b/; # All values must have an 'a' and a 'b'
470 all_keys match qr/x/; # All keys must have an 'x'
471
472 ...
473
474 end(); # optional, enforces that no other keys are present.
475 };
476
477 $check = hash { ... }
478 This is used to define a hash check.
479
480 field $NAME => $VAL
481 field $NAME => $CHECK
482 Specify a field check. This will check the hash key specified by
483 $NAME and ensure it matches the value in $VAL. You can put any
484 valid check in $VAL, such as the result of another call to "array {
485 ... }", "DNE()", etc.
486
487 Note: This function can only be used inside a hash builder sub, and
488 must be called in void context.
489
490 all_keys($CHECK1, $CHECK2, ...)
491 Add checks that apply to all keys. You can put this anywhere in the
492 hash block, and can call it any number of times with any number of
493 arguments.
494
495 all_vals($CHECK1, $CHECK2, ...)
496 all_values($CHECK1, $CHECK2, ...)
497 Add checks that apply to all values. You can put this anywhere in
498 the hash block, and can call it any number of times with any number
499 of arguments.
500
501 end()
502 Enforce that no keys are found in the hash other than those
503 specified. This is essentially the "use strict" of a hash check.
504 This can be used anywhere in the hash builder, though typically it
505 is placed at the end.
506
507 etc()
508 Ignore any extra keys found in the hash. This is the opposite of
509 "end()". This can be used anywhere in the hash builder, though
510 typically it is placed at the end.
511
512 DNE()
513 This is a handy check that can be used with "field()" to ensure
514 that a field (D)oes (N)ot (E)xist.
515
516 field foo => DNE();
517
518 ARRAY BUILDER
519 Note: None of these are exported by default. You need to request them.
520
521 $check = array {
522 # Uses the next index, in this case index 0;
523 item 'a';
524
525 # Gets index 1 automatically
526 item 'b';
527
528 # Specify the index
529 item 2 => 'c';
530
531 # We skipped index 3, which means we don't care what it is.
532 item 4 => 'e';
533
534 # Gets index 5.
535 item 'f';
536
537 # Remove any REMAINING items that contain 0-9.
538 filter_items { grep {!m/[0-9]/} @_ };
539
540 # Set checks that apply to all items. Can be done multiple times, and
541 # each call can define multiple checks, all will be run.
542 all_items match qr/a/, match qr/b/;
543 all_items match qr/x/;
544
545 # Of the remaining items (after the filter is applied) the next one
546 # (which is now index 6) should be 'g'.
547 item 6 => 'g';
548
549 item 7 => DNE; # Ensure index 7 does not exist.
550
551 end(); # Ensure no other indexes exist.
552 };
553
554 $check = array { ... }
555 item $VAL
556 item $CHECK
557 item $IDX, $VAL
558 item $IDX, $CHECK
559 Add an expected item to the array. If $IDX is not specified it will
560 automatically calculate it based on the last item added. You can
561 skip indexes, which means you do not want them to be checked.
562
563 You can provide any value to check in $VAL, or you can provide any
564 valid check object.
565
566 Note: Items MUST be added in order.
567
568 Note: This function can only be used inside an array, bag or subset
569 builder sub, and must be called in void context.
570
571 filter_items { my @remaining = @_; ...; return @filtered }
572 This function adds a filter, all items remaining in the array from
573 the point the filter is reached will be passed into the filter sub
574 as arguments, the sub should return only the items that should be
575 checked.
576
577 Note: This function can only be used inside an array builder sub,
578 and must be called in void context.
579
580 all_items($CHECK1, $CHECK2, ...)
581 Add checks that apply to all items. You can put this anywhere in
582 the array block, and can call it any number of times with any
583 number of arguments.
584
585 end()
586 Enforce that there are no indexes after the last one specified.
587 This will not force checking of skipped indexes.
588
589 etc()
590 Ignore any extra items found in the array. This is the opposite of
591 "end()". This can be used anywhere in the array builder, though
592 typically it is placed at the end.
593
594 DNE()
595 This is a handy check that can be used with "item()" to ensure that
596 an index (D)oes (N)ot (E)xist.
597
598 item 5 => DNE();
599
600 BAG BUILDER
601 Note: None of these are exported by default. You need to request them.
602
603 $check = bag {
604 item 'a';
605 item 'b';
606
607 end(); # Ensure no other elements exist.
608 };
609
610 A bag is like an array, but we don't care about the order of the items.
611 In the example, $check would match both "['a','b']" and "['b','a']".
612
613 $check = bag { ... }
614 item $VAL
615 item $CHECK
616 Add an expected item to the bag.
617
618 You can provide any value to check in $VAL, or you can provide any
619 valid check object.
620
621 Note: This function can only be used inside an array, bag or subset
622 builder sub, and must be called in void context.
623
624 all_items($CHECK1, $CHECK2, ...)
625 Add checks that apply to all items. You can put this anywhere in
626 the bag block, and can call it any number of times with any number
627 of arguments.
628
629 end()
630 Enforce that there are no more items after the last one specified.
631
632 etc()
633 Ignore any extra items found in the array. This is the opposite of
634 "end()". This can be used anywhere in the bag builder, though
635 typically it is placed at the end.
636
637 ORDERED SUBSET BUILDER
638 Note: None of these are exported by default. You need to request them.
639
640 $check = subset {
641 item 'a';
642 item 'b';
643 item 'c';
644
645 # Doesn't matter if the array has 'd', the check will skip past any
646 # unknown items until it finds the next one in our subset.
647
648 item 'e';
649 item 'f';
650 };
651
652 $check = subset { ... }
653 item $VAL
654 item $CHECK
655 Add an expected item to the subset.
656
657 You can provide any value to check in $VAL, or you can provide any
658 valid check object.
659
660 Note: Items MUST be added in order.
661
662 Note: This function can only be used inside an array, bag or subset
663 builder sub, and must be called in void context.
664
665 META BUILDER
666 Note: None of these are exported by default. You need to request them.
667
668 my $check = meta {
669 prop blessed => 'My::Module'; # Ensure value is blessed as our package
670 prop reftype => 'HASH'; # Ensure value is a blessed hash
671 prop isa => 'My::Base'; # Ensure value is an instance of our class
672 prop size => 4; # Check the number of hash keys
673 prop this => ...; # Check the item itself
674 };
675
676 meta { ... }
677 meta_check { ... }
678 Build a meta check. If you are using Moose then the "meta()"
679 function would conflict with the one exported by Moose, in such
680 cases "meta_check()" is available. Neither is exported by default.
681
682 prop $NAME => $VAL
683 prop $NAME => $CHECK
684 Check the property specified by $name against the value or check.
685
686 Valid properties are:
687
688 'blessed'
689 What package (if any) the thing is blessed as.
690
691 'reftype'
692 Reference type (if any) the thing is.
693
694 'isa'
695 What class the thing is an instance of.
696
697 'this'
698 The thing itself.
699
700 'size'
701 For array references this returns the number of elements. For
702 hashes this returns the number of keys. For everything else
703 this returns undef.
704
705 OBJECT BUILDER
706 Note: None of these are exported by default. You need to request them.
707
708 my $check = object {
709 call foo => 1; # Call the 'foo' method, check the result.
710
711 # Call the specified sub-ref as a method on the object, check the
712 # result. This is useful for wrapping methods that return multiple
713 # values.
714 call sub { [ shift->get_list ] } => [...];
715
716 # This can be used to ensure a method does not exist.
717 call nope => DNE();
718
719 # Check the hash key 'foo' of the underlying reference, this only works
720 # on blessed hashes.
721 field foo => 1;
722
723 # Check the value of index 4 on the underlying reference, this only
724 # works on blessed arrays.
725 item 4 => 'foo';
726
727 # Check the meta-property 'blessed' of the object.
728 prop blessed => 'My::Module';
729
730 # Check if the object is an instance of the specified class.
731 prop isa => 'My::Base';
732
733 # Ensure only the specified hash keys or array indexes are present in
734 # the underlying hash. Has no effect on meta-property checks or method
735 # checks.
736 end();
737 };
738
739 $check = object { ... }
740 Specify an object check for use in comparisons.
741
742 call $METHOD_NAME => $RESULT
743 call $METHOD_NAME => $CHECK
744 call [$METHOD_NAME, @METHOD_ARGS] => $RESULT
745 call [$METHOD_NAME, @METHOD_ARGS] => $CHECK
746 call sub { ... }, $RESULT
747 call sub { ... }, $CHECK
748 Call the specified method (or coderef) and verify the result. If
749 you pass an arrayref, the first element must be the method name,
750 the others are the arguments it will be called with.
751
752 The coderef form is useful if you need to do something more
753 complex.
754
755 my $ref = sub {
756 local $SOME::GLOBAL::THING = 3;
757 return [shift->get_values_for('thing')];
758 };
759
760 call $ref => ...;
761
762 call_list $METHOD_NAME => $RESULT
763 call_list $METHOD_NAME => $CHECK
764 call_list [$METHOD_NAME, @METHOD_ARGS] => $RESULT
765 call_list [$METHOD_NAME, @METHOD_ARGS] => $CHECK
766 call_list sub { ... }, $RESULT
767 call_list sub { ... }, $CHECK
768 Same as "call", but the method is invoked in list context, and the
769 result is always an arrayref.
770
771 call_list get_items => [ ... ];
772
773 call_hash $METHOD_NAME => $RESULT
774 call_hash $METHOD_NAME => $CHECK
775 call_hash [$METHOD_NAME, @METHOD_ARGS] => $RESULT
776 call_hash [$METHOD_NAME, @METHOD_ARGS] => $CHECK
777 call_hash sub { ... }, $RESULT
778 call_hash sub { ... }, $CHECK
779 Same as "call", but the method is invoked in list context, and the
780 result is always a hashref. This will warn if the method returns an
781 odd number of values.
782
783 call_hash get_items => { ... };
784
785 field $NAME => $VAL
786 Works just like it does for hash checks.
787
788 item $VAL
789 item $IDX, $VAL
790 Works just like it does for array checks.
791
792 prop $NAME => $VAL
793 prop $NAME => $CHECK
794 Check the property specified by $name against the value or check.
795
796 Valid properties are:
797
798 'blessed'
799 What package (if any) the thing is blessed as.
800
801 'reftype'
802 Reference type (if any) the thing is.
803
804 'isa'
805 What class the thing is an instance of.
806
807 'this'
808 The thing itself.
809
810 'size'
811 For array references this returns the number of elements. For
812 hashes this returns the number of keys. For everything else
813 this returns undef.
814
815 DNE()
816 Can be used with "item", or "field" to ensure the hash field or
817 array index does not exist. Can also be used with "call" to ensure
818 a method does not exist.
819
820 end()
821 Turn on strict array/hash checking, ensuring that no extra
822 keys/indexes are present.
823
824 etc()
825 Ignore any extra items found in the hash/array. This is the
826 opposite of "end()". This can be used anywhere in the builder,
827 though typically it is placed at the end.
828
829 EVENT BUILDERS
830 Note: None of these are exported by default. You need to request them.
831
832 Check that we got an event of a specified type:
833
834 my $check = event 'Ok';
835
836 Check for details about the event:
837
838 my $check = event Ok => sub {
839 # Check for a failure
840 call pass => 0;
841
842 # Effective pass after TODO/SKIP are accounted for.
843 call effective_pass => 1;
844
845 # Check the diagnostics
846 call diag => [ match qr/Failed test foo/ ];
847
848 # Check the file the event reports to
849 prop file => 'foo.t';
850
851 # Check the line number the event reports o
852 prop line => '42';
853
854 # You can check the todo/skip values as well:
855 prop skip => 'broken';
856 prop todo => 'fixme';
857
858 # Thread-id and process-id where event was generated
859 prop tid => 123;
860 prop pid => 123;
861 };
862
863 You can also provide a fully qualified event package with the '+'
864 prefix:
865
866 my $check = event '+My::Event' => sub { ... }
867
868 You can also provide a hashref instead of a sub to directly check hash
869 values of the event:
870
871 my $check = event Ok => { pass => 1, ... };
872
873 USE IN OTHER BUILDERS
874
875 You can use these all in other builders, simply use them in void
876 context to have their value(s) appended to the build.
877
878 my $check = array {
879 event Ok => { ... };
880 event Note => { ... };
881
882 fail_events Ok => { pass => 0 };
883 # Get a Diag for free.
884 };
885
886 SPECIFICS
887
888 $check = event $TYPE;
889 $check = event $TYPE => sub { ... };
890 $check = event $TYPE => { ... };
891 This works just like an object builder. In addition to supporting
892 everything the object check supports, you also have to specify the
893 event type, and many extra meta-properties are available.
894
895 Extra properties are:
896
897 'file'
898 File name to which the event reports (for use in diagnostics).
899
900 'line'
901 Line number to which the event reports (for use in
902 diagnostics).
903
904 'package'
905 Package to which the event reports (for use in diagnostics).
906
907 'subname'
908 Sub that was called to generate the event (example: "ok()").
909
910 'skip'
911 Set to the skip value if the result was generated by skipping
912 tests.
913
914 'todo'
915 Set to the todo value if TODO was set when the event was
916 generated.
917
918 'trace'
919 The "at file foo.t line 42" string that will be used in
920 diagnostics.
921
922 'tid'
923 Thread ID in which the event was generated.
924
925 'pid'
926 Process ID in which the event was generated.
927
928 NOTE: Event checks have an implicit "etc()" added. This means you
929 need to use "end()" if you want to fail on unexpected hash keys or
930 array indexes. This implicit "etc()" extends to all forms,
931 including builder, hashref, and no argument.
932
933 @checks = fail_events $TYPE;
934 @checks = fail_events $TYPE => sub { ... };
935 @checks = fail_events $TYPE => { ... };
936 Just like "event()" documented above. The difference is that this
937 produces two events, the one you specify, and a "Diag" after it.
938 There are no extra checks in the Diag.
939
940 Use this to validate a simple failure where you do not want to be
941 bothered with the default diagnostics. It only adds a single Diag
942 check, so if your failure has custom diagnostics you will need to
943 add checks for them.
944
946 The source code repository for Test2-Suite can be found at
947 https://github.com/Test-More/Test2-Suite/.
948
950 Chad Granum <exodist@cpan.org>
951
953 Chad Granum <exodist@cpan.org>
954
956 Copyright 2018 Chad Granum <exodist@cpan.org>.
957
958 This program is free software; you can redistribute it and/or modify it
959 under the same terms as Perl itself.
960
961 See http://dev.perl.org/licenses/
962
963
964
965perl v5.32.0 2020-12-16 Test2::Tools::Compare(3)