Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / type_constraints / container_type_constraint.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Moose::Util::TypeConstraints;
9 use Moose::Meta::TypeConstraint::Parameterized;
10
11 # Array of Ints
12
13 my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
14     name           => 'ArrayRef[Int]',
15     parent         => find_type_constraint('ArrayRef'),
16     type_parameter => find_type_constraint('Int'),
17 );
18 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
19 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
20
21 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
22 ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
23 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
24
25 ok(!$array_of_ints->check(1), '... 1 failed successfully');
26 ok(!$array_of_ints->check({}), '... {} failed successfully');
27 ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
28
29 # Hash of Ints
30
31 my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
32     name           => 'HashRef[Int]',
33     parent         => find_type_constraint('HashRef'),
34     type_parameter => find_type_constraint('Int'),
35 );
36 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
37 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
38
39 ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
40 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
41 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
42
43 ok(!$hash_of_ints->check(1), '... 1 failed successfully');
44 ok(!$hash_of_ints->check([]), '... [] failed successfully');
45 ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
46
47 # Array of Array of Ints
48
49 my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
50     name           => 'ArrayRef[ArrayRef[Int]]',
51     parent         => find_type_constraint('ArrayRef'),
52     type_parameter => $array_of_ints,
53 );
54 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
55 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
56
57 ok($array_of_array_of_ints->check(
58     [[ 1, 2, 3 ], [ 4, 5, 6 ]]
59 ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
60 ok(!$array_of_array_of_ints->check(
61     [[ 1, 2, 3 ], [ qw/foo bar/ ]]
62 ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
63
64 {
65     my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
66     isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
67
68     my $param_type = $anon_type->type_parameter;
69     isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );
70 }
71
72 done_testing;