Commit | Line | Data |
32726d88 |
1 | use strict; |
2 | use warnings; |
3 | |
54cc4bad |
4 | use Test::More; |
32726d88 |
5 | |
32726d88 |
6 | { |
5a0d4921 |
7 | |
32726d88 |
8 | package Standard; |
9 | |
10 | use Moose; |
11 | |
12 | has 'thing' => ( is => 'rw' ); |
13 | } |
14 | |
15 | { |
5a0d4921 |
16 | |
32726d88 |
17 | package Stricter; |
18 | |
3e532dbe |
19 | use Moose; |
32726d88 |
20 | use MooseX::StrictConstructor; |
21 | |
22 | has 'thing' => ( is => 'rw' ); |
23 | } |
24 | |
25 | { |
5a0d4921 |
26 | |
2ffa7b60 |
27 | package Subclass; |
28 | |
3e532dbe |
29 | use Moose; |
2ffa7b60 |
30 | use MooseX::StrictConstructor; |
31 | |
32 | extends 'Stricter'; |
33 | |
34 | has 'size' => ( is => 'rw' ); |
35 | } |
36 | |
37 | { |
5a0d4921 |
38 | |
32726d88 |
39 | package Tricky; |
40 | |
3e532dbe |
41 | use Moose; |
32726d88 |
42 | use MooseX::StrictConstructor; |
43 | |
44 | has 'thing' => ( is => 'rw' ); |
45 | |
5a0d4921 |
46 | sub BUILD { |
32726d88 |
47 | my $self = shift; |
48 | my $params = shift; |
49 | |
50 | delete $params->{spy}; |
51 | } |
52 | } |
53 | |
58812bf9 |
54 | { |
5a0d4921 |
55 | |
a83dec43 |
56 | package InitArg; |
57 | |
3e532dbe |
58 | use Moose; |
a83dec43 |
59 | use MooseX::StrictConstructor; |
60 | |
61 | has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); |
62 | has 'size' => ( is => 'rw', 'init_arg' => undef ); |
63 | } |
64 | |
65 | { |
5a0d4921 |
66 | |
0f795b43 |
67 | package ImmutableInitArg; |
68 | |
3e532dbe |
69 | use Moose; |
0f795b43 |
70 | use MooseX::StrictConstructor; |
71 | |
72 | has 'thing' => ( is => 'rw', 'init_arg' => 'other' ); |
73 | has 'size' => ( is => 'rw', 'init_arg' => undef ); |
74 | |
75 | no Moose; |
76 | __PACKAGE__->meta()->make_immutable(); |
77 | } |
78 | |
79 | { |
5a0d4921 |
80 | |
58812bf9 |
81 | package Immutable; |
82 | |
3e532dbe |
83 | use Moose; |
58812bf9 |
84 | use MooseX::StrictConstructor; |
85 | |
86 | has 'thing' => ( is => 'rw' ); |
87 | |
88 | no Moose; |
89 | __PACKAGE__->meta()->make_immutable(); |
90 | } |
91 | |
c001451a |
92 | { |
5a0d4921 |
93 | |
c001451a |
94 | package ImmutableTricky; |
95 | |
3e532dbe |
96 | use Moose; |
c001451a |
97 | use MooseX::StrictConstructor; |
98 | |
99 | has 'thing' => ( is => 'rw' ); |
100 | |
5a0d4921 |
101 | sub BUILD { |
c001451a |
102 | my $self = shift; |
103 | my $params = shift; |
104 | |
105 | delete $params->{spy}; |
106 | } |
5c3f24ed |
107 | |
108 | no Moose; |
109 | __PACKAGE__->meta()->make_immutable(); |
c001451a |
110 | } |
111 | |
32726d88 |
112 | eval { Standard->new( thing => 1, bad => 99 ) }; |
113 | is( $@, '', 'standard Moose class ignores unknown params' ); |
114 | |
115 | eval { Stricter->new( thing => 1, bad => 99 ) }; |
5a0d4921 |
116 | like( $@, qr/unknown attribute.+: bad/, |
117 | 'strict constructor blows up on unknown params' ); |
32726d88 |
118 | |
0f795b43 |
119 | eval { Subclass->new( thing => 1, size => 'large' ) }; |
120 | is( $@, '', 'subclass constructor handles known attributes correctly' ); |
121 | |
32726d88 |
122 | eval { Tricky->new( thing => 1, spy => 99 ) }; |
5a0d4921 |
123 | is( $@, '', |
124 | 'can work around strict constructor by deleting params in BUILD()' ); |
32726d88 |
125 | |
126 | eval { Tricky->new( thing => 1, agent => 99 ) }; |
5a0d4921 |
127 | like( $@, qr/unknown attribute.+: agent/, |
128 | 'Tricky still blows up on unknown params other than spy' ); |
2ffa7b60 |
129 | |
130 | eval { Subclass->new( thing => 1, bad => 99 ) }; |
5a0d4921 |
131 | like( $@, qr/unknown attribute.+: bad/, |
132 | 'subclass constructor blows up on unknown params' ); |
2ffa7b60 |
133 | |
0f795b43 |
134 | eval { InitArg->new( thing => 1 ) }; |
5a0d4921 |
135 | like( |
136 | $@, qr/unknown attribute.+: thing/, |
137 | 'InitArg blows up with attribute name' |
138 | ); |
0f795b43 |
139 | |
140 | eval { InitArg->new( size => 1 ) }; |
5a0d4921 |
141 | like( |
142 | $@, qr/unknown attribute.+: size/, |
143 | 'InitArg blows up when given attribute with undef init_arg' |
144 | ); |
0f795b43 |
145 | |
146 | eval { InitArg->new( other => 1 ) }; |
5a0d4921 |
147 | is( |
148 | $@, '', |
149 | 'InitArg works when given proper init_arg' |
150 | ); |
0f795b43 |
151 | |
152 | eval { ImmutableInitArg->new( thing => 1 ) }; |
5a0d4921 |
153 | like( |
154 | $@, qr/unknown attribute.+: thing/, |
155 | 'ImmutableInitArg blows up with attribute name' |
156 | ); |
0f795b43 |
157 | |
158 | eval { ImmutableInitArg->new( size => 1 ) }; |
5a0d4921 |
159 | like( |
160 | $@, qr/unknown attribute.+: size/, |
161 | 'ImmutableInitArg blows up when given attribute with undef init_arg' |
162 | ); |
0f795b43 |
163 | |
164 | eval { ImmutableInitArg->new( other => 1 ) }; |
5a0d4921 |
165 | is( |
166 | $@, '', |
167 | 'ImmutableInitArg works when given proper init_arg' |
168 | ); |
58812bf9 |
169 | |
170 | eval { Immutable->new( thing => 1, bad => 99 ) }; |
5a0d4921 |
171 | like( |
172 | $@, qr/unknown attribute.+: bad/, |
173 | 'strict constructor in immutable class blows up on unknown params' |
174 | ); |
c001451a |
175 | |
176 | eval { ImmutableTricky->new( thing => 1, spy => 99 ) }; |
5a0d4921 |
177 | is( |
178 | $@, '', |
179 | 'immutable class can work around strict constructor by deleting params in BUILD()' |
180 | ); |
c001451a |
181 | |
182 | eval { ImmutableTricky->new( thing => 1, agent => 99 ) }; |
5a0d4921 |
183 | like( |
184 | $@, qr/unknown attribute.+: agent/, |
185 | 'ImmutableTricky still blows up on unknown params other than spy' |
186 | ); |
54cc4bad |
187 | |
188 | done_testing(); |