Sort list of bad params for consistency
[gitmo/MooseX-StrictConstructor.git] / t / basic.t
CommitLineData
32726d88 1use strict;
2use warnings;
3
2ffa7b60 4use Test::More tests => 6;
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
49
50eval { Standard->new( thing => 1, bad => 99 ) };
51is( $@, '', 'standard Moose class ignores unknown params' );
52
53eval { Stricter->new( thing => 1, bad => 99 ) };
54like( $@, qr/unknown attribute.+: bad/, 'strict constructor blows up on unknown params' );
55
56eval { Tricky->new( thing => 1, spy => 99 ) };
57is( $@, '', 'can work around strict constructor by deleting params in BUILD()' );
58
59eval { Tricky->new( thing => 1, agent => 99 ) };
60like( $@, qr/unknown attribute.+: agent/, 'Tricky still blows up on unknown params other than spy' );
2ffa7b60 61
62eval { Subclass->new( thing => 1, bad => 99 ) };
63like( $@, qr/unknown attribute.+: bad/, 'subclass constructor blows up on unknown params' );
64
65eval { Subclass->new( thing => 1, size => 'large' ) };
66is( $@, '', 'subclass constructor handles known attributes correctly' );