X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fbasic.t;h=81f0f6b6aec193017fd6da81657a70f18b93e3c9;hb=d78f8e746b0fc4ab8e44dfa9ea7dcc8f28cce880;hp=57ab8adcdf1513d3a0496cce729823f0cc24bfa6;hpb=5a0d49213abe7095b652c725efaad35cff6e24b9;p=gitmo%2FMooseX-StrictConstructor.git diff --git a/t/basic.t b/t/basic.t index 57ab8ad..81f0f6b 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,10 +1,11 @@ use strict; use warnings; -use Test::More tests => 15; +use Test::Exception; +use Test::Moose qw( with_immutable ); +use Test::More; { - package Standard; use Moose; @@ -13,7 +14,6 @@ use Test::More tests => 15; } { - package Stricter; use Moose; @@ -23,7 +23,6 @@ use Test::More tests => 15; } { - package Subclass; use Moose; @@ -35,7 +34,6 @@ use Test::More tests => 15; } { - package Tricky; use Moose; @@ -52,7 +50,6 @@ use Test::More tests => 15; } { - package InitArg; use Moose; @@ -62,125 +59,44 @@ use Test::More tests => 15; 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; +my @classes = qw( Standard Stricter Subclass Tricky InitArg ); - use Moose; - use MooseX::StrictConstructor; +with_immutable { + lives_ok { Standard->new( thing => 1, bad => 99 ) } + 'standard Moose class ignores unknown params'; - has 'thing' => ( is => 'rw' ); + throws_ok { Stricter->new( thing => 1, bad => 99 ) } + qr/unknown attribute.+: bad/, + 'strict constructor blows up on unknown params'; - no Moose; - __PACKAGE__->meta()->make_immutable(); -} + lives_ok { Subclass->new( thing => 1, size => 'large' ) } + 'subclass constructor handles known attributes correctly'; -{ + throws_ok { Subclass->new( thing => 1, bad => 99 ) } + qr/unknown attribute.+: bad/, + 'subclass correctly recognizes bad attribute'; - package ImmutableTricky; + lives_ok { Tricky->new( thing => 1, spy => 99 ) } + 'can work around strict constructor by deleting params in BUILD()'; - use Moose; - use MooseX::StrictConstructor; + throws_ok { Tricky->new( thing => 1, agent => 99 ) } + qr/unknown attribute.+: agent/, + 'Tricky still blows up on unknown params other than spy'; - has 'thing' => ( is => 'rw' ); + throws_ok { Subclass->new( thing => 1, bad => 99 ) } + qr/unknown attribute.+: bad/, + 'subclass constructor blows up on unknown params'; - sub BUILD { - my $self = shift; - my $params = shift; + throws_ok { InitArg->new( thing => 1 ) } + qr/unknown attribute.+: thing/, + 'InitArg blows up with attribute name'; - delete $params->{spy}; - } + throws_ok { InitArg->new( size => 1 ) } + qr/unknown attribute.+: size/, + 'InitArg blows up when given attribute with undef init_arg'; - no Moose; - __PACKAGE__->meta()->make_immutable(); -} + lives_ok { InitArg->new( other => 1 ) } + 'InitArg works when given proper init_arg'; +} @classes; -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' ); - -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()' ); - -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 { 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' -); - -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' -); +done_testing();