Test::MostlyLike - Relaxed checking of deep data structures.
my $got = [qw/foo bar baz/]; mostly_like( $got, ['foo', qr/a/], "Deeply nested structure matches (mostly)" );
A tool based on is_deeply from Test::More. This tool produces nearly identical diagnostics. This tool gives you extra control by letting you check only the parts of the structure you care about, ignoring the rest.
is_deeply
Generates a single ok event with diagnostics to help you find any failures.
Got should be the data structure you want to test. $expect should be a data structure representing what you expect to see. Unlike is_deeply any keys in $got that do not exist in $expect will be ignored.
$got
$expect
When an a blessed object is encountered in the $got structure, any fields listed in $expect will be called as methods on the $got object. See the object/direct element access section below for bypassing this.
Any keys or attributes in $got will be ignored unless the also exist in $expect
my $got = { foo => 1, bar => 2 }; my $expect = { foo => 1 }; mostly_like($got, $expect, "Ignores 'bar'");
If you want to check that a value is not set:
my $got = { foo => 1, bar => 2 }; my $expect = { foo => 1, bar => undef }; mostly_like($got, $expect, "Will fail since 'bar' has a value");
my $got = 'foo'; my $expect = 'foo'; mostly_like($got, $expect, "Check a value directly");
Also works for deeply nested structures
mostly_like( [ {stuff => 'foo bar baz'}, ], [ {stuff => 'foo bar baz'}, ], "Check a value directly, nested" );
my $got = 'foo bar baz'; my $expect = qr/bar/; mostly_like($got, $expect, 'Match');
Works nested as well:
mostly_like( [ {stuff => 'foo bar baz'}, ], [ {stuff => qr/bar/}, ], "Check a value directly, nested" );
my $got = [qw/foo bar baz/]; my $exp = [qw/foo bar/]; mostly_like($got, $exp, "Ignores unspecified indexes");
You can also just check specific indexes:
my $got = [qw/foo bar baz/]; my $exp = { ':1' => 'bar' }; mostly_like($got, $exp, "Only checks array index 1");
When doing this the index must always be prefixed with ':'.
my $got = { foo => 1, bar => 2 }; my $exp = { foo => 1 }; mostly_like($got, $exp, "Only checks foo");
sub foo { $_[0]->{foo} } my $got = bless {foo => 1}, __PACKAGE__; my $exp = { foo => 1 }; mostly_like($got, $exp, 'Checks the return of $got->foo()');
Sometimes methods return lists, in such cases you can wrap them in arrayrefs or hashrefs:
sub list { qw/foo bar baz/ } sub dict { foo => 0, bar => 1, baz => 2 } my $got = bless {}, __PACKAGE__; my $exp = { '[list]' => [ qw/foo bar baz/ ], '[dict]' => { foo => 0, bar => 1, baz => 2 }, }; mostly_like($got, $exp, "Wrapped the method calls");
Sometimes you want to ignore the methods and get the hash value directly.
sub foo { die "do not call me" } my $got = bless { foo => 'secret' }, __PACKAGE__; my $exp = { ':foo' => 'secret' }; mostly_like($got, $exp, "Did not call the fatal method");
The source code repository for Test::More can be found at http://github.com/Test-More/test-more/.
The following people have all contributed to the Test-More dist (sorted using VIM's sort function).
There has been a lot of code migration between modules, here are all the original copyrights together:
Copyright 2014 Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
Originally authored by Michael G Schwern <schwern@pobox.com> with much inspiration from Joshua Pritikin's Test module and lots of help from Barrie Slaymaker, Tony Bowden, blackstar.co.uk, chromatic, Fergal Daly and the perl-qa gang.
Idea by Tony Bowden and Paul Johnson, code by Michael G Schwern <schwern@pobox.com>, wardrobe by Calvin Klein.
Copyright 2001-2008 by Michael G Schwern <schwern@pobox.com>.
To the extent possible under law, 唐鳳 has waived all copyright and related or neighboring rights to Test-use-ok.
This work is published from Taiwan.
http://creativecommons.org/publicdomain/zero/1.0
This module is copyright 2005 Fergal Daly <fergal@esatclear.ie>, some parts are based on other people's work.
Under the same license as Perl itself
Copyright Mark Fowler <mark@twoshortplanks.com> 2002, 2004.
To install Test::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Simple
CPAN shell
perl -MCPAN -e shell install Test::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.