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' );
40 use MooseX::StrictConstructor;
42 has 'thing' => ( is => 'rw' );
48 delete $params->{spy};
56 use MooseX::StrictConstructor;
58 has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
59 has 'size' => ( is => 'rw', 'init_arg' => undef );
62 my @classes = qw( Standard Stricter Subclass Tricky InitArg );
66 exception { Standard->new( thing => 1, bad => 99 ) }, undef,
67 'standard Moose class ignores unknown params'
71 exception { Stricter->new( thing => 1, bad => 99 ) },
72 qr/unknown attribute.+: bad/,
73 'strict constructor blows up on unknown params'
77 exception { Subclass->new( thing => 1, size => 'large' ) }, undef,
78 'subclass constructor handles known attributes correctly'
82 exception { Subclass->new( thing => 1, bad => 99 ) },
83 qr/unknown attribute.+: bad/,
84 'subclass correctly recognizes bad attribute'
88 exception { Tricky->new( thing => 1, spy => 99 ) }, undef,
89 'can work around strict constructor by deleting params in BUILD()'
93 exception { Tricky->new( thing => 1, agent => 99 ) },
94 qr/unknown attribute.+: agent/,
95 'Tricky still blows up on unknown params other than spy'
99 exception { Subclass->new( thing => 1, bad => 99 ) },
100 qr/unknown attribute.+: bad/,
101 'subclass constructor blows up on unknown params'
105 exception { InitArg->new( thing => 1 ) },
106 qr/unknown attribute.+: thing/,
107 'InitArg blows up with attribute name'
111 exception { InitArg->new( size => 1 ) },
112 qr/unknown attribute.+: size/,
113 'InitArg blows up when given attribute with undef init_arg'
117 exception { InitArg->new( other => 1 ) }, undef,
118 'InitArg works when given proper init_arg'