From: Guillermo Roditi Date: Thu, 15 Nov 2007 21:58:47 +0000 (+0000) Subject: bug in inlined constructor and tests X-Git-Tag: 0_30^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ca168e893b016063dedbb35e1c3bbf5fcc5b50bb;p=gitmo%2FMoose.git bug in inlined constructor and tests --- diff --git a/Changes b/Changes index a8a1b14..caa9e8d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension Moose + * Moose::Meta::Method::Constructor + -builder related bug in inlined constructor. (groditi) + * Moose::Meta::Method::Accessor - genereate unnecessary calls to predicates and refactor code generation for runtime speed (groditi) @@ -7,6 +10,11 @@ Revision history for Perl extension Moose * Moose::Util::TypeConstraints - fix ClassName constraint to introspect symbol table + * t/ + - New tests for builder bug. Upon instantiation, if an + attribute had a builder, no value and was not lazy the + builder default was not getting run, oops. (groditi) + 0.29 Tues. Nov. 13, 2007 * Moose::Meta::Attribute - Fix error message on missing builder method (groditi) diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index c43f520..337051e 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -7,7 +7,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; -our $VERSION = '0.02'; +our $VERSION = '0.03'; our $AUTHORITY = 'cpan:STEVAN'; use base 'Moose::Meta::Method'; @@ -116,7 +116,7 @@ sub _generate_slot_initializer { '|| confess "Attribute (' . $attr->name . ') is required";'); } - if ($attr->has_default && !($is_moose && $attr->is_lazy)) { + if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) { push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {'; @@ -134,8 +134,13 @@ sub _generate_slot_initializer { push @source => "} else {"; - my $default = $self->_generate_default_value($attr, $index); - + my $default; + if( $attr->has_default ){ + $default = $self->_generate_default_value($attr, $index); + } else { + my $builder = $attr->builder; + $default = '$instance->' . $builder; + } push @source => ('my $val = ' . $default . ';'); push @source => $self->_generate_type_constraint_check( $attr, diff --git a/t/300_immutable/001_immutable_moose.t b/t/300_immutable/001_immutable_moose.t index bf21347..77df718 100644 --- a/t/300_immutable/001_immutable_moose.t +++ b/t/300_immutable/001_immutable_moose.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 16; use Test::Exception; BEGIN { @@ -25,8 +25,10 @@ BEGIN { #there is a TC present. has 'foos' => (is => 'ro', lazy_build => 1); has 'bars' => (isa => 'Str', is => 'ro', lazy_build => 1); - sub _build_foos{ "many foos" } - sub _build_bars{ "many bars" } + has 'bazes' => (isa => 'Str', is => 'ro', builder => '_build_bazes'); + sub _build_foos { "many foos" } + sub _build_bars { "many bars" } + sub _build_bazes { "many bazes" } } { @@ -34,13 +36,15 @@ BEGIN { my $meta = Foo->meta; lives_ok{ Foo->new } "lazy_build works"; - is(Foo->new->foos, 'many foos' , "correct value for 'foos'"); - is(Foo->new->bars, 'many bars' , "correct value for 'bars'"); + is(Foo->new->foos, 'many foos' , "correct value for 'foos' before inlining constructor"); + is(Foo->new->bars, 'many bars' , "correct value for 'bars' before inlining constructor"); + is(Foo->new->bazes, 'many bazes' , "correct value for 'bazes' before inlining constructor"); lives_ok{ $meta->make_immutable } "Foo is imutable"; dies_ok{ $meta->add_role($foo_role) } "Add Role is locked"; lives_ok{ Foo->new } "Inlined constructor works with lazy_build"; - is(Foo->new->foos, 'many foos' , "correct value for 'foos'"); - is(Foo->new->bars, 'many bars' , "correct value for 'bars'"); + is(Foo->new->foos, 'many foos' , "correct value for 'foos' after inlining constructor"); + is(Foo->new->bars, 'many bars' , "correct value for 'bars' after inlining constructor"); + is(Foo->new->bazes, 'many bazes' , "correct value for 'bazes' after inlining constructor"); lives_ok{ $meta->make_mutable } "Foo is mutable"; lives_ok{ $meta->add_role($foo_role) } "Add Role is unlocked";