From: Dave Rolsky Date: Mon, 22 Sep 2008 15:14:38 +0000 (+0000) Subject: Fix handling of anon subtype with both constraint & message X-Git-Tag: 0.59~44 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e3979c3e3cf061a660e1ed3447da812d45962aa2;p=gitmo%2FMoose.git Fix handling of anon subtype with both constraint & message --- diff --git a/Changes b/Changes index 5d030f3..429818f 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,11 @@ Revision history for Perl extension Moose +0.59 + * Moose::Util::TypeConstraints + - Creating a anonymous subtype with both a constraint and a + message failed with a very unhelpful error. Reported by + t0m. (Dave Rolsky) + 0.58 !! This release has an incompatible change regarding !! !! how roles add methods to a class !! diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index d3d6dd7..9a16eac 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -5,6 +5,7 @@ use strict; use warnings; use Carp (); +use List::MoreUtils qw( all ); use Scalar::Util 'blessed'; use Moose::Exporter; @@ -289,12 +290,18 @@ sub subtype ($$;$$$) { # this adds an undef for the name # if this is an anon-subtype: # subtype(Num => where { $_ % 2 == 0 }) # anon 'even' subtype - # but if the last arg is not a code - # ref then it is a subtype alias: + # or + # subtype(Num => where { $_ % 2 == 0 }) message { "$_ must be an even number" } + # + # but if the last arg is not a code ref then it is a subtype + # alias: + # # subtype(MyNumbers => as Num); # now MyNumbers is the same as Num # ... yeah I know it's ugly code # - SL - unshift @_ => undef if scalar @_ <= 2 && ('CODE' eq ref($_[1])); + unshift @_ => undef if scalar @_ == 2 && ( 'CODE' eq ref( $_[-1] ) ); + unshift @_ => undef + if scalar @_ == 3 && all { ref($_) =~ /^(?:CODE|HASH)$/ } @_[ 1, 2 ]; goto &_create_type_constraint; } diff --git a/t/040_type_constraints/001_util_type_constraints.t b/t/040_type_constraints/001_util_type_constraints.t index 2c52f9a..b09ada8 100644 --- a/t/040_type_constraints/001_util_type_constraints.t +++ b/t/040_type_constraints/001_util_type_constraints.t @@ -3,14 +3,13 @@ use strict; use warnings; -use Test::More tests => 44; +use Test::More tests => 52; use Test::Exception; use Scalar::Util (); -BEGIN { - use_ok('Moose::Util::TypeConstraints'); -} +use Moose::Util::TypeConstraints; + type Number => where { Scalar::Util::looks_like_number($_) }; type String @@ -60,6 +59,23 @@ is($negative->check('Foo'), undef, '... this is not a negative number'); ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number'); ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String'); +my $negative2 = subtype Number => where { $_ < 0 } => message {"$_ is not a negative number"}; + +ok(defined $negative2, '... got a value back from negative'); +isa_ok($negative2, 'Moose::Meta::TypeConstraint'); + +ok($negative2->check(-5), '... this is a negative number'); +ok(!defined($negative2->check(5)), '... this is not a negative number'); +is($negative2->check('Foo'), undef, '... this is not a negative number'); + +ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number'); +ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String'); + +ok($negative2->has_message, '... it has a message'); +is($negative2->validate(2), + '2 is not a negative number', + '... validated unsuccessfully (got error)'); + # check some meta-details my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');