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