Test2::Compare::Custom - Custom field check for comparisons.
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.
my $cus = Test2::Compare::Custom->new( name => 'IsRef', operator => 'ref(...)', code => sub { my ($got, $exists, $operator, $name) = @_; return ref($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" );
Your custom sub will be passed 4 arguments:
code => sub { my ($got, $exists, $operator, $name) = @_; return ref($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.
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).
$exists
The operator specified at construction.
The name provided at construction.
Returns the coderef provided at construction.
Returns the name provided at construction.
Returns the operator provided at construction.
The source code repository for Test2-Suite can be found at http://github.com/Test-More/Test2-Suite/.
Copyright 2016 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/
To install Test2::Suite, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test2::Suite
CPAN shell
perl -MCPAN -e shell install Test2::Suite
For more information on module installation, please visit the detailed CPAN module installation guide.