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