Commit | Line | Data |
8de73ff1 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
8de73ff1 |
8 | |
9 | BEGIN { |
10 | use_ok("Moose::Util::TypeConstraints"); |
11 | } |
12 | |
b10dde3a |
13 | is( exception { |
8de73ff1 |
14 | subtype 'MyCollections' => as 'ArrayRef | HashRef'; |
b10dde3a |
15 | }, undef, '... created the subtype special okay' ); |
8de73ff1 |
16 | |
17 | { |
18 | my $t = find_type_constraint('MyCollections'); |
19 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
20 | |
21 | is($t->name, 'MyCollections', '... name is correct'); |
22 | |
23 | my $p = $t->parent; |
24 | isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); |
25 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
26 | |
eb4c4e82 |
27 | is($p->name, 'ArrayRef|HashRef', '... parent name is correct'); |
8de73ff1 |
28 | |
29 | ok($t->check([]), '... validated it correctly'); |
d03bd989 |
30 | ok($t->check({}), '... validated it correctly'); |
8de73ff1 |
31 | ok(!$t->check(1), '... validated it correctly'); |
32 | } |
33 | |
b10dde3a |
34 | is( exception { |
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 | }; |
b10dde3a |
46 | }, undef, '... created the subtype special okay' ); |
8de73ff1 |
47 | |
48 | { |
49 | my $t = find_type_constraint('MyCollectionsExtended'); |
50 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
51 | |
52 | is($t->name, 'MyCollectionsExtended', '... name is correct'); |
53 | |
54 | my $p = $t->parent; |
55 | isa_ok($p, 'Moose::Meta::TypeConstraint::Union'); |
56 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
57 | |
eb4c4e82 |
58 | is($p->name, 'ArrayRef|HashRef', '... parent name is correct'); |
8de73ff1 |
59 | |
60 | ok(!$t->check([]), '... validated it correctly'); |
d03bd989 |
61 | ok($t->check([1, 2]), '... validated it correctly'); |
62 | |
63 | ok(!$t->check({}), '... validated it correctly'); |
64 | ok($t->check({ one => 1, two => 2 }), '... validated it correctly'); |
65 | |
8de73ff1 |
66 | ok(!$t->check(1), '... validated it correctly'); |
67 | } |
68 | |
a28e50e4 |
69 | done_testing; |