NAME
Test::MostlyLike - Relaxed checking of deep data structures.
SYNOPSYS
my $got = [qw/foo bar baz/];
mostly_like(
$got,
['foo', qr/a/],
"Deeply nested structure matches (mostly)"
);
DESCRIPTION
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.
EXPORTS
- $bool = mostly_like($got, $expect, $name)
-
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.
WHAT TO 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
IGNORING THINGS YOU DO NOT CARE ABOUT
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");
EXACT MATCHES
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"
);
REGEX MATCHES
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"
);
ARRAY ELEMENT MATCHES
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 ':'.
HASH ELEMENT MATCHES
my $got = { foo => 1, bar => 2 };
my $exp = { foo => 1 };
mostly_like($got, $exp, "Only checks foo");
OBJECT METHOD MATCHES
UNALTERED
sub foo { $_[0]->{foo} }
my $got = bless {foo => 1}, __PACKAGE__;
my $exp = { foo => 1 };
mostly_like($got, $exp, 'Checks the return of $got->foo()');
WRAPPED
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");
DIRECT ELEMENT ACCESS
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");
SOURCE
The source code repository for Test::More can be found at http://github.com/Test-More/test-more/.
MAINTAINER
AUTHORS
The following people have all contributed to the Test-More dist (sorted using VIM's sort function).
- Chad Granum <exodist@cpan.org>
- Fergal Daly <fergal@esatclear.ie>>
- Mark Fowler <mark@twoshortplanks.com>
- Michael G Schwern <schwern@pobox.com>
- 唐鳳
COPYRIGHT
There has been a lot of code migration between modules, here are all the original copyrights together:
- Test::Stream
- Test::Stream::Tester
- Test::MostlyLike
-
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
- Test::Simple
- Test::More
- Test::Builder
-
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>.
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
- Test::use::ok
-
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.
- Test::Tester
-
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
See http://www.perl.com/perl/misc/Artistic.html
- Test::Builder::Tester
-
Copyright Mark Fowler <mark@twoshortplanks.com> 2002, 2004.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.