Test::Fatal::matchfor - match exceptions by class name or regexp
Let's say you're testing the following Moose class...
package Goose { use Moose; has feather_count => (is => 'rw', isa => 'Int'); }
In Moose 2.1102 and above, exception objects are thrown, but in earlier versions of Moose, only string errors are given.
So we might want to test it something like this:
use Test::More; use Test::Fatal; use Goose; my $e = exception { Goose->new(feather_count => 3.1) }; ref($e) ? isa_ok($e, 'Moose::Exception::ValidationFailedForTypeConstraint') : like($e, qr{does not pass the type constraint})
This module provides a small shortcut for that pattern:
use Test::More; use Test::Fatal; use Test::Fatal::matchfor; use Goose; my $e = exception { Goose->new(feather_count => 3.1) }; is( $e, matchfor( 'Moose::Exception::ValidationFailedForTypeConstraint', qr{does not pass the type constraint}, ), );
Test::Fatal::matchfor exports the matchfor function which accepts a list of class/role names and regular expressions, and constructs an object overloading == and eq to return true if compared for equality against a string that matches one of those regular expressions, or an object that isa/does one of those class/role names.
matchfor
==
eq
So for example, to check a type constraint error in Moose, you might use:
my $tc_err = matchfor( 'Moose::Exception::ValidationFailedForTypeConstraint', 'Moose::Exception::ValidationFailedForInlinedTypeConstraint', qr{does not pass the type constraint}, ), is($exception, $tc_err, "encountered error as expected");
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Test-Fatal-matchfor.
Test::Fatal, Moose::Exception.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Test::Fatal::matchfor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Fatal::matchfor
CPAN shell
perl -MCPAN -e shell install Test::Fatal::matchfor
For more information on module installation, please visit the detailed CPAN module installation guide.