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 );
65 lives_ok { Standard->new( thing => 1, bad => 99 ) }
66 'standard Moose class ignores unknown params';
68 throws_ok { Stricter->new( thing => 1, bad => 99 ) }
69 qr/unknown attribute.+: bad/,
70 'strict constructor blows up on unknown params';
72 lives_ok { Subclass->new( thing => 1, size => 'large' ) }
73 'subclass constructor handles known attributes correctly';
75 throws_ok { Subclass->new( thing => 1, bad => 99 ) }
76 qr/unknown attribute.+: bad/,
77 'subclass correctly recognizes bad attribute';
79 lives_ok { Tricky->new( thing => 1, spy => 99 ) }
80 'can work around strict constructor by deleting params in BUILD()';
82 throws_ok { Tricky->new( thing => 1, agent => 99 ) }
83 qr/unknown attribute.+: agent/,
84 'Tricky still blows up on unknown params other than spy';
86 throws_ok { Subclass->new( thing => 1, bad => 99 ) }
87 qr/unknown attribute.+: bad/,
88 'subclass constructor blows up on unknown params';
90 throws_ok { InitArg->new( thing => 1 ) }
91 qr/unknown attribute.+: thing/,
92 'InitArg blows up with attribute name';
94 throws_ok { InitArg->new( size => 1 ) }
95 qr/unknown attribute.+: size/,
96 'InitArg blows up when given attribute with undef init_arg';
98 lives_ok { InitArg->new( other => 1 ) }
99 'InitArg works when given proper init_arg';