| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Test2::Tools::Compare 3perl" |
| 58 |
.TH Test2::Tools::Compare 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Test2::Tools::Compare \- Tools for comparing deep data structures. |
| 65 |
.SH DESCRIPTION |
| 66 |
.IX Header "DESCRIPTION" |
| 67 |
Test::More had \f(CWis_deeply()\fR. This library is the Test2 version that can |
| 68 |
be used to compare data structures, but goes a step further in that it provides |
| 69 |
tools for building a data structure specification against which you can verify |
| 70 |
your data. There are both 'strict' and 'relaxed' versions of the tools. |
| 71 |
.SH SYNOPSIS |
| 72 |
.IX Header "SYNOPSIS" |
| 73 |
.Vb 1 |
| 74 |
\& use Test2::Tools::Compare; |
| 75 |
\& |
| 76 |
\& # Hash for demonstration purposes |
| 77 |
\& my $some_hash = {a => 1, b => 2, c => 3}; |
| 78 |
\& |
| 79 |
\& # Strict checking, everything must match |
| 80 |
\& is( |
| 81 |
\& $some_hash, |
| 82 |
\& {a => 1, b => 2, c => 3}, |
| 83 |
\& "The hash we got matches our expectations" |
| 84 |
\& ); |
| 85 |
\& |
| 86 |
\& # Relaxed Checking, only fields we care about are checked, and we can use a |
| 87 |
\& # regex to approximate a field. |
| 88 |
\& like( |
| 89 |
\& $some_hash, |
| 90 |
\& {a => 1, b => qr/\eA[0\-9]+\ez/}, |
| 91 |
\& "\*(Aqa\*(Aq is 1, \*(Aqb\*(Aq is an integer, we don\*(Aqt care about \*(Aqc\*(Aq." |
| 92 |
\& ); |
| 93 |
.Ve |
| 94 |
.SS ADVANCED |
| 95 |
.IX Subsection "ADVANCED" |
| 96 |
Declarative hash, array, and objects builders are available that allow you to |
| 97 |
generate specifications. These are more verbose than simply providing a hash, |
| 98 |
but have the advantage that every component you specify has a line number |
| 99 |
associated. This is helpful for debugging as the failure output will tell you |
| 100 |
not only which fields was incorrect, but also the line on which you declared |
| 101 |
the field. |
| 102 |
.PP |
| 103 |
.Vb 11 |
| 104 |
\& use Test2::Tools::Compare qw{ |
| 105 |
\& is like isnt unlike |
| 106 |
\& match mismatch validator |
| 107 |
\& hash array bag object meta number float rounded within string subset bool |
| 108 |
\& in_set not_in_set check_set |
| 109 |
\& item field call call_list call_hash prop check all_items all_keys all_vals all_values |
| 110 |
\& etc end filter_items |
| 111 |
\& T F D DF E DNE FDNE U L |
| 112 |
\& event fail_events |
| 113 |
\& exact_ref |
| 114 |
\& }; |
| 115 |
\& |
| 116 |
\& is( |
| 117 |
\& $some_hash, |
| 118 |
\& hash { |
| 119 |
\& field a => 1; |
| 120 |
\& field b => 2; |
| 121 |
\& field c => 3; |
| 122 |
\& }, |
| 123 |
\& "Hash matches spec" |
| 124 |
\& ); |
| 125 |
.Ve |
| 126 |
.SH "COMPARISON TOOLS" |
| 127 |
.IX Header "COMPARISON TOOLS" |
| 128 |
.ie n .IP "$bool = is($got, $expect)" 4 |
| 129 |
.el .IP "\f(CW$bool\fR = is($got, \f(CW$expect\fR)" 4 |
| 130 |
.IX Item "$bool = is($got, $expect)" |
| 131 |
.PD 0 |
| 132 |
.ie n .IP "$bool = is($got, $expect, $name)" 4 |
| 133 |
.el .IP "\f(CW$bool\fR = is($got, \f(CW$expect\fR, \f(CW$name\fR)" 4 |
| 134 |
.IX Item "$bool = is($got, $expect, $name)" |
| 135 |
.ie n .IP "$bool = is($got, $expect, $name, @diag)" 4 |
| 136 |
.el .IP "\f(CW$bool\fR = is($got, \f(CW$expect\fR, \f(CW$name\fR, \f(CW@diag\fR)" 4 |
| 137 |
.IX Item "$bool = is($got, $expect, $name, @diag)" |
| 138 |
.PD |
| 139 |
\&\f(CW$got\fR is the data structure you want to check. \f(CW$expect\fR is what you want |
| 140 |
\&\f(CW$got\fR to look like. \f(CW$name\fR is an optional name for the test. \f(CW@diag\fR is |
| 141 |
optional diagnostics messages that will be printed to STDERR in event of |
| 142 |
failure, they will not be displayed when the comparison is successful. The |
| 143 |
boolean true/false result of the comparison is returned. |
| 144 |
.Sp |
| 145 |
This is the strict checker. The strict checker requires a perfect match between |
| 146 |
\&\f(CW$got\fR and \f(CW$expect\fR. All hash fields must be specified, all array items must |
| 147 |
be present, etc. All non\-scalar/hash/array/regex references must be identical |
| 148 |
(same memory address). Scalar, hash and array references will be traversed and |
| 149 |
compared. Regex references will be compared to see if they have the same |
| 150 |
pattern. |
| 151 |
.Sp |
| 152 |
.Vb 5 |
| 153 |
\& is( |
| 154 |
\& $some_hash, |
| 155 |
\& {a => 1, b => 2, c => 3}, |
| 156 |
\& "The hash we got matches our expectations" |
| 157 |
\& ); |
| 158 |
.Ve |
| 159 |
.Sp |
| 160 |
The only exception to strictness is when it is given an \f(CW$expect\fR object that |
| 161 |
was built from a specification, in which case the specification determines the |
| 162 |
strictness. Strictness only applies to literal values/references that are |
| 163 |
provided and converted to a specification for you. |
| 164 |
.Sp |
| 165 |
.Vb 9 |
| 166 |
\& is( |
| 167 |
\& $some_hash, |
| 168 |
\& hash { # Note: the hash function is not exported by default |
| 169 |
\& field a => 1; |
| 170 |
\& field b => match(qr/\eA[0\-9]+\ez/); # Note: The match function is not exported by default |
| 171 |
\& # Don\*(Aqt care about other fields. |
| 172 |
\& }, |
| 173 |
\& "The hash comparison is not strict" |
| 174 |
\& ); |
| 175 |
.Ve |
| 176 |
.Sp |
| 177 |
This works for both deep and shallow structures. For instance you can use this |
| 178 |
to compare two strings: |
| 179 |
.Sp |
| 180 |
.Vb 1 |
| 181 |
\& is(\*(Aqfoo\*(Aq, \*(Aqfoo\*(Aq, "strings match"); |
| 182 |
.Ve |
| 183 |
.Sp |
| 184 |
\&\fBNote\fR: This is not the tool to use if you want to check if two references are |
| 185 |
the same exact reference, use \f(CWref_is()\fR from the |
| 186 |
Test2::Tools::Ref plugin instead. \fIMost\fR of the time this will |
| 187 |
work as well, however there are problems if your reference contains a cycle and |
| 188 |
refers back to itself at some point. If this happens, an exception will be |
| 189 |
thrown to break an otherwise infinite recursion. |
| 190 |
.Sp |
| 191 |
\&\fBNote\fR: Non-reference values will be compared as strings using \f(CW\*(C`eq\*(C'\fR, so that |
| 192 |
means strings '2.0' and '2' will not match, but numeric 2.0 and 2 will, since |
| 193 |
they are both stringified to '2'. |
| 194 |
.ie n .IP "$bool = isnt($got, $expect)" 4 |
| 195 |
.el .IP "\f(CW$bool\fR = isnt($got, \f(CW$expect\fR)" 4 |
| 196 |
.IX Item "$bool = isnt($got, $expect)" |
| 197 |
.PD 0 |
| 198 |
.ie n .IP "$bool = isnt($got, $expect, $name)" 4 |
| 199 |
.el .IP "\f(CW$bool\fR = isnt($got, \f(CW$expect\fR, \f(CW$name\fR)" 4 |
| 200 |
.IX Item "$bool = isnt($got, $expect, $name)" |
| 201 |
.ie n .IP "$bool = isnt($got, $expect, $name, @diag)" 4 |
| 202 |
.el .IP "\f(CW$bool\fR = isnt($got, \f(CW$expect\fR, \f(CW$name\fR, \f(CW@diag\fR)" 4 |
| 203 |
.IX Item "$bool = isnt($got, $expect, $name, @diag)" |
| 204 |
.PD |
| 205 |
Opposite of \f(CWis()\fR. Does all the same checks, but passes when there is a |
| 206 |
mismatch. |
| 207 |
.ie n .IP "$bool = like($got, $expect)" 4 |
| 208 |
.el .IP "\f(CW$bool\fR = like($got, \f(CW$expect\fR)" 4 |
| 209 |
.IX Item "$bool = like($got, $expect)" |
| 210 |
.PD 0 |
| 211 |
.ie n .IP "$bool = like($got, $expect, $name)" 4 |
| 212 |
.el .IP "\f(CW$bool\fR = like($got, \f(CW$expect\fR, \f(CW$name\fR)" 4 |
| 213 |
.IX Item "$bool = like($got, $expect, $name)" |
| 214 |
.ie n .IP "$bool = like($got, $expect, $name, @diag)" 4 |
| 215 |
.el .IP "\f(CW$bool\fR = like($got, \f(CW$expect\fR, \f(CW$name\fR, \f(CW@diag\fR)" 4 |
| 216 |
.IX Item "$bool = like($got, $expect, $name, @diag)" |
| 217 |
.PD |
| 218 |
\&\f(CW$got\fR is the data structure you want to check. \f(CW$expect\fR is what you want |
| 219 |
\&\f(CW$got\fR to look like. \f(CW$name\fR is an optional name for the test. \f(CW@diag\fR is |
| 220 |
optional diagnostics messages that will be printed to STDERR in event of |
| 221 |
failure, they will not be displayed when the comparison is successful. The |
| 222 |
boolean true/false result of the comparison is returned. |
| 223 |
.Sp |
| 224 |
This is the relaxed checker. This will ignore hash keys or array indexes that |
| 225 |
you do not actually specify in your \f(CW$expect\fR structure. In addition regex and |
| 226 |
sub references will be used as validators. If you provide a regex using |
| 227 |
\&\f(CW\*(C`qr/.../\*(C'\fR, the regex itself will be used to validate the corresponding value |
| 228 |
in the \f(CW$got\fR structure. The same is true for coderefs, the value is passed in |
| 229 |
as the first argument (and in \f(CW$_\fR) and the sub should return a boolean value. |
| 230 |
In this tool regexes will stringify the thing they are checking. |
| 231 |
.Sp |
| 232 |
.Vb 5 |
| 233 |
\& like( |
| 234 |
\& $some_hash, |
| 235 |
\& {a => 1, b => qr/\eA[0\-9]+\ez/}, |
| 236 |
\& "\*(Aqa\*(Aq is 1, \*(Aqb\*(Aq is an integer, we don\*(Aqt care about other fields" |
| 237 |
\& ); |
| 238 |
.Ve |
| 239 |
.Sp |
| 240 |
This works for both deep and shallow structures. For instance you can use this |
| 241 |
to compare two strings: |
| 242 |
.Sp |
| 243 |
.Vb 1 |
| 244 |
\& like(\*(Aqfoo bar\*(Aq, qr/^foo/, "string matches the pattern"); |
| 245 |
.Ve |
| 246 |
.ie n .IP "$bool = unlike($got, $expect)" 4 |
| 247 |
.el .IP "\f(CW$bool\fR = unlike($got, \f(CW$expect\fR)" 4 |
| 248 |
.IX Item "$bool = unlike($got, $expect)" |
| 249 |
.PD 0 |
| 250 |
.ie n .IP "$bool = unlike($got, $expect, $name)" 4 |
| 251 |
.el .IP "\f(CW$bool\fR = unlike($got, \f(CW$expect\fR, \f(CW$name\fR)" 4 |
| 252 |
.IX Item "$bool = unlike($got, $expect, $name)" |
| 253 |
.ie n .IP "$bool = unlike($got, $expect, $name, @diag)" 4 |
| 254 |
.el .IP "\f(CW$bool\fR = unlike($got, \f(CW$expect\fR, \f(CW$name\fR, \f(CW@diag\fR)" 4 |
| 255 |
.IX Item "$bool = unlike($got, $expect, $name, @diag)" |
| 256 |
.PD |
| 257 |
Opposite of \f(CWlike()\fR. Does all the same checks, but passes when there is a |
| 258 |
mismatch. |
| 259 |
.PP |
| 260 |
The \f(CWis()\fR, \f(CWisnt()\fR, \f(CWlike()\fR, and \f(CWunlike()\fR functions can be made |
| 261 |
to dump \f(CW$got\fR using Data::Dumper when tests fail by setting the |
| 262 |
\&\f(CW\*(C`T2_AUTO_DUMP\*(C'\fR environment variable to "1". (Alternatively, \f(CW\*(C`T2_AUTO_DUMP\*(C'\fR |
| 263 |
can be set to the name of a Perl module providing a compatible \f(CWDump()\fR |
| 264 |
method.) The \f(CW\*(C`T2_AUTO_DEPARSE\*(C'\fR environment variable can be used to |
| 265 |
enable Data::Dumper's deparsing of coderefs. |
| 266 |
.SS "QUICK CHECKS" |
| 267 |
.IX Subsection "QUICK CHECKS" |
| 268 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 269 |
.PP |
| 270 |
Quick checks are a way to quickly generate a common value specification. These |
| 271 |
can be used in structures passed into \f(CW\*(C`is\*(C'\fR and \f(CW\*(C`like\*(C'\fR through the \f(CW$expect\fR |
| 272 |
argument. |
| 273 |
.PP |
| 274 |
Example: |
| 275 |
.PP |
| 276 |
.Vb 1 |
| 277 |
\& is($foo, T(), \*(Aq$foo has a true value\*(Aq); |
| 278 |
.Ve |
| 279 |
.ie n .IP "$check = T()" 4 |
| 280 |
.el .IP "\f(CW$check\fR = T()" 4 |
| 281 |
.IX Item "$check = T()" |
| 282 |
This verifies that the value in the corresponding \f(CW$got\fR structure is |
| 283 |
true, any true value will do. |
| 284 |
.Sp |
| 285 |
.Vb 1 |
| 286 |
\& is($foo, T(), \*(Aq$foo has a true value\*(Aq); |
| 287 |
\& |
| 288 |
\& is( |
| 289 |
\& { a => \*(Aqxxx\*(Aq }, |
| 290 |
\& { a => T() }, |
| 291 |
\& "The \*(Aqa\*(Aq key is true" |
| 292 |
\& ); |
| 293 |
.Ve |
| 294 |
.ie n .IP "$check = F()" 4 |
| 295 |
.el .IP "\f(CW$check\fR = F()" 4 |
| 296 |
.IX Item "$check = F()" |
| 297 |
This verifies that the value in the corresponding \f(CW$got\fR structure is |
| 298 |
false, any false value will do, \fBbut the value must exist\fR. |
| 299 |
.Sp |
| 300 |
.Vb 1 |
| 301 |
\& is($foo, F(), \*(Aq$foo has a false value\*(Aq); |
| 302 |
\& |
| 303 |
\& is( |
| 304 |
\& { a => 0 }, |
| 305 |
\& { a => F() }, |
| 306 |
\& "The \*(Aqa\*(Aq key is false" |
| 307 |
\& ); |
| 308 |
.Ve |
| 309 |
.Sp |
| 310 |
It is important to note that a nonexistent value does not count as false. This |
| 311 |
check will generate a failing test result: |
| 312 |
.Sp |
| 313 |
.Vb 5 |
| 314 |
\& is( |
| 315 |
\& { a => 1 }, |
| 316 |
\& { a => 1, b => F() }, |
| 317 |
\& "The \*(Aqb\*(Aq key is false" |
| 318 |
\& ); |
| 319 |
.Ve |
| 320 |
.Sp |
| 321 |
This will produce the following output: |
| 322 |
.Sp |
| 323 |
.Vb 8 |
| 324 |
\& not ok 1 \- The b key is false |
| 325 |
\& # Failed test "The \*(Aqb\*(Aq key is false" |
| 326 |
\& # at some_file.t line 10. |
| 327 |
\& # +\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+ |
| 328 |
\& # | PATH | GOT | OP | CHECK | |
| 329 |
\& # +\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+ |
| 330 |
\& # | {b} | <DOES NOT EXIST> | FALSE | FALSE() | |
| 331 |
\& # +\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-+ |
| 332 |
.Ve |
| 333 |
.Sp |
| 334 |
In Perl, you can have behavior that is different for a missing key vs. a false |
| 335 |
key, so it was decided not to count a completely absent value as false. |
| 336 |
See the \f(CWDNE()\fR shortcut below for checking that a field is missing. |
| 337 |
.Sp |
| 338 |
If you want to check for false and/or DNE use the \f(CWFDNE()\fR check. |
| 339 |
.ie n .IP "$check = D()" 4 |
| 340 |
.el .IP "\f(CW$check\fR = D()" 4 |
| 341 |
.IX Item "$check = D()" |
| 342 |
This is to verify that the value in the \f(CW$got\fR structure is defined. Any value |
| 343 |
other than \f(CW\*(C`undef\*(C'\fR will pass. |
| 344 |
.Sp |
| 345 |
This will pass: |
| 346 |
.Sp |
| 347 |
.Vb 1 |
| 348 |
\& is(\*(Aqfoo\*(Aq, D(), \*(Aqfoo is defined\*(Aq); |
| 349 |
.Ve |
| 350 |
.Sp |
| 351 |
This will fail: |
| 352 |
.Sp |
| 353 |
.Vb 1 |
| 354 |
\& is(undef, D(), \*(Aqfoo is defined\*(Aq); |
| 355 |
.Ve |
| 356 |
.ie n .IP "$check = U()" 4 |
| 357 |
.el .IP "\f(CW$check\fR = U()" 4 |
| 358 |
.IX Item "$check = U()" |
| 359 |
This is to verify that the value in the \f(CW$got\fR structure is undefined. |
| 360 |
.Sp |
| 361 |
This will pass: |
| 362 |
.Sp |
| 363 |
.Vb 1 |
| 364 |
\& is(undef, U(), \*(Aqnot defined\*(Aq); |
| 365 |
.Ve |
| 366 |
.Sp |
| 367 |
This will fail: |
| 368 |
.Sp |
| 369 |
.Vb 1 |
| 370 |
\& is(\*(Aqfoo\*(Aq, U(), \*(Aqnot defined\*(Aq); |
| 371 |
.Ve |
| 372 |
.ie n .IP "$check = \fBDF()\fR" 4 |
| 373 |
.el .IP "\f(CW$check\fR = \fBDF()\fR" 4 |
| 374 |
.IX Item "$check = DF()" |
| 375 |
This is to verify that the value in the \f(CW$got\fR structure is defined but false. |
| 376 |
Any false value other than \f(CW\*(C`undef\*(C'\fR will pass. |
| 377 |
.Sp |
| 378 |
This will pass: |
| 379 |
.Sp |
| 380 |
.Vb 1 |
| 381 |
\& is(0, DF(), \*(Aqfoo is defined but false\*(Aq); |
| 382 |
.Ve |
| 383 |
.Sp |
| 384 |
These will fail: |
| 385 |
.Sp |
| 386 |
.Vb 2 |
| 387 |
\& is(undef, DF(), \*(Aqfoo is defined but false\*(Aq); |
| 388 |
\& is(1, DF(), \*(Aqfoo is defined but false\*(Aq); |
| 389 |
.Ve |
| 390 |
.ie n .IP "$check = E()" 4 |
| 391 |
.el .IP "\f(CW$check\fR = E()" 4 |
| 392 |
.IX Item "$check = E()" |
| 393 |
This can be used to check that a value exists. This is useful to check that an |
| 394 |
array has more values, or to check that a key exists in a hash, even if the |
| 395 |
value is undefined. |
| 396 |
.Sp |
| 397 |
These pass: |
| 398 |
.Sp |
| 399 |
.Vb 2 |
| 400 |
\& is([\*(Aqa\*(Aq, \*(Aqb\*(Aq, undef], [\*(Aqa\*(Aq, \*(Aqb\*(Aq, E()], "There is a third item in the array"); |
| 401 |
\& is({a => 1, b => 2}, {a => 1, b => E()}, "The \*(Aqb\*(Aq key exists in the hash"); |
| 402 |
.Ve |
| 403 |
.Sp |
| 404 |
These will fail: |
| 405 |
.Sp |
| 406 |
.Vb 2 |
| 407 |
\& is([\*(Aqa\*(Aq, \*(Aqb\*(Aq], [\*(Aqa\*(Aq, \*(Aqb\*(Aq, E()], "Third item exists"); |
| 408 |
\& is({a => 1}, {a => 1, b => E()}, "\*(Aqb\*(Aq key exists"); |
| 409 |
.Ve |
| 410 |
.ie n .IP "$check = \fBDNE()\fR" 4 |
| 411 |
.el .IP "\f(CW$check\fR = \fBDNE()\fR" 4 |
| 412 |
.IX Item "$check = DNE()" |
| 413 |
This can be used to check that no value exists. This is useful to check the end |
| 414 |
bound of an array, or to check that a key does not exist in a hash. |
| 415 |
.Sp |
| 416 |
These pass: |
| 417 |
.Sp |
| 418 |
.Vb 2 |
| 419 |
\& is([\*(Aqa\*(Aq, \*(Aqb\*(Aq], [\*(Aqa\*(Aq, \*(Aqb\*(Aq, DNE()], "There is no third item in the array"); |
| 420 |
\& is({a => 1}, {a => 1, b => DNE()}, "The \*(Aqb\*(Aq key does not exist in the hash"); |
| 421 |
.Ve |
| 422 |
.Sp |
| 423 |
These will fail: |
| 424 |
.Sp |
| 425 |
.Vb 2 |
| 426 |
\& is([\*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqc\*(Aq], [\*(Aqa\*(Aq, \*(Aqb\*(Aq, DNE()], "No third item"); |
| 427 |
\& is({a => 1, b => 2}, {a => 1, b => DNE()}, "No \*(Aqb\*(Aq key"); |
| 428 |
.Ve |
| 429 |
.ie n .IP "$check = \fBFDNE()\fR" 4 |
| 430 |
.el .IP "\f(CW$check\fR = \fBFDNE()\fR" 4 |
| 431 |
.IX Item "$check = FDNE()" |
| 432 |
This is a combination of \f(CWF()\fR and \f(CWDNE()\fR. This will pass for a false value, |
| 433 |
or a nonexistent value. |
| 434 |
.ie n .IP "$check = L()" 4 |
| 435 |
.el .IP "\f(CW$check\fR = L()" 4 |
| 436 |
.IX Item "$check = L()" |
| 437 |
This is to verify that the value in the \f(CW$got\fR structure is defined and |
| 438 |
has length. Any value other than \f(CW\*(C`undef\*(C'\fR or the empty string will pass |
| 439 |
(including references). |
| 440 |
.Sp |
| 441 |
These will pass: |
| 442 |
.Sp |
| 443 |
.Vb 2 |
| 444 |
\& is(\*(Aqfoo\*(Aq, L(), \*(Aqvalue is defined and has length\*(Aq); |
| 445 |
\& is([], L(), \*(Aqvalue is defined and has length\*(Aq); |
| 446 |
.Ve |
| 447 |
.Sp |
| 448 |
These will fail: |
| 449 |
.Sp |
| 450 |
.Vb 2 |
| 451 |
\& is(undef, L(), \*(Aqvalue is defined and has length\*(Aq); |
| 452 |
\& is(\*(Aq\*(Aq, L(), \*(Aqvalue is defined and has length\*(Aq); |
| 453 |
.Ve |
| 454 |
.SS "VALUE SPECIFICATIONS" |
| 455 |
.IX Subsection "VALUE SPECIFICATIONS" |
| 456 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 457 |
.ie n .IP "$check = string ""...""" 4 |
| 458 |
.el .IP "\f(CW$check\fR = string ""...""" 4 |
| 459 |
.IX Item "$check = string ""...""" |
| 460 |
Verify that the value matches the given string using the \f(CW\*(C`eq\*(C'\fR operator. |
| 461 |
.ie n .IP "$check = !string ""...""" 4 |
| 462 |
.el .IP "\f(CW$check\fR = !string ""...""" 4 |
| 463 |
.IX Item "$check = !string ""...""" |
| 464 |
Verify that the value does not match the given string using the \f(CW\*(C`ne\*(C'\fR operator. |
| 465 |
.ie n .IP "$check = number ...;" 4 |
| 466 |
.el .IP "\f(CW$check\fR = number ...;" 4 |
| 467 |
.IX Item "$check = number ...;" |
| 468 |
Verify that the value matches the given number using the \f(CW\*(C`==\*(C'\fR operator. |
| 469 |
.ie n .IP "$check = !number ...;" 4 |
| 470 |
.el .IP "\f(CW$check\fR = !number ...;" 4 |
| 471 |
.IX Item "$check = !number ...;" |
| 472 |
Verify that the value does not match the given number using the \f(CW\*(C`!=\*(C'\fR operator. |
| 473 |
.ie n .IP "$check = number_lt ...;" 4 |
| 474 |
.el .IP "\f(CW$check\fR = number_lt ...;" 4 |
| 475 |
.IX Item "$check = number_lt ...;" |
| 476 |
.PD 0 |
| 477 |
.ie n .IP "$check = number_le ...;" 4 |
| 478 |
.el .IP "\f(CW$check\fR = number_le ...;" 4 |
| 479 |
.IX Item "$check = number_le ...;" |
| 480 |
.ie n .IP "$check = number_ge ...;" 4 |
| 481 |
.el .IP "\f(CW$check\fR = number_ge ...;" 4 |
| 482 |
.IX Item "$check = number_ge ...;" |
| 483 |
.ie n .IP "$check = number_gt ...;" 4 |
| 484 |
.el .IP "\f(CW$check\fR = number_gt ...;" 4 |
| 485 |
.IX Item "$check = number_gt ...;" |
| 486 |
.PD |
| 487 |
Verify that the value is less than, less than or equal to, greater than or |
| 488 |
equal to, or greater than the given number. |
| 489 |
.ie n .IP "$check = float ...;" 4 |
| 490 |
.el .IP "\f(CW$check\fR = float ...;" 4 |
| 491 |
.IX Item "$check = float ...;" |
| 492 |
Verify that the value is approximately equal to the given number. |
| 493 |
.Sp |
| 494 |
If a 'precision' parameter is specified, both operands will be |
| 495 |
rounded to 'precision' number of fractional decimal digits and |
| 496 |
compared with \f(CW\*(C`eq\*(C'\fR. |
| 497 |
.Sp |
| 498 |
.Vb 1 |
| 499 |
\& is($near_val, float($val, precision => 4), "Near 4 decimal digits"); |
| 500 |
.Ve |
| 501 |
.Sp |
| 502 |
Otherwise, the check will be made within a range of +/\- 'tolerance', |
| 503 |
with a default 'tolerance' of 1e\-08. |
| 504 |
.Sp |
| 505 |
.Vb 1 |
| 506 |
\& is( $near_val, float($val, tolerance => 0.01), "Almost there..."); |
| 507 |
.Ve |
| 508 |
.Sp |
| 509 |
See also \f(CW\*(C`within\*(C'\fR and \f(CW\*(C`rounded\*(C'\fR. |
| 510 |
.ie n .IP "$check = !float ...;" 4 |
| 511 |
.el .IP "\f(CW$check\fR = !float ...;" 4 |
| 512 |
.IX Item "$check = !float ...;" |
| 513 |
Verify that the value is not approximately equal to the given number. |
| 514 |
.Sp |
| 515 |
If a 'precision' parameter is specified, both operands will be |
| 516 |
rounded to 'precision' number of fractional decimal digits and |
| 517 |
compared with \f(CW\*(C`eq\*(C'\fR. |
| 518 |
.Sp |
| 519 |
Otherwise, the check will be made within a range of +/\- 'tolerance', |
| 520 |
with a default 'tolerance' of 1e\-08. |
| 521 |
.Sp |
| 522 |
See also \f(CW\*(C`!within\*(C'\fR and \f(CW\*(C`!rounded\*(C'\fR. |
| 523 |
.ie n .IP "$check = within($num, $tolerance);" 4 |
| 524 |
.el .IP "\f(CW$check\fR = within($num, \f(CW$tolerance\fR);" 4 |
| 525 |
.IX Item "$check = within($num, $tolerance);" |
| 526 |
Verify that the value approximately matches the given number, |
| 527 |
within a range of +/\- \f(CW$tolerance\fR. Compared using the \f(CW\*(C`==\*(C'\fR operator. |
| 528 |
.Sp |
| 529 |
\&\f(CW$tolerance\fR is optional and defaults to 1e\-08. |
| 530 |
.ie n .IP "$check = !within($num, $tolerance);" 4 |
| 531 |
.el .IP "\f(CW$check\fR = !within($num, \f(CW$tolerance\fR);" 4 |
| 532 |
.IX Item "$check = !within($num, $tolerance);" |
| 533 |
Verify that the value does not approximately match the given number within a range of +/\- \f(CW$tolerance\fR. Compared using the \f(CW\*(C`!=\*(C'\fR operator. |
| 534 |
.Sp |
| 535 |
\&\f(CW$tolerance\fR is optional and defaults to 1e\-08. |
| 536 |
.ie n .IP "$check = rounded($num, $precision);" 4 |
| 537 |
.el .IP "\f(CW$check\fR = rounded($num, \f(CW$precision\fR);" 4 |
| 538 |
.IX Item "$check = rounded($num, $precision);" |
| 539 |
Verify that the value approximately matches the given number, when both are rounded to \f(CW$precision\fR number of fractional digits. Compared using the \f(CW\*(C`eq\*(C'\fR operator. |
| 540 |
.ie n .IP "$check = !rounded($num, $precision);" 4 |
| 541 |
.el .IP "\f(CW$check\fR = !rounded($num, \f(CW$precision\fR);" 4 |
| 542 |
.IX Item "$check = !rounded($num, $precision);" |
| 543 |
Verify that the value does not approximately match the given number, when both are rounded to \f(CW$precision\fR number of fractional digits. Compared using the \f(CW\*(C`ne\*(C'\fR operator. |
| 544 |
.ie n .IP "$check = bool ...;" 4 |
| 545 |
.el .IP "\f(CW$check\fR = bool ...;" 4 |
| 546 |
.IX Item "$check = bool ...;" |
| 547 |
Verify the value has the same boolean value as the given argument (XNOR). |
| 548 |
.ie n .IP "$check = !bool ...;" 4 |
| 549 |
.el .IP "\f(CW$check\fR = !bool ...;" 4 |
| 550 |
.IX Item "$check = !bool ...;" |
| 551 |
Verify the value has a different boolean value from the given argument (XOR). |
| 552 |
.ie n .IP "$check = check_isa ...;" 4 |
| 553 |
.el .IP "\f(CW$check\fR = check_isa ...;" 4 |
| 554 |
.IX Item "$check = check_isa ...;" |
| 555 |
Verify the value is an instance of the given class name. |
| 556 |
.ie n .IP "$check = !check_isa ...;" 4 |
| 557 |
.el .IP "\f(CW$check\fR = !check_isa ...;" 4 |
| 558 |
.IX Item "$check = !check_isa ...;" |
| 559 |
Verify the value is not an instance of the given class name. |
| 560 |
.ie n .IP "$check = match qr/.../" 4 |
| 561 |
.el .IP "\f(CW$check\fR = match qr/.../" 4 |
| 562 |
.IX Item "$check = match qr/.../" |
| 563 |
.PD 0 |
| 564 |
.ie n .IP "$check = !mismatch qr/.../" 4 |
| 565 |
.el .IP "\f(CW$check\fR = !mismatch qr/.../" 4 |
| 566 |
.IX Item "$check = !mismatch qr/.../" |
| 567 |
.PD |
| 568 |
Verify that the value matches the regex pattern. This form of pattern check |
| 569 |
will \fBNOT\fR stringify references being checked. |
| 570 |
.Sp |
| 571 |
\&\fBNote:\fR \f(CW\*(C`!mismatch()\*(C'\fR is documented for completion, please do not use it. |
| 572 |
.ie n .IP "$check = !match qr/.../" 4 |
| 573 |
.el .IP "\f(CW$check\fR = !match qr/.../" 4 |
| 574 |
.IX Item "$check = !match qr/.../" |
| 575 |
.PD 0 |
| 576 |
.ie n .IP "$check = mismatch qr/.../" 4 |
| 577 |
.el .IP "\f(CW$check\fR = mismatch qr/.../" 4 |
| 578 |
.IX Item "$check = mismatch qr/.../" |
| 579 |
.PD |
| 580 |
Verify that the value does not match the regex pattern. This form of pattern |
| 581 |
check will \fBNOT\fR stringify references being checked. |
| 582 |
.Sp |
| 583 |
\&\fBNote:\fR \f(CWmismatch()\fR was created before overloading of \f(CW\*(C`!\*(C'\fR for \f(CWmatch()\fR |
| 584 |
was a thing. |
| 585 |
.ie n .IP "$check = validator(sub{ ... })" 4 |
| 586 |
.el .IP "\f(CW$check\fR = validator(sub{ ... })" 4 |
| 587 |
.IX Item "$check = validator(sub{ ... })" |
| 588 |
.PD 0 |
| 589 |
.ie n .IP "$check = validator($NAME => sub{ ... })" 4 |
| 590 |
.el .IP "\f(CW$check\fR = validator($NAME => sub{ ... })" 4 |
| 591 |
.IX Item "$check = validator($NAME => sub{ ... })" |
| 592 |
.ie n .IP "$check = validator($OP, $NAME, sub{ ... })" 4 |
| 593 |
.el .IP "\f(CW$check\fR = validator($OP, \f(CW$NAME\fR, sub{ ... })" 4 |
| 594 |
.IX Item "$check = validator($OP, $NAME, sub{ ... })" |
| 595 |
.PD |
| 596 |
The coderef is the only required argument. The coderef should check that the |
| 597 |
value is what you expect and return a boolean true or false. Optionally, |
| 598 |
you can specify a name and operator that are used in diagnostics. They are also |
| 599 |
provided to the sub itself as named parameters. |
| 600 |
.Sp |
| 601 |
Check the value using this sub. The sub gets the value in \f(CW$_\fR, and it |
| 602 |
receives the value and several other items as named parameters. |
| 603 |
.Sp |
| 604 |
.Vb 2 |
| 605 |
\& my $check = validator(sub { |
| 606 |
\& my %params = @_; |
| 607 |
\& |
| 608 |
\& # These both work: |
| 609 |
\& my $got = $_; |
| 610 |
\& my $got = $params{got}; |
| 611 |
\& |
| 612 |
\& # Check if a value exists at all |
| 613 |
\& my $exists = $params{exists} |
| 614 |
\& |
| 615 |
\& # What $OP (if any) did we specify when creating the validator |
| 616 |
\& my $operator = $params{operator}; |
| 617 |
\& |
| 618 |
\& # What name (if any) did we specify when creating the validator |
| 619 |
\& my $name = $params{name}; |
| 620 |
\& |
| 621 |
\& ... |
| 622 |
\& |
| 623 |
\& return $bool; |
| 624 |
\& } |
| 625 |
.Ve |
| 626 |
.ie n .IP "$check = exact_ref($ref)" 4 |
| 627 |
.el .IP "\f(CW$check\fR = exact_ref($ref)" 4 |
| 628 |
.IX Item "$check = exact_ref($ref)" |
| 629 |
Check that the value is exactly the same reference as the one provided. |
| 630 |
.SS "SET BUILDERS" |
| 631 |
.IX Subsection "SET BUILDERS" |
| 632 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 633 |
.ie n .IP "my $check = check_set($check1, $check2, ...)" 4 |
| 634 |
.el .IP "my \f(CW$check\fR = check_set($check1, \f(CW$check2\fR, ...)" 4 |
| 635 |
.IX Item "my $check = check_set($check1, $check2, ...)" |
| 636 |
Check that the value matches ALL of the specified checks. |
| 637 |
.ie n .IP "my $check = in_set($check1, $check2, ...)" 4 |
| 638 |
.el .IP "my \f(CW$check\fR = in_set($check1, \f(CW$check2\fR, ...)" 4 |
| 639 |
.IX Item "my $check = in_set($check1, $check2, ...)" |
| 640 |
Check that the value matches ONE OR MORE of the specified checks. |
| 641 |
.ie n .IP "not_in_set($check1, $check2, ...)" 4 |
| 642 |
.el .IP "not_in_set($check1, \f(CW$check2\fR, ...)" 4 |
| 643 |
.IX Item "not_in_set($check1, $check2, ...)" |
| 644 |
Check that the value DOES NOT match ANY of the specified checks. |
| 645 |
.ie n .IP "check $thing" 4 |
| 646 |
.el .IP "check \f(CW$thing\fR" 4 |
| 647 |
.IX Item "check $thing" |
| 648 |
Check that the value matches the specified thing. |
| 649 |
.SS "HASH BUILDER" |
| 650 |
.IX Subsection "HASH BUILDER" |
| 651 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 652 |
.PP |
| 653 |
.Vb 3 |
| 654 |
\& $check = hash { |
| 655 |
\& field foo => 1; |
| 656 |
\& field bar => 2; |
| 657 |
\& |
| 658 |
\& # Ensure the \*(Aqbaz\*(Aq keys does not even exist in the hash. |
| 659 |
\& field baz => DNE(); |
| 660 |
\& |
| 661 |
\& # Ensure the key exists, but is set to undef |
| 662 |
\& field bat => undef; |
| 663 |
\& |
| 664 |
\& # Any check can be used |
| 665 |
\& field boo => $check; |
| 666 |
\& |
| 667 |
\& # Set checks that apply to all keys or values. Can be done multiple |
| 668 |
\& # times, and each call can define multiple checks, all will be run. |
| 669 |
\& all_vals match qr/a/, match qr/b/; # All values must have an \*(Aqa\*(Aq and a \*(Aqb\*(Aq |
| 670 |
\& all_keys match qr/x/; # All keys must have an \*(Aqx\*(Aq |
| 671 |
\& |
| 672 |
\& ... |
| 673 |
\& |
| 674 |
\& end(); # optional, enforces that no other keys are present. |
| 675 |
\& }; |
| 676 |
.Ve |
| 677 |
.ie n .IP "$check = hash { ... }" 4 |
| 678 |
.el .IP "\f(CW$check\fR = hash { ... }" 4 |
| 679 |
.IX Item "$check = hash { ... }" |
| 680 |
This is used to define a hash check. |
| 681 |
.ie n .IP "field $NAME => $VAL" 4 |
| 682 |
.el .IP "field \f(CW$NAME\fR => \f(CW$VAL\fR" 4 |
| 683 |
.IX Item "field $NAME => $VAL" |
| 684 |
.PD 0 |
| 685 |
.ie n .IP "field $NAME => $CHECK" 4 |
| 686 |
.el .IP "field \f(CW$NAME\fR => \f(CW$CHECK\fR" 4 |
| 687 |
.IX Item "field $NAME => $CHECK" |
| 688 |
.PD |
| 689 |
Specify a field check. This will check the hash key specified by \f(CW$NAME\fR and |
| 690 |
ensure it matches the value in \f(CW$VAL\fR. You can put any valid check in \f(CW$VAL\fR, |
| 691 |
such as the result of another call to \f(CW\*(C`array { ... }\*(C'\fR, \f(CWDNE()\fR, etc. |
| 692 |
.Sp |
| 693 |
\&\fBNote:\fR This function can only be used inside a hash builder sub, and must be |
| 694 |
called in void context. |
| 695 |
.ie n .IP "all_keys($CHECK1, $CHECK2, ...)" 4 |
| 696 |
.el .IP "all_keys($CHECK1, \f(CW$CHECK2\fR, ...)" 4 |
| 697 |
.IX Item "all_keys($CHECK1, $CHECK2, ...)" |
| 698 |
Add checks that apply to all keys. You can put this anywhere in the hash |
| 699 |
block, and can call it any number of times with any number of arguments. |
| 700 |
.ie n .IP "all_vals($CHECK1, $CHECK2, ...)" 4 |
| 701 |
.el .IP "all_vals($CHECK1, \f(CW$CHECK2\fR, ...)" 4 |
| 702 |
.IX Item "all_vals($CHECK1, $CHECK2, ...)" |
| 703 |
.PD 0 |
| 704 |
.ie n .IP "all_values($CHECK1, $CHECK2, ...)" 4 |
| 705 |
.el .IP "all_values($CHECK1, \f(CW$CHECK2\fR, ...)" 4 |
| 706 |
.IX Item "all_values($CHECK1, $CHECK2, ...)" |
| 707 |
.PD |
| 708 |
Add checks that apply to all values. You can put this anywhere in the hash |
| 709 |
block, and can call it any number of times with any number of arguments. |
| 710 |
.IP \fBend()\fR 4 |
| 711 |
.IX Item "end()" |
| 712 |
Enforce that no keys are found in the hash other than those specified. This is |
| 713 |
essentially the \f(CW\*(C`use strict\*(C'\fR of a hash check. This can be used anywhere in the |
| 714 |
hash builder, though typically it is placed at the end. |
| 715 |
.IP \fBetc()\fR 4 |
| 716 |
.IX Item "etc()" |
| 717 |
Ignore any extra keys found in the hash. This is the opposite of \f(CWend()\fR. |
| 718 |
This can be used anywhere in the hash builder, though typically it is placed at |
| 719 |
the end. |
| 720 |
.IP \fBDNE()\fR 4 |
| 721 |
.IX Item "DNE()" |
| 722 |
This is a handy check that can be used with \f(CWfield()\fR to ensure that a field |
| 723 |
(D)oes (N)ot (E)xist. |
| 724 |
.Sp |
| 725 |
.Vb 1 |
| 726 |
\& field foo => DNE(); |
| 727 |
.Ve |
| 728 |
.SS "ARRAY BUILDER" |
| 729 |
.IX Subsection "ARRAY BUILDER" |
| 730 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 731 |
.PP |
| 732 |
.Vb 3 |
| 733 |
\& $check = array { |
| 734 |
\& # Uses the next index, in this case index 0; |
| 735 |
\& item \*(Aqa\*(Aq; |
| 736 |
\& |
| 737 |
\& # Gets index 1 automatically |
| 738 |
\& item \*(Aqb\*(Aq; |
| 739 |
\& |
| 740 |
\& # Specify the index |
| 741 |
\& item 2 => \*(Aqc\*(Aq; |
| 742 |
\& |
| 743 |
\& # We skipped index 3, which means we don\*(Aqt care what it is. |
| 744 |
\& item 4 => \*(Aqe\*(Aq; |
| 745 |
\& |
| 746 |
\& # Gets index 5. |
| 747 |
\& item \*(Aqf\*(Aq; |
| 748 |
\& |
| 749 |
\& # Remove any REMAINING items that contain 0\-9. |
| 750 |
\& filter_items { grep {!m/[0\-9]/} @_ }; |
| 751 |
\& |
| 752 |
\& # Set checks that apply to all items. Can be done multiple times, and |
| 753 |
\& # each call can define multiple checks, all will be run. |
| 754 |
\& all_items match qr/a/, match qr/b/; |
| 755 |
\& all_items match qr/x/; |
| 756 |
\& |
| 757 |
\& # Of the remaining items (after the filter is applied) the next one |
| 758 |
\& # (which is now index 6) should be \*(Aqg\*(Aq. |
| 759 |
\& item 6 => \*(Aqg\*(Aq; |
| 760 |
\& |
| 761 |
\& item 7 => DNE; # Ensure index 7 does not exist. |
| 762 |
\& |
| 763 |
\& end(); # Ensure no other indexes exist. |
| 764 |
\& }; |
| 765 |
.Ve |
| 766 |
.ie n .IP "$check = array { ... }" 4 |
| 767 |
.el .IP "\f(CW$check\fR = array { ... }" 4 |
| 768 |
.IX Item "$check = array { ... }" |
| 769 |
.PD 0 |
| 770 |
.ie n .IP "item $VAL" 4 |
| 771 |
.el .IP "item \f(CW$VAL\fR" 4 |
| 772 |
.IX Item "item $VAL" |
| 773 |
.ie n .IP "item $CHECK" 4 |
| 774 |
.el .IP "item \f(CW$CHECK\fR" 4 |
| 775 |
.IX Item "item $CHECK" |
| 776 |
.ie n .IP "item $IDX, $VAL" 4 |
| 777 |
.el .IP "item \f(CW$IDX\fR, \f(CW$VAL\fR" 4 |
| 778 |
.IX Item "item $IDX, $VAL" |
| 779 |
.ie n .IP "item $IDX, $CHECK" 4 |
| 780 |
.el .IP "item \f(CW$IDX\fR, \f(CW$CHECK\fR" 4 |
| 781 |
.IX Item "item $IDX, $CHECK" |
| 782 |
.PD |
| 783 |
Add an expected item to the array. If \f(CW$IDX\fR is not specified it will |
| 784 |
automatically calculate it based on the last item added. You can skip indexes, |
| 785 |
which means you do not want them to be checked. |
| 786 |
.Sp |
| 787 |
You can provide any value to check in \f(CW$VAL\fR, or you can provide any valid |
| 788 |
check object. |
| 789 |
.Sp |
| 790 |
\&\fBNote:\fR Items MUST be added in order. |
| 791 |
.Sp |
| 792 |
\&\fBNote:\fR This function can only be used inside an array, bag or subset |
| 793 |
builder sub, and must be called in void context. |
| 794 |
.ie n .IP "filter_items { my @remaining = @_; ...; return @filtered }" 4 |
| 795 |
.el .IP "filter_items { my \f(CW@remaining\fR = \f(CW@_\fR; ...; return \f(CW@filtered\fR }" 4 |
| 796 |
.IX Item "filter_items { my @remaining = @_; ...; return @filtered }" |
| 797 |
This function adds a filter, all items remaining in the array from the point |
| 798 |
the filter is reached will be passed into the filter sub as arguments, the sub |
| 799 |
should return only the items that should be checked. |
| 800 |
.Sp |
| 801 |
\&\fBNote:\fR This function can only be used inside an array builder sub, and must |
| 802 |
be called in void context. |
| 803 |
.ie n .IP "all_items($CHECK1, $CHECK2, ...)" 4 |
| 804 |
.el .IP "all_items($CHECK1, \f(CW$CHECK2\fR, ...)" 4 |
| 805 |
.IX Item "all_items($CHECK1, $CHECK2, ...)" |
| 806 |
Add checks that apply to all items. You can put this anywhere in the array |
| 807 |
block, and can call it any number of times with any number of arguments. |
| 808 |
.IP \fBend()\fR 4 |
| 809 |
.IX Item "end()" |
| 810 |
Enforce that there are no indexes after the last one specified. This will not |
| 811 |
force checking of skipped indexes. |
| 812 |
.IP \fBetc()\fR 4 |
| 813 |
.IX Item "etc()" |
| 814 |
Ignore any extra items found in the array. This is the opposite of \f(CWend()\fR. |
| 815 |
This can be used anywhere in the array builder, though typically it is placed |
| 816 |
at the end. |
| 817 |
.IP \fBDNE()\fR 4 |
| 818 |
.IX Item "DNE()" |
| 819 |
This is a handy check that can be used with \f(CWitem()\fR to ensure that an index |
| 820 |
(D)oes (N)ot (E)xist. |
| 821 |
.Sp |
| 822 |
.Vb 1 |
| 823 |
\& item 5 => DNE(); |
| 824 |
.Ve |
| 825 |
.SS "BAG BUILDER" |
| 826 |
.IX Subsection "BAG BUILDER" |
| 827 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 828 |
.PP |
| 829 |
.Vb 3 |
| 830 |
\& $check = bag { |
| 831 |
\& item \*(Aqa\*(Aq; |
| 832 |
\& item \*(Aqb\*(Aq; |
| 833 |
\& |
| 834 |
\& end(); # Ensure no other elements exist. |
| 835 |
\& }; |
| 836 |
.Ve |
| 837 |
.PP |
| 838 |
A bag is like an array, but we don't care about the order of the |
| 839 |
items. In the example, \f(CW$check\fR would match both \f(CW\*(C`[\*(Aqa\*(Aq,\*(Aqb\*(Aq]\*(C'\fR and |
| 840 |
\&\f(CW\*(C`[\*(Aqb\*(Aq,\*(Aqa\*(Aq]\*(C'\fR. |
| 841 |
.ie n .IP "$check = bag { ... }" 4 |
| 842 |
.el .IP "\f(CW$check\fR = bag { ... }" 4 |
| 843 |
.IX Item "$check = bag { ... }" |
| 844 |
.PD 0 |
| 845 |
.ie n .IP "item $VAL" 4 |
| 846 |
.el .IP "item \f(CW$VAL\fR" 4 |
| 847 |
.IX Item "item $VAL" |
| 848 |
.ie n .IP "item $CHECK" 4 |
| 849 |
.el .IP "item \f(CW$CHECK\fR" 4 |
| 850 |
.IX Item "item $CHECK" |
| 851 |
.PD |
| 852 |
Add an expected item to the bag. |
| 853 |
.Sp |
| 854 |
You can provide any value to check in \f(CW$VAL\fR, or you can provide any valid |
| 855 |
check object. |
| 856 |
.Sp |
| 857 |
\&\fBNote:\fR This function can only be used inside an array, bag or subset |
| 858 |
builder sub, and must be called in void context. |
| 859 |
.ie n .IP "all_items($CHECK1, $CHECK2, ...)" 4 |
| 860 |
.el .IP "all_items($CHECK1, \f(CW$CHECK2\fR, ...)" 4 |
| 861 |
.IX Item "all_items($CHECK1, $CHECK2, ...)" |
| 862 |
Add checks that apply to all items. You can put this anywhere in the bag |
| 863 |
block, and can call it any number of times with any number of arguments. |
| 864 |
.IP \fBend()\fR 4 |
| 865 |
.IX Item "end()" |
| 866 |
Enforce that there are no more items after the last one specified. |
| 867 |
.IP \fBetc()\fR 4 |
| 868 |
.IX Item "etc()" |
| 869 |
Ignore any extra items found in the array. This is the opposite of \f(CWend()\fR. |
| 870 |
This can be used anywhere in the bag builder, though typically it is placed |
| 871 |
at the end. |
| 872 |
.SS "ORDERED SUBSET BUILDER" |
| 873 |
.IX Subsection "ORDERED SUBSET BUILDER" |
| 874 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 875 |
.PP |
| 876 |
.Vb 4 |
| 877 |
\& $check = subset { |
| 878 |
\& item \*(Aqa\*(Aq; |
| 879 |
\& item \*(Aqb\*(Aq; |
| 880 |
\& item \*(Aqc\*(Aq; |
| 881 |
\& |
| 882 |
\& # Doesn\*(Aqt matter if the array has \*(Aqd\*(Aq, the check will skip past any |
| 883 |
\& # unknown items until it finds the next one in our subset. |
| 884 |
\& |
| 885 |
\& item \*(Aqe\*(Aq; |
| 886 |
\& item \*(Aqf\*(Aq; |
| 887 |
\& }; |
| 888 |
.Ve |
| 889 |
.ie n .IP "$check = subset { ... }" 4 |
| 890 |
.el .IP "\f(CW$check\fR = subset { ... }" 4 |
| 891 |
.IX Item "$check = subset { ... }" |
| 892 |
.PD 0 |
| 893 |
.ie n .IP "item $VAL" 4 |
| 894 |
.el .IP "item \f(CW$VAL\fR" 4 |
| 895 |
.IX Item "item $VAL" |
| 896 |
.ie n .IP "item $CHECK" 4 |
| 897 |
.el .IP "item \f(CW$CHECK\fR" 4 |
| 898 |
.IX Item "item $CHECK" |
| 899 |
.PD |
| 900 |
Add an expected item to the subset. |
| 901 |
.Sp |
| 902 |
You can provide any value to check in \f(CW$VAL\fR, or you can provide any valid |
| 903 |
check object. |
| 904 |
.Sp |
| 905 |
\&\fBNote:\fR Items MUST be added in order. |
| 906 |
.Sp |
| 907 |
\&\fBNote:\fR This function can only be used inside an array, bag or subset |
| 908 |
builder sub, and must be called in void context. |
| 909 |
.SS "META BUILDER" |
| 910 |
.IX Subsection "META BUILDER" |
| 911 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 912 |
.PP |
| 913 |
.Vb 7 |
| 914 |
\& my $check = meta { |
| 915 |
\& prop blessed => \*(AqMy::Module\*(Aq; # Ensure value is blessed as our package |
| 916 |
\& prop reftype => \*(AqHASH\*(Aq; # Ensure value is a blessed hash |
| 917 |
\& prop isa => \*(AqMy::Base\*(Aq; # Ensure value is an instance of our class |
| 918 |
\& prop size => 4; # Check the number of hash keys |
| 919 |
\& prop this => ...; # Check the item itself |
| 920 |
\& }; |
| 921 |
.Ve |
| 922 |
.IP "meta { ... }" 4 |
| 923 |
.IX Item "meta { ... }" |
| 924 |
.PD 0 |
| 925 |
.IP "meta_check { ... }" 4 |
| 926 |
.IX Item "meta_check { ... }" |
| 927 |
.PD |
| 928 |
Build a meta check. If you are using Moose then the \f(CWmeta()\fR function would |
| 929 |
conflict with the one exported by Moose, in such cases \f(CWmeta_check()\fR is |
| 930 |
available. Neither is exported by default. |
| 931 |
.ie n .IP "prop $NAME => $VAL" 4 |
| 932 |
.el .IP "prop \f(CW$NAME\fR => \f(CW$VAL\fR" 4 |
| 933 |
.IX Item "prop $NAME => $VAL" |
| 934 |
.PD 0 |
| 935 |
.ie n .IP "prop $NAME => $CHECK" 4 |
| 936 |
.el .IP "prop \f(CW$NAME\fR => \f(CW$CHECK\fR" 4 |
| 937 |
.IX Item "prop $NAME => $CHECK" |
| 938 |
.PD |
| 939 |
Check the property specified by \f(CW$name\fR against the value or check. |
| 940 |
.Sp |
| 941 |
Valid properties are: |
| 942 |
.RS 4 |
| 943 |
.IP 'blessed' 4 |
| 944 |
.IX Item "'blessed'" |
| 945 |
What package (if any) the thing is blessed as. |
| 946 |
.IP 'reftype' 4 |
| 947 |
.IX Item "'reftype'" |
| 948 |
Reference type (if any) the thing is. |
| 949 |
.IP 'isa' 4 |
| 950 |
.IX Item "'isa'" |
| 951 |
What class the thing is an instance of. |
| 952 |
.IP 'this' 4 |
| 953 |
.IX Item "'this'" |
| 954 |
The thing itself. |
| 955 |
.IP 'size' 4 |
| 956 |
.IX Item "'size'" |
| 957 |
For array references this returns the number of elements. For hashes this |
| 958 |
returns the number of keys. For everything else this returns undef. |
| 959 |
.RE |
| 960 |
.RS 4 |
| 961 |
.RE |
| 962 |
.SS "OBJECT BUILDER" |
| 963 |
.IX Subsection "OBJECT BUILDER" |
| 964 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 965 |
.PP |
| 966 |
.Vb 2 |
| 967 |
\& my $check = object { |
| 968 |
\& call foo => 1; # Call the \*(Aqfoo\*(Aq method, check the result. |
| 969 |
\& |
| 970 |
\& # Call the specified sub\-ref as a method on the object, check the |
| 971 |
\& # result. This is useful for wrapping methods that return multiple |
| 972 |
\& # values. |
| 973 |
\& call sub { [ shift\->get_list ] } => [...]; |
| 974 |
\& |
| 975 |
\& # This can be used to ensure a method does not exist. |
| 976 |
\& call nope => DNE(); |
| 977 |
\& |
| 978 |
\& # Check the hash key \*(Aqfoo\*(Aq of the underlying reference, this only works |
| 979 |
\& # on blessed hashes. |
| 980 |
\& field foo => 1; |
| 981 |
\& |
| 982 |
\& # Check the value of index 4 on the underlying reference, this only |
| 983 |
\& # works on blessed arrays. |
| 984 |
\& item 4 => \*(Aqfoo\*(Aq; |
| 985 |
\& |
| 986 |
\& # Check the meta\-property \*(Aqblessed\*(Aq of the object. |
| 987 |
\& prop blessed => \*(AqMy::Module\*(Aq; |
| 988 |
\& |
| 989 |
\& # Check if the object is an instance of the specified class. |
| 990 |
\& prop isa => \*(AqMy::Base\*(Aq; |
| 991 |
\& |
| 992 |
\& # Ensure only the specified hash keys or array indexes are present in |
| 993 |
\& # the underlying hash. Has no effect on meta\-property checks or method |
| 994 |
\& # checks. |
| 995 |
\& end(); |
| 996 |
\& }; |
| 997 |
.Ve |
| 998 |
.ie n .IP "$check = object { ... }" 4 |
| 999 |
.el .IP "\f(CW$check\fR = object { ... }" 4 |
| 1000 |
.IX Item "$check = object { ... }" |
| 1001 |
Specify an object check for use in comparisons. |
| 1002 |
.ie n .IP "call $METHOD_NAME => $RESULT" 4 |
| 1003 |
.el .IP "call \f(CW$METHOD_NAME\fR => \f(CW$RESULT\fR" 4 |
| 1004 |
.IX Item "call $METHOD_NAME => $RESULT" |
| 1005 |
.PD 0 |
| 1006 |
.ie n .IP "call $METHOD_NAME => $CHECK" 4 |
| 1007 |
.el .IP "call \f(CW$METHOD_NAME\fR => \f(CW$CHECK\fR" 4 |
| 1008 |
.IX Item "call $METHOD_NAME => $CHECK" |
| 1009 |
.ie n .IP "call [$METHOD_NAME, @METHOD_ARGS] => $RESULT" 4 |
| 1010 |
.el .IP "call [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$RESULT\fR" 4 |
| 1011 |
.IX Item "call [$METHOD_NAME, @METHOD_ARGS] => $RESULT" |
| 1012 |
.ie n .IP "call [$METHOD_NAME, @METHOD_ARGS] => $CHECK" 4 |
| 1013 |
.el .IP "call [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$CHECK\fR" 4 |
| 1014 |
.IX Item "call [$METHOD_NAME, @METHOD_ARGS] => $CHECK" |
| 1015 |
.ie n .IP "call sub { ... }, $RESULT" 4 |
| 1016 |
.el .IP "call sub { ... }, \f(CW$RESULT\fR" 4 |
| 1017 |
.IX Item "call sub { ... }, $RESULT" |
| 1018 |
.ie n .IP "call sub { ... }, $CHECK" 4 |
| 1019 |
.el .IP "call sub { ... }, \f(CW$CHECK\fR" 4 |
| 1020 |
.IX Item "call sub { ... }, $CHECK" |
| 1021 |
.PD |
| 1022 |
Call the specified method (or coderef) and verify the result. If you |
| 1023 |
pass an arrayref, the first element must be the method name, the |
| 1024 |
others are the arguments it will be called with. |
| 1025 |
.Sp |
| 1026 |
The coderef form is useful if you need to do something more complex. |
| 1027 |
.Sp |
| 1028 |
.Vb 4 |
| 1029 |
\& my $ref = sub { |
| 1030 |
\& local $SOME::GLOBAL::THING = 3; |
| 1031 |
\& return [shift\->get_values_for(\*(Aqthing\*(Aq)]; |
| 1032 |
\& }; |
| 1033 |
\& |
| 1034 |
\& call $ref => ...; |
| 1035 |
.Ve |
| 1036 |
.ie n .IP "call_list $METHOD_NAME => $RESULT" 4 |
| 1037 |
.el .IP "call_list \f(CW$METHOD_NAME\fR => \f(CW$RESULT\fR" 4 |
| 1038 |
.IX Item "call_list $METHOD_NAME => $RESULT" |
| 1039 |
.PD 0 |
| 1040 |
.ie n .IP "call_list $METHOD_NAME => $CHECK" 4 |
| 1041 |
.el .IP "call_list \f(CW$METHOD_NAME\fR => \f(CW$CHECK\fR" 4 |
| 1042 |
.IX Item "call_list $METHOD_NAME => $CHECK" |
| 1043 |
.ie n .IP "call_list [$METHOD_NAME, @METHOD_ARGS] => $RESULT" 4 |
| 1044 |
.el .IP "call_list [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$RESULT\fR" 4 |
| 1045 |
.IX Item "call_list [$METHOD_NAME, @METHOD_ARGS] => $RESULT" |
| 1046 |
.ie n .IP "call_list [$METHOD_NAME, @METHOD_ARGS] => $CHECK" 4 |
| 1047 |
.el .IP "call_list [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$CHECK\fR" 4 |
| 1048 |
.IX Item "call_list [$METHOD_NAME, @METHOD_ARGS] => $CHECK" |
| 1049 |
.ie n .IP "call_list sub { ... }, $RESULT" 4 |
| 1050 |
.el .IP "call_list sub { ... }, \f(CW$RESULT\fR" 4 |
| 1051 |
.IX Item "call_list sub { ... }, $RESULT" |
| 1052 |
.ie n .IP "call_list sub { ... }, $CHECK" 4 |
| 1053 |
.el .IP "call_list sub { ... }, \f(CW$CHECK\fR" 4 |
| 1054 |
.IX Item "call_list sub { ... }, $CHECK" |
| 1055 |
.PD |
| 1056 |
Same as \f(CW\*(C`call\*(C'\fR, but the method is invoked in list context, and the |
| 1057 |
result is always an arrayref. |
| 1058 |
.Sp |
| 1059 |
.Vb 1 |
| 1060 |
\& call_list get_items => [ ... ]; |
| 1061 |
.Ve |
| 1062 |
.ie n .IP "call_hash $METHOD_NAME => $RESULT" 4 |
| 1063 |
.el .IP "call_hash \f(CW$METHOD_NAME\fR => \f(CW$RESULT\fR" 4 |
| 1064 |
.IX Item "call_hash $METHOD_NAME => $RESULT" |
| 1065 |
.PD 0 |
| 1066 |
.ie n .IP "call_hash $METHOD_NAME => $CHECK" 4 |
| 1067 |
.el .IP "call_hash \f(CW$METHOD_NAME\fR => \f(CW$CHECK\fR" 4 |
| 1068 |
.IX Item "call_hash $METHOD_NAME => $CHECK" |
| 1069 |
.ie n .IP "call_hash [$METHOD_NAME, @METHOD_ARGS] => $RESULT" 4 |
| 1070 |
.el .IP "call_hash [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$RESULT\fR" 4 |
| 1071 |
.IX Item "call_hash [$METHOD_NAME, @METHOD_ARGS] => $RESULT" |
| 1072 |
.ie n .IP "call_hash [$METHOD_NAME, @METHOD_ARGS] => $CHECK" 4 |
| 1073 |
.el .IP "call_hash [$METHOD_NAME, \f(CW@METHOD_ARGS\fR] => \f(CW$CHECK\fR" 4 |
| 1074 |
.IX Item "call_hash [$METHOD_NAME, @METHOD_ARGS] => $CHECK" |
| 1075 |
.ie n .IP "call_hash sub { ... }, $RESULT" 4 |
| 1076 |
.el .IP "call_hash sub { ... }, \f(CW$RESULT\fR" 4 |
| 1077 |
.IX Item "call_hash sub { ... }, $RESULT" |
| 1078 |
.ie n .IP "call_hash sub { ... }, $CHECK" 4 |
| 1079 |
.el .IP "call_hash sub { ... }, \f(CW$CHECK\fR" 4 |
| 1080 |
.IX Item "call_hash sub { ... }, $CHECK" |
| 1081 |
.PD |
| 1082 |
Same as \f(CW\*(C`call\*(C'\fR, but the method is invoked in list context, and the |
| 1083 |
result is always a hashref. This will warn if the method returns an |
| 1084 |
odd number of values. |
| 1085 |
.Sp |
| 1086 |
.Vb 1 |
| 1087 |
\& call_hash get_items => { ... }; |
| 1088 |
.Ve |
| 1089 |
.ie n .IP "field $NAME => $VAL" 4 |
| 1090 |
.el .IP "field \f(CW$NAME\fR => \f(CW$VAL\fR" 4 |
| 1091 |
.IX Item "field $NAME => $VAL" |
| 1092 |
Works just like it does for hash checks. |
| 1093 |
.ie n .IP "item $VAL" 4 |
| 1094 |
.el .IP "item \f(CW$VAL\fR" 4 |
| 1095 |
.IX Item "item $VAL" |
| 1096 |
.PD 0 |
| 1097 |
.ie n .IP "item $IDX, $VAL" 4 |
| 1098 |
.el .IP "item \f(CW$IDX\fR, \f(CW$VAL\fR" 4 |
| 1099 |
.IX Item "item $IDX, $VAL" |
| 1100 |
.PD |
| 1101 |
Works just like it does for array checks. |
| 1102 |
.ie n .IP "prop $NAME => $VAL" 4 |
| 1103 |
.el .IP "prop \f(CW$NAME\fR => \f(CW$VAL\fR" 4 |
| 1104 |
.IX Item "prop $NAME => $VAL" |
| 1105 |
.PD 0 |
| 1106 |
.ie n .IP "prop $NAME => $CHECK" 4 |
| 1107 |
.el .IP "prop \f(CW$NAME\fR => \f(CW$CHECK\fR" 4 |
| 1108 |
.IX Item "prop $NAME => $CHECK" |
| 1109 |
.PD |
| 1110 |
Check the property specified by \f(CW$name\fR against the value or check. |
| 1111 |
.Sp |
| 1112 |
Valid properties are: |
| 1113 |
.RS 4 |
| 1114 |
.IP 'blessed' 4 |
| 1115 |
.IX Item "'blessed'" |
| 1116 |
What package (if any) the thing is blessed as. |
| 1117 |
.IP 'reftype' 4 |
| 1118 |
.IX Item "'reftype'" |
| 1119 |
Reference type (if any) the thing is. |
| 1120 |
.IP 'isa' 4 |
| 1121 |
.IX Item "'isa'" |
| 1122 |
What class the thing is an instance of. |
| 1123 |
.IP 'this' 4 |
| 1124 |
.IX Item "'this'" |
| 1125 |
The thing itself. |
| 1126 |
.IP 'size' 4 |
| 1127 |
.IX Item "'size'" |
| 1128 |
For array references this returns the number of elements. For hashes this |
| 1129 |
returns the number of keys. For everything else this returns undef. |
| 1130 |
.RE |
| 1131 |
.RS 4 |
| 1132 |
.RE |
| 1133 |
.IP \fBDNE()\fR 4 |
| 1134 |
.IX Item "DNE()" |
| 1135 |
Can be used with \f(CW\*(C`item\*(C'\fR, or \f(CW\*(C`field\*(C'\fR to ensure the hash field or array index |
| 1136 |
does not exist. Can also be used with \f(CW\*(C`call\*(C'\fR to ensure a method does not |
| 1137 |
exist. |
| 1138 |
.IP \fBend()\fR 4 |
| 1139 |
.IX Item "end()" |
| 1140 |
Turn on strict array/hash checking, ensuring that no extra keys/indexes |
| 1141 |
are present. |
| 1142 |
.IP \fBetc()\fR 4 |
| 1143 |
.IX Item "etc()" |
| 1144 |
Ignore any extra items found in the hash/array. This is the opposite of |
| 1145 |
\&\f(CWend()\fR. This can be used anywhere in the builder, though typically it is |
| 1146 |
placed at the end. |
| 1147 |
.SS "EVENT BUILDERS" |
| 1148 |
.IX Subsection "EVENT BUILDERS" |
| 1149 |
\&\fBNote: None of these are exported by default. You need to request them.\fR |
| 1150 |
.PP |
| 1151 |
Check that we got an event of a specified type: |
| 1152 |
.PP |
| 1153 |
.Vb 1 |
| 1154 |
\& my $check = event \*(AqOk\*(Aq; |
| 1155 |
.Ve |
| 1156 |
.PP |
| 1157 |
Check for details about the event: |
| 1158 |
.PP |
| 1159 |
.Vb 3 |
| 1160 |
\& my $check = event Ok => sub { |
| 1161 |
\& # Check for a failure |
| 1162 |
\& call pass => 0; |
| 1163 |
\& |
| 1164 |
\& # Effective pass after TODO/SKIP are accounted for. |
| 1165 |
\& call effective_pass => 1; |
| 1166 |
\& |
| 1167 |
\& # Check the diagnostics |
| 1168 |
\& call diag => [ match qr/Failed test foo/ ]; |
| 1169 |
\& |
| 1170 |
\& # Check the file the event reports to |
| 1171 |
\& prop file => \*(Aqfoo.t\*(Aq; |
| 1172 |
\& |
| 1173 |
\& # Check the line number the event reports to |
| 1174 |
\& prop line => \*(Aq42\*(Aq; |
| 1175 |
\& |
| 1176 |
\& # You can check the todo/skip values as well: |
| 1177 |
\& prop skip => \*(Aqbroken\*(Aq; |
| 1178 |
\& prop todo => \*(Aqfixme\*(Aq; |
| 1179 |
\& |
| 1180 |
\& # Thread\-id and process\-id where event was generated |
| 1181 |
\& prop tid => 123; |
| 1182 |
\& prop pid => 123; |
| 1183 |
\& }; |
| 1184 |
.Ve |
| 1185 |
.PP |
| 1186 |
You can also provide a fully qualified event package with the '+' prefix: |
| 1187 |
.PP |
| 1188 |
.Vb 1 |
| 1189 |
\& my $check = event \*(Aq+My::Event\*(Aq => sub { ... } |
| 1190 |
.Ve |
| 1191 |
.PP |
| 1192 |
You can also provide a hashref instead of a sub to directly check hash values |
| 1193 |
of the event: |
| 1194 |
.PP |
| 1195 |
.Vb 1 |
| 1196 |
\& my $check = event Ok => { pass => 1, ... }; |
| 1197 |
.Ve |
| 1198 |
.PP |
| 1199 |
\fIUSE IN OTHER BUILDERS\fR |
| 1200 |
.IX Subsection "USE IN OTHER BUILDERS" |
| 1201 |
.PP |
| 1202 |
You can use these all in other builders, simply use them in void context to |
| 1203 |
have their value(s) appended to the build. |
| 1204 |
.PP |
| 1205 |
.Vb 3 |
| 1206 |
\& my $check = array { |
| 1207 |
\& event Ok => { ... }; |
| 1208 |
\& event Note => { ... }; |
| 1209 |
\& |
| 1210 |
\& fail_events Ok => { pass => 0 }; |
| 1211 |
\& # Get a Diag for free. |
| 1212 |
\& }; |
| 1213 |
.Ve |
| 1214 |
.PP |
| 1215 |
\fISPECIFICS\fR |
| 1216 |
.IX Subsection "SPECIFICS" |
| 1217 |
.ie n .IP "$check = event $TYPE;" 4 |
| 1218 |
.el .IP "\f(CW$check\fR = event \f(CW$TYPE\fR;" 4 |
| 1219 |
.IX Item "$check = event $TYPE;" |
| 1220 |
.PD 0 |
| 1221 |
.ie n .IP "$check = event $TYPE => sub { ... };" 4 |
| 1222 |
.el .IP "\f(CW$check\fR = event \f(CW$TYPE\fR => sub { ... };" 4 |
| 1223 |
.IX Item "$check = event $TYPE => sub { ... };" |
| 1224 |
.ie n .IP "$check = event $TYPE => { ... };" 4 |
| 1225 |
.el .IP "\f(CW$check\fR = event \f(CW$TYPE\fR => { ... };" 4 |
| 1226 |
.IX Item "$check = event $TYPE => { ... };" |
| 1227 |
.PD |
| 1228 |
This works just like an object builder. In addition to supporting everything |
| 1229 |
the object check supports, you also have to specify the event type, and many |
| 1230 |
extra meta-properties are available. |
| 1231 |
.Sp |
| 1232 |
Extra properties are: |
| 1233 |
.RS 4 |
| 1234 |
.IP 'file' 4 |
| 1235 |
.IX Item "'file'" |
| 1236 |
File name to which the event reports (for use in diagnostics). |
| 1237 |
.IP 'line' 4 |
| 1238 |
.IX Item "'line'" |
| 1239 |
Line number to which the event reports (for use in diagnostics). |
| 1240 |
.IP 'package' 4 |
| 1241 |
.IX Item "'package'" |
| 1242 |
Package to which the event reports (for use in diagnostics). |
| 1243 |
.IP 'subname' 4 |
| 1244 |
.IX Item "'subname'" |
| 1245 |
Sub that was called to generate the event (example: \f(CWok()\fR). |
| 1246 |
.IP 'skip' 4 |
| 1247 |
.IX Item "'skip'" |
| 1248 |
Set to the skip value if the result was generated by skipping tests. |
| 1249 |
.IP 'todo' 4 |
| 1250 |
.IX Item "'todo'" |
| 1251 |
Set to the todo value if TODO was set when the event was generated. |
| 1252 |
.IP 'trace' 4 |
| 1253 |
.IX Item "'trace'" |
| 1254 |
The \f(CW\*(C`at file foo.t line 42\*(C'\fR string that will be used in diagnostics. |
| 1255 |
.IP 'tid' 4 |
| 1256 |
.IX Item "'tid'" |
| 1257 |
Thread ID in which the event was generated. |
| 1258 |
.IP 'pid' 4 |
| 1259 |
.IX Item "'pid'" |
| 1260 |
Process ID in which the event was generated. |
| 1261 |
.RE |
| 1262 |
.RS 4 |
| 1263 |
.Sp |
| 1264 |
\&\fBNOTE\fR: Event checks have an implicit \f(CWetc()\fR added. This means you need to |
| 1265 |
use \f(CWend()\fR if you want to fail on unexpected hash keys or array indexes. This |
| 1266 |
implicit \f(CWetc()\fR extends to all forms, including builder, hashref, and no |
| 1267 |
argument. |
| 1268 |
.RE |
| 1269 |
.ie n .IP "@checks = fail_events $TYPE;" 4 |
| 1270 |
.el .IP "\f(CW@checks\fR = fail_events \f(CW$TYPE\fR;" 4 |
| 1271 |
.IX Item "@checks = fail_events $TYPE;" |
| 1272 |
.PD 0 |
| 1273 |
.ie n .IP "@checks = fail_events $TYPE => sub { ... };" 4 |
| 1274 |
.el .IP "\f(CW@checks\fR = fail_events \f(CW$TYPE\fR => sub { ... };" 4 |
| 1275 |
.IX Item "@checks = fail_events $TYPE => sub { ... };" |
| 1276 |
.ie n .IP "@checks = fail_events $TYPE => { ... };" 4 |
| 1277 |
.el .IP "\f(CW@checks\fR = fail_events \f(CW$TYPE\fR => { ... };" 4 |
| 1278 |
.IX Item "@checks = fail_events $TYPE => { ... };" |
| 1279 |
.PD |
| 1280 |
Just like \f(CWevent()\fR documented above. The difference is that this produces two |
| 1281 |
events, the one you specify, and a \f(CW\*(C`Diag\*(C'\fR after it. There are no extra checks |
| 1282 |
in the Diag. |
| 1283 |
.Sp |
| 1284 |
Use this to validate a simple failure where you do not want to be bothered with |
| 1285 |
the default diagnostics. It only adds a single Diag check, so if your failure |
| 1286 |
has custom diagnostics you will need to add checks for them. |
| 1287 |
.SH SOURCE |
| 1288 |
.IX Header "SOURCE" |
| 1289 |
The source code repository for Test2\-Suite can be found at |
| 1290 |
\&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR. |
| 1291 |
.SH MAINTAINERS |
| 1292 |
.IX Header "MAINTAINERS" |
| 1293 |
.IP "Chad Granum <[email protected]>" 4 |
| 1294 |
.IX Item "Chad Granum <[email protected]>" |
| 1295 |
.SH AUTHORS |
| 1296 |
.IX Header "AUTHORS" |
| 1297 |
.PD 0 |
| 1298 |
.IP "Chad Granum <[email protected]>" 4 |
| 1299 |
.IX Item "Chad Granum <[email protected]>" |
| 1300 |
.PD |
| 1301 |
.SH COPYRIGHT |
| 1302 |
.IX Header "COPYRIGHT" |
| 1303 |
Copyright 2018 Chad Granum <[email protected]>. |
| 1304 |
.PP |
| 1305 |
This program is free software; you can redistribute it and/or |
| 1306 |
modify it under the same terms as Perl itself. |
| 1307 |
.PP |
| 1308 |
See \fIhttp://dev.perl.org/licenses/\fR |