stop using excludes within moose, since it's no longer necessary
[gitmo/Moose.git] / t / type_constraints / container_type_constraint.t
CommitLineData
d67145ed 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
d67145ed 7
28fdde7f 8use Moose::Util::TypeConstraints;
9use Moose::Meta::TypeConstraint::Parameterized;
d67145ed 10
11# Array of Ints
12
0fbd4b0a 13my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 14 name => 'ArrayRef[Int]',
15 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 16 type_parameter => find_type_constraint('Int'),
d67145ed 17);
0fbd4b0a 18isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 19isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
20
21ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
22ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully');
23ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully');
24
25ok(!$array_of_ints->check(1), '... 1 failed successfully');
26ok(!$array_of_ints->check({}), '... {} failed successfully');
27ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully');
28
29# Hash of Ints
30
0fbd4b0a 31my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 32 name => 'HashRef[Int]',
33 parent => find_type_constraint('HashRef'),
0fbd4b0a 34 type_parameter => find_type_constraint('Int'),
d67145ed 35);
0fbd4b0a 36isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 37isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
38
39ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
40ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully');
41ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully');
42
43ok(!$hash_of_ints->check(1), '... 1 failed successfully');
44ok(!$hash_of_ints->check([]), '... [] failed successfully');
45ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully');
46
47# Array of Array of Ints
48
0fbd4b0a 49my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
d67145ed 50 name => 'ArrayRef[ArrayRef[Int]]',
51 parent => find_type_constraint('ArrayRef'),
0fbd4b0a 52 type_parameter => $array_of_ints,
d67145ed 53);
0fbd4b0a 54isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
d67145ed 55isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
56
57ok($array_of_array_of_ints->check(
58 [[ 1, 2, 3 ], [ 4, 5, 6 ]]
59), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully');
60ok(!$array_of_array_of_ints->check(
61 [[ 1, 2, 3 ], [ qw/foo bar/ ]]
62), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully');
63
2b6c2a5c 64{
65 my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]');
66 isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' );
54718169 67
2b6c2a5c 68 my $param_type = $anon_type->type_parameter;
69 isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' );
70}
a28e50e4 71
72done_testing;