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