From: Stevan Little Date: Sat, 7 Oct 2006 11:01:47 +0000 (+0000) Subject: lazy attr bug found by ashley X-Git-Tag: 0_14~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4fd69d6c1e9ed820d8ea46f9aabf5afc705a4db0;p=gitmo%2FMoose.git lazy attr bug found by ashley --- diff --git a/Changes b/Changes index 7e5e0a9..351e50f 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,12 @@ Revision history for Perl extension Moose +0.14 + + * Moose::Meta::Attribute + - fixed lazy attributes which were not getting + checked with the type constraint (thanks ashley) + - added tests for this + 0.13 Sat. Sept. 30, 2006 ++ NOTE ++ This version of Moose *must* have Class::MOP 0.35 in order diff --git a/README b/README index 914c6bf..1690d5a 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Moose version 0.13 +Moose version 0.14 =========================== See the individual module documentation for more information diff --git a/lib/Moose.pm b/lib/Moose.pm index 150bc31..8bac829 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -4,7 +4,7 @@ package Moose; use strict; use warnings; -our $VERSION = '0.13'; +our $VERSION = '0.14'; use Scalar::Util 'blessed', 'reftype'; use Carp 'confess'; diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index a6fd964..437a0b0 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -7,7 +7,7 @@ use warnings; use Scalar::Util 'blessed', 'weaken', 'reftype'; use Carp 'confess'; -our $VERSION = '0.07'; +our $VERSION = '0.08'; use Moose::Util::TypeConstraints (); @@ -267,6 +267,24 @@ sub _inline_check_required { sub _inline_check_lazy { my $self = shift; return '' unless $self->is_lazy; + if ($self->has_type_constraint) { + # NOTE: + # this could probably be cleaned + # up and streamlined a little more + return 'unless (exists $_[0]->{$attr_name}) {' . + ' if ($attr->has_default) {' . + ' my $default = $attr->default($_[0]);' . + ' (defined($attr->type_constraint->check($default)))' . + ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' . + ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' . + ' if defined($default);' . + ' $_[0]->{$attr_name} = $default; ' . + ' }' . + ' else {' . + ' $_[0]->{$attr_name} = undef;' . + ' }' . + '}'; + } return '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)' . 'unless exists $_[0]->{$attr_name};'; } diff --git a/t/071_misc_attribute_tests.t b/t/071_misc_attribute_tests.t index eb22962..c2ddfaf 100644 --- a/t/071_misc_attribute_tests.t +++ b/t/071_misc_attribute_tests.t @@ -3,35 +3,69 @@ use strict; use warnings; -use Test::More tests => 7; +use Test::More tests => 10; use Test::Exception; BEGIN { use_ok('Moose'); } -## Roles - { - package Test::TheDefaultFor::ArrayRef::and::HashRef; - use Moose; + { + package Test::TheDefaultFor::ArrayRef::and::HashRef; + use Moose; - has 'array_ref' => (is => 'rw', isa => 'ArrayRef'); - has 'hash_ref' => (is => 'rw', isa => 'HashRef'); + has 'array_ref' => (is => 'rw', isa => 'ArrayRef'); + has 'hash_ref' => (is => 'rw', isa => 'HashRef'); + + } + + my $test = Test::TheDefaultFor::ArrayRef::and::HashRef->new; + isa_ok($test, 'Test::TheDefaultFor::ArrayRef::and::HashRef'); + + is_deeply($test->array_ref, [], '.... got the right default value'); + is_deeply($test->hash_ref, {}, '.... got the right default value'); + + my $test2 = Test::TheDefaultFor::ArrayRef::and::HashRef->new( + array_ref => [ 1, 2, [] ], + hash_ref => { one => 1, two => 2, three => {} }, + ); + isa_ok($test2, 'Test::TheDefaultFor::ArrayRef::and::HashRef'); + is_deeply($test2->array_ref, [ 1, 2, [] ], '.... got the right default value'); + is_deeply($test2->hash_ref, { one => 1, two => 2, three => {} }, '.... got the right default value'); } -my $test = Test::TheDefaultFor::ArrayRef::and::HashRef->new; -isa_ok($test, 'Test::TheDefaultFor::ArrayRef::and::HashRef'); +{ + { + package Test::For::Lazy::TypeConstraint; + use Moose; + use Moose::Util::TypeConstraints; -is_deeply($test->array_ref, [], '.... got the right default value'); -is_deeply($test->hash_ref, {}, '.... got the right default value'); + has 'bad_lazy_attr' => ( + is => 'rw', + isa => 'ArrayRef', + lazy => 1, + default => sub { "test" }, + ); + + has 'good_lazy_attr' => ( + is => 'rw', + isa => 'ArrayRef', + lazy => 1, + default => sub { [] }, + ); -my $test2 = Test::TheDefaultFor::ArrayRef::and::HashRef->new( - array_ref => [ 1, 2, [] ], - hash_ref => { one => 1, two => 2, three => {} }, -); -isa_ok($test2, 'Test::TheDefaultFor::ArrayRef::and::HashRef'); + } -is_deeply($test2->array_ref, [ 1, 2, [] ], '.... got the right default value'); -is_deeply($test2->hash_ref, { one => 1, two => 2, three => {} }, '.... got the right default value'); \ No newline at end of file + my $test = Test::For::Lazy::TypeConstraint->new; + isa_ok($test, 'Test::For::Lazy::TypeConstraint'); + + dies_ok { + $test->bad_lazy_attr; + } '... this does not work'; + + lives_ok { + $test->good_lazy_attr; + } '... this does not work'; +}