6 use Test::More tests => 24;
10 use_ok('Mouse::Util::TypeConstraints');
11 use_ok('Mouse::Meta::TypeConstraint::Parameterized');
16 my $array_of_ints = Mouse::Meta::TypeConstraint::Parameterized->new(
17 name => 'ArrayRef[Int]',
18 parent => find_type_constraint('ArrayRef'),
19 type_parameter => find_type_constraint('Int'),
21 isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized');
22 isa_ok($array_of_ints, 'Mouse::Meta::TypeConstraint');
24 ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
25 ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
26 ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
28 ok(!$array_of_ints->check(1), '... 1 failed successfully');
29 ok(!$array_of_ints->check({}), '... {} failed successfully');
30 ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
34 my $hash_of_ints = Mouse::Meta::TypeConstraint::Parameterized->new(
35 name => 'HashRef[Int]',
36 parent => find_type_constraint('HashRef'),
37 type_parameter => find_type_constraint('Int'),
39 isa_ok($hash_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized');
40 isa_ok($hash_of_ints, 'Mouse::Meta::TypeConstraint');
42 ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
43 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
44 ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
46 ok(!$hash_of_ints->check(1), '... 1 failed successfully');
47 ok(!$hash_of_ints->check([]), '... [] failed successfully');
48 ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
50 # Array of Array of Ints
52 my $array_of_array_of_ints = Mouse::Meta::TypeConstraint::Parameterized->new(
53 name => 'ArrayRef[ArrayRef[Int]]',
54 parent => find_type_constraint('ArrayRef'),
55 type_parameter => $array_of_ints,
57 isa_ok($array_of_array_of_ints, 'Mouse::Meta::TypeConstraint::Parameterized');
58 isa_ok($array_of_array_of_ints, 'Mouse::Meta::TypeConstraint');
60 ok($array_of_array_of_ints->check(
61 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
62 ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
63 ok(!$array_of_array_of_ints->check(
64 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
65 ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
68 my $anon_type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
69 isa_ok( $anon_type, 'Mouse::Meta::TypeConstraint::Parameterized' );
71 my $param_type = $anon_type->type_parameter;
72 isa_ok( $param_type, 'Mouse::Meta::TypeConstraint::Class' );