5 use Test::Moose qw( with_immutable );
13 has 'thing' => ( is => 'rw' );
20 use MooseX::StrictConstructor;
22 has 'thing' => ( is => 'rw' );
29 use MooseX::StrictConstructor;
33 has 'size' => ( is => 'rw' );
37 package StrictSubclass;
43 has 'size' => ( is => 'rw' );
47 package OtherStrictSubclass;
50 use MooseX::StrictConstructor;
54 has 'size' => ( is => 'rw' );
61 use MooseX::StrictConstructor;
63 has 'thing' => ( is => 'rw' );
69 delete $params->{spy};
77 use MooseX::StrictConstructor;
79 has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
80 has 'size' => ( is => 'rw', 'init_arg' => undef );
84 = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg );
88 exception { Standard->new( thing => 1, bad => 99 ) }, undef,
89 'standard Moose class ignores unknown params'
93 exception { Stricter->new( thing => 1, bad => 99 ) },
94 qr/unknown attribute.+: bad/,
95 'strict constructor blows up on unknown params'
99 exception { Subclass->new( thing => 1, size => 'large' ) }, undef,
100 'subclass constructor handles known attributes correctly'
104 exception { Subclass->new( thing => 1, bad => 99 ) },
105 qr/unknown attribute.+: bad/,
106 'subclass correctly recognizes bad attribute'
110 exception { StrictSubclass->new( thing => 1, size => 'large', ) },
112 q{subclass that doesn't use strict constructor handles known attributes correctly}
116 exception { StrictSubclass->new( thing => 1, bad => 99 ) },
117 qr/unknown attribute.+: bad/,
118 q{subclass that doesn't use strict correctly recognizes bad attribute}
122 exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) },
124 q{strict subclass from parent that doesn't use strict constructor handles known attributes correctly}
128 exception { OtherStrictSubclass->new( thing => 1, bad => 99 ) },
129 qr/unknown attribute.+: bad/,
130 q{strict subclass from parent that doesn't use strict correctly recognizes bad attribute}
134 exception { Tricky->new( thing => 1, spy => 99 ) }, undef,
135 'can work around strict constructor by deleting params in BUILD()'
139 exception { Tricky->new( thing => 1, agent => 99 ) },
140 qr/unknown attribute.+: agent/,
141 'Tricky still blows up on unknown params other than spy'
145 exception { InitArg->new( thing => 1 ) },
146 qr/unknown attribute.+: thing/,
147 'InitArg blows up with attribute name'
151 exception { InitArg->new( size => 1 ) },
152 qr/unknown attribute.+: size/,
153 'InitArg blows up when given attribute with undef init_arg'
157 exception { InitArg->new( other => 1 ) }, undef,
158 'InitArg works when given proper init_arg'