Commit | Line | Data |
d67145ed |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
d67145ed |
7 | |
28fdde7f |
8 | use Moose::Util::TypeConstraints; |
9 | use Moose::Meta::TypeConstraint::Parameterized; |
d67145ed |
10 | |
11 | # Array of Ints |
12 | |
0fbd4b0a |
13 | my $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 |
18 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
19 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint'); |
20 | |
21 | ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully'); |
22 | ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully'); |
23 | ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully'); |
24 | |
25 | ok(!$array_of_ints->check(1), '... 1 failed successfully'); |
26 | ok(!$array_of_ints->check({}), '... {} failed successfully'); |
27 | ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully'); |
28 | |
29 | # Hash of Ints |
30 | |
0fbd4b0a |
31 | my $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 |
36 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
37 | isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint'); |
38 | |
39 | ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully'); |
40 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', 3 => 'three' }), '... { 1 => one, 2 => two, 3 => three } failed successfully'); |
41 | ok(!$hash_of_ints->check({ 1 => 'one', 2 => 'two', three => 3 }), '... { 1 => one, 2 => two, three => 3 } failed successfully'); |
42 | |
43 | ok(!$hash_of_ints->check(1), '... 1 failed successfully'); |
44 | ok(!$hash_of_ints->check([]), '... [] failed successfully'); |
45 | ok(!$hash_of_ints->check(sub { () }), '... sub { () } failed successfully'); |
46 | |
47 | # Array of Array of Ints |
48 | |
0fbd4b0a |
49 | my $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 |
54 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
d67145ed |
55 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint'); |
56 | |
57 | ok($array_of_array_of_ints->check( |
58 | [[ 1, 2, 3 ], [ 4, 5, 6 ]] |
59 | ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully'); |
60 | ok(!$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 | |
72 | done_testing; |