X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fbasic.t;h=24f62ce122a9ba4c426e98419f5496b20702f745;hb=d2d0fb5ec1ca61dbf8a44dd87fe3fa9ef112e50c;hp=cb0fec78866ad49e4eca24c3118b4bfb40236bb1;hpb=c001451a92b1a0e8f33e4854d1694140cde35d53;p=gitmo%2FMooseX-StrictConstructor.git diff --git a/t/basic.t b/t/basic.t index cb0fec7..24f62ce 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 => 15; { @@ -15,6 +15,7 @@ use Test::More tests => 9; { package Stricter; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); @@ -23,6 +24,7 @@ use Test::More tests => 9; { package Subclass; + use Moose; use MooseX::StrictConstructor; extends 'Stricter'; @@ -33,6 +35,7 @@ use Test::More tests => 9; { package Tricky; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); @@ -47,8 +50,32 @@ use Test::More tests => 9; } { + package InitArg; + + use Moose; + use MooseX::StrictConstructor; + + has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); + has 'size' => ( is => 'rw', 'init_arg' => undef ); +} + +{ + package ImmutableInitArg; + + use Moose; + 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 Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); @@ -60,6 +87,7 @@ use Test::More tests => 9; { package ImmutableTricky; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); @@ -71,6 +99,9 @@ use Test::More tests => 9; delete $params->{spy}; } + + no Moose; + __PACKAGE__->meta()->make_immutable(); } @@ -80,6 +111,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()' ); @@ -89,8 +123,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/,