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