From: Todd Hepler Date: Tue, 16 Sep 2008 22:20:58 +0000 (+0000) Subject: check for a valid attribute name in Moose::Meta::Role X-Git-Tag: 0.58~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f9b5f5f8085fae77d94044917700cc32710e1a26;p=gitmo%2FMoose.git check for a valid attribute name in Moose::Meta::Role added testcase for odd has() calls squashed a warning exposed by that test in Moose::Meta::Class --- diff --git a/Changes b/Changes index b24158c..4641c31 100644 --- a/Changes +++ b/Changes @@ -76,6 +76,11 @@ Revision history for Perl extension Moose Moose::Util::MetaRole was used on the two corresponding classes, then the difference in roles is reconciled for the subclass's metaclass. (Dave Rolsky) + - Squashed an warning in _process_attribute (thepler) + + * Moose::Meta::Role + - throw exceptions (sooner) for invalid attribute names (thepler) + - added tests for this (thepler) 0.57 Wed September 3, 2008 * Moose::Intro diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 4d07d55..d518d16 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -563,7 +563,7 @@ sub _process_attribute { @args = %{$args[0]} if scalar @args == 1 && ref($args[0]) eq 'HASH'; - if ($name =~ /^\+(.*)/) { + if (($name || '') =~ /^\+(.*)/) { return $self->_process_inherited_attribute($1, @args); } else { diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 701f117..e5596e7 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -121,6 +121,8 @@ foreach my $action ( sub add_attribute { my $self = shift; my $name = shift; + (defined $name && $name) + || Moose->throw_error("You must provide a name for the attribute"); my $attr_desc; if (scalar @_ == 1 && ref($_[0]) eq 'HASH') { $attr_desc = $_[0]; diff --git a/t/020_attributes/023_attribute_names.t b/t/020_attributes/023_attribute_names.t new file mode 100644 index 0000000..f63b948 --- /dev/null +++ b/t/020_attributes/023_attribute_names.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More tests => 8; +use Test::Exception; + +# note: not sure about "" and 0 being illegal attribute names +# but I'm just copying what Class::MOP::Attribute does + +my $exception_regex = qr/You must provide a name for the attribute/; +{ + package My::Role; + use Moose::Role; + ::throws_ok{ has; } $exception_regex, 'has; fails'; + ::throws_ok{ has undef; } $exception_regex, 'has undef; fails'; + ::throws_ok{ has ""; } $exception_regex, 'has ""; fails'; + ::throws_ok{ has 0; } $exception_regex, 'has 0; fails'; +} + +{ + package My::Class; + use Moose; + ::throws_ok{ has; } $exception_regex, 'has; fails'; + ::throws_ok{ has undef; } $exception_regex, 'has undef; fails'; + ::throws_ok{ has ""; } $exception_regex, 'has ""; fails'; + ::throws_ok{ has 0; } $exception_regex, 'has 0; fails'; +} +