4 use Test::More tests => 9;
12 has 'thing' => ( is => 'rw' );
18 use MooseX::StrictConstructor;
20 has 'thing' => ( is => 'rw' );
26 use MooseX::StrictConstructor;
30 has 'size' => ( is => 'rw' );
36 use MooseX::StrictConstructor;
38 has 'thing' => ( is => 'rw' );
45 delete $params->{spy};
52 use MooseX::StrictConstructor;
54 has 'thing' => ( is => 'rw' );
57 __PACKAGE__->meta()->make_immutable();
61 package ImmutableTricky;
63 use MooseX::StrictConstructor;
65 has 'thing' => ( is => 'rw' );
72 delete $params->{spy};
76 __PACKAGE__->meta()->make_immutable();
80 eval { Standard->new( thing => 1, bad => 99 ) };
81 is( $@, '', 'standard Moose class ignores unknown params' );
83 eval { Stricter->new( thing => 1, bad => 99 ) };
84 like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
86 eval { Tricky->new( thing => 1, spy => 99 ) };
87 is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
89 eval { Tricky->new( thing => 1, agent => 99 ) };
90 like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
92 eval { Subclass->new( thing => 1, bad => 99 ) };
93 like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
95 eval { Subclass->new( thing => 1, size => 'large' ) };
96 is( $@, '', 'subclass constructor handles known attributes correctly' );
98 eval { Immutable->new( thing => 1, bad => 99 ) };
99 like( $@, qr/unknown attribute.+: bad/,
100 'strict constructor in immutable class blows up on unknown params' );
102 eval { ImmutableTricky->new( thing => 1, spy => 99 ) };
104 'immutable class can work around strict constructor by deleting params in BUILD()' );
106 eval { ImmutableTricky->new( thing => 1, agent => 99 ) };
107 like( $@, qr/unknown attribute.+: agent/,
108 'ImmutableTricky still blows up on unknown params other than spy' );