Support is => 'bare' for compatibility
[gitmo/Mouse.git] / t / 000-recipes / 001_point.t
CommitLineData
d56e8388 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
78b163fb 6use Test::More;
7BEGIN {
8 if (eval "require Class::Method::Modifiers; 1") {
e578d610 9 plan tests => 59;
78b163fb 10 }
11 else {
12 plan skip_all => "Class::Method::Modifiers required for this test";
13 }
14}
15
ba2da7f2 16use Mouse::Util;
eab81545 17use 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
11d41528 39 has 'z' => (isa => 'Int', is => 'bare');
d56e8388 40
41 after 'clear' => sub {
42 my $self = shift;
43 $self->{z} = 0;
44 };
45
8632b6fe 46 __PACKAGE__->meta->make_immutable();
d56e8388 47}
48
49my $point = Point->new(x => 1, y => 2);
50isa_ok($point, 'Point');
51isa_ok($point, 'Mouse::Object');
52
53is($point->x, 1, '... got the right value for x');
54is($point->y, 2, '... got the right value for y');
55
56$point->y(10);
57is($point->y, 10, '... got the right (changed) value for y');
58
59dies_ok {
60 $point->y('Foo');
61} '... cannot assign a non-Int to y';
62
63dies_ok {
64 $point->x(1000);
65} '... cannot assign to a read-only method';
66is($point->x, 1, '... got the right (un-changed) value for x');
67
68$point->clear();
69
70is($point->x, 0, '... got the right (cleared) value for x');
71is($point->y, 0, '... got the right (cleared) value for y');
72
73# check the type constraints on the constructor
74
75lives_ok {
76 Point->new(x => 0, y => 0);
77} '... can assign a 0 to x and y';
78
79dies_ok {
80 Point->new(x => 10, y => 'Foo');
81} '... cannot assign a non-Int to y';
82
83dies_ok {
84 Point->new(x => 'Foo', y => 10);
85} '... cannot assign a non-Int to x';
86
87# Point3D
88
89my $point3d = Point3D->new({ x => 10, y => 15, z => 3 });
90isa_ok($point3d, 'Point3D');
91isa_ok($point3d, 'Point');
92isa_ok($point3d, 'Mouse::Object');
93
94is($point3d->x, 10, '... got the right value for x');
95is($point3d->y, 15, '... got the right value for y');
96is($point3d->{'z'}, 3, '... got the right value for z');
97
98dies_ok {
99 $point3d->z;
100} '... there is no method for z';
101
102$point3d->clear();
103
104is($point3d->x, 0, '... got the right (cleared) value for x');
105is($point3d->y, 0, '... got the right (cleared) value for y');
106is($point3d->{'z'}, 0, '... got the right (cleared) value for z');
107
108dies_ok {
109 Point3D->new(x => 10, y => 'Foo', z => 3);
110} '... cannot assign a non-Int to y';
111
112dies_ok {
113 Point3D->new(x => 'Foo', y => 10, z => 3);
114} '... cannot assign a non-Int to x';
115
116dies_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
122can_ok('Point', 'meta');
123isa_ok(Point->meta, 'Mouse::Meta::Class');
124
125can_ok('Point3D', 'meta');
126isa_ok(Point3D->meta, 'Mouse::Meta::Class');
127
128isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
129
130# poke at Point
131
132is_deeply(
133 [ Point->meta->superclasses ],
134 [ 'Mouse::Object' ],
135 '... Point got the automagic base class');
136
e578d610 137my @Point_methods = qw(meta new x y clear DESTROY);
d56e8388 138my @Point_attrs = ('x', 'y');
139
8632b6fe 140is_deeply(
141 [ sort @Point_methods ],
142 [ sort Point->meta->get_method_list() ],
143 '... we match the method list for Point');
d56e8388 144
8632b6fe 145SKIP: {
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
158foreach 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
172is_deeply(
173 [ Point3D->meta->superclasses ],
174 [ 'Point' ],
175 '... Point3D gets the parent given to it');
176
e578d610 177my @Point3D_methods = qw(new meta clear DESTROY);
d56e8388 178my @Point3D_attrs = ('z');
179
180SKIP: {
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
198foreach 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