From: Karen Etheridge Date: Fri, 8 Apr 2011 17:55:46 +0000 (-0700) Subject: also test adding strictness in a subclass -- and fix mutable case (thanks doy!) :) X-Git-Tag: v0.14~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-StrictConstructor.git;a=commitdiff_plain;h=1ac1adaa4eff9e2237bd50af383b7d93aa1bcabf also test adding strictness in a subclass -- and fix mutable case (thanks doy!) :) --- diff --git a/lib/MooseX/StrictConstructor/Trait/Class.pm b/lib/MooseX/StrictConstructor/Trait/Class.pm index 0c73318..07f203a 100644 --- a/lib/MooseX/StrictConstructor/Trait/Class.pm +++ b/lib/MooseX/StrictConstructor/Trait/Class.pm @@ -29,6 +29,18 @@ around '_inline_BUILDALL' => sub { ); }; +# if the Object role is applied first, and then a superclass added, we just +# lost our BUILDALL modification. +after superclasses => sub +{ + my $self = shift; + return if not @_; + Moose::Util::MetaRole::apply_base_class_roles( + for => $self->name, + roles => ['MooseX::StrictConstructor::Role::Object'], + ) +}; + 1; # ABSTRACT: A role to make immutable constructors strict diff --git a/t/basic.t b/t/basic.t index 1738d92..04046d1 100644 --- a/t/basic.t +++ b/t/basic.t @@ -44,6 +44,17 @@ use Test::More; } { + package OtherStrictSubclass; + + use Moose; + use MooseX::StrictConstructor; + + extends 'Standard'; + + has 'size' => ( is => 'rw' ); +} + +{ package Tricky; use Moose; @@ -69,7 +80,7 @@ use Test::More; has 'size' => ( is => 'rw', 'init_arg' => undef ); } -my @classes = qw( Standard Stricter Subclass StrictSubclass Tricky InitArg ); +my @classes = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg ); with_immutable { is( @@ -106,6 +117,17 @@ with_immutable { ); is( + exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) }, undef, + '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/, + '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()' );