From: Stevan Little Date: Sun, 20 Sep 2009 05:13:32 +0000 (-0400) Subject: adding an error case for no matching (and tests) X-Git-Tag: 0.92~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1d39d7095202b55711cb2a2393462eb7057ce0e4;p=gitmo%2FMoose.git adding an error case for no matching (and tests) --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index ac9b560..b5ff3f7 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -467,9 +467,11 @@ sub match_on_type { return $action->($to_match); } } + (defined $default) + || __PACKAGE__->_throw_error("No cases matched for $to_match"); { local $_ = $to_match; - return $default->($to_match) if $default; + return $default->($to_match); } } diff --git a/t/040_type_constraints/036_match_type_operator.t b/t/040_type_constraints/036_match_type_operator.t index 6656651..3728ea8 100644 --- a/t/040_type_constraints/036_match_type_operator.t +++ b/t/040_type_constraints/036_match_type_operator.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 22; +use Test::More tests => 23; use Test::Exception; use Moose::Util::TypeConstraints; @@ -174,6 +174,18 @@ is( '... got the right pretty printed values' ); +# some error cases + +sub not_enough_matches { + my $x = shift; + match_on_type $x => + Undef => sub { 'hello undef world' }, + CodeRef => sub { $_->('Hello code ref world') }; +} + +throws_ok { + not_enough_matches( [] ) +} qr/No cases matched for /, '... not enough matches';