also test a class whose parent is strict
[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;
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 Tricky;
48
49     use Moose;
50     use MooseX::StrictConstructor;
51
52     has 'thing' => ( is => 'rw' );
53
54     sub BUILD {
55         my $self   = shift;
56         my $params = shift;
57
58         delete $params->{spy};
59     }
60 }
61
62 {
63     package InitArg;
64
65     use Moose;
66     use MooseX::StrictConstructor;
67
68     has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
69     has 'size'  => ( is => 'rw', 'init_arg' => undef );
70 }
71
72 my @classes = qw( Standard Stricter Subclass StrictSubclass Tricky InitArg );
73
74 with_immutable {
75     is(
76         exception { Standard->new( thing => 1, bad => 99 ) }, undef,
77         'standard Moose class ignores unknown params'
78     );
79
80     like(
81         exception { Stricter->new( thing => 1, bad => 99 ) },
82         qr/unknown attribute.+: bad/,
83         'strict constructor blows up on unknown params'
84     );
85
86     is(
87         exception { Subclass->new( thing => 1, size => 'large' ) }, undef,
88         'subclass constructor handles known attributes correctly'
89     );
90
91     like(
92         exception { Subclass->new( thing => 1, bad => 99 ) },
93         qr/unknown attribute.+: bad/,
94         'subclass correctly recognizes bad attribute'
95     );
96
97     is(
98         exception { StrictSubclass->new( thing => 1, size => 'large', ) }, undef,
99         'subclass that doesn\'t use strict constructor handles known attributes correctly'
100     );
101
102     like(
103         exception { StrictSubclass->new( thing => 1, bad => 99 ) },
104         qr/unknown attribute.+: bad/,
105         'subclass that doesn\'t use strict correctly recognizes bad attribute'
106     );
107
108     is(
109         exception { Tricky->new( thing => 1, spy => 99 ) }, undef,
110         'can work around strict constructor by deleting params in BUILD()'
111     );
112
113     like(
114         exception { Tricky->new( thing => 1, agent => 99 ) },
115         qr/unknown attribute.+: agent/,
116         'Tricky still blows up on unknown params other than spy'
117     );
118
119     like(
120         exception { InitArg->new( thing => 1 ) },
121         qr/unknown attribute.+: thing/,
122         'InitArg blows up with attribute name'
123     );
124
125     like(
126         exception { InitArg->new( size => 1 ) },
127         qr/unknown attribute.+: size/,
128         'InitArg blows up when given attribute with undef init_arg'
129     );
130
131     is(
132         exception { InitArg->new( other => 1 ) }, undef,
133         'InitArg works when given proper init_arg'
134     );
135 }
136 @classes;
137
138 done_testing();