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