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