4 use Test::More tests => 15;
12 has 'thing' => ( is => 'rw' );
19 use MooseX::StrictConstructor;
21 has 'thing' => ( is => 'rw' );
28 use MooseX::StrictConstructor;
32 has 'size' => ( is => 'rw' );
39 use MooseX::StrictConstructor;
41 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 );
63 package ImmutableInitArg;
66 use MooseX::StrictConstructor;
68 has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
69 has 'size' => ( is => 'rw', 'init_arg' => undef );
72 __PACKAGE__->meta()->make_immutable();
79 use MooseX::StrictConstructor;
81 has 'thing' => ( is => 'rw' );
84 __PACKAGE__->meta()->make_immutable();
88 package ImmutableTricky;
91 use MooseX::StrictConstructor;
93 has 'thing' => ( is => 'rw' );
100 delete $params->{spy};
104 __PACKAGE__->meta()->make_immutable();
108 eval { Standard->new( thing => 1, bad => 99 ) };
109 is( $@, '', 'standard Moose class ignores unknown params' );
111 eval { Stricter->new( thing => 1, bad => 99 ) };
112 like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
114 eval { Subclass->new( thing => 1, size => 'large' ) };
115 is( $@, '', 'subclass constructor handles known attributes correctly' );
117 eval { Tricky->new( thing => 1, spy => 99 ) };
118 is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
120 eval { Tricky->new( thing => 1, agent => 99 ) };
121 like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
123 eval { Subclass->new( thing => 1, bad => 99 ) };
124 like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
126 eval { InitArg->new( thing => 1 ) };
127 like( $@, qr/unknown attribute.+: thing/,
128 'InitArg blows up with attribute name' );
130 eval { InitArg->new( size => 1 ) };
131 like( $@, qr/unknown attribute.+: size/,
132 'InitArg blows up when given attribute with undef init_arg' );
134 eval { InitArg->new( other => 1 ) };
136 'InitArg works when given proper init_arg' );
138 eval { ImmutableInitArg->new( thing => 1 ) };
139 like( $@, qr/unknown attribute.+: thing/,
140 'ImmutableInitArg blows up with attribute name' );
142 eval { ImmutableInitArg->new( size => 1 ) };
143 like( $@, qr/unknown attribute.+: size/,
144 'ImmutableInitArg blows up when given attribute with undef init_arg' );
146 eval { ImmutableInitArg->new( other => 1 ) };
148 'ImmutableInitArg works when given proper init_arg' );
150 eval { Immutable->new( thing => 1, bad => 99 ) };
151 like( $@, qr/unknown attribute.+: bad/,
152 'strict constructor in immutable class blows up on unknown params' );
154 eval { ImmutableTricky->new( thing => 1, spy => 99 ) };
156 'immutable class can work around strict constructor by deleting params in BUILD()' );
158 eval { ImmutableTricky->new( thing => 1, agent => 99 ) };
159 like( $@, qr/unknown attribute.+: agent/,
160 'ImmutableTricky still blows up on unknown params other than spy' );