Fix typo in test description
[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 BEGIN {
9     use_ok('Moose::Util::TypeConstraints');
10     use_ok('Moose::Meta::TypeConstraint::Parameterized');
11 }
12
13 # Array of Ints
14
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'),
19 );
20 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
21 isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
22
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');
26
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');
30
31 # Hash of Ints
32
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'),
37 );
38 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
39 isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
40
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');
44
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');
48
49 # Array of Array of Ints
50
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,
55 );
56 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
57 isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
58
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');
65
66 {
67     my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
68     isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
69
70     my $param_type = $anon_type->type_parameter;
71     isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );
72 }
73
74 done_testing;