4 use Test::More tests => 12;
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', 'init_arg' => 'other' );
55 has 'size' => ( is => 'rw', 'init_arg' => undef );
61 use MooseX::StrictConstructor;
63 has 'thing' => ( is => 'rw' );
66 __PACKAGE__->meta()->make_immutable();
70 package ImmutableTricky;
72 use MooseX::StrictConstructor;
74 has 'thing' => ( is => 'rw' );
81 delete $params->{spy};
85 __PACKAGE__->meta()->make_immutable();
89 eval { Standard->new( thing => 1, bad => 99 ) };
90 is( $@, '', 'standard Moose class ignores unknown params' );
92 eval { Stricter->new( thing => 1, bad => 99 ) };
93 like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
95 eval { Tricky->new( thing => 1, spy => 99 ) };
96 is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
98 eval { Tricky->new( thing => 1, agent => 99 ) };
99 like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
101 eval { Subclass->new( thing => 1, bad => 99 ) };
102 like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
104 eval { Subclass->new( thing => 1, size => 'large' ) };
105 is( $@, '', 'subclass constructor handles known attributes correctly' );
107 eval { Immutable->new( thing => 1, bad => 99 ) };
108 like( $@, qr/unknown attribute.+: bad/,
109 'strict constructor in immutable class blows up on unknown params' );
111 eval { ImmutableTricky->new( thing => 1, spy => 99 ) };
113 'immutable class can work around strict constructor by deleting params in BUILD()' );
115 eval { ImmutableTricky->new( thing => 1, agent => 99 ) };
116 like( $@, qr/unknown attribute.+: agent/,
117 'ImmutableTricky still blows up on unknown params other than spy' );
119 eval { InitArg->new( thing => 1 ) };
120 like( $@, qr/unknown attribute.+: thing/,
121 'InitArg blows up with attribute name' );
123 eval { InitArg->new( size => 1 ) };
124 like( $@, qr/unknown attribute.+: size/,
125 'InitArg blows up when given attribute with undef init_arg' );
127 eval { InitArg->new( other => 1 ) };
129 'InitArg works when given proper init_arg' );