X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fbasic.t;h=9c26f8de6c5edc846df8bff9075ed71b994648d7;hb=168a0115c0d0be829c5332fa2d5dd05d109ef4b2;hp=d91d3c93d279d68760b81ce4c65be240a5b8b7b0;hpb=32726d885ed56a2628ec0eccf18b2e898d1ca8ca;p=gitmo%2FMooseX-StrictConstructor.git diff --git a/t/basic.t b/t/basic.t index d91d3c9..9c26f8d 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 9; { @@ -21,6 +21,16 @@ use Test::More tests => 4; } { + package Subclass; + + use MooseX::StrictConstructor; + + extends 'Stricter'; + + has 'size' => ( is => 'rw' ); +} + +{ package Tricky; use MooseX::StrictConstructor; @@ -36,6 +46,36 @@ use Test::More tests => 4; } } +{ + package Immutable; + + use MooseX::StrictConstructor; + + has 'thing' => ( is => 'rw' ); + + no Moose; + __PACKAGE__->meta()->make_immutable(); +} + +{ + package ImmutableTricky; + + 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' ); @@ -48,3 +88,21 @@ 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' ); + +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 { Immutable->new( thing => 1, bad => 99 ) }; +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' );