s/container/parameterized/
[gitmo/Moose.git] / t / 040_type_constraints / 011_container_type_constraint.t
CommitLineData
d67145ed 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
b17a3035 6use Test::More tests => 22;
d67145ed 7use Test::Exception;
8
b17a3035 9BEGIN {
d67145ed 10 use_ok('Moose::Util::TypeConstraints');
0fbd4b0a 11 use_ok('Moose::Meta::TypeConstraint::Parameterized');
d67145ed 12}
13
14# Array of Ints
15
0fbd4b0a 16my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 17 name => 'ArrayRef[Int]',
18 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 19 type_parameter => find_type_constraint('Int'),
d67145ed 20);
0fbd4b0a 21isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 22isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
23
24ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
25ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
26ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
27
28ok(!$array_of_ints->check(1), '... 1 failed successfully');
29ok(!$array_of_ints->check({}), '... {} failed successfully');
30ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
31
32# Hash of Ints
33
0fbd4b0a 34my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 35 name => 'HashRef[Int]',
36 parent => find_type_constraint('HashRef'),
0fbd4b0a 37 type_parameter => find_type_constraint('Int'),
d67145ed 38);
0fbd4b0a 39isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 40isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
41
42ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
43ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
44ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
45
46ok(!$hash_of_ints->check(1), '... 1 failed successfully');
47ok(!$hash_of_ints->check([]), '... [] failed successfully');
48ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
49
50# Array of Array of Ints
51
0fbd4b0a 52my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 53 name => 'ArrayRef[ArrayRef[Int]]',
54 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 55 type_parameter => $array_of_ints,
d67145ed 56);
0fbd4b0a 57isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 58isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
59
60ok($array_of_array_of_ints->check(
61 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
62), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
63ok(!$array_of_array_of_ints->check(
64 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
65), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
66