Commit | Line | Data |
a0f8153d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
a0f8153d |
8 | |
9 | BEGIN { |
10 | use_ok("Moose::Util::TypeConstraints"); |
11 | } |
12 | |
b10dde3a |
13 | is( exception { |
a0f8153d |
14 | subtype 'MySpecialHash' => as 'HashRef[Int]'; |
b10dde3a |
15 | }, undef, '... created the subtype special okay' ); |
a0f8153d |
16 | |
17 | { |
18 | my $t = find_type_constraint('MySpecialHash'); |
19 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
20 | |
21 | is($t->name, 'MySpecialHash', '... name is correct'); |
22 | |
23 | my $p = $t->parent; |
24 | isa_ok($p, 'Moose::Meta::TypeConstraint::Parameterized'); |
25 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
26 | |
27 | is($p->name, 'HashRef[Int]', '... parent name is correct'); |
28 | |
9ceb576e |
29 | ok($t->check({ one => 1, two => 2 }), '... validated {one=>1, two=>2} correctly'); |
a0f8153d |
30 | ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly'); |
dabed765 |
31 | |
32 | ok( $t->equals($t), "equals to self" ); |
33 | ok( !$t->equals( $t->parent ), "not equal to parent" ); |
34 | ok( $t->parent->equals( $t->parent ), "parent equals to self" ); |
4c015454 |
35 | |
36 | ok( !$t->is_a_type_of("ThisTypeDoesNotExist"), "not a non existant type" ); |
37 | ok( !$t->is_subtype_of("ThisTypeDoesNotExist"), "not a subtype of a non existant type" ); |
a0f8153d |
38 | } |
39 | |
b10dde3a |
40 | is( exception { |
d03bd989 |
41 | subtype 'MySpecialHashExtended' |
a0f8153d |
42 | => as 'HashRef[Int]' |
43 | => where { |
44 | # all values are less then 10 |
45 | (scalar grep { $_ < 10 } values %{$_}) ? 1 : undef |
46 | }; |
b10dde3a |
47 | }, undef, '... created the subtype special okay' ); |
a0f8153d |
48 | |
49 | { |
50 | my $t = find_type_constraint('MySpecialHashExtended'); |
51 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
52 | |
53 | is($t->name, 'MySpecialHashExtended', '... name is correct'); |
54 | |
55 | my $p = $t->parent; |
56 | isa_ok($p, 'Moose::Meta::TypeConstraint::Parameterized'); |
57 | isa_ok($p, 'Moose::Meta::TypeConstraint'); |
58 | |
59 | is($p->name, 'HashRef[Int]', '... parent name is correct'); |
60 | |
61 | ok($t->check({ one => 1, two => 2 }), '... validated it correctly'); |
9ceb576e |
62 | ok(!$t->check({ zero => 10, one => 11, two => 12 }), '... validated { zero => 10, one => 11, two => 12 } correctly'); |
a0f8153d |
63 | ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly'); |
64 | } |
65 | |
b10dde3a |
66 | is( exception { |
36dbd105 |
67 | subtype 'MyNonSpecialHash' |
68 | => as "HashRef" |
69 | => where { keys %$_ == 3 }; |
b10dde3a |
70 | }, undef ); |
36dbd105 |
71 | |
72 | { |
73 | my $t = find_type_constraint('MyNonSpecialHash'); |
74 | |
75 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
76 | isa_ok($t, 'Moose::Meta::TypeConstraint::Parameterizable'); |
77 | |
78 | ok( $t->check({ one => 1, two => "foo", three => [] }), "validated" ); |
79 | ok( !$t->check({ one => 1 }), "failed" ); |
80 | } |
81 | |
82 | { |
83 | my $t = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MyNonSpecialHash[Int]'); |
84 | |
85 | isa_ok($t, 'Moose::Meta::TypeConstraint'); |
86 | |
87 | ok( $t->check({ one => 1, two => 2, three => 3 }), "validated" ); |
88 | ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" ); |
89 | ok( !$t->check({ one => 1 }), "failed" ); |
90 | } |
94330b08 |
91 | |
92 | { |
93 | ## Because to throw errors in M:M:Parameterizable needs Moose loaded in |
94 | ## order to throw errors. In theory the use Moose belongs to that class |
95 | ## but when I put it there causes all sorts or trouble. In theory this is |
96 | ## never a real problem since you are likely to use Moose somewhere when you |
97 | ## are creating type constraints. |
98 | use Moose (); |
d03bd989 |
99 | |
94330b08 |
100 | my $MyArrayRefInt = subtype 'MyArrayRefInt', |
101 | as 'ArrayRef[Int]'; |
102 | |
103 | my $BiggerInt = subtype 'BiggerInt', |
104 | as 'Int', |
105 | where {$_>10}; |
106 | |
107 | my $SubOfMyArrayRef = subtype 'SubOfMyArrayRef', |
108 | as 'MyArrayRefInt[BiggerInt]'; |
d03bd989 |
109 | |
94330b08 |
110 | ok $MyArrayRefInt->check([1,2,3]), '[1,2,3] is okay'; |
d03bd989 |
111 | ok ! $MyArrayRefInt->check(["a","b"]), '["a","b"] is not'; |
94330b08 |
112 | ok $BiggerInt->check(100), '100 is big enough'; |
d03bd989 |
113 | ok ! $BiggerInt->check(5), '5 is big enough'; |
94330b08 |
114 | ok $SubOfMyArrayRef->check([15,20,25]), '[15,20,25] is a bunch of big ints'; |
115 | ok ! $SubOfMyArrayRef->check([15,5,25]), '[15,5,25] is NOT a bunch of big ints'; |
d03bd989 |
116 | |
b10dde3a |
117 | like( exception { |
94330b08 |
118 | my $SubOfMyArrayRef = subtype 'SubSubOfMyArrayRef', |
d03bd989 |
119 | as 'SubOfMyArrayRef[Str]'; |
b10dde3a |
120 | }, qr/Str is not a subtype of BiggerInt/, 'Failed to parameterize with a bad type parameter' ); |
94330b08 |
121 | } |
a28e50e4 |
122 | |
2c29c0e7 |
123 | { |
124 | my $RefToInt = subtype as 'ScalarRef[Int]'; |
125 | |
126 | ok $RefToInt->check(\1), '\1 is okay'; |
127 | ok !$RefToInt->check(1), '1 is not'; |
128 | ok !$RefToInt->check(\"foo"), '\"foo" is not'; |
129 | } |
130 | |
a28e50e4 |
131 | done_testing; |