From: Yuval Kogman Date: Tue, 22 Sep 2009 13:40:52 +0000 (+0300) Subject: Test semantics for raw_value X-Git-Tag: 0.92~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=312e0f0ca905063976cb0687da5d0b656157008b;p=gitmo%2FMoose.git Test semantics for raw_value --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 0ebc1b8..ef88e8b 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -426,8 +426,10 @@ sub initialize_instance_slot { $val = $self->_coerce_and_verify( $val, $instance ); $self->set_initial_value($instance, $val); - $meta_instance->weaken_slot_value($instance, $self->name) - if ref $val && $self->is_weak_ref; + + if ( ref $val && $self->is_weak_ref ) { + $self->weaken_value($instance); + } } sub _call_builder { @@ -493,18 +495,15 @@ sub set_value { $value = $self->_coerce_and_verify( $value, $instance ); - my $meta_instance = Class::MOP::Class->initialize(blessed($instance)) - ->get_meta_instance; - my @old; if ( $self->has_trigger && $self->has_value($instance) ) { @old = $self->get_value($instance, 'for trigger'); } - $meta_instance->set_slot_value($instance, $attr_name, $value); + $self->SUPER::set_value($instance, $value); - if (ref $value && $self->is_weak_ref) { - $meta_instance->weaken_slot_value($instance, $attr_name); + if ( ref $value && $self->is_weak_ref ) { + $self->weaken_value($instance); } if ($self->has_trigger) { @@ -512,6 +511,15 @@ sub set_value { } } +sub weaken_value { + my ( $self, $instance ) = @_; + + my $meta_instance = + Class::MOP::Class->initialize( blessed($instance) )->get_meta_instance; + + $meta_instance->weaken_slot_value( $instance, $self->name ); +} + sub get_value { my ($self, $instance, $for_trigger) = @_; diff --git a/t/020_attributes/001_attribute_reader_generation.t b/t/020_attributes/001_attribute_reader_generation.t index d6a4183..407633d 100644 --- a/t/020_attributes/001_attribute_reader_generation.t +++ b/t/020_attributes/001_attribute_reader_generation.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 20; use Test::Exception; @@ -59,6 +59,23 @@ use Test::Exception; } { + my $foo = Foo->new; + isa_ok($foo, 'Foo'); + + my $attr = $foo->meta->find_attribute_by_name("lazy_foo"); + + isa_ok( $attr, "Moose::Meta::Attribute" ); + + ok( $attr->is_lazy, "it's lazy" ); + + is( $attr->get_raw_value($foo), undef, "raw value" ); + + is( $attr->get_value($foo), 10, "lazy value" ); + + is( $attr->get_raw_value($foo), 10, "raw value" ); +} + +{ my $foo = Foo->new(foo => 10, lazy_foo => 100); isa_ok($foo, 'Foo'); diff --git a/t/020_attributes/004_attribute_triggers.t b/t/020_attributes/004_attribute_triggers.t index baa2a2a..974879d 100644 --- a/t/020_attributes/004_attribute_triggers.t +++ b/t/020_attributes/004_attribute_triggers.t @@ -5,7 +5,7 @@ use warnings; use Scalar::Util 'isweak'; -use Test::More tests => 42; +use Test::More tests => 43; use Test::Exception; @@ -189,6 +189,15 @@ use Test::Exception; 'trigger called correctly on second set via meta-API', ); @Foo::calls = (); + + $attr->set_raw_value( $foo, 4 ); + + is_deeply( + \@Foo::calls, + [ ], + 'trigger not called using set_raw_value method', + ); + @Foo::calls = (); } {