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 );
83 my @classes = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg );
87 exception { Standard->new( thing => 1, bad => 99 ) }, undef,
88 'standard Moose class ignores unknown params'
92 exception { Stricter->new( thing => 1, bad => 99 ) },
93 qr/unknown attribute.+: bad/,
94 'strict constructor blows up on unknown params'
98 exception { Subclass->new( thing => 1, size => 'large' ) }, undef,
99 'subclass constructor handles known attributes correctly'
103 exception { Subclass->new( thing => 1, bad => 99 ) },
104 qr/unknown attribute.+: bad/,
105 'subclass correctly recognizes bad attribute'
109 exception { StrictSubclass->new( thing => 1, size => 'large', ) }, undef,
110 'subclass that doesn\'t use strict constructor handles known attributes correctly'
114 exception { StrictSubclass->new( thing => 1, bad => 99 ) },
115 qr/unknown attribute.+: bad/,
116 'subclass that doesn\'t use strict correctly recognizes bad attribute'
120 exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) }, undef,
121 'strict subclass from parent that doesn\'t use strict constructor handles known attributes correctly'
125 exception { OtherStrictSubclass->new( thing => 1, bad => 99 ) },
126 qr/unknown attribute.+: bad/,
127 'strict subclass from parent that doesn\'t use strict correctly recognizes bad attribute'
131 exception { Tricky->new( thing => 1, spy => 99 ) }, undef,
132 'can work around strict constructor by deleting params in BUILD()'
136 exception { Tricky->new( thing => 1, agent => 99 ) },
137 qr/unknown attribute.+: agent/,
138 'Tricky still blows up on unknown params other than spy'
142 exception { InitArg->new( thing => 1 ) },
143 qr/unknown attribute.+: thing/,
144 'InitArg blows up with attribute name'
148 exception { InitArg->new( size => 1 ) },
149 qr/unknown attribute.+: size/,
150 'InitArg blows up when given attribute with undef init_arg'
154 exception { InitArg->new( other => 1 ) }, undef,
155 'InitArg works when given proper init_arg'