9 use_ok('Moose::Util::TypeConstraints');
10 use_ok('Moose::Meta::TypeConstraint::Parameterized');
15 my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
16 name => 'ArrayRef[Int]',
17 parent => find_type_constraint('ArrayRef'),
18 type_parameter => find_type_constraint('Int'),
20 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
21 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
23 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
24 ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
25 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
27 ok(!$array_of_ints->check(1), '... 1 failed successfully');
28 ok(!$array_of_ints->check({}), '... {} failed successfully');
29 ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
33 my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
34 name => 'HashRef[Int]',
35 parent => find_type_constraint('HashRef'),
36 type_parameter => find_type_constraint('Int'),
38 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
39 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
41 ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
42 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
43 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
45 ok(!$hash_of_ints->check(1), '... 1 failed successfully');
46 ok(!$hash_of_ints->check([]), '... [] failed successfully');
47 ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
49 # Array of Array of Ints
51 my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
52 name => 'ArrayRef[ArrayRef[Int]]',
53 parent => find_type_constraint('ArrayRef'),
54 type_parameter => $array_of_ints,
56 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
57 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
59 ok($array_of_array_of_ints->check(
60 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
61 ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
62 ok(!$array_of_array_of_ints->check(
63 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
64 ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
67 my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
68 isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
70 my $param_type = $anon_type->type_parameter;
71 isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );