Test::Deep::Filter - Perform a filter on a matched element before doing sub-matching
version 0.001001
This module exists so that one can apply some kind of transform of the content of some node, before applying a deep testing structure against it.
This is especially the kind of domain Test::Deep::YAML and Test::Deep::JSON are targeted at, but implemented in a more generic way.
cmp_deeply( $got, { payload => filter(\&decode_json, { x => 1 }) } )
This would perform matching against a Perl data structure called $got, and find its key called payload, and, if existing, filter the key via &decode_json, and then compare the resulting structure with { x => 1 }.
Perl
$got
payload
&decode_json
{ x => 1 }
This would in theory be equivalent to:
cmp_deeply( $got, { payload => json({ x => 1 }) } )
Except may be tailored for what ever local decoding scheme you may require, e.g.:
cmp_deeply( $got, { payload => filter(sub { my ( $got ) = @_; return parse_ini( $got ); }, { x => 1 }) });
filter
my $object = filter( \&filter_function, $expected ); cmp_deeply( $got, filter( \&filter_function, $expected ));
A filter can take any value into &filter_function, and return any value and the Test::Deep infrastructure will handle the rest.
&filter_function
Test::Deep
If the value cannot be parsed, or is of a type you cannot handle, or the internal logic returns an invalid value ( including any failures in internal logic that fail to parse, etc) then you must cause the function to throw an exception with "die", and the nested $expected will be assumed to be false.
$expected
The "die" message will be used as a diagnostic for why the match failed.
Kent Fredric <kentnl@cpan.org>
This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Test::Deep::Filter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Deep::Filter
CPAN shell
perl -MCPAN -e shell install Test::Deep::Filter
For more information on module installation, please visit the detailed CPAN module installation guide.