X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fbasic.t;h=57ab8adcdf1513d3a0496cce729823f0cc24bfa6;hb=5a0d49213abe7095b652c725efaad35cff6e24b9;hp=e65d3f2931d6cf874c753a7b7b03f73db6c1feb5;hpb=58812bf9dd6d58f5046d97509b1508ee2fde2e64;p=gitmo%2FMooseX-StrictConstructor.git diff --git a/t/basic.t b/t/basic.t index e65d3f2..57ab8ad 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,10 +1,10 @@ use strict; use warnings; -use Test::More tests => 7; - +use Test::More tests => 15; { + package Standard; use Moose; @@ -13,16 +13,20 @@ use Test::More tests => 7; } { + package Stricter; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); } { + package Subclass; + use Moose; use MooseX::StrictConstructor; extends 'Stricter'; @@ -31,14 +35,15 @@ use Test::More tests => 7; } { + package Tricky; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); - sub BUILD - { + sub BUILD { my $self = shift; my $params = shift; @@ -47,8 +52,35 @@ use Test::More tests => 7; } { + + 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' ); @@ -57,25 +89,98 @@ use Test::More tests => 7; __PACKAGE__->meta()->make_immutable(); } +{ + + package ImmutableTricky; + + use Moose; + use MooseX::StrictConstructor; + + has 'thing' => ( is => 'rw' ); + + sub BUILD { + my $self = shift; + my $params = shift; + + delete $params->{spy}; + } + + no Moose; + __PACKAGE__->meta()->make_immutable(); +} eval { Standard->new( thing => 1, bad => 99 ) }; 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' ); +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()' ); +is( $@, '', + 'can work around strict constructor by deleting params in BUILD()' ); eval { Tricky->new( thing => 1, agent => 99 ) }; -like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' ); +like( $@, qr/unknown attribute.+: agent/, + 'Tricky still blows up on unknown params other than spy' ); 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' ); +like( $@, qr/unknown attribute.+: bad/, + 'subclass constructor blows up on unknown params' ); + +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/, - 'strict constructor in immutable class blows up on unknown params' ); +like( + $@, qr/unknown attribute.+: bad/, + 'strict constructor in immutable class blows up on unknown params' +); + +eval { ImmutableTricky->new( thing => 1, spy => 99 ) }; +is( + $@, '', + 'immutable class can work around strict constructor by deleting params in BUILD()' +); + +eval { ImmutableTricky->new( thing => 1, agent => 99 ) }; +like( + $@, qr/unknown attribute.+: agent/, + 'ImmutableTricky still blows up on unknown params other than spy' +);