From: Shawn M Moore Date: Fri, 15 Jun 2012 20:21:29 +0000 (-0500) Subject: Native trait type constraint exception X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c13dade946115349f9e910c0efadb927eb650f62;p=gitmo%2FMoose.git Native trait type constraint exception --- diff --git a/lib/Moose/Meta/Method/Accessor/Native/Collection.pm b/lib/Moose/Meta/Method/Accessor/Native/Collection.pm index ffed041..8b6550e 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Collection.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Collection.pm @@ -96,7 +96,8 @@ sub _inline_check_member_constraint { my $self = shift; my ($new_value) = @_; - my $attr_name = $self->associated_attribute->name; + my $attr_name = quotemeta($self->associated_attribute->name); + my $type_name = quotemeta($self->_tc_member_type->name); my $check = $self->_tc_member_type->can_be_inlined @@ -110,7 +111,10 @@ sub _inline_check_member_constraint { '"A new member value for ' . $attr_name . ' does not pass its type constraint because: "' . ' . ' . 'do { local $_ = $new_val; $member_message->($new_val) }', - 'data => $new_val', + 'class => "Moose::Exception::TypeConstraint",' . + 'attribute_name => \'' . $attr_name . '\',' . + 'type_name => \'' . $type_name . '\',' . + 'value => ' . $new_value ) . ';', '}', '}', diff --git a/t/exceptions/attribute_types.t b/t/exceptions/attribute_types.t index 4de5200..d940c28 100644 --- a/t/exceptions/attribute_types.t +++ b/t/exceptions/attribute_types.t @@ -7,6 +7,14 @@ use Test::More; use Test::Fatal; use Moose::Util::TypeConstraints; +# no attribute +my $exception = exception { find_type_constraint('Int')->assert_valid("eek") }; +isa_ok($exception, 'Moose::Exception::TypeConstraint'); +is($exception->message, q{Validation failed for 'Int' with value "eek"}, 'exception->message'); +is($exception->value, "eek", 'exception->value'); +is($exception->type_name, "Int", 'exception->type_name'); +is($exception->attribute_name, undef, 'exception->attribute_name'); + BEGIN { package Foo; use Moose; @@ -15,15 +23,26 @@ BEGIN { is => 'rw', isa => 'Int', ); + + has items => ( + traits => ['Array'], + is => 'ro', + isa => 'ArrayRef[Int]', + handles => { + add_item => 'push', + }, + ); } -my $exception = exception { Foo->new(count => "test") }; +# constructor +$exception = exception { Foo->new(count => "test") }; isa_ok($exception, 'Moose::Exception::TypeConstraint'); is($exception->message, q{Attribute (count) does not pass the type constraint because: Validation failed for 'Int' with value "test"}, 'exception->message'); is($exception->value, "test", 'exception->value'); is($exception->type_name, "Int", 'exception->type_name'); is($exception->attribute_name, "count", 'exception->attribute_name'); +# setter my $value = []; $exception = exception { Foo->new->count($value) }; isa_ok($exception, 'Moose::Exception::TypeConstraint'); @@ -32,11 +51,13 @@ is($exception->value, $value, 'exception->value'); is($exception->type_name, "Int", 'exception->type_name'); is($exception->attribute_name, "count", 'exception->attribute_name'); -$exception = exception { find_type_constraint('Int')->assert_valid("eek") }; +# native array push +$value = {}; +$exception = exception { Foo->new->add_item($value) }; isa_ok($exception, 'Moose::Exception::TypeConstraint'); -is($exception->message, q{Validation failed for 'Int' with value "eek"}, 'exception->message'); -is($exception->value, "eek", 'exception->value'); +is($exception->message, q[A new member value for items does not pass its type constraint because: Validation failed for 'Int' with value { }], 'exception->message'); +is($exception->value, $value, 'exception->value'); is($exception->type_name, "Int", 'exception->type_name'); -is($exception->attribute_name, undef, 'exception->attribute_name'); +is($exception->attribute_name, "items", 'exception->attribute_name'); done_testing;