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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#! perl
# These tests were copied nearly verbatim from the Moosex::StrictConstructor
# distribution.
use strict;
use warnings;
use Test::Fatal;
use Test::More 0.88;
{
package Standard;
use Moo;
has 'thing' => ( is => 'rw' );
}
{
package Stricter;
use Moo;
use MooX::StrictConstructor;
has 'thing' => ( is => 'rw' );
}
{
package Subclass;
use Moo;
use MooX::StrictConstructor;
extends 'Stricter';
has 'size' => ( is => 'rw' );
}
{
package StrictSubclass;
use Moo;
extends 'Stricter';
has 'size' => ( is => 'rw' );
}
{
package OtherStrictSubclass;
use Moo;
use MooX::StrictConstructor;
extends 'Standard';
has 'size' => ( is => 'rw' );
}
{
package Tricky;
use Moo;
use MooX::StrictConstructor;
has 'thing' => ( is => 'rw' );
sub BUILD {
my $self = shift;
my $params = shift;
delete $params->{spy};
}
}
{
package TrickyBuildArgs;
use Moo;
use MooX::StrictConstructor;
has 'thing' => ( is => 'rw' );
sub BUILDARGS {
my ($self, %params) = @_;
delete $params{spy};
return \%params;
}
}
{
package InitArg;
use Moo;
use MooX::StrictConstructor;
has 'thing' => ( is => 'rw', 'init_arg' => 'other' );
has 'size' => ( is => 'rw', 'init_arg' => undef );
}
{
package InTrace;
sub be_strict {
Stricter->new( password => 'unredacted' )
}
}
is( exception { Standard->new( thing => 1, bad => 99 ) },
undef, 'standard Moo class ignores unknown params' );
like(
exception { Stricter->new( thing => 1, bad => 99 ) },
qr/unknown attribute.+: bad/,
'strict constructor blows up on unknown params'
);
is( exception { Subclass->new( thing => 1, size => 'large' ) },
undef, 'subclass constructor handles known attributes correctly' );
like(
exception { Subclass->new( thing => 1, bad => 99 ) },
qr/unknown attribute.+: bad/,
'subclass correctly recognizes bad attribute'
);
is( exception { StrictSubclass->new( thing => 1, size => 'large', ) },
undef,
q{subclass that doesn't use strict constructor handles known attributes correctly}
);
like(
exception { StrictSubclass->new( thing => 1, bad => 99 ) },
qr/unknown attribute.+: bad/,
q{subclass that doesn't use strict correctly recognizes bad attribute}
);
is( exception { OtherStrictSubclass->new( thing => 1, size => 'large', ) },
undef,
q{strict subclass from parent that doesn't use strict constructor handles known attributes correctly}
);
like(
exception { OtherStrictSubclass->new( thing => 1, bad => 99 ) },
qr/unknown attribute.+: bad/,
q{strict subclass from parent that doesn't use strict correctly recognizes bad attribute}
);
TODO: {
local $TODO = "Moose trick. Doesn't work 'cuz my code runs before object built.";
is( exception { Tricky->new( thing => 1, spy => 99 ) },
undef,
'can work around strict constructor by deleting params in BUILD()' );
}
is( exception { TrickyBuildArgs->new( thing => 1, spy => 99 ) },
undef,
'can work around strict constructor by deleting params in BUILDARGS()'
);
like(
exception { Tricky->new( thing => 1, agent => 99 ) },
qr/unknown attribute.+: agent/,
'Tricky still blows up on unknown params other than spy'
);
like(
exception { InitArg->new( thing => 1 ) },
qr/unknown attribute.+: thing/,
'InitArg blows up with attribute name'
);
like(
exception { InitArg->new( size => 1 ) },
qr/unknown attribute.+: size/,
'InitArg blows up when given attribute with undef init_arg'
);
is( exception { InitArg->new( other => 1 ) },
undef, 'InitArg works when given proper init_arg' );
unlike( exception{ InTrace->be_strict },
qr/unredacted/,
'Stack traces are not dumped by default'
);
done_testing();
|