Test2::...:Compare(3)
๐ธ NAME ๐ธ
Test2::Tools::Compare โ bestie, this is the toolkit for comparing deep data structures. (Straight out of the User Contributed Perl Documentation, no cap.)
๐ DESCRIPTION ๐
So back in the day, Test::More had is_deeply(). Cute. But this is the Test2 era, and this library is the glow-up: yes it compares data structures, but it goes a whole step further and gives you tools to build a specification for a data structure, and then you verify your actual data against that spec. Chef's kiss. ๐
And the best part? There are both 'strict' and 'relaxed' versions of the tools, so you can match exactly when you need to and vibe loosely when you don't. Slay.
โจ SYNOPSIS โจ
use Test2::Tools::Compare;
# Hash for demonstration purposes
my $some_hash = {a => 1, b => 2, c => 3};
# Strict checking, everything must match
is(
$some_hash,
{a => 1, b => 2, c => 3},
"The hash we got matches our expectations"
);
# Relaxed Checking, only fields we care about are checked, and we can use a
# regex to approximate a field.
like(
$some_hash,
{a => 1, b => qr/\A[0-9]+\z/},
"'a' is 1, 'b' is an integer, we don't care about 'c'."
);
๐ฆ ADVANCED ๐ฆ
Okay so there are declarative hash, array, and object builders that let you generate specifications. They're more verbose than just handing over a plain hash, but the flex is real: every single component you specify has a line number attached to it. Which is literally a lifesaver for debugging, because when something fails the output tells you not just which field was wrong but the exact line where you declared that field. Iconic behavior. ๐
use Test2::Tools::Compare qw{
is like isnt unlike
match mismatch validator
hash array bag object meta number float rounded within string subset bool
in_set not_in_set check_set
item field call call_list call_hash prop check all_items all_keys all_vals all_values
etc end filter_items
T F D DF E DNE FDNE U L
event fail_events
exact_ref
};
is(
$some_hash,
hash {
field a => 1;
field b => 2;
field c => 3;
},
"Hash matches spec"
);
๐ COMPARISON TOOLS ๐
๐ฅ is โ the strict one
$bool = is($got, $expect)
$bool = is($got, $expect, $name)
$bool = is($got, $expect, $name, @diag)
So $got is the data structure you wanna check, $expect is what you want $got to look like, $name is an optional name for the test, and @diag is optional diagnostic messages that get printed to STDERR if it fails โ they won't show up when the comparison is successful, so nobody's getting spammed. The boolean true/false result of the comparison gets returned. ๐ซถ
This is the strict checker, and bestie, it is strict strict. It requires a perfect match between $got and $expect. All hash fields must be specified, all array items must be present, etc. All non-scalar/hash/array/regex references must be identical (same memory address). Scalar, hash and array references will be traversed and compared. Regex references get compared to see if they have the same pattern.
is(
$some_hash,
{a => 1, b => 2, c => 3},
"The hash we got matches our expectations"
);
The one exception to all that strictness is when it's given an $expect object that was built from a specification โ in that case the specification decides the strictness. Strictness only applies to literal values/references that you provide and that get converted to a specification for you.
is(
$some_hash,
hash { # Note: the hash function is not exported by default
field a => 1;
field b => match(qr/\A[0-9]+\z/); # Note: The match function is not exported by default
# Don't care about other fields.
},
"The hash comparison is not strict"
);
This works for both deep and shallow structures. Like, you can literally use it to compare two strings:
is('foo', 'foo', "strings match");
โ ๏ธ Note: this is not the tool to use if you want to check whether two references are the same exact reference โ useref_is()from theTest2::Tools::Refplugin instead. Most of the timeiswill work as well, but there are problems if your reference contains a cycle and refers back to itself at some point. If that happens, an exception gets thrown to break an otherwise infinite recursion. ๐ญ
โ ๏ธ Note: non-reference values will be compared as strings using"eq", which means strings'2.0'and'2'will not match, but numeric2.0and2will, since they're both stringified to'2'. The way that works. ๐
๐ isnt โ the strict one's opposite
$bool = isnt($got, $expect)
$bool = isnt($got, $expect, $name)
$bool = isnt($got, $expect, $name, @diag)
Opposite of is(). Does all the same checks, but passes when there's a mismatch. Periodt.
๐ฆ like โ the relaxed one
$bool = like($got, $expect)
$bool = like($got, $expect, $name)
$bool = like($got, $expect, $name, @diag)
Same cast of characters: $got is the data structure you wanna check, $expect is what you want $got to look like, $name is the optional test name, @diag is optional diagnostics printed to STDERR on failure (not displayed when the comparison is successful), and the boolean true/false result of the comparison is returned. โจ
This is the relaxed checker, and she is chill. It ignores hash keys or array indexes that you don't actually specify in your $expect structure. On top of that, regex and sub references get used as validators. If you provide a regex using "qr/.../", the regex itself validates the corresponding value in the $got structure. Same deal for coderefs: the value gets passed in as the first argument (and in $_) and the sub should return a boolean value. In this tool, regexes will stringify the thing they're checking.
like(
$some_hash,
{a => 1, b => qr/\A[0-9]+\z/},
"'a' is 1, 'b' is an integer, we don't care about other fields"
);
Works for both deep and shallow structures โ yes, you can compare two strings with it:
like('foo bar', qr/^foo/, "string matches the pattern");
๐ unlike โ the relaxed one's opposite
$bool = unlike($got, $expect)
$bool = unlike($got, $expect, $name)
$bool = unlike($got, $expect, $name, @diag)
Opposite of like(). Does all the same checks, but passes when there's a mismatch. ๐
Also, heads up: the is(), isnt(), like(), and unlike() functions can be made to dump $got using Data::Dumper when tests fail by setting the "T2_AUTO_DUMP" environment variable to "1". (Alternatively, "T2_AUTO_DUMP" can be set to the name of a Perl module providing a compatible Dump() method.) And the "T2_AUTO_DEPARSE" environment variable can be used to enable Data::Dumper's deparsing of coderefs. iykyk ๐ง
โก QUICK CHECKS โก
๐ Note: none of these are exported by default. You need to request them.
Quick checks are a way to quickly generate a common value specification. You can drop them into structures passed into "is" and "like" through the $expect argument.
Example:
is($foo, T(), '$foo has a true value');
โญ $check = T()
Verifies that the value in the corresponding $got structure is true. Any true value will do.
is($foo, T(), '$foo has a true value');
is(
{ a => 'xxx' },
{ a => T() },
"The 'a' key is true"
);
๐ $check = F()
Verifies that the value in the corresponding $got structure is false. Any false value will do, but the value must exist.
is($foo, F(), '$foo has a false value');
is(
{ a => 0 },
{ a => F() },
"The 'a' key is false"
);
It is important to note that a nonexistent value does not count as false. This check will generate a failing test result:
is(
{ a => 1 },
{ a => 1, b => F() },
"The 'b' key is false"
);
Which produces the following output:
not ok 1 - The b key is false
# Failed test "The 'b' key is false"
# at some_file.t line 10.
# +------+------------------+-------+---------+
# | PATH | GOT | OP | CHECK |
# +------+------------------+-------+---------+
# | {b} | <DOES NOT EXIST> | FALSE | FALSE() |
# +------+------------------+-------+---------+
In Perl you can have behavior that's different for a missing key vs. a false key, so the decision was made not to count a completely absent value as false. See the DNE() shortcut below for checking that a field is missing. ๐
If you want to check for false and/or DNE, use the FDNE() check.
๐ $check = D()
Verifies that the value in the $got structure is defined. Any value other than "undef" passes.
This passes:
is('foo', D(), 'foo is defined');
This fails:
is(undef, D(), 'foo is defined');
๐ $check = U()
Verifies that the value in the $got structure is undefined.
This passes:
is(undef, U(), 'not defined');
This fails:
is('foo', U(), 'not defined');
๐ง $check = DF()
Verifies that the value in the $got structure is defined but false. Any false value other than "undef" passes.
This passes:
is(0, DF(), 'foo is defined but false');
These fail:
is(undef, DF(), 'foo is defined but false');
is(1, DF(), 'foo is defined but false');
๐ธ $check = E()
Checks that a value exists. Super useful for checking that an array has more values, or that a key exists in a hash โ even if the value is undef.
These pass:
is(['a', 'b', undef], ['a', 'b', E()], "There is a third item in the array");
is({a => 1, b => 2}, {a => 1, b => E()}, "The 'b' key exists in the hash");
These fail:
is(['a', 'b'], ['a', 'b', E()], "Third item exists");
is({a => 1}, {a => 1, b => E()}, "'b' key exists");
๐ $check = DNE()
Checks that no value exists. Perfect for checking the end bound of an array, or that a key does not exist in a hash.
These pass:
is(['a', 'b'], ['a', 'b', DNE()], "There is no third item in the array");
is({a => 1}, {a => 1, b => DNE()}, "The 'b' key does not exist in the hash");
These fail:
is(['a', 'b', 'c'], ['a', 'b', DNE()], "No third item");
is({a => 1, b => 2}, {a => 1, b => DNE()}, "No 'b' key");
$check = FDNE()โ this is literallyF()andDNE()holding hands ๐ซถ. It passes when the value is false, or when the value doesnโt exist at all. No cap.$check = L()โ checks that the value in the$gotstructure is defined and has length. Anything that isnโtundefor the empty string passes, including references ๐ .
These will pass:
is('foo', L(), 'value is defined and has length');
is([], L(), 'value is defined and has length');
These will fail:
is(undef, L(), 'value is defined and has length');
is('', L(), 'value is defined and has length');
๐ธ VALUE SPECIFICATIONS ๐ธ
Note: None of these are exported by default. You need to request them.
$check = string "..."โ verifies the value matches the given string using theeqoperator.$check = !string "..."โ verifies the value does not match the given string using theneoperator.$check = number ...;โ verifies the value matches the given number using the==operator.$check = !number ...;โ verifies the value does not match the given number using the!=operator.$check = number_lt ...;$check = number_le ...;$check = number_ge ...;$check = number_gt ...;โ verify that the value is less than, less than or equal to, greater than or equal to, or greater than the given number. Comparators, but make it fashion โจ.$check = float ...;โ verifies the value is approximately equal to the given number.
If a 'precision' parameter is specified, both operands will be rounded to 'precision' number of fractional decimal digits and compared with eq. Slay.
is($near_val, float($val, precision => 4), "Near 4 decimal digits");
Otherwise, the check will be made within a range of +/- 'tolerance', with a default 'tolerance' of 1e-08.
is( $near_val, float($val, tolerance => 0.01), "Almost there...");
See also "within" and "rounded".
$check = !float ...;โ verifies the value is not approximately equal to the given number.
If a 'precision' parameter is specified, both operands will be rounded to 'precision' number of fractional decimal digits and compared with eq.
Otherwise, the check will be made within a range of +/- 'tolerance', with a default 'tolerance' of 1e-08.
See also "!within" and "!rounded".
$check = within($num, $tolerance);โ verifies the value approximately matches the given number, within a range of +/-$tolerance. Compared using the==operator.
$tolerance is optional and defaults to 1e-08.
$check = !within($num, $tolerance);โ verifies the value does not approximately match the given number within a range of +/-$tolerance. Compared using the!=operator.
$tolerance is optional and defaults to 1e-08.
$check = rounded($num, $precision);โ verifies the value approximately matches the given number, when both are rounded to$precisionnumber of fractional digits. Compared using theeqoperator.$check = !rounded($num, $precision);โ verifies the value does not approximately match the given number, when both are rounded to$precisionnumber of fractional digits. Compared using theneoperator.$check = bool ...;โ verifies the value has the same boolean value as the given argument (XNOR). Matching vibes only ๐.$check = !bool ...;โ verifies the value has a different boolean value from the given argument (XOR). Opposite energy ๐.$check = check_isa ...;โ verifies the value is an instance of the given class name.$check = !check_isa ...;โ verifies the value is not an instance of the given class name.$check = match qr/.../$check = !mismatch qr/.../โ verifies that the value matches the regex pattern. This form of pattern check will NOT stringify references being checked.
Note: "!mismatch()" is documented for completion, please do not use it.
$check = !match qr/.../$check = mismatch qr/.../โ verifies that the value does not match the regex pattern. This form of pattern check will NOT stringify references being checked.
Note:mismatch()was created before overloading of"!"formatch()was a thing.
$check = validator(sub{ ... })$check = validator($NAME => sub{ ... })$check = validator($OP, $NAME, sub{ ... })โ the coderef is the only required argument. The coderef should check that the value is what you expect and return a boolean true or false. Optionally, you can specify a name and operator that are used in diagnostics. They are also provided to the sub itself as named parameters.
Check the value using this sub. The sub gets the value in $_, and it receives the value and several other items as named parameters.
my $check = validator(sub {
my %params = @_;
# These both work:
my $got = $_;
my $got = $params{got};
# Check if a value exists at all
my $exists = $params{exists}
# What $OP (if any) did we specify when creating the validator
my $operator = $params{operator};
# What name (if any) did we specify when creating the validator
my $name = $params{name};
...
return $bool;
}
$check = exact_ref($ref)โ checks that the value is exactly the same reference as the one provided. Same exact reference, no impostor energy ๐.
๐ SET BUILDERS ๐
Note: None of these are exported by default. You need to request them.
my $check = check_set($check1, $check2, ...)โ checks that the value matches ALL of the specified checks.my $check = in_set($check1, $check2, ...)โ checks that the value matches ONE OR MORE of the specified checks.not_in_set($check1, $check2, ...)โ checks that the value DOES NOT match ANY of the specified checks.check $thingโ checks that the value matches the specified thing.
๐ฆ HASH BUILDER ๐ฆ
Note: None of these are exported by default. You need to request them.
$check = hash {
field foo => 1;
field bar => 2;
# Ensure the 'baz' keys does not even exist in the hash.
field baz => DNE();
# Ensure the key exists, but is set to undef
field bat => undef;
# Any check can be used
field boo => $check;
# Set checks that apply to all keys or values. Can be done multiple
# times, and each call can define multiple checks, all will be run.
all_vals match qr/a/, match qr/b/; # All values must have an 'a' and a 'b'
all_keys match qr/x/; # All keys must have an 'x'
...
end(); # optional, enforces that no other keys are present.
};
$check = hash { ... }โ this is used to define a hash check. Hash builder era ๐ .field $NAME => $VALfield $NAME => $CHECKโ specify a field check. This will check the hash key specified by$NAMEand ensure it matches the value in$VAL. You can put any valid check in$VAL, such as the result of another call to"array { ... }",DNE(), etc.
Note: This function can only be used inside a hash builder sub, and must be called in void context.
all_keys($CHECK1, $CHECK2, ...)โ add checks that apply to all keys. You can put this anywhere in the hash block, and can call it any number of times with any number of arguments.all_vals($CHECK1, $CHECK2, ...)all_values($CHECK1, $CHECK2, ...)โ add checks that apply to all values. You can put this anywhere in the hash block, and can call it any number of times with any number of arguments.end()โ enforce that no keys are found in the hash other than those specified. This is essentially the"use strict"of a hash check. This can be used anywhere in the hash builder, though typically it is placed at the end.etc()โ ignore any extra keys found in the hash. This is the opposite ofend(). This can be used anywhere in the hash builder, though typically it is placed at the end.DNE()โ this is a handy check that can be used withfield()to ensure that a field (D)oes (N)ot (E)xist. Iconic.
field foo => DNE();
๐ ARRAY BUILDER ๐
Note: None of these are exported by default. You need to request them.
$check = array {
# Uses the next index, in this case index 0;
item 'a';
# Gets index 1 automatically
item 'b';
# Specify the index
item 2 => 'c';
# We skipped index 3, which means we don't care what it is.
item 4 => 'e';
# Gets index 5.
item 'f';
# Remove any REMAINING items that contain 0-9.
filter_items { grep {!m/[0-9]/} @_ };
# Set checks that apply to all items. Can be done multiple times, and
# each call can define multiple checks, all will be run.
all_items match qr/a/, match qr/b/;
all_items match qr/x/;
# Of the remaining items (after the filter is applied) the next one
# (which is now index 6) should be 'g'.
item 6 => 'g';
item 7 => DNE; # Ensure index 7 does not exist.
end(); # Ensure no other indexes exist.
};
$check = array { ... }item $VALitem $CHECKitem $IDX, $VALitem $IDX, $CHECKโ add an expected item to the array. If$IDXis not specified it will automatically calculate it based on the last item added. You can skip indexes, which means you do not want them to be checked.
You can provide any value to check in $VAL, or you can provide any valid check object.
Note: Items MUST be added in order.
Note: This function can only be used inside an array, bag or subset builder sub, and must be called in void context.
filter_items { my @remaining = @_; ...; return @filtered }โ this function adds a filter. All items remaining in the array from the point the filter is reached will be passed into the filter sub as arguments, and the sub should return only the items that should be checked.
Note: This function can only be used inside an array builder sub, and must be called in void context.
all_items($CHECK1, $CHECK2, ...)โ add checks that apply to all items. You can put this anywhere in the array block, and can call it any number of times with any number of arguments.end()โ enforce that there are no indexes after the last one specified. This will not force checking of skipped indexes.
etc()
Ignore any extra items found in the array. This is the opposite of end(). This can be used anywhere in the array builder, though typically it is placed at the end. No extra vibes allowed unless you say so ๐
DNE()
This is a handy check that can be used with item() to ensure that an index (D)oes (N)ot (E)xist.
item 5 => DNE();
๐ BAG BUILDER ๐
Note: None of these are exported by default. You need to request them. fr fr, ask for them by name ๐ซถ
$check = bag {
item 'a';
item 'b';
end(); # Ensure no other elements exist.
};
A bag is like an array, but we donโt care about the order of the items. In the example, $check would match both ['a','b'] and ['b','a']. order? never heard of her ๐
$check = bag { ... }โ build the bag check.item $VAL/item $CHECKโ Add an expected item to the bag. You can provide any value to check in$VAL, or you can provide any valid check object.
Note: This function can only be used inside an array, bag or subset builder sub, and must be called in void context.
all_items($CHECK1, $CHECK2, ...)โ Add checks that apply to all items. You can put this anywhere in the bag block, and can call it any number of times with any number of arguments.end()โ Enforce that there are no more items after the last one specified.etc()โ Ignore any extra items found in the array. This is the opposite ofend(). This can be used anywhere in the bag builder, though typically it is placed at the end.
๐งบ ORDERED SUBSET BUILDER ๐งบ
Note: None of these are exported by default. You need to request them. go ask, bestie ๐
$check = subset {
item 'a';
item 'b';
item 'c';
# Doesn't matter if the array has 'd', the check will skip past any
# unknown items until it finds the next one in our subset.
item 'e';
item 'f';
};
$check = subset { ... }โ build the ordered subset check.item $VAL/item $CHECKโ Add an expected item to the subset. You can provide any value to check in$VAL, or you can provide any valid check object.
Note: Items MUST be added in order. this is not a drill ๐ญ Note: This function can only be used inside an array, bag or subset builder sub, and must be called in void context.
๐ฎ META BUILDER ๐ฎ
Note: None of these are exported by default. You need to request them. you know the drill ๐
my $check = meta {
prop blessed => 'My::Module'; # Ensure value is blessed as our package
prop reftype => 'HASH'; # Ensure value is a blessed hash
prop isa => 'My::Base'; # Ensure value is an instance of our class
prop size => 4; # Check the number of hash keys
prop this => ...; # Check the item itself
};
meta { ... }meta_check { ... }โ Build a meta check. If you are using Moose then themeta()function would conflict with the one exported by Moose, in such casesmeta_check()is available. Neither is exported by default. Moose drama, classic ๐ซprop $NAME => $VALprop $NAME => $CHECKโ Check the property specified by$nameagainst the value or check.
Valid properties are:
'blessed'โ What package (if any) the thing is blessed as.'reftype'โ Reference type (if any) the thing is.'isa'โ What class the thing is an instance of.'this'โ The thing itself.'size'โ For array references this returns the number of elements. For hashes this returns the number of keys. For everything else this returnsundef.
๐งธ OBJECT BUILDER ๐งธ
Note: None of these are exported by default. You need to request them. request them or they wonโt show up โจ
my $check = object {
call foo => 1; # Call the 'foo' method, check the result.
# Call the specified sub-ref as a method on the object, check the
# result. This is useful for wrapping methods that return multiple
# values.
call sub { [ shift->get_list ] } => [...];
# This can be used to ensure a method does not exist.
call nope => DNE();
# Check the hash key 'foo' of the underlying reference, this only works
# on blessed hashes.
field foo => 1;
# Check the value of index 4 on the underlying reference, this only
# works on blessed arrays.
item 4 => 'foo';
# Check the meta-property 'blessed' of the object.
prop blessed => 'My::Module';
# Check if the object is an instance of the specified class.
prop isa => 'My::Base';
# Ensure only the specified hash keys or array indexes are present in
# the underlying hash. Has no effect on meta-property checks or method
# checks.
end();
};
$check = object { ... }โ Specify an object check for use in comparisons. main character object energy ๐
call calling all methods ๐
call $METHOD_NAME => $RESULTcall $METHOD_NAME => $CHECKcall [$METHOD_NAME, @METHOD_ARGS] => $RESULTcall [$METHOD_NAME, @METHOD_ARGS] => $CHECKcall sub { ... }, $RESULTcall sub { ... }, $CHECK
Call the specified method (or coderef) and verify the result. If you pass an arrayref, the first element must be the method name, the others are the arguments it will be called with.
The coderef form is useful if you need to do something more complex.
my $ref = sub {
local $SOME::GLOBAL::THING = 3;
return [shift->get_values_for('thing')];
};
call $ref => ...;
call_list list context bestie ๐
call_list $METHOD_NAME => $RESULTcall_list $METHOD_NAME => $CHECKcall_list [$METHOD_NAME, @METHOD_ARGS] => $RESULTcall_list [$METHOD_NAME, @METHOD_ARGS] => $CHECKcall_list sub { ... }, $RESULTcall_list sub { ... }, $CHECK
Same as call, but the method is invoked in list context, and the result is always an arrayref.
call_list get_items => [ ... ];
call_hash hash context, slay ๐๏ธ
call_hash $METHOD_NAME => $RESULTcall_hash $METHOD_NAME => $CHECKcall_hash [$METHOD_NAME, @METHOD_ARGS] => $RESULTcall_hash [$METHOD_NAME, @METHOD_ARGS] => $CHECKcall_hash sub { ... }, $RESULTcall_hash sub { ... }, $CHECK
Same as call, but the method is invoked in list context, and the result is always a hashref. This will warn if the method returns an odd number of values. odd number of values? it WILL warn, no cap ๐ญ
call_hash get_items => { ... };
field hash-key checking ๐
field $NAME => $VALโ Works just like it does for hash checks.
item array-index checking ๐งฎ
item $VALitem $IDX, $VALโ Works just like it does for array checks.
prop meta-property checking ๐
prop $NAME => $VALprop $NAME => $CHECKโ Check the property specified by$nameagainst the value or check.
Valid properties are:
'blessed'โ What package (if any) the thing is blessed as.'reftype'โ Reference type (if any) the thing is.'isa'โ What class the thing is an instance of.'this'โ The thing itself.'size'โ For array references this returns the number of elements. For hashes this returns the number of keys. For everything else this returnsundef.
DNE()
Can be used with item, or field to ensure the hash field or array index does not exist. Can also be used with call to ensure a method does not exist. does not exist = iconique ๐
end()
Turn on strict array/hash checking, ensuring that no extra keys/indexes are present. strict mode has entered the chat ๐จ
etc()
Ignore any extra items found in the hash/array. This is the opposite of end(). This can be used anywhere in the builder, though typically it is placed at the end.
๐ช EVENT BUILDERS ๐ช
Note: None of these are exported by default. You need to request them. yes, again, request them ๐ซถ
Check that we got an event of a specified type:
my $check = event 'Ok';
Check for details about the event:
my $check = event Ok => sub {
# Check for a failure
call pass => 0;
# Effective pass after TODO/SKIP are accounted for.
call effective_pass => 1;
# Check the diagnostics
call diag => [ match qr/Failed test foo/ ];
# Check the file the event reports to
prop file => 'foo.t';
# Check the line number the event reports to
prop line => '42';
# You can check the todo/skip values as well:
prop skip => 'broken';
prop todo => 'fixme';
# Thread-id and process-id where event was generated
prop tid => 123;
prop pid => 123;
};
You can also provide a fully qualified event package with the '+' prefix:
my $check = event '+My::Event' => sub { ... }
You can also provide a hashref instead of a sub to directly check hash values of the event:
my $check = event Ok => { pass => 1, ... };
๐งฉ USE IN OTHER BUILDERS ๐งฉ
You can use these all in other builders, simply use them in void context to have their value(s) appended to the build. void context? we love a low-key cameo โจ
my $check = array {
event Ok => { ... };
event Note => { ... };
fail_events Ok => { pass => 0 };
# Get a Diag for free.
};
๐ SPECIFICS ๐
$check = event $TYPE;
$check = event $TYPE => sub { ... };
$check = event $TYPE => { ... };
This works just like an object builder. In addition to supporting everything the object check supports, you also have to specify the event type, and many extra meta-properties are available.
Extra properties are:
'file'โ File name to which the event reports (for use in diagnostics).'line'โ Line number to which the event reports (for use in diagnostics).'package'โ Package to which the event reports (for use in diagnostics).'subname'โ Sub that was called to generate the event (example:ok()).'skip'โ Set to the skip value if the result was generated by skipping tests.'todo'โ Set to the todo value if TODO was set when the event was generated.'trace'โ The"at file foo.t line 42"string that will be used in diagnostics.'tid'โ Thread ID in which the event was generated.'pid'โ Process ID in which the event was generated.
NOTE: Event checks have an implicitetc()added. This means you need to useend()if you want to fail on unexpected hash keys or array indexes. This implicitetc()extends to all forms, including builder, hashref, and no argument. so if you want strict vibes, you gotta say it withend()๐
@checks = fail_events $TYPE;
@checks = fail_events $TYPE => sub { ... };
@checks = fail_events $TYPE => { ... };
okay so this is literally the same vibe as event() documented above ๐ โ same call shapes, same energy. The one iconic plot twist? It produces two events: the one you specify, plus a "Diag" right after it. And no cap, there are no extra checks in that Diag โ itโs just the one diag check, periodt ๐
Use this to validate a simple failure where you truly cannot be bothered with the default diagnostics ๐โจ. It only adds a single Diag check, so if your failure has custom diagnostics, youโll need to add checks for those yourself. iykyk ๐ซถ
๐ฅ SOURCE ๐ฅ
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/ โ go stare at it, bestie ๐โจ
๐ MAINTAINERS ๐
- Chad Granum <exodist@cpan.org>
๐ AUTHORS ๐
- Chad Granum <exodist@cpan.org>
๐ COPYRIGHT ๐
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
Cute little sign-off bestie: this page is from perl v5.42.0, dated 2025-07-28 โ source/version and date, all accounted for, mwah ๐๐
๐ฆ where this page comes from
- opensuse-leap 16.0: perl-Test-Simple 1.302214-bp160.1.2 ยท Artistic-1.0 OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms