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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
=pod
=encoding utf-8
=head1 PURPOSE
Test L<Types::TypeTiny::to_TypeTiny> pseudo-coercion and the
L<Types::TypeTiny::_ForeignTypeConstraint> type.
=head1 DEPENDENCIES
This test requires L<Moose> 2.0000, L<Mouse> 1.00, and L<Moo> 1.000000.
Otherwise, it is skipped.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 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 strict;
use warnings;
use lib qw( ./lib ./t/lib ../inc ./inc );
# Test::Requires calls ->import on Moose/Mouse, so be sure
# to import them into dummy packages.
{ package XXX; use Test::Requires { Moose => '2.0000' } };
{ package YYY; use Test::Requires { Mouse => '1.00' } };
{ package ZZZ; use Test::Requires { Moo => '1.000000' } };
use Test::More;
use Test::TypeTiny -all;
use Types::TypeTiny -all;
use Types::Standard qw(Int);
use Moose::Util::TypeConstraints qw(find_type_constraint);
ok(TypeTiny->has_coercion, "TypeTiny->has_coercion");
subtest "Coercion from built-in Moose type constraint object" => sub
{
my $orig = find_type_constraint("Int");
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted a Moose type constraint to a Type::Tiny one');
is($type->name, 'Int', '... which has the correct name');
ok($type->can_be_inlined, '... and which can be inlined');
note $type->inline_check('$X');
subtest "... and it works" => sub
{
should_pass(123, $type);
should_fail(3.3, $type);
};
# This doesn't provide the same message because Type::Tiny isn't
# really coercing a Moose type constraint, it's just grabbing `Int`
# from Types::Standard.
#
# is(
# $type->get_message(3.3),
# $orig->get_message(3.3),
# '... and provides proper message',
# );
};
subtest "Coercion from custom Moose type constraint object" => sub
{
my $orig = 'Moose::Meta::TypeConstraint'->new(
name => 'EvenInt',
parent => find_type_constraint("Int"),
constraint => sub {
my ( $value ) = @_;
$value % 2 == 0;
},
inlined => sub {
my ( $self, $var ) = @_;
return sprintf(
'do { %s } && !( %s %% 2 )',
$self->parent->_inline_check( $var ),
$var,
);
},
message => sub {
my ( $value ) = @_;
return find_type_constraint("Int")->check( $value )
? "$value isn't an integer at all"
: "$value is odd";
},
);
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted a Moose type constraint to a Type::Tiny one');
is($type->display_name, 'EvenInt', '... which has the correct display_name');
ok($type->can_be_inlined, '... and which can be inlined');
note $type->inline_check('$X');
subtest "... and it works" => sub
{
should_fail(3.3, $type);
should_fail(123, $type);
should_pass(124, $type);
};
is(
$type->get_message(3.3),
$orig->get_message(3.3),
'... and provides proper message',
);
};
my %moose_ptype_opts = (
name => 'ArrayOrHashRef',
parent => find_type_constraint('Ref'),
constraint => sub {
my $value = @_ ? pop : $_;
ref($value) eq 'HASH' or ref($value) eq 'ARRAY';
},
constraint_generator => sub {
my $param = shift;
return sub {
my $value = @_ ? pop : $_;
if (ref($value) eq 'ARRAY') {
($param->check($_) or return) for @$value;
return 1;
}
elsif (ref($value) eq 'HASH') {
($param->check($_) or return) for values %$value;
return 1;
}
return;
};
},
);
my $ptype_tests = sub {
my $moose = Moose::Meta::TypeConstraint::Parameterizable->new(%moose_ptype_opts);
# wow, the Moose API is stupid; need to do this
Moose::Util::TypeConstraints::register_type_constraint($moose);
Moose::Util::TypeConstraints::add_parameterizable_type($moose);
note "Moose native type, no parameters";
ok( $moose->check([]) );
ok( $moose->check({}) );
ok( $moose->check([1..10]) );
ok( $moose->check({foo => 1, bar => 2}) );
ok( $moose->check(['hello world']) );
ok( ! $moose->check(\1) );
ok( ! $moose->check(42) );
note "Moose native type, parameterized with Moose type";
my $moose_with_moose = $moose->parameterize( find_type_constraint('Int') );
ok( $moose_with_moose->check([]) );
ok( $moose_with_moose->check({}) );
ok( $moose_with_moose->check([1..10]) );
ok( $moose_with_moose->check({foo => 1, bar => 2}) );
ok( ! $moose_with_moose->check(['hello world']) );
ok( ! $moose_with_moose->check(\1) );
ok( ! $moose_with_moose->check(42) );
note "Moose native type, parameterized with TT type";
my $moose_with_tt = $moose->parameterize( Int );
ok( $moose_with_tt->check([]) );
ok( $moose_with_tt->check({}) );
ok( $moose_with_tt->check([1..10]) );
ok( $moose_with_tt->check({foo => 1, bar => 2}) );
ok( ! $moose_with_tt->check(['hello world']) );
ok( ! $moose_with_tt->check(\1) );
ok( ! $moose_with_tt->check(42) );
note 'TT type, no parameters';
my $tt = Types::TypeTiny::to_TypeTiny($moose);
isa_ok($tt, 'Type::Tiny');
is($tt->display_name, $moose_ptype_opts{name});
should_pass([], $tt);
should_pass({}, $tt);
should_pass([1..10], $tt);
should_pass({foo => 1, bar => 2}, $tt);
should_pass(['hello world'], $tt);
should_fail(\1, $tt);
should_fail(42, $tt);
note 'TT type, parameterized with Moose type';
my $tt_with_moose = $tt->of( find_type_constraint('Int') );
should_pass([], $tt_with_moose);
should_pass({}, $tt_with_moose);
should_pass([1..10], $tt_with_moose);
should_pass({foo => 1, bar => 2}, $tt_with_moose);
should_fail(['hello world'], $tt_with_moose);
should_fail(\1, $tt_with_moose);
should_fail(42, $tt_with_moose);
note 'TT type, parameterized with TT type';
my $tt_with_tt = $tt->of( Int );
should_pass([], $tt_with_tt);
should_pass({}, $tt_with_tt);
should_pass([1..10], $tt_with_tt);
should_pass({foo => 1, bar => 2}, $tt_with_tt);
should_fail(['hello world'], $tt_with_tt);
should_fail(\1, $tt_with_tt);
should_fail(42, $tt_with_tt);
return (
$moose,
$moose_with_moose,
$moose_with_tt,
$tt,
$tt_with_moose,
$tt_with_tt,
);
};
subtest "Coercion from Moose parameterizable type constraint object" => sub {
$ptype_tests->();
};
# Moose cannot handle two parameterizable types sharing a name
$moose_ptype_opts{name} .= '2';
$moose_ptype_opts{inlined} = sub {
my $var = pop;
sprintf('ref(%s) =~ /^(HASH|ARRAY)$/', $var);
};
$moose_ptype_opts{inline_generator} = sub {
my ($base, $param, $var) = @_;
my $code = sprintf qq{do{
if (ref($var) eq 'ARRAY') {
my \$okay = 1;
(%s or ((\$okay=0), last)) for \@{$var};
\$okay;
}
elsif (ref($var) eq 'HASH') {
my \$okay = 1;
(%s or ((\$okay=0), last)) for values %%{$var};
\$okay;
}
else {
0;
}
}}, ($param->_inline_check('$_')) x 2;
$code;
};
subtest "Coercion from Moose parameterizable type constraint object with inlining" => sub
{
my @types = $ptype_tests->();
note 'check everything can be inlined';
for my $type (@types) {
ok( $type->can_be_inlined );
ok( length($type->_inline_check('$xxx')) );
}
note( $types[-1]->inline_check('$VALUE') );
};
subtest "Coercion from Moose enum type constraint" => sub {
my $moose = Moose::Util::TypeConstraints::enum(Foo => [qw/ foo bar baz /]);
ok( $moose->check("foo") );
ok( ! $moose->check("quux") );
ok( ! $moose->check(\1) );
ok( ! $moose->check(undef) );
my $tt = Types::TypeTiny::to_TypeTiny($moose);
ok( $tt->check("foo") );
ok( ! $tt->check("quux") );
ok( ! $tt->check(\1) );
ok( ! $tt->check(undef) );
isa_ok($tt, 'Type::Tiny::Enum');
is_deeply($tt->values, $moose->values);
ok $tt->can_be_inlined;
note( $tt->inline_check('$STR') );
};
subtest "Coercion from Moose class type constraint" => sub {
my $moose = Moose::Util::TypeConstraints::class_type(FooObj => { class => 'MyApp::Foo' });
my $tt = Types::TypeTiny::to_TypeTiny($moose);
isa_ok($tt, 'Type::Tiny::Class');
is($tt->class, $moose->class);
ok $tt->can_be_inlined;
note( $tt->inline_check('$OBJECT') );
};
subtest "Coercion from Moose role type constraint" => sub {
my $moose = Moose::Util::TypeConstraints::role_type(DoesFoo => { role => 'MyApp::Foo' });
my $tt = Types::TypeTiny::to_TypeTiny($moose);
isa_ok($tt, 'Type::Tiny::Role');
is($tt->role, $moose->role);
ok $tt->can_be_inlined;
note( $tt->inline_check('$OBJECT') );
};
subtest "Coercion from Moose duck type constraint" => sub {
my $moose = Moose::Util::TypeConstraints::duck_type(FooInterface => [qw/foo bar baz/]);
my $tt = Types::TypeTiny::to_TypeTiny($moose);
isa_ok($tt, 'Type::Tiny::Duck');
is_deeply([ sort @{$tt->methods} ], [ sort @{$moose->methods} ]);
ok $tt->can_be_inlined;
note( $tt->inline_check('$OBJECT') );
};
subtest "Coercion from Moose union type constraint" => sub {
my $moose = Moose::Util::TypeConstraints::union(
'ContainerThang',
[ find_type_constraint('ArrayRef'), find_type_constraint('HashRef'), ]
);
my $tt = Types::TypeTiny::to_TypeTiny($moose);
is($tt->display_name, 'ContainerThang');
isa_ok($tt, 'Type::Tiny::Union');
ok($tt->[0] == Types::Standard::ArrayRef);
ok($tt->[1] == Types::Standard::HashRef);
ok $tt->can_be_inlined;
note( $tt->inline_check('$REF') );
};
subtest "Coercion from Mouse type constraint object" => sub
{
my $orig = Mouse::Util::TypeConstraints::find_type_constraint("Int");
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted a Mouse type constraint to a Type::Tiny one');
subtest "... and it works" => sub
{
should_pass(123, $type);
should_fail(3.3, $type);
};
is(
$type->get_message(3.3),
$orig->get_message(3.3),
'... and provides proper message',
);
};
subtest "Coercion from predicate-like coderef" => sub
{
my $orig = sub { $_[0] =~ /\A-?[0-9]+\z/ };
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted the coderef to a Type::Tiny object');
subtest "... and it works" => sub
{
should_pass(123, $type);
should_fail(3.3, $type);
};
};
subtest "Coercion from assertion-like coderef" => sub
{
my $orig = sub { $_[0] =~ /\A-?[0-9]+\z/ or die("not an integer") };
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted the coderef to a Type::Tiny object');
subtest "... and it works" => sub
{
should_pass(123, $type);
should_fail(3.3, $type);
};
like(
$type->validate(3.3),
qr/\Anot an integer/,
'... and provides proper message',
);
};
subtest "Coercion from Sub::Quote coderef" => sub
{
require Sub::Quote;
my $orig = Sub::Quote::quote_sub(q{ $_[0] =~ /\A-?[0-9]+\z/ });
my $type = to_TypeTiny $orig;
should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);
should_pass($type, TypeTiny, 'to_TypeTiny converted the coderef to a Type::Tiny object');
ok($type->can_be_inlined, '... which can be inlined');
note $type->inline_check('$X');
subtest "... and it works" => sub
{
should_pass(123, $type);
should_fail(3.3, $type);
};
};
done_testing;
|