Commit | Line | Data |
8de73ff1 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2c78d811 |
6 | use Test::More tests => 21; |
8de73ff1 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok("Moose::Util::TypeConstraints"); |
11 | } |
12 | |
13 | lives_ok { |
14 | subtype 'MyCollections' => as 'ArrayRef | HashRef'; |
1808c2da |
15 | } 'created the subtype special okay'; |
8de73ff1 |
16 | |
17 | { |
18 | my $t = find_type_constraint('MyCollections'); |
19 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
20 | |
1808c2da |
21 | is($t->name, 'MyCollections', 'name is correct'); |
8de73ff1 |
22 | |
23 | my $p = $t->parent; |
24 | isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); |
25 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
26 | |
1808c2da |
27 | is($p->name, 'ArrayRef|HashRef', 'parent name is correct'); |
8de73ff1 |
28 | |
1808c2da |
29 | ok($t->check([]), 'validated it correctly'); |
30 | ok($t->check({}), 'validated it correctly'); |
31 | ok(!$t->check(1), 'validated it correctly'); |
8de73ff1 |
32 | } |
33 | |
34 | lives_ok { |
d03bd989 |
35 | subtype 'MyCollectionsExtended' |
eb4c4e82 |
36 | => as 'ArrayRef|HashRef' |
8de73ff1 |
37 | => where { |
38 | if (ref($_) eq 'ARRAY') { |
39 | return if scalar(@$_) < 2; |
40 | } |
41 | elsif (ref($_) eq 'HASH') { |
d03bd989 |
42 | return if scalar(keys(%$_)) < 2; |
8de73ff1 |
43 | } |
44 | 1; |
45 | }; |
1808c2da |
46 | } 'created the subtype special okay'; |
8de73ff1 |
47 | |
48 | { |
49 | my $t = find_type_constraint('MyCollectionsExtended'); |
50 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
51 | |
1808c2da |
52 | is($t->name, 'MyCollectionsExtended', 'name is correct'); |
8de73ff1 |
53 | |
54 | my $p = $t->parent; |
55 | isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); |
56 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
57 | |
1808c2da |
58 | is($p->name, 'ArrayRef|HashRef', 'parent name is correct'); |
8de73ff1 |
59 | |
1808c2da |
60 | ok(!$t->check([]), 'validated it correctly'); |
61 | ok($t->check([1, 2]), 'validated it correctly'); |
d03bd989 |
62 | |
1808c2da |
63 | ok(!$t->check({}), 'validated it correctly'); |
64 | ok($t->check({ one => 1, two => 2 }), 'validated it correctly'); |
d03bd989 |
65 | |
1808c2da |
66 | ok(!$t->check(1), 'validated it correctly'); |
8de73ff1 |
67 | } |
68 | |
69 | |