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