look at init_arg, not attribute name
[gitmo/MooseX-StrictConstructor.git] / t / basic.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 12;
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 {
24     package Subclass;
25
26     use MooseX::StrictConstructor;
27
28     extends 'Stricter';
29
30     has 'size' => ( is => 'rw' );
31 }
32
33 {
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
49 {
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 {
59     package Immutable;
60
61     use MooseX::StrictConstructor;
62
63     has 'thing' => ( is => 'rw' );
64
65     no Moose;
66     __PACKAGE__->meta()->make_immutable();
67 }
68
69 {
70     package ImmutableTricky;
71
72     use MooseX::StrictConstructor;
73
74     has 'thing' => ( is => 'rw' );
75
76     sub BUILD
77     {
78         my $self   = shift;
79         my $params = shift;
80
81         delete $params->{spy};
82     }
83
84     no Moose;
85     __PACKAGE__->meta()->make_immutable();
86 }
87
88
89 eval { Standard->new( thing => 1, bad => 99 ) };
90 is( $@, '', 'standard Moose class ignores unknown params' );
91
92 eval { Stricter->new( thing => 1, bad => 99 ) };
93 like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
94
95 eval { Tricky->new( thing => 1, spy => 99 ) };
96 is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
97
98 eval { Tricky->new( thing => 1, agent => 99 ) };
99 like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
100
101 eval { Subclass->new( thing => 1, bad => 99 ) };
102 like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
103
104 eval { Subclass->new( thing => 1, size => 'large' ) };
105 is( $@, '', 'subclass constructor handles known attributes correctly' );
106
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' );
110
111 eval { ImmutableTricky->new( thing => 1, spy => 99 ) };
112 is( $@, '',
113     'immutable class can work around strict constructor by deleting params in BUILD()' );
114
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' );
118
119 eval { InitArg->new( thing => 1 ) };
120 like( $@, qr/unknown attribute.+: thing/,
121       'InitArg blows up with attribute name' );
122
123 eval { InitArg->new( size => 1 ) };
124 like( $@, qr/unknown attribute.+: size/,
125       'InitArg blows up when given attribute with undef init_arg' );
126
127 eval { InitArg->new( other => 1 ) };
128 is( $@, '',
129     'InitArg works when given proper init_arg' );