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 );
85 eval q[package MyRole; use Moose::Role; use MooseX::StrictConstructor;];
88 qr/can only be applied to Moose classes/,
89 "can't apply MXSC to a role"
95 eval q[package Nothing; use MooseX::StrictConstructor;];
98 qr/can only be applied to Moose classes/,
99 "can't apply MXSC to a random package",
104 = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg );
108 exception { Standard->new( thing => 1, bad => 99 ) }, undef,
109 'standard Moose class ignores unknown params'
113 exception { Stricter->new( thing => 1, bad => 99 ) },
114 qr/unknown attribute.+: bad/,
115 'strict constructor blows up on unknown params'
119 exception { Subclass->new( thing => 1, size => 'large' ) }, undef,
120 'subclass constructor handles known attributes correctly'
124 exception { Subclass->new( thing => 1, bad => 99 ) },
125 qr/unknown attribute.+: bad/,
126 'subclass correctly recognizes bad attribute'
130 exception { StrictSubclass->new( thing => 1, size => 'large', ) },
132 q{subclass that doesn't use strict constructor handles known attributes correctly}
136 exception { StrictSubclass->new( thing => 1, bad => 99 ) },
137 qr/unknown attribute.+: bad/,
138 q{subclass that doesn't use strict correctly recognizes bad attribute}
142 exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) },
144 q{strict subclass from parent that doesn't use strict constructor handles known attributes correctly}
148 exception { OtherStrictSubclass->new( thing => 1, bad => 99 ) },
149 qr/unknown attribute.+: bad/,
150 q{strict subclass from parent that doesn't use strict correctly recognizes bad attribute}
154 exception { Tricky->new( thing => 1, spy => 99 ) }, undef,
155 'can work around strict constructor by deleting params in BUILD()'
159 exception { Tricky->new( thing => 1, agent => 99 ) },
160 qr/unknown attribute.+: agent/,
161 'Tricky still blows up on unknown params other than spy'
165 exception { InitArg->new( thing => 1 ) },
166 qr/unknown attribute.+: thing/,
167 'InitArg blows up with attribute name'
171 exception { InitArg->new( size => 1 ) },
172 qr/unknown attribute.+: size/,
173 'InitArg blows up when given attribute with undef init_arg'
177 exception { InitArg->new( other => 1 ) }, undef,
178 'InitArg works when given proper init_arg'