From: Dave Rolsky Date: Sat, 19 Apr 2008 00:09:19 +0000 (+0000) Subject: look at init_arg, not attribute name X-Git-Tag: 0.04~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a83dec4353207816d24e09f0f3a7afc2200369c4;p=gitmo%2FMooseX-StrictConstructor.git look at init_arg, not attribute name --- diff --git a/Changes b/Changes index 3698670..c59bdf0 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,9 @@ +0.04 2008-04-18 + +- This module did not respcet the init_arg attribute setting, and used + the attribute name instead. Reported by Matt Trout. RT #34507. + + 0.03 2007-11-20 - Require Moose 0.26, since that's first version to offer init_meta(), diff --git a/lib/MooseX/Object/StrictConstructor.pm b/lib/MooseX/Object/StrictConstructor.pm index 1862290..55b5d91 100644 --- a/lib/MooseX/Object/StrictConstructor.pm +++ b/lib/MooseX/Object/StrictConstructor.pm @@ -17,13 +17,18 @@ after 'BUILDALL' => sub my $self = shift; my $params = shift; - my %attrs = map { $_->name() => 1 } $self->meta()->compute_all_applicable_attributes(); + my %attrs = + ( map { $_ => 1 } + grep { defined } + map { $_->init_arg() } + $self->meta()->compute_all_applicable_attributes() + ); my @bad = sort grep { ! $attrs{$_} } keys %{ $params }; if (@bad) { - confess "Found unknown attribute(s) passed to the constructor: @bad"; + confess "Found unknown attribute(s) init_arg passed to the constructor: @bad"; } return; diff --git a/lib/MooseX/StrictConstructor.pm b/lib/MooseX/StrictConstructor.pm index d4f16e5..ea1f719 100644 --- a/lib/MooseX/StrictConstructor.pm +++ b/lib/MooseX/StrictConstructor.pm @@ -3,7 +3,7 @@ package MooseX::StrictConstructor; use strict; use warnings; -our $VERSION = '0.03'; +our $VERSION = '0.04'; use Moose; use MooseX::Object::StrictConstructor; @@ -54,8 +54,8 @@ MooseX::StrictConstructor - Make your object constructors blow up on unknown att Using this class to load Moose instead of just loading using Moose itself makes your constructors "strict". If your constructor is called -with an attribute that your class does not declare, then it calls -"Carp::confess()". This is a great way to catch small typos. +with an attribute init argument that your class does not declare, then +it calls "Carp::confess()". This is a great way to catch small typos. =head2 Subverting Strictness diff --git a/t/basic.t b/t/basic.t index 9c26f8d..65aef05 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 12; { @@ -47,6 +47,15 @@ use Test::More tests => 9; } { + package InitArg; + + use MooseX::StrictConstructor; + + has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); + has 'size' => ( is => 'rw', 'init_arg' => undef ); +} + +{ package Immutable; use MooseX::StrictConstructor; @@ -106,3 +115,15 @@ is( $@, '', eval { ImmutableTricky->new( thing => 1, agent => 99 ) }; like( $@, qr/unknown attribute.+: agent/, 'ImmutableTricky still blows up on unknown params other than spy' ); + +eval { InitArg->new( thing => 1 ) }; +like( $@, qr/unknown attribute.+: thing/, + 'InitArg blows up with attribute name' ); + +eval { InitArg->new( size => 1 ) }; +like( $@, qr/unknown attribute.+: size/, + 'InitArg blows up when given attribute with undef init_arg' ); + +eval { InitArg->new( other => 1 ) }; +is( $@, '', + 'InitArg works when given proper init_arg' );