From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

=pod
=encoding utf-8
=head1 PURPOSE
Check type constraints work with L<Mouse>. Checks values that should pass
and should fail; checks error messages.
=head1 DEPENDENCIES
Uses the bundled BiggerLib.pm type library.
Test is skipped if Mouse 1.00 is not available.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2013-2014, 2017-2025 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 lib qw( ./lib ./t/lib ../inc ./inc );
use Test::Requires { Mouse => 1.00 };
{
package Local::Class;
use Mouse;
use BiggerLib -all;
has small => (is => "ro", isa => SmallInteger);
has big => (is => "ro", isa => BigInteger);
}
is(
exception { "Local::Class"->new(small => 9, big => 12) },
undef,
"some values that should pass their type constraint",
);
isnt(
exception { "Local::Class"->new(small => 100) },
undef,
"direct violation of type constraint",
);
isnt(
exception { "Local::Class"->new(small => 5.5) },
undef,
"violation of parent type constraint",
);
isnt(
exception { "Local::Class"->new(small => "five point five") },
undef,
"violation of grandparent type constraint",
);
isnt(
exception { "Local::Class"->new(small => []) },
undef,
"violation of great-grandparent type constraint",
);
ok(
Mouse::Util::is_a_type_constraint(BiggerLib::SmallInteger),
"Mouse::Util::is_a_type_constraint accepts Type::Tiny type constraints",
);
note "Coercion...";
{
package TmpNS1;
subtype 'MyInt', as 'Int';
coerce 'MyInt', from 'ArrayRef', via { scalar(@$_) };
my $type = Types::TypeTiny::to_TypeTiny(find_type_constraint('MyInt'));
::ok($type->has_coercion, 'types converted from Mouse retain coercions');
::is($type->coerce([qw/a b c/]), 3, '... which work');
}
done_testing;