Commit | Line | Data |
f1917f58 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
f1917f58 |
7 | |
28fdde7f |
8 | use Moose::Util::TypeConstraints; |
9 | use Moose::Meta::TypeConstraint::Parameterized; |
f1917f58 |
10 | |
11 | my $r = Moose::Util::TypeConstraints->get_type_constraint_registry; |
12 | |
13 | ## Containers in unions ... |
14 | |
15 | # Array of Ints or Strings |
16 | |
3796382a |
17 | my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int|Str]'); |
0fbd4b0a |
18 | isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Parameterized'); |
f1917f58 |
19 | |
20 | ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), '... this passed the type check'); |
21 | ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), '... this passed the type check'); |
22 | ok($array_of_ints_or_strings->check([ 'one', 'two', 'three' ]), '... this passed the type check'); |
23 | |
24 | ok(!$array_of_ints_or_strings->check([ 1, [], 'three' ]), '... this didnt pass the type check'); |
25 | |
26 | $r->add_type_constraint($array_of_ints_or_strings); |
27 | |
28 | # Array of Ints or HashRef |
29 | |
0fbd4b0a |
30 | my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]'); |
31 | isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Parameterized'); |
f1917f58 |
32 | |
33 | ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), '... this passed the type check'); |
34 | ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), '... this passed the type check'); |
35 | ok($array_of_ints_or_hash_ref->check([ {}, {}, {} ]), '... this passed the type check'); |
36 | |
37 | ok(!$array_of_ints_or_hash_ref->check([ {}, [], 3 ]), '... this didnt pass the type check'); |
38 | |
39 | $r->add_type_constraint($array_of_ints_or_hash_ref); |
40 | |
41 | # union of Arrays of Str | Int or Arrays of Int | Hash |
42 | |
d03bd989 |
43 | # we can't build this using the simplistic parser |
f1917f58 |
44 | # we have, so we have to do it by hand - SL |
45 | |
3796382a |
46 | my $pure_insanity = Moose::Util::TypeConstraints::create_type_constraint_union('ArrayRef[Int|Str] | ArrayRef[Int | HashRef]'); |
f1917f58 |
47 | isa_ok($pure_insanity, 'Moose::Meta::TypeConstraint::Union'); |
48 | |
49 | ok($pure_insanity->check([ 1, {}, 3 ]), '... this passed the type check'); |
50 | ok($pure_insanity->check([ 1, 'Str', 3 ]), '... this passed the type check'); |
51 | |
52 | ok(!$pure_insanity->check([ 1, {}, 'foo' ]), '... this didnt pass the type check'); |
53 | ok(!$pure_insanity->check([ [], {}, 1 ]), '... this didnt pass the type check'); |
54 | |
55 | ## Nested Containers ... |
56 | |
57 | # Array of Ints |
58 | |
0fbd4b0a |
59 | my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]'); |
60 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
f1917f58 |
61 | isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint'); |
62 | |
63 | ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully'); |
64 | ok(!$array_of_ints->check([qw/foo bar baz/]), '... [qw/foo bar baz/] failed successfully'); |
65 | ok(!$array_of_ints->check([ 1, 2, 3, qw/foo bar/]), '... [ 1, 2, 3, qw/foo bar/] failed successfully'); |
66 | |
67 | ok(!$array_of_ints->check(1), '... 1 failed successfully'); |
68 | ok(!$array_of_ints->check({}), '... {} failed successfully'); |
69 | ok(!$array_of_ints->check(sub { () }), '... sub { () } failed successfully'); |
70 | |
71 | # Array of Array of Ints |
72 | |
0fbd4b0a |
73 | my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]'); |
74 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
f1917f58 |
75 | isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint'); |
76 | |
77 | ok($array_of_array_of_ints->check( |
78 | [[ 1, 2, 3 ], [ 4, 5, 6 ]] |
79 | ), '... [[ 1, 2, 3 ], [ 4, 5, 6 ]] passed successfully'); |
80 | ok(!$array_of_array_of_ints->check( |
81 | [[ 1, 2, 3 ], [ qw/foo bar/ ]] |
82 | ), '... [[ 1, 2, 3 ], [ qw/foo bar/ ]] failed successfully'); |
83 | |
84 | # Array of Array of Array of Ints |
85 | |
0fbd4b0a |
86 | my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]'); |
87 | isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized'); |
f1917f58 |
88 | isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint'); |
89 | |
90 | ok($array_of_array_of_array_of_ints->check( |
91 | [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] |
92 | ), '... [[[ 1, 2, 3 ], [ 4, 5, 6 ]], [[ 7, 8, 9 ]]] passed successfully'); |
93 | ok(!$array_of_array_of_array_of_ints->check( |
94 | [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] |
95 | ), '... [[[ 1, 2, 3 ]], [[ qw/foo bar/ ]]] failed successfully'); |
96 | |
a28e50e4 |
97 | done_testing; |