From: jjn1056 Date: Tue, 2 Aug 2011 13:45:45 +0000 (-0700) Subject: Merge pull request #3 from brianphillips/master X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=42b7727c8174938673ad934ca8232a5da63bbff7;hp=fe86999888c107545264144620f3ccdf1b243f68;p=gitmo%2FMooseX-Dependent.git Merge pull request #3 from brianphillips/master Customized error messages for parameterized types --- diff --git a/lib/MooseX/Meta/TypeConstraint/Parameterizable.pm b/lib/MooseX/Meta/TypeConstraint/Parameterizable.pm index 44f2abc..fbef572 100644 --- a/lib/MooseX/Meta/TypeConstraint/Parameterizable.pm +++ b/lib/MooseX/Meta/TypeConstraint/Parameterizable.pm @@ -192,7 +192,7 @@ sub parameterize { constraining_value => $args, parent_type_constraint=>$self->parent_type_constraint, constraining_value_type_constraint => $self->constraining_value_type_constraint, - ($self->has_message ? (message => $self->message) : ()), + ($self->has_message ? (message => sub { $self->message->( @_, $args ) } ) : ()), ); } } diff --git a/t/07-no-message.t b/t/07-no-message.t index 4cb4219..e615112 100644 --- a/t/07-no-message.t +++ b/t/07-no-message.t @@ -19,7 +19,7 @@ use Test::More; $int >= length($string) ? 1:0; }; - has short_string => ( is => 'rw', isa => Varchar[5] ); + has short_string => ( is => 'rw', isa => Varchar[5] ); } my $obj = Test::MyMooseClass->new(short_string => 'four'); diff --git a/t/08-custom-messages.t b/t/08-custom-messages.t new file mode 100644 index 0000000..b53eb6d --- /dev/null +++ b/t/08-custom-messages.t @@ -0,0 +1,24 @@ +use strict; +use warnings; + +use Test::More; +use MooseX::Types -declare=>[qw( SizedArray )]; +use MooseX::Types::Parameterizable qw(Parameterizable); +use MooseX::Types::Moose qw( Int ArrayRef ); + +ok subtype( + SizedArray, + as Parameterizable[ArrayRef,Int], + where { + my ($value, $max) = @_; + @$value > $max + }, + message { + my($value, $max) = @_; + return sprintf('%d > %d', scalar(@$value), $max); + } +), 'Created parameterized type'; + +is SizedArray([3])->get_message([1..4]), q{4 > 3}, 'custom message'; + +done_testing;