From: nperez Date: Mon, 18 May 2009 19:29:10 +0000 (-0500) Subject: Add back rafl and rhesa's original changes and tweak them for steven's okay for defau... X-Git-Tag: 0.85~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed337aad67299c2a61eca03d876600404a0fc7a2;p=gitmo%2FClass-MOP.git Add back rafl and rhesa's original changes and tweak them for steven's okay for default => Class::MOP::Method --- diff --git a/Changes b/Changes index a9250b7..20c7caf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,13 @@ Revision history for Perl extension Class-MOP. + + * Class::MOP::Attribute + - Allow default values to be Class::MOP::Methods. + (Florian Ragwitz) + - Test the above. (Rhesa Rozendaal) + - Tweak original commit so the intent matches the accepted behavior + (Nicholas Perez) + 0.84 Tue, May 12, 2009 * Makefile.PL - Depend on Text::Exception 0.27 to avoid failing tests ond old diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index aba370d..37dc2f9 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -248,7 +248,9 @@ sub get_write_method_ref { } sub is_default_a_coderef { - ('CODE' eq ref($_[0]->{'default'})) + my ($value) = $_[0]->{'default'}; + return unless ref($value); + return ref($value) eq 'CODE' || (blessed($value) && $value->isa('Class::MOP::Method')); } sub default { diff --git a/t/020_attribute.t b/t/020_attribute.t index cad88e2..ee5e60b 100644 --- a/t/020_attribute.t +++ b/t/020_attribute.t @@ -3,11 +3,12 @@ use warnings; use Scalar::Util 'reftype', 'blessed'; -use Test::More tests => 100; +use Test::More tests => 104; use Test::Exception; use Class::MOP; use Class::MOP::Attribute; +use Class::MOP::Method; dies_ok { Class::MOP::Attribute->name } q{... can't call name() as a class method}; @@ -225,3 +226,21 @@ dies_ok { Class::MOP::Attribute->name } q{... can't call name() as a class metho is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder'); } + +{ + for my $value ({}, bless({}, 'Foo')) { + throws_ok { + Class::MOP::Attribute->new('$foo', default => $value); + } qr/References are not allowed as default values/; + } +} + +{ + my $attr; + lives_ok { + my $meth = Class::MOP::Method->wrap(sub {shift}, name => 'foo', package_name => 'bar'); + $attr = Class::MOP::Attribute->new('$foo', default => $meth); + } 'Class::MOP::Methods accepted as default'; + + is($attr->default(42), 42, 'passthrough for default on attribute'); +}