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