8 use Moose::Util::TypeConstraints;
9 use Moose::Meta::TypeConstraint::Parameterized;
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'),
18 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
19 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
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');
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');
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'),
36 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
37 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
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');
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');
47 # Array of Array of Ints
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,
54 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
55 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
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');
65 my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
66 isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
68 my $param_type = $anon_type->type_parameter;
69 isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );