X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-StrictConstructor.git;a=blobdiff_plain;f=t%2Fbasic.t;h=5252dc9a5200a8ea07aaead513cdea33a50a359a;hp=576f0ee6177b2e709283c3ff2c765d10ad693eec;hb=85670a7af9884967634b056948eecff0d46a273d;hpb=0f795b436e24989d610c910cfb66e7827c12c96d diff --git a/t/basic.t b/t/basic.t index 576f0ee..5252dc9 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,8 +1,9 @@ use strict; use warnings; -use Test::More tests => 15; - +use Test::Fatal; +use Test::Moose qw( with_immutable ); +use Test::More 0.88; { package Standard; @@ -15,6 +16,7 @@ use Test::More tests => 15; { package Stricter; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); @@ -23,6 +25,7 @@ use Test::More tests => 15; { package Subclass; + use Moose; use MooseX::StrictConstructor; extends 'Stricter'; @@ -31,123 +34,130 @@ use Test::More tests => 15; } { - package Tricky; - - use MooseX::StrictConstructor; - - has 'thing' => ( is => 'rw' ); - - sub BUILD - { - my $self = shift; - my $params = shift; - - delete $params->{spy}; - } -} + package StrictSubclass; -{ - package InitArg; - - use MooseX::StrictConstructor; - - has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); - has 'size' => ( is => 'rw', 'init_arg' => undef ); -} - -{ - package ImmutableInitArg; - - use MooseX::StrictConstructor; + use Moose; - has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); - has 'size' => ( is => 'rw', 'init_arg' => undef ); + extends 'Stricter'; - no Moose; - __PACKAGE__->meta()->make_immutable(); + has 'size' => ( is => 'rw' ); } { - package Immutable; + package OtherStrictSubclass; + use Moose; use MooseX::StrictConstructor; - has 'thing' => ( is => 'rw' ); + extends 'Standard'; - no Moose; - __PACKAGE__->meta()->make_immutable(); + has 'size' => ( is => 'rw' ); } { - package ImmutableTricky; + package Tricky; + use Moose; use MooseX::StrictConstructor; has 'thing' => ( is => 'rw' ); - sub BUILD - { + sub BUILD { my $self = shift; my $params = shift; delete $params->{spy}; } - - no Moose; - __PACKAGE__->meta()->make_immutable(); } +{ + package InitArg; -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' ); + use Moose; + use MooseX::StrictConstructor; -eval { Immutable->new( thing => 1, bad => 99 ) }; -like( $@, qr/unknown attribute.+: bad/, - 'strict constructor in immutable class blows up on unknown params' ); + has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); + has 'size' => ( is => 'rw', 'init_arg' => undef ); +} -eval { ImmutableTricky->new( thing => 1, spy => 99 ) }; -is( $@, '', - 'immutable class can work around strict constructor by deleting params in BUILD()' ); +my @classes + = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg ); + +with_immutable { + is( + exception { Standard->new( thing => 1, bad => 99 ) }, undef, + 'standard Moose class ignores unknown params' + ); + + like( + exception { Stricter->new( thing => 1, bad => 99 ) }, + qr/unknown attribute.+: bad/, + 'strict constructor blows up on unknown params' + ); + + is( + exception { Subclass->new( thing => 1, size => 'large' ) }, undef, + 'subclass constructor handles known attributes correctly' + ); + + like( + exception { Subclass->new( thing => 1, bad => 99 ) }, + qr/unknown attribute.+: bad/, + 'subclass correctly recognizes bad attribute' + ); + + is( + exception { StrictSubclass->new( thing => 1, size => 'large', ) }, + undef, + q{subclass that doesn't use strict constructor handles known attributes correctly} + ); + + like( + exception { StrictSubclass->new( thing => 1, bad => 99 ) }, + qr/unknown attribute.+: bad/, + q{subclass that doesn't use strict correctly recognizes bad attribute} + ); + + is( + exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) }, + undef, + q{strict subclass from parent that doesn't use strict constructor handles known attributes correctly} + ); + + like( + exception { OtherStrictSubclass->new( thing => 1, bad => 99 ) }, + qr/unknown attribute.+: bad/, + q{strict subclass from parent that doesn't use strict correctly recognizes bad attribute} + ); + + is( + exception { Tricky->new( thing => 1, spy => 99 ) }, undef, + 'can work around strict constructor by deleting params in BUILD()' + ); + + like( + exception { Tricky->new( thing => 1, agent => 99 ) }, + qr/unknown attribute.+: agent/, + 'Tricky still blows up on unknown params other than spy' + ); + + like( + exception { InitArg->new( thing => 1 ) }, + qr/unknown attribute.+: thing/, + 'InitArg blows up with attribute name' + ); + + like( + exception { InitArg->new( size => 1 ) }, + qr/unknown attribute.+: size/, + 'InitArg blows up when given attribute with undef init_arg' + ); + + is( + exception { InitArg->new( other => 1 ) }, undef, + 'InitArg works when given proper init_arg' + ); +} +@classes; -eval { ImmutableTricky->new( thing => 1, agent => 99 ) }; -like( $@, qr/unknown attribute.+: agent/, - 'ImmutableTricky still blows up on unknown params other than spy' ); +done_testing();