Commit | Line | Data |
d67145ed |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2b6c2a5c |
6 | use Test::More tests => 24; |
d67145ed |
7 | use Test::Exception; |
8 | |
d03bd989 |
9 | BEGIN { |
10 | use_ok('Moose::Util::TypeConstraints'); |
11 | use_ok('Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
12 | } |
13 | |
14 | # Array of Ints |
15 | |
0fbd4b0a |
16 | my $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 |
21 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
22 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint'); |
23 | |
1808c2da |
24 | ok($array_of_ints->check([ 1, 2, 3, 4 ]), '[ 1, 2, 3, 4 ] passed successfully'); |
25 | ok(!$array_of_ints->check([qw/foo bar baz/]), '[qw/foo bar baz/] failed successfully'); |
26 | ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '[ 1, 2, 3, qw/foo bar/] failed successfully'); |
d67145ed |
27 | |
1808c2da |
28 | ok(!$array_of_ints->check(1), '1 failed successfully'); |
29 | ok(!$array_of_ints->check({}), '{} failed successfully'); |
30 | ok(!$array_of_ints->check(sub { () }), 'sub { () } failed successfully'); |
d67145ed |
31 | |
32 | # Hash of Ints |
33 | |
0fbd4b0a |
34 | my $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 |
39 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
40 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint'); |
41 | |
1808c2da |
42 | ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '{ one => 1, two => 2, three => 3 } passed successfully'); |
43 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '{ 1 => one, 2 => two, 3 => three } failed successfully'); |
44 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '{ 1 => one, 2 => two, three => 3 } failed successfully'); |
d67145ed |
45 | |
1808c2da |
46 | ok(!$hash_of_ints->check(1), '1 failed successfully'); |
47 | ok(!$hash_of_ints->check([]), '[] failed successfully'); |
48 | ok(!$hash_of_ints->check(sub { () }), 'sub { () } failed successfully'); |
d67145ed |
49 | |
50 | # Array of Array of Ints |
51 | |
0fbd4b0a |
52 | my $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 |
57 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
58 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint'); |
59 | |
60 | ok($array_of_array_of_ints->check( |
61 | [[ 1, 2, 3 ], [ 4, 5, 6 ]] |
1808c2da |
62 | ), '[[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully'); |
d67145ed |
63 | ok(!$array_of_array_of_ints->check( |
64 | [[ 1, 2, 3 ], [ qw/foo bar/ ]] |
1808c2da |
65 | ), '[[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully'); |
d67145ed |
66 | |
2b6c2a5c |
67 | { |
68 | my $anon_type = Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Foo]'); |
69 | isa_ok( $anon_type, 'Moose::Meta::TypeConstraint::Parameterized' ); |
54718169 |
70 | |
2b6c2a5c |
71 | my $param_type = $anon_type->type_parameter; |
72 | isa_ok( $param_type, 'Moose::Meta::TypeConstraint::Class' ); |
73 | } |