Commit | Line | Data |
d67145ed |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More no_plan => 1; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | use_ok('Moose::Util::TypeConstraints'); |
12 | use_ok('Moose::Meta::TypeConstraint::Container'); |
13 | } |
14 | |
15 | # Array of Ints |
16 | |
17 | my $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 | ); |
22 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Container'); |
23 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint'); |
24 | |
25 | ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully'); |
26 | ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully'); |
27 | ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully'); |
28 | |
29 | ok(!$array_of_ints->check(1), '... 1 failed successfully'); |
30 | ok(!$array_of_ints->check({}), '... {} failed successfully'); |
31 | ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully'); |
32 | |
33 | # Hash of Ints |
34 | |
35 | my $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 | ); |
40 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Container'); |
41 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint'); |
42 | |
43 | ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully'); |
44 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully'); |
45 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully'); |
46 | |
47 | ok(!$hash_of_ints->check(1), '... 1 failed successfully'); |
48 | ok(!$hash_of_ints->check([]), '... [] failed successfully'); |
49 | ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully'); |
50 | |
51 | # Array of Array of Ints |
52 | |
53 | my $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 | ); |
58 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Container'); |
59 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint'); |
60 | |
61 | ok($array_of_array_of_ints->check( |
62 | [[ 1, 2, 3 ], [ 4, 5, 6 ]] |
63 | ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully'); |
64 | ok(!$array_of_array_of_ints->check( |
65 | [[ 1, 2, 3 ], [ qw/foo bar/ ]] |
66 | ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully'); |
67 | |