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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
=pod
=encoding utf-8
=head1 PURPOSE
Check type constraints and coercions work with L<Moose> native attibute
traits.
=head1 DEPENDENCIES
Test is skipped if Moose 2.1210 is not available.
(The feature should work in older versions of Moose, but older versions
of Test::Moose conflict with newer versions of Test::Builder.)
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
use Test::More;
use Test::Requires { Moose => '2.1210' };
use Test::Fatal;
use Test::TypeTiny qw( matchfor );
use Test::Moose qw( with_immutable );
use Types::Standard -types;
# For testing Array trait
{
package MyCollection;
use Moose;
use Types::Standard qw( ArrayRef Object );
has things => (
is => 'ro',
isa => ArrayRef[ Object ],
traits => [ 'Array' ],
handles => { add => 'push' },
);
}
# for testing Hash trait
my %attributes = (
hashref => HashRef,
hashref_int => HashRef[Int],
map => Map,
map_strint => Map[Str, Int],
);
{
package MyHashes;
use Moose;
while (my ($attr, $type) = each %attributes)
{
has $attr => (
traits => ['Hash'],
is => 'ro',
isa => $type,
handles => {
"$attr\_get" => 'get',
"$attr\_set" => 'set',
"$attr\_has" => 'exists',
},
default => sub { +{} },
);
}
}
# For testing coercions
{
package Mini::Milk;
use Moose;
use Types::Standard qw( Int InstanceOf );
has i => (is => 'ro', isa => Int);
around BUILDARGS => sub {
my $next = shift;
my $class = shift;
return { i => $_[0] }
if @_==1 and not ref $_[0];
$class->$next(@_);
}
}
my $minimilk = InstanceOf->of('Mini::Milk')->plus_constructors(Num, "new");
{
package MyCollection2;
use Moose;
use Types::Standard qw( ArrayRef );
has things => (
is => 'ro',
isa => ArrayRef[ $minimilk ],
traits => [ 'Array' ],
handles => { add => 'push' },
coerce => 1,
);
}
{
package MyCollection3;
use Moose;
use Types::Standard qw( ArrayRef );
has things => (
is => 'ro',
isa => (ArrayRef[ $minimilk ])->create_child_type(coercion => 1),
traits => [ 'Array' ],
handles => { add => 'push' },
coerce => 1,
);
}
{
package MyHashes2;
use Moose;
use Types::Standard qw( HashRef Map Int );
has hash => (
traits => ['Hash'],
is => 'ro',
isa => HashRef[ $minimilk ],
coerce => 1,
handles => {
"hash_get" => 'get',
"hash_set" => 'set',
},
default => sub { +{} },
);
has 'map' => (
traits => ['Hash'],
is => 'ro',
isa => Map[ Int, $minimilk ],
coerce => 1,
handles => {
"map_get" => 'get',
"map_set" => 'set',
},
default => sub { +{} },
);
}
{
package MyHashes3;
use Moose;
use Types::Standard qw( HashRef Map Int );
has hash => (
traits => ['Hash'],
is => 'ro',
isa => (HashRef[ $minimilk ])->create_child_type(coercion => 1),
coerce => 1,
handles => {
"hash_get" => 'get',
"hash_set" => 'set',
},
default => sub { +{} },
);
has 'map' => (
traits => ['Hash'],
is => 'ro',
isa => (Map[ Int, $minimilk ])->create_child_type(coercion => 1),
coerce => 1,
handles => {
"map_get" => 'get',
"map_set" => 'set',
},
default => sub { +{} },
);
}
WEIRD_ERROR: {
my $c = MyCollection3
->meta
->get_attribute('things')
->type_constraint
->coercion
->compiled_coercion;
my $input = [ Mini::Milk->new(0), 1, 2, 3 ];
my $output = $c->($input);
my $expected = [ map Mini::Milk->new($_), 0..3 ];
is_deeply($output, $expected)
or diag( B::Deparse->new->coderef2text($c) );
}
my $i = 0;
with_immutable
{
note($i++ ? "MUTABLE" : "IMMUTABLE");
subtest "Array trait with type ArrayRef[Object]" => sub
{
my $coll = MyCollection->new(things => []);
ok(
!exception { $coll->add(bless {}, "Monkey") },
'pushing ok value',
);
is(
exception { $coll->add({})},
matchfor(
'Moose::Exception::ValidationFailedForInlineTypeConstraint',
qr{^A new member value for things does not pass its type constraint because:},
),
'pushing not ok value',
);
};
my %subtests = (
MyCollection2 => "Array trait with type ArrayRef[InstanceOf] and coercion",
MyCollection3 => "Array trait with type ArrayRef[InstanceOf] and coercion and subtyping",
);
for my $class (sort keys %subtests)
{
subtest $subtests{$class} => sub
{
my $coll = $class->new(things => []);
is(
exception {
$coll->add( 'Mini::Milk'->new(i => 0) );
$coll->add(1);
$coll->add(2);
$coll->add(3);
},
undef,
'pushing ok values',
);
my $things = $coll->things;
for my $i (0 .. 3)
{
isa_ok($things->[$i], 'Mini::Milk', "\$things->[$i]");
is($things->[$i]->i, $i, "\$things->[$i]->i == $i");
}
};
}
for my $attr (sort keys %attributes)
{
my $type = $attributes{$attr};
my $getter = "$attr\_get";
my $setter = "$attr\_set";
my $predicate = "$attr\_has";
subtest "Hash trait with type $type" => sub
{
my $obj = MyHashes->new;
is_deeply($obj->$attr, {}, 'default empty hash');
$obj->$setter(foo => 666);
$obj->$setter(bar => 999);
is($obj->$getter('foo'), 666, 'getter');
is($obj->$getter('bar'), 999, 'getter');
$obj->$setter(bar => 42);
is($obj->$getter('bar'), 42, 'setter');
ok($obj->$predicate('foo'), 'predicate');
ok($obj->$predicate('bar'), 'predicate');
ok(!$obj->$predicate('baz'), 'predicate - negatory');
is_deeply($obj->$attr, { foo => 666, bar => 42 }, 'correct hash');
like(
exception { $obj->$setter(baz => 3.141592) },
qr/type constraint/,
'cannot add non-Int value',
) if $attr =~ /int$/;
done_testing;
};
}
%subtests = (
MyHashes2 => "Hash trait with types HashRef[InstanceOf] and Map[Int,InstanceOf]; and coercion",
MyHashes3 => "Hash trait with types HashRef[InstanceOf] and Map[Int,InstanceOf]; and coercion and subtyping",
);
for my $class (sort keys %subtests)
{
subtest $subtests{$class} => sub
{
my $H = $class->new();
is(
exception {
$H->hash_set( 0, 'Mini::Milk'->new(i => 0) );
$H->hash_set( 1, 1 );
$H->hash_set( 2, 2 );
$H->hash_set( 3, 3 );
},
undef,
'adding ok values to HashRef',
);
is(
exception {
$H->map_set( 4, 'Mini::Milk'->new(i => 4) );
$H->map_set( 5, 5 );
$H->map_set( 6, 6 );
$H->map_set( 7, 7 );
},
undef,
'adding ok values to Map',
);
my $h = $H->hash;
for my $i (0 .. 3)
{
isa_ok($h->{$i}, 'Mini::Milk', "\$h->{$i}");
is($h->{$i}->i, $i, "\$h->{$i}->i == .$i");
}
my $m = $H->map;
for my $i (4 .. 7)
{
isa_ok($m->{$i}, 'Mini::Milk', "\$m->{$i}");
is($m->{$i}->i, $i, "\$m->{$i}->i == .$i");
}
};
}
} qw(
MyCollection
MyCollection2
MyCollection3
MyHashes
Mini::Milk
);
done_testing;
|