TEST2::TOOLS::COMPARE(3pm)
π NAME
Test2::Tools::Compare β tools for comparing deep data structures, bestie. That's the whole vibe π«Ά
πΈ DESCRIPTION
Okay so listen. Back in the day, Test::More had is_deeply(). This library is the Test2 glow-up of that β it can compare data structures, sure, but it goes a step further: it gives you tools for building a data structure specification that you then check your data against. Like, you write down what you expect, and it goes "yep β¨" or "ma'am, no π".
And there are both 'strict' and 'relaxed' versions of the tools, so you can pick your energy.
β¨ 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
So there are these declarative hash, array, and object builders you can use to generate specifications. Ngl, they're more verbose than just yeeting a hash at it β but they have one huge serve: every component you specify gets a line number attached to it ππ. That's amazing for debugging, because when a test fails, the output tells you not only which fields were wrong, but also the exact line where you declared that field. Iconic behavior, honestly.
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()
$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, and $name is an optional name for the test. @diag is optional diagnostic messages that get printed to STDERR if the test fails β they will not show up when the comparison is successful, so don't worry about clutter π
. And the boolean true/false result of the comparison gets returned to you. Slay.
This is the strict checker, and bestie, she is strict. She requires a perfect match between $got and $expect. Every hash field must be specified. Every array item must be present. Etc, etc. Also: all non-scalar/hash/array/regex references must be identical (same memory address, fr fr). Scalar, hash and array references get 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 only exception to the strictness is when you hand her an $expect object that was built from a specification β in that case the specification itself 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 wanna check whether two references are the same exact reference. For that, useref_is()from theTest2::Tools::Refplugin instead. Most of the time this will work as well, BUT there are problems if your reference contains a cycle and refers back to itself at some point β in that case an exception gets thrown to break what would otherwise be infinite recursion. π
Note: non-reference values get compared as strings using"eq", which means the strings'2.0'and'2'will not match, but the numeric2.0and2will, since they both stringify to'2'.
π isnt()
$bool = isnt($got, $expect)
$bool = isnt($got, $expect, $name)
$bool = isnt($got, $expect, $name, @diag)
The opposite of is(), periodt. Does all the same checks, but passes when there's a mismatch. That's it, that's her π
π like()
$bool = like($got, $expect)
$bool = like($got, $expect, $name)
$bool = like($got, $expect, $name, @diag)
$got is the data structure you wanna check, $expect is what you want $got to look like, $name is an optional test name, and @diag is optional diagnostics printed to STDERR on failure β again, they do not show up when the comparison is successful. The boolean true/false result of the comparison is returned. π¦
This is the relaxed checker. She ignores hash keys or array indexes that you don't actually specify in your $expect structure β she's chill like that. 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 β for instance, you can use it to compare two strings:
like('foo bar', qr/^foo/, "string matches the pattern");
π unlike()
$bool = unlike($got, $expect)
$bool = unlike($got, $expect, $name)
$bool = unlike($got, $expect, $name, @diag)
The opposite of like(). Does all the same checks, but passes when there's a mismatch. Matching energy, opposite direction π
Here's a little quality-of-life note for the whole family: 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. Obsessed with a helpful env var, honestly πβ¨
πΈ QUICK CHECKS πΈ
Note: none of these are exported by default. You gotta request them. Ask and you shall receive, bestie π«Ά
Quick checks are a way to quickly generate a common value specification. These can be used in structures passed into "is" and "like" through the $expect argument.
Example:
is($foo, T(), '$foo has a true value');
β T()
$check = T()
This verifies that the value in the corresponding $got structure is true. Any true value will do, no cap.
is($foo, T(), '$foo has a true value');
is(
{ a => 'xxx' },
{ a => T() },
"The 'a' key is true"
);
β F()
$check = F()
This 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's important to note that a nonexistent value does not count as false. Like, this check will generate a failing test result:
is(
{ a => 1 },
{ a => 1, b => F() },
"The 'b' key is false"
);
Which will produce 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 versus a false key, so it was decided not to count a completely absent value as false. See the DNE() shortcut below for checking that a field is missing. And if you wanna check for false and/or DNE, use the FDNE() check. Easy as that π
β D()
$check = D()
This one verifies that the value in the $got structure is defined. Any value other than "undef" will pass.
This will pass:
is('foo', D(), 'foo is defined');
This will fail:
is(undef, D(), 'foo is defined');
β U()
$check = U()
This one verifies that the value in the $got structure is undefined. The mirror image, bestie πͺ
This will pass:
is(undef, U(), 'not defined');
This will fail:
is('foo', U(), 'not defined');
β DF()
$check = DF()
This verifies that the value in the $got structure is defined but false. Any false value other than "undef" will pass.
This will pass:
is(0, DF(), 'foo is defined but false');
These will fail:
is(undef, DF(), 'foo is defined but false');
is(1, DF(), 'foo is defined but false');
β E()
$check = E()
This can be used to check 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 undefined.
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 will fail:
is(['a', 'b'], ['a', 'b', E()], "Third item exists");
is({a => 1}, {a => 1, b => E()}, "'b' key exists");
β DNE()
$check = DNE()
This one checks that no value exists. The moment for checking the end bound of an array, or checking that a key does not exist in a hash. "Does Not Exist" energy, and she commits to it π
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 will 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()β literally F() and DNE() smashed together π«Ά. Passes if the value is false or doesn't exist at all. Two-for-one deal, no cap.$check = L()β vibe check for "is this thing defined and does it have length?" π Anything that isn'tundefand isn't the empty string sails through (references included, they're built different).
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 β¨
Heads up bestie π None of these are exported by default. You need to request them.
$check = string "..."β makes sure the value matches that string using theeqoperator. Classic.$check = !string "..."β opposite energy π: makes sure the value does not match that string, using theneoperator.$check = number ...;β makes sure the value matches the given number using the==operator.$check = !number ...;β makes sure the value does not match the given number, using the!=operator.$check = number_lt ...;$check = number_le ...;$check = number_ge ...;$check = number_gt ...;β these four icons π check that the value is less than, less than or equal to, greater than or equal to, or greater than the given number. Pick your fighter.$check = float ...;β makes sure the value is approximately equal to the given number π§.
If you pass a 'precision' parameter, both operands get rounded to 'precision' number of fractional decimal digits and then compared with eq.
is($near_val, float($val, precision => 4), "Near 4 decimal digits");
Otherwise the check happens within a range of +/- 'tolerance', and 'tolerance' defaults to 1e-08.
is( $near_val, float($val, tolerance => 0.01), "Almost there...");
See also "within" and "rounded" π
$check = !float ...;β makes sure the value is not approximately equal to the given number.
If you pass a 'precision' parameter, both operands get rounded to 'precision' number of fractional decimal digits and compared with eq.
Otherwise the check happens within a range of +/- 'tolerance', with a default 'tolerance' of 1e-08.
See also "!within" and "!rounded".
$check = within($num, $tolerance);β makes sure the value approximately matches the given number, within a range of +/-$tolerance. Comparison uses the==operator.
$tolerance is optional and defaults to 1e-08.
$check = !within($num, $tolerance);β makes sure the value does not approximately match the given number within a range of +/-$tolerance. Comparison uses the!=operator.
$tolerance is optional and defaults to 1e-08.
$check = rounded($num, $precision);β makes sure the value approximately matches the given number once both are rounded to$precisionnumber of fractional digits π. Comparison uses theeqoperator.$check = !rounded($num, $precision);β makes sure the value does not approximately match the given number once both are rounded to$precisionnumber of fractional digits. Comparison uses theneoperator.$check = bool ...;β makes sure the value has the same boolean value as the given argument (XNOR) β.$check = !bool ...;β makes sure the value has a different boolean value from the given argument (XOR).$check = check_isa ...;β makes sure the value is an instance of the given class name π.$check = !check_isa ...;β makes sure the value is not an instance of the given class name.$check = match qr/.../$check = !mismatch qr/.../β makes sure the value matches the regex pattern π₯. Big note: this form of pattern check will NOT stringify references being checked.
Note: "!mismatch()" is documented for completion, please do not use it. It's giving deprecated, bestie π
$check = !match qr/.../$check = mismatch qr/.../β makes sure 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. Ancient lore πΊ
$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 get used in diagnostics β they're also handed to the sub itself as named parameters.
Check the value using this sub. The sub gets the value in $_, and it receives the value plus 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 you provided. Same object, same address, no clones allowed π .
πΈ 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. All of them. Periodt.my $check = in_set($check1, $check2, ...)β checks that the value matches ONE OR MORE of the specified checks. Pick a lane, any lane β¨.not_in_set($check1, $check2, ...)β checks that the value DOES NOT match ANY of the specified checks. None. Zero. Nada π ββοΈ.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 how you define a hash check. Iconic behavior honestly π.field $NAME => $VALfield $NAME => $CHECKβ specify a field check π. This checks the hash key specified by$NAMEand makes sure it matches the value in$VAL. You can put any valid check in$VAL, like the result of another call to"array { ... }",DNE(), whatever you want.
Note: This function can only be used inside a hash builder sub, and must be called in void context. Rude but true π«
all_keys($CHECK1, $CHECK2, ...)β adds checks that apply to all keys. You can drop this anywhere in the hash block, and call it any number of times with any number of arguments π.all_vals($CHECK1, $CHECK2, ...)all_values($CHECK1, $CHECK2, ...)β adds checks that apply to all values. Same deal: anywhere in the hash block, any number of calls, any number of arguments β¨.end()β enforces that no keys show up in the hash other than the ones you specified. This is basically the"use strict"of a hash check π . Can go anywhere in the hash builder, though bestie, you'll usually want it at the end.etc()β ignores any extra keys found in the hash. It's the literal opposite ofend()π. Can go anywhere in the hash builder, though typically it goes at the end.DNE()β handy little check you can use withfield()to make sure a field (D)oes (N)ot (E)xist π.
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$IDXisn't specified it'll auto-calculate based on the last item you added. You can skip indexes, which literally means you don't want them checked.
You can hand it any value to check in $VAL, or you can hand it any valid check object.
Note: Items MUST be added in order. No skipping around, bestie π
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 adds a filter π§. Everything still remaining in the array from the point the filter is reached gets passed into the filter sub as arguments, and the sub should return only the items that should be checked. Curate your own feed.
Note: This function can only be used inside an array builder sub, and must be called in void context.
all_items($CHECK1, $CHECK2, ...)β adds checks that apply to all items β. Drop it anywhere in the array block, call it any number of times with any number of arguments.end()β enforces that there are no indexes after the last one you specified. Lowkey important: this will not force checking of skipped indexes π.etc()β this one's the chill one π it just ignores any extra items it finds in the array. Literally the opposite ofend(). You can drop it anywhere in the array builder, but bestie, conventionally it goes at the very end. πDNE()β okay this is such a handy little icon β¨ team it up withitem()to make sure an index (D)oes (N)ot (E)xist. Obsessed:
item 5 => DNE();
π BAG BUILDER π
Heads up bestie: none of these are exported by default. You gotta request them, no cap. π
$check = bag {
item 'a';
item 'b';
end(); # Ensure no other elements exist.
};
So a bag is like an array, except we literally do not care about the order of the items. In the example above, $check would match both ['a','b'] and ['b','a']. Order? Never heard of her. π
$check = bag { ... }
item $VAL
item $CHECK
- Add an expected item to the bag. π«Ά
- You can hand it any value to check in
$VAL, or you can hand it any valid check object. - Note: this function can only be used inside an array, bag or subset builder sub, and it must be called in void context. Like, rules are rules. π
all_items($CHECK1, $CHECK2, ...)
- Adds checks that apply to all items. You can put this anywhere in the bag block, and you can call it any number of times with any number of arguments. Go off. π₯
end()
- Enforces that there are no more items after the last one you specified. Strict queen behavior. π
etc()
- Ignores any extra items found in the array. This is the opposite of
end(). Can be used anywhere in the bag builder, though typically it's placed at the end. πΈ
π¦ ORDERED SUBSET BUILDER π¦
Heads up again: none of these are exported by default. Request them, 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 { ... }
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. Not negotiable, sorry not sorry. π
- Note: this function can only be used inside an array, bag or subset builder sub, and must be called in void context.
π§ META BUILDER π§
Same deal: none of these are exported by default. You need to request them. π
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 an instance of our class
prop size => 4; # Check the number of hash keys
prop this => ...; # Check the item itself
};
meta { ... }
meta_check { ... }
- Builds a meta check. β¨ Now listen β if you're using Moose, then the
meta()function would conflict with the one exported by Moose. In those cases,meta_check()is available. Neither one is exported by default. π
prop $NAME => $VAL
prop $NAME => $CHECK
- Checks the property specified by
$nameagainst the value or check.
Valid properties are, and yes I'm listing all of them π«Ά:
'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 π
Yep, you guessed it: none of these are exported by default. Request them. π
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. Boom, that's it. β¨
call $METHOD_NAME => $RESULT
call $METHOD_NAME => $CHECK
call [$METHOD_NAME, @METHOD_ARGS] => $RESULT
call [$METHOD_NAME, @METHOD_ARGS] => $CHECK
call sub { ... }, $RESULT
call 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, and the others are the arguments it'll be called with.
- The coderef form is so useful when you need to do something more complex, iykyk:
my $ref = sub {
local $SOME::GLOBAL::THING = 3;
return [shift->get_values_for('thing')];
};
call $ref => ...;
call_list $METHOD_NAME => $RESULT
call_list $METHOD_NAME => $CHECK
call_list [$METHOD_NAME, @METHOD_ARGS] => $RESULT
call_list [$METHOD_NAME, @METHOD_ARGS] => $CHECK
call_list sub { ... }, $RESULT
call_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 $METHOD_NAME => $RESULT
call_hash $METHOD_NAME => $CHECK
call_hash [$METHOD_NAME, @METHOD_ARGS] => $RESULT
call_hash [$METHOD_NAME, @METHOD_ARGS] => $CHECK
call_hash sub { ... }, $RESULT
call_hash sub { ... }, $CHECK
- Same as
call, but the method is invoked in list context, and the result is always a hashref. π This one will warn if the method returns an odd number of values. Like, awkward. π
call_hash get_items => { ... };
field $NAME => $VAL
- Works just like it does for hash checks. π
item $VAL
item $IDX, $VAL
- Works just like it does for array checks. β¨
prop $NAME => $VAL
prop $NAME => $CHECK
- Checks the property specified by
$nameagainst the value or check.
Valid properties, all of them, no skips π«Ά:
'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
itemorfieldto make sure the hash field or array index does not exist. Can also be used withcallto make sure a method does not exist. Very "you don't exist bestie" of it. π
end()
- Turns on strict array/hash checking, making sure no extra keys/indexes are present. Main character energy. π
etc()
- Ignores any extra items found in the hash/array. This is the opposite of
end(). Can be used anywhere in the builder, though typically it's placed at the end. πΈ
π₯ EVENT BUILDERS π₯
Bestie, same rule: none of these are exported by default. You need to 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 { ... }
And 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 and their value(s) get appended to the build. Slay. β¨
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. π On top of supporting everything the object check supports, you also have to specify the event type, and many extra meta-properties are available. So many. Iconic.
Extra properties are, and I'm listing every single one π€:
'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 β builder, hashref, and no argument. Fr fr, don't forget this one. π
@checks = fail_events $TYPE;
@checks = fail_events $TYPE => sub { ... };
@checks = fail_events $TYPE => { ... };
Okay so this is literally just like event() bestie β the one documented above, no cap. The difference? It produces two events: the one you specify, plus a "Diag" right after it. And there are no extra checks in that Diag. Periodt.
Use this when you wanna validate a simple failure and you truly cannot be bothered with the default diagnostics. It only adds a single Diag check, so if your failure has custom diagnostics, you will need to add checks for them yourself. No shortcuts there, sorry bestie π
π SOURCE π
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go star it or whatever β¨
π 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/
And thatβs the whole vibe check, bestie β this page is from perl v5.42.3, dated 2026-11-03. Catch you in the next manpage ππ¦
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl 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