Commit | Line | Data |
d56e8388 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
78b163fb |
6 | use Test::More; |
7 | BEGIN { |
8 | if (eval "require Class::Method::Modifiers; 1") { |
7af70116 |
9 | plan tests => 57; |
78b163fb |
10 | } |
11 | else { |
12 | plan skip_all => "Class::Method::Modifiers required for this test"; |
13 | } |
14 | } |
15 | |
ba2da7f2 |
16 | use Mouse::Util; |
eab81545 |
17 | use Test::Exception; |
d56e8388 |
18 | |
d56e8388 |
19 | { |
20 | package Point; |
21 | use Mouse; |
22 | |
23 | has 'x' => (isa => 'Int', is => 'ro'); |
24 | has 'y' => (isa => 'Int', is => 'rw'); |
25 | |
26 | sub clear { |
27 | my $self = shift; |
28 | $self->{x} = 0; |
29 | $self->y(0); |
30 | } |
31 | |
8632b6fe |
32 | __PACKAGE__->meta->make_immutable(); |
d56e8388 |
33 | }{ |
34 | package Point3D; |
35 | use Mouse; |
36 | |
37 | extends 'Point'; |
38 | |
39 | has 'z' => (isa => 'Int'); |
40 | |
41 | after 'clear' => sub { |
42 | my $self = shift; |
43 | $self->{z} = 0; |
44 | }; |
45 | |
8632b6fe |
46 | __PACKAGE__->meta->make_immutable(); |
d56e8388 |
47 | } |
48 | |
49 | my $point = Point->new(x => 1, y => 2); |
50 | isa_ok($point, 'Point'); |
51 | isa_ok($point, 'Mouse::Object'); |
52 | |
53 | is($point->x, 1, '... got the right value for x'); |
54 | is($point->y, 2, '... got the right value for y'); |
55 | |
56 | $point->y(10); |
57 | is($point->y, 10, '... got the right (changed) value for y'); |
58 | |
59 | dies_ok { |
60 | $point->y('Foo'); |
61 | } '... cannot assign a non-Int to y'; |
62 | |
63 | dies_ok { |
64 | $point->x(1000); |
65 | } '... cannot assign to a read-only method'; |
66 | is($point->x, 1, '... got the right (un-changed) value for x'); |
67 | |
68 | $point->clear(); |
69 | |
70 | is($point->x, 0, '... got the right (cleared) value for x'); |
71 | is($point->y, 0, '... got the right (cleared) value for y'); |
72 | |
73 | # check the type constraints on the constructor |
74 | |
75 | lives_ok { |
76 | Point->new(x => 0, y => 0); |
77 | } '... can assign a 0 to x and y'; |
78 | |
79 | dies_ok { |
80 | Point->new(x => 10, y => 'Foo'); |
81 | } '... cannot assign a non-Int to y'; |
82 | |
83 | dies_ok { |
84 | Point->new(x => 'Foo', y => 10); |
85 | } '... cannot assign a non-Int to x'; |
86 | |
87 | # Point3D |
88 | |
89 | my $point3d = Point3D->new({ x => 10, y => 15, z => 3 }); |
90 | isa_ok($point3d, 'Point3D'); |
91 | isa_ok($point3d, 'Point'); |
92 | isa_ok($point3d, 'Mouse::Object'); |
93 | |
94 | is($point3d->x, 10, '... got the right value for x'); |
95 | is($point3d->y, 15, '... got the right value for y'); |
96 | is($point3d->{'z'}, 3, '... got the right value for z'); |
97 | |
98 | dies_ok { |
99 | $point3d->z; |
100 | } '... there is no method for z'; |
101 | |
102 | $point3d->clear(); |
103 | |
104 | is($point3d->x, 0, '... got the right (cleared) value for x'); |
105 | is($point3d->y, 0, '... got the right (cleared) value for y'); |
106 | is($point3d->{'z'}, 0, '... got the right (cleared) value for z'); |
107 | |
108 | dies_ok { |
109 | Point3D->new(x => 10, y => 'Foo', z => 3); |
110 | } '... cannot assign a non-Int to y'; |
111 | |
112 | dies_ok { |
113 | Point3D->new(x => 'Foo', y => 10, z => 3); |
114 | } '... cannot assign a non-Int to x'; |
115 | |
116 | dies_ok { |
117 | Point3D->new(x => 0, y => 10, z => 'Bar'); |
118 | } '... cannot assign a non-Int to z'; |
119 | |
120 | # test some class introspection |
121 | |
122 | can_ok('Point', 'meta'); |
123 | isa_ok(Point->meta, 'Mouse::Meta::Class'); |
124 | |
125 | can_ok('Point3D', 'meta'); |
126 | isa_ok(Point3D->meta, 'Mouse::Meta::Class'); |
127 | |
128 | isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well'); |
129 | |
130 | # poke at Point |
131 | |
132 | is_deeply( |
133 | [ Point->meta->superclasses ], |
134 | [ 'Mouse::Object' ], |
135 | '... Point got the automagic base class'); |
136 | |
137 | my @Point_methods = qw(meta new x y clear); |
138 | my @Point_attrs = ('x', 'y'); |
139 | |
8632b6fe |
140 | is_deeply( |
141 | [ sort @Point_methods ], |
142 | [ sort Point->meta->get_method_list() ], |
143 | '... we match the method list for Point'); |
d56e8388 |
144 | |
8632b6fe |
145 | SKIP: { |
146 | skip "Mouse has no method introspection", 1 + @Point_methods; |
d56e8388 |
147 | |
148 | is_deeply( |
149 | [ sort @Point_attrs ], |
150 | [ sort Point->meta->get_attribute_list() ], |
151 | '... we match the attribute list for Point'); |
152 | |
153 | foreach my $method (@Point_methods) { |
154 | ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"'); |
155 | } |
156 | } |
157 | |
158 | foreach my $attr_name (@Point_attrs ) { |
159 | ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"'); |
160 | my $attr = Point->meta->get_attribute($attr_name); |
161 | ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint'); |
162 | |
163 | SKIP: { |
164 | skip "Mouse type constraints are not objects", 2; |
165 | isa_ok($attr->type_constraint, 'Mouse::Meta::TypeConstraint'); |
166 | is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint'); |
167 | } |
168 | } |
169 | |
170 | # poke at Point3D |
171 | |
172 | is_deeply( |
173 | [ Point3D->meta->superclasses ], |
174 | [ 'Point' ], |
175 | '... Point3D gets the parent given to it'); |
176 | |
177 | my @Point3D_methods = qw(new meta clear); |
178 | my @Point3D_attrs = ('z'); |
179 | |
180 | SKIP: { |
181 | skip "Mouse has no method introspection", 2 + @Point3D_methods; |
182 | |
183 | is_deeply( |
184 | [ sort @Point3D_methods ], |
185 | [ sort Point3D->meta->get_method_list() ], |
186 | '... we match the method list for Point3D'); |
187 | |
188 | is_deeply( |
189 | [ sort @Point3D_attrs ], |
190 | [ sort Point3D->meta->get_attribute_list() ], |
191 | '... we match the attribute list for Point3D'); |
192 | |
193 | foreach my $method (@Point3D_methods) { |
194 | ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"'); |
195 | } |
196 | }; |
197 | |
198 | foreach my $attr_name (@Point3D_attrs ) { |
199 | ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"'); |
200 | my $attr = Point3D->meta->get_attribute($attr_name); |
201 | ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint'); |
202 | SKIP: { |
203 | skip "Mouse type constraints are not objects", 2; |
204 | isa_ok($attr->type_constraint, 'Mouse::Meta::TypeConstraint'); |
205 | is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint'); |
206 | }; |
207 | } |
208 | |