From 1279ccc9e428866638d87ab91d4c5844241d5b48 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 24 Mar 2011 13:57:27 -0500 Subject: also need to check attribute type constraints during dep resolution --- lib/Bread/Board/Declare/Role/Service.pm | 11 ++++++++--- t/30-type-checks.t | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/Bread/Board/Declare/Role/Service.pm b/lib/Bread/Board/Declare/Role/Service.pm index e0b36f9..96bdaf7 100644 --- a/lib/Bread/Board/Declare/Role/Service.pm +++ b/lib/Bread/Board/Declare/Role/Service.pm @@ -29,12 +29,17 @@ around get => sub { my $self = shift; my $container = $self->parent_container; + my $attr = $self->associated_attribute; - if ($self->associated_attribute->has_value($container)) { - return $self->associated_attribute->get_value($container); + if ($attr->has_value($container)) { + return $attr->get_value($container); } - return $self->$orig(@_); + my $val = $self->$orig(@_); + $attr->verify_against_type_constraint($val, instance => $container) + if $attr->has_type_constraint; + + return $val; }; =method parent_container diff --git a/t/30-type-checks.t b/t/30-type-checks.t index 9133da3..b7718ac 100644 --- a/t/30-type-checks.t +++ b/t/30-type-checks.t @@ -15,6 +15,19 @@ use Test::Moose; isa => 'Ref', value => 'FOO', ); + + has bar => ( + is => 'ro', + isa => 'Str', + block => sub { {} }, + ); + + has baz => ( + is => 'ro', + isa => 'HashRef', + block => sub { shift->param('bar') }, + dependencies => ['bar'], + ); } with_immutable { @@ -22,6 +35,12 @@ with_immutable { like(exception { $foo->foo }, qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Ref' with value FOO/, "error when service returns invalid value"); + like(exception { $foo->bar }, + qr/^Attribute \(bar\) does not pass the type constraint because: Validation failed for 'Str' with value HASH/, + "error when service returns invalid value"); + like(exception { $foo->baz }, + qr/^Attribute \(bar\) does not pass the type constraint because: Validation failed for 'Str' with value HASH/, + "error when service returns invalid value, even as a dependency"); } 'Foo'; done_testing; -- cgit v1.2.3-54-g00ecf