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