Commit | Line | Data |
39aba5c9 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
39aba5c9 |
8 | |
28fdde7f |
9 | use Moose::Util::TypeConstraints; |
10 | use Moose::Meta::TypeConstraint::Parameterized; |
39aba5c9 |
11 | |
b10dde3a |
12 | is( exception { |
39aba5c9 |
13 | subtype 'AlphaKeyHash' => as 'HashRef' |
14 | => where { |
15 | # no keys match non-alpha |
16 | (grep { /[^a-zA-Z]/ } keys %$_) == 0 |
17 | }; |
b10dde3a |
18 | }, undef, '... created the subtype special okay' ); |
39aba5c9 |
19 | |
b10dde3a |
20 | is( exception { |
39aba5c9 |
21 | subtype 'Trihash' => as 'AlphaKeyHash' |
22 | => where { |
23 | keys(%$_) == 3 |
24 | }; |
b10dde3a |
25 | }, undef, '... created the subtype special okay' ); |
39aba5c9 |
26 | |
b10dde3a |
27 | is( exception { |
39aba5c9 |
28 | subtype 'Noncon' => as 'Item'; |
b10dde3a |
29 | }, undef, '... created the subtype special okay' ); |
39aba5c9 |
30 | |
31 | { |
32 | my $t = find_type_constraint('AlphaKeyHash'); |
33 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
34 | |
35 | is($t->name, 'AlphaKeyHash', '... name is correct'); |
36 | |
37 | my $p = $t->parent; |
38 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
39 | |
40 | is($p->name, 'HashRef', '... parent name is correct'); |
41 | |
42 | ok($t->check({ one => 1, two => 2 }), '... validated it correctly'); |
43 | ok(!$t->check({ one1 => 1, two2 => 2 }), '... validated it correctly'); |
dabed765 |
44 | |
45 | ok( $t->equals($t), "equals to self" ); |
46 | ok( !$t->equals($t->parent), "not equal to parent" ); |
39aba5c9 |
47 | } |
48 | |
620db045 |
49 | my $hoi = Moose::Util::TypeConstraints::find_or_parse_type_constraint('AlphaKeyHash[Int]'); |
39aba5c9 |
50 | |
51 | ok($hoi->check({ one => 1, two => 2 }), '... validated it correctly'); |
52 | ok(!$hoi->check({ one1 => 1, two2 => 2 }), '... validated it correctly'); |
53 | ok(!$hoi->check({ one => 'uno', two => 'dos' }), '... validated it correctly'); |
54 | ok(!$hoi->check({ one1 => 'un', two2 => 'deux' }), '... validated it correctly'); |
55 | |
dabed765 |
56 | ok( $hoi->equals($hoi), "equals to self" ); |
57 | ok( !$hoi->equals($hoi->parent), "equals to self" ); |
58 | ok( !$hoi->equals(find_type_constraint('AlphaKeyHash')), "not equal to unparametrized self" ); |
59 | ok( $hoi->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "Blah", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Int") ) ), "equal to clone" ); |
60 | ok( !$hoi->equals( Moose::Meta::TypeConstraint::Parameterized->new( name => "Oink", parent => find_type_constraint("AlphaKeyHash"), type_parameter => find_type_constraint("Str") ) ), "not equal to different parameter" ); |
61 | |
620db045 |
62 | my $th = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Trihash[Bool]'); |
39aba5c9 |
63 | |
64 | ok(!$th->check({ one => 1, two => 1 }), '... validated it correctly'); |
65 | ok($th->check({ one => 1, two => 0, three => 1 }), '... validated it correctly'); |
66 | ok(!$th->check({ one => 1, two => 2, three => 1 }), '... validated it correctly'); |
67 | ok(!$th->check({foo1 => 1, bar2 => 0, baz3 => 1}), '... validated it correctly'); |
68 | |
b10dde3a |
69 | isnt( exception { |
39aba5c9 |
70 | Moose::Meta::TypeConstraint::Parameterized->new( |
71 | name => 'Str[Int]', |
72 | parent => find_type_constraint('Str'), |
73 | type_parameter => find_type_constraint('Int'), |
74 | ); |
b10dde3a |
75 | }, undef, 'non-containers cannot be parameterized' ); |
39aba5c9 |
76 | |
b10dde3a |
77 | isnt( exception { |
39aba5c9 |
78 | Moose::Meta::TypeConstraint::Parameterized->new( |
79 | name => 'Noncon[Int]', |
80 | parent => find_type_constraint('Noncon'), |
81 | type_parameter => find_type_constraint('Int'), |
82 | ); |
b10dde3a |
83 | }, undef, 'non-containers cannot be parameterized' ); |
39aba5c9 |
84 | |
a28e50e4 |
85 | done_testing; |