Test2::...:Custom(3)

πŸ“¦ version 1.302214, in opensuse-leap 16.0

πŸ’– 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 if got exists at all. If exists is false then it means got is not simply undef, 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 the stringify_got flag 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 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