From: Dave Rolsky Date: Sat, 19 Apr 2008 03:45:47 +0000 (+0000) Subject: respect init_arg in immutable classes X-Git-Tag: 0.05~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0f795b436e24989d610c910cfb66e7827c12c96d;p=gitmo%2FMooseX-StrictConstructor.git respect init_arg in immutable classes --- diff --git a/Changes b/Changes index c59bdf0..ad89455 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,11 @@ +0.05 2008-04-18 + +- The fix in 0.04 only worked for non-immutable classes. + + 0.04 2008-04-18 -- This module did not respcet the init_arg attribute setting, and used +- This module did not respect the init_arg attribute setting, and used the attribute name instead. Reported by Matt Trout. RT #34507. diff --git a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm b/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm index 970bd9c..9e82ec0 100644 --- a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm +++ b/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm @@ -15,7 +15,12 @@ override '_generate_BUILDALL' => sub ## no critic RequireArgUnpacking my $source = super(); $source .= ";\n" if $source; - my @attrs = map { $_->name() . ' => 1,' } @{ $self->attributes() }; + my @attrs = + ( map { "$_ => 1," } + grep { defined } + map { $_->init_arg() } + @{ $self->attributes() } + ); $source .= <<"EOF"; my \%attrs = (@attrs); diff --git a/t/basic.t b/t/basic.t index 65aef05..576f0ee 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 12; +use Test::More tests => 15; { @@ -56,6 +56,18 @@ use Test::More tests => 12; } { + package ImmutableInitArg; + + use MooseX::StrictConstructor; + + has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); + has 'size' => ( is => 'rw', 'init_arg' => undef ); + + no Moose; + __PACKAGE__->meta()->make_immutable(); +} + +{ package Immutable; use MooseX::StrictConstructor; @@ -92,6 +104,9 @@ is( $@, '', 'standard Moose class ignores unknown params' ); eval { Stricter->new( thing => 1, bad => 99 ) }; like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' ); +eval { Subclass->new( thing => 1, size => 'large' ) }; +is( $@, '', 'subclass constructor handles known attributes correctly' ); + eval { Tricky->new( thing => 1, spy => 99 ) }; is( $@, '', 'can work around strict constructor by deleting params in BUILD()' ); @@ -101,8 +116,29 @@ like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown para eval { Subclass->new( thing => 1, bad => 99 ) }; like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' ); -eval { Subclass->new( thing => 1, size => 'large' ) }; -is( $@, '', 'subclass constructor handles known attributes correctly' ); +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' ); + +eval { ImmutableInitArg->new( thing => 1 ) }; +like( $@, qr/unknown attribute.+: thing/, + 'ImmutableInitArg blows up with attribute name' ); + +eval { ImmutableInitArg->new( size => 1 ) }; +like( $@, qr/unknown attribute.+: size/, + 'ImmutableInitArg blows up when given attribute with undef init_arg' ); + +eval { ImmutableInitArg->new( other => 1 ) }; +is( $@, '', + 'ImmutableInitArg works when given proper init_arg' ); eval { Immutable->new( thing => 1, bad => 99 ) }; like( $@, qr/unknown attribute.+: bad/, @@ -115,15 +151,3 @@ 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' );