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