From: Stevan Little Date: Wed, 16 Aug 2006 21:32:05 +0000 (+0000) Subject: no ref in the defaults X-Git-Tag: 0_33~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=148b469742669e1a506538200f624dcdaeeb510a;p=gitmo%2FClass-MOP.git no ref in the defaults --- diff --git a/Changes b/Changes index e1cf049..f7ddf66 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,11 @@ Revision history for Perl extension Class-MOP. +0.33 + * Class::MOP::Attribute + - reference values (other than CODE refs) + are no longer allowed for defaults + - added tests for this + 0.32 Sat. Aug. 12, 2006 + added Class::MOP::Object so that the metamodel is more complete (and closer diff --git a/README b/README index ca3e898..8b810ad 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Class::MOP version 0.32 +Class::MOP version 0.33 =========================== See the individual module documentation for more information diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 855ef20..3fd61ed 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -13,7 +13,7 @@ use Class::MOP::Method; use Class::MOP::Class::Immutable; -our $VERSION = '0.32'; +our $VERSION = '0.33'; our $AUTHORITY = 'cpan:STEVAN'; ## ---------------------------------------------------------------------------- @@ -267,6 +267,11 @@ Class::MOP::Attribute->meta->add_method('new' => sub { || confess "You must provide a name for the attribute"; $options{init_arg} = $name if not exists $options{init_arg}; + + (Class::MOP::Attribute::is_default_a_coderef(\%options)) + || confess("References are not allowed as default values, you must ". + "wrap then in a CODE reference (ex: sub { [] } and not [])") + if exists $options{default} && ref $options{default}; # return the new object $class->meta->new_object(name => $name, %options); diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index ca93efe..4e3eb09 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -7,7 +7,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'reftype', 'weaken'; -our $VERSION = '0.11'; +our $VERSION = '0.12'; our $AUTHORITY = 'cpan:STEVAN'; sub meta { @@ -34,6 +34,11 @@ sub new { $options{init_arg} = $name if not exists $options{init_arg}; + (is_default_a_coderef(\%options)) + || confess("References are not allowed as default values, you must ". + "wrap then in a CODE reference (ex: sub { [] } and not [])") + if exists $options{default} && ref $options{default}; + bless { name => $name, accessor => $options{accessor}, diff --git a/t/021_attribute_errors_and_edge_cases.t b/t/021_attribute_errors_and_edge_cases.t index 1b8c514..2974b8d 100644 --- a/t/021_attribute_errors_and_edge_cases.t +++ b/t/021_attribute_errors_and_edge_cases.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 20; +use Test::More tests => 23; use Test::Exception; BEGIN { @@ -11,15 +11,40 @@ BEGIN { use_ok('Class::MOP::Attribute'); } +# most values are static { - my $regexp = qr/hello (.*)/; - my $attr = Class::MOP::Attribute->new('$test' => ( - default => $regexp - )); + dies_ok { + Class::MOP::Attribute->new('$test' => ( + default => qr/hello (.*)/ + )); + } '... no refs for defaults'; + + dies_ok { + Class::MOP::Attribute->new('$test' => ( + default => [] + )); + } '... no refs for defaults'; + + dies_ok { + Class::MOP::Attribute->new('$test' => ( + default => {} + )); + } '... no refs for defaults'; + - ok($attr->has_default, '... we have a default value'); - is($attr->default, $regexp, '... and got the value we expected'); + dies_ok { + Class::MOP::Attribute->new('$test' => ( + default => \(my $var) + )); + } '... no refs for defaults'; + + dies_ok { + Class::MOP::Attribute->new('$test' => ( + default => bless {} => 'Foo' + )); + } '... no refs for defaults'; + } { # bad construtor args