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