Skip to content

Commit

Permalink
tests for _ForeignTypeConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyink committed Jan 31, 2019
1 parent 4189077 commit b370f16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
27 changes: 23 additions & 4 deletions t/20-unit/Types-TypeTiny/coercion.t
Expand Up @@ -4,7 +4,8 @@
=head1 PURPOSE
Test L<Types::TypeTiny::to_TypeTiny> pseudo-coercion.
Test L<Types::TypeTiny::to_TypeTiny> pseudo-coercion and the
L<Types::TypeTiny::_ForeignTypeConstraint> type.
=head1 DEPENDENCIES
Expand Down Expand Up @@ -46,6 +47,9 @@ subtest "Coercion from 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');
Expand All @@ -70,6 +74,9 @@ 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
{
Expand All @@ -85,7 +92,11 @@ subtest "Coercion from Mouse type constraint object" => sub

subtest "Coercion from predicate-like coderef" => sub
{
my $type = to_TypeTiny sub { $_[0] =~ /\A-?[0-9]+\z/ };
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
Expand All @@ -97,7 +108,11 @@ subtest "Coercion from predicate-like coderef" => sub

subtest "Coercion from assertion-like coderef" => sub
{
my $type = to_TypeTiny sub { $_[0] =~ /\A-?[0-9]+\z/ or die("not an integer") };
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
Expand All @@ -115,7 +130,11 @@ subtest "Coercion from assertion-like coderef" => sub
subtest "Coercion from Sub::Quote coderef" => sub
{
require Sub::Quote;
my $type = to_TypeTiny Sub::Quote::quote_sub(q{ $_[0] =~ /\A-?[0-9]+\z/ });
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');
Expand Down
8 changes: 6 additions & 2 deletions t/30-integration/Validation-Class-Simple/basic.t
Expand Up @@ -32,17 +32,21 @@ use Test::More;
use Test::Requires { "Validation::Class" => "7.900017" };
use Test::TypeTiny;

use Types::TypeTiny qw( to_TypeTiny );
use Types::TypeTiny qw( to_TypeTiny _ForeignTypeConstraint );
use Validation::Class::Simple;

my $type = to_TypeTiny "Validation::Class::Simple"->new(
my $orig = "Validation::Class::Simple"->new(
fields => {
name => { required => 1, pattern => qr{^\w+(\s\w+)*$}, filters => [qw/trim/] },
email => { required => 1 },
pass => { required => 1 },
pass2 => { required => 1, matches => 'pass' },
},
);
my $type = to_TypeTiny $orig;

should_pass($orig, _ForeignTypeConstraint);
should_fail($type, _ForeignTypeConstraint);

isa_ok($type, "Type::Tiny", 'can create a child type constraint from Validation::Class::Simple');

Expand Down

0 comments on commit b370f16

Please sign in to comment.