Perl Diver 2.31
Main Environment Variables Perl Default Values Perl Config - Summary Perl Config - Full Installed Modules List Directory uptime Docs

Module Documentation
Details and documentation about a specific module, including version and documentation (if available). Note that while links to perldoc.com and search.cpan.org are provided, the module may be part of a larger distribution. If you reach a File Not Found page on either site, please try the parent module.

Test2::Compare::Custom

Name Test2::Compare::Custom
Version 0.000144
Located at /usr/local/share/perl5
File /usr/local/share/perl5/Test2/Compare/Custom.pm
Is Core No
Search CPAN for this module Test2::Compare::Custom
Documentation Test2::Compare::Custom
Module Details Test2::Compare::Custom


NAME

Test2::Compare::Custom - Custom field check for comparisons.


DESCRIPTION

Sometimes you want to do something complicated or unusual when validating a field 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 provides a way for you to write custom checks for fields in deep comparisons.


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.

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$name = $cus->name
Returns the name provided at construction.

$op = $cus->operator$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)


SOURCE

The source code repository for Test2-Suite can be found at https://github.com/Test-More/Test2-Suite/.


MAINTAINERS

Chad Granum


AUTHORS

Chad Granum <exodist@cpan.org>Chad Granum
Daniel Böhmer


COPYRIGHT

Copyright 2018 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/

Perl Diver brought to you by ScriptSolutions.com © 1997- 2024