Test2::...:Custom(3)
π NAME π
Okay bestie, this page is Test2::Compare::Custom β a custom field check for comparisons. The header vibes say Test2::...:Custom(3) and the manual is the User Contributed Perl Documentation. So yeah, it's giving "write your own check for a field inside a deep data structure" energy π
.
β¨ DESCRIPTION β¨
Sometimes you wanna do something complicated or unusual when validating a field that's nested inside a deep data structure. You could pull it out of the structure and test it separately, or you can use this to embed the check. This gives you a way to write custom checks for fields in deep comparisons. No cap, that's the whole point π¦.
πΈ SYNOPSIS πΈ
my $cus = Test2::Compare::Custom->new(
name => 'IsRef',
operator => 'ref(...)',
stringify_got => 1,
code => sub {
my %args = @_;
return $args{got} ? 1 : 0;
},
);
# Pass
is(
{ a => 1, ref => {}, b => 2 },
{ a => 1, ref => $cus, b => 2 },
"This will pass"
);
# Fail
is(
{a => 1, ref => 'notref', b => 2},
{a => 1, ref => $cus, b => 2},
"This will fail"
);
π ARGUMENTS π
Your custom sub will be passed 4 arguments in a hash:
code => sub {
my %args = @_;
# provides got, exists, operator, name
return ref($args{got}) ? 1 : 0;
},
$_ is also localized to got to make it easier for those who need to use regexes. Iconic.
got,$_β the value to be checked.existsβ this will be a boolean. This will be true ifgotexists at all. Ifexistsis false then it meansgotis not simplyundef, but doesn't exist at all (think checking the value of a hash key that does not exist).operatorβ the operator specified at construction.nameβ the name provided at construction.
π₯ METHODS π₯
$code = $cus->codeβ returns the coderef provided at construction.$name = $cus->nameβ returns the name provided at construction.$op = $cus->operatorβ returns the operator provided at construction.$stringify = $cus->stringify_gotβ returns thestringify_gotflag provided at construction.$bool = $cus->verify(got => $got, exists => $bool)β yup, that's the verify signature, exactly as written. π
π SOURCE π
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go off, repository π.
π MAINTAINERS π
- Chad Granum
<exodist@cpan.org>
π«Ά AUTHORS π«Ά
- Chad Granum
<exodist@cpan.org> - Daniel BΓΆhmer
<dboehmer@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 page, bestie. Sign-off time: perl v5.42.0, dated 2025-07-28, on Test2::...:Custom(3). 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