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=81f0f6b6aec193017fd6da81657a70f18b93e3c9;hb=85670a7af9884967634b056948eecff0d46a273d;hpb=40ab99f602114722d42528f31e5cfca599b31ada diff --git a/t/basic.t b/t/basic.t index 81f0f6b..5252dc9 100644 --- a/t/basic.t +++ b/t/basic.t @@ -1,9 +1,9 @@ use strict; use warnings; -use Test::Exception; +use Test::Fatal; use Test::Moose qw( with_immutable ); -use Test::More; +use Test::More 0.88; { package Standard; @@ -34,6 +34,27 @@ use Test::More; } { + package StrictSubclass; + + use Moose; + + extends 'Stricter'; + + has 'size' => ( is => 'rw' ); +} + +{ + package OtherStrictSubclass; + + use Moose; + use MooseX::StrictConstructor; + + extends 'Standard'; + + has 'size' => ( is => 'rw' ); +} + +{ package Tricky; use Moose; @@ -59,44 +80,84 @@ use Test::More; has 'size' => ( is => 'rw', 'init_arg' => undef ); } -my @classes = qw( Standard Stricter Subclass Tricky InitArg ); +my @classes + = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg ); with_immutable { - lives_ok { Standard->new( thing => 1, bad => 99 ) } - 'standard Moose class ignores unknown params'; - - throws_ok { Stricter->new( thing => 1, bad => 99 ) } - qr/unknown attribute.+: bad/, - 'strict constructor blows up on unknown params'; - - 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'; - - lives_ok { Tricky->new( thing => 1, spy => 99 ) } - 'can work around strict constructor by deleting params in BUILD()'; - - throws_ok { Tricky->new( thing => 1, agent => 99 ) } - qr/unknown attribute.+: agent/, - 'Tricky still blows up on unknown params other than spy'; - - throws_ok { Subclass->new( thing => 1, bad => 99 ) } - qr/unknown attribute.+: bad/, - 'subclass constructor blows up on unknown params'; - - throws_ok { InitArg->new( thing => 1 ) } - qr/unknown attribute.+: thing/, - 'InitArg blows up with attribute name'; - - throws_ok { InitArg->new( size => 1 ) } - qr/unknown attribute.+: size/, - 'InitArg blows up when given attribute with undef init_arg'; - - lives_ok { InitArg->new( other => 1 ) } - 'InitArg works when given proper init_arg'; -} @classes; + 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; done_testing();