6 use Test::More tests => 58;
7 use Mouse::Util ':test';
17 has 'x' => (isa => 'Int', is => 'ro');
18 has 'y' => (isa => 'Int', is => 'rw');
32 has 'z' => (isa => 'Int');
34 after 'clear' => sub {
41 my $point = Point->new(x => 1, y => 2);
42 isa_ok($point, 'Point');
43 isa_ok($point, 'Mouse::Object');
45 is($point->x, 1, '... got the right value for x');
46 is($point->y, 2, '... got the right value for y');
49 is($point->y, 10, '... got the right (changed) value for y');
53 } '... cannot assign a non-Int to y';
57 } '... cannot assign to a read-only method';
58 is($point->x, 1, '... got the right (un-changed) value for x');
62 is($point->x, 0, '... got the right (cleared) value for x');
63 is($point->y, 0, '... got the right (cleared) value for y');
65 # check the type constraints on the constructor
68 Point->new(x => 0, y => 0);
69 } '... can assign a 0 to x and y';
72 Point->new(x => 10, y => 'Foo');
73 } '... cannot assign a non-Int to y';
76 Point->new(x => 'Foo', y => 10);
77 } '... cannot assign a non-Int to x';
81 my $point3d = Point3D->new({ x => 10, y => 15, z => 3 });
82 isa_ok($point3d, 'Point3D');
83 isa_ok($point3d, 'Point');
84 isa_ok($point3d, 'Mouse::Object');
86 is($point3d->x, 10, '... got the right value for x');
87 is($point3d->y, 15, '... got the right value for y');
88 is($point3d->{'z'}, 3, '... got the right value for z');
92 } '... there is no method for z';
96 is($point3d->x, 0, '... got the right (cleared) value for x');
97 is($point3d->y, 0, '... got the right (cleared) value for y');
98 is($point3d->{'z'}, 0, '... got the right (cleared) value for z');
101 Point3D->new(x => 10, y => 'Foo', z => 3);
102 } '... cannot assign a non-Int to y';
105 Point3D->new(x => 'Foo', y => 10, z => 3);
106 } '... cannot assign a non-Int to x';
109 Point3D->new(x => 0, y => 10, z => 'Bar');
110 } '... cannot assign a non-Int to z';
112 # test some class introspection
114 can_ok('Point', 'meta');
115 isa_ok(Point->meta, 'Mouse::Meta::Class');
117 can_ok('Point3D', 'meta');
118 isa_ok(Point3D->meta, 'Mouse::Meta::Class');
120 isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
125 [ Point->meta->superclasses ],
127 '... Point got the automagic base class');
129 my @Point_methods = qw(meta new x y clear);
130 my @Point_attrs = ('x', 'y');
133 skip "Mouse has no method introspection", 2 + @Point_methods;
136 [ sort @Point_methods ],
137 [ sort Point->meta->get_method_list() ],
138 '... we match the method list for Point');
141 [ sort @Point_attrs ],
142 [ sort Point->meta->get_attribute_list() ],
143 '... we match the attribute list for Point');
145 foreach my $method (@Point_methods) {
146 ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"');
150 foreach my $attr_name (@Point_attrs ) {
151 ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"');
152 my $attr = Point->meta->get_attribute($attr_name);
153 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
156 skip "Mouse type constraints are not objects", 2;
157 isa_ok($attr->type_constraint, 'Mouse::Meta::TypeConstraint');
158 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');
165 [ Point3D->meta->superclasses ],
167 '... Point3D gets the parent given to it');
169 my @Point3D_methods = qw(new meta clear);
170 my @Point3D_attrs = ('z');
173 skip "Mouse has no method introspection", 2 + @Point3D_methods;
176 [ sort @Point3D_methods ],
177 [ sort Point3D->meta->get_method_list() ],
178 '... we match the method list for Point3D');
181 [ sort @Point3D_attrs ],
182 [ sort Point3D->meta->get_attribute_list() ],
183 '... we match the attribute list for Point3D');
185 foreach my $method (@Point3D_methods) {
186 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
190 foreach my $attr_name (@Point3D_attrs ) {
191 ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"');
192 my $attr = Point3D->meta->get_attribute($attr_name);
193 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
195 skip "Mouse type constraints are not objects", 2;
196 isa_ok($attr->type_constraint, 'Mouse::Meta::TypeConstraint');
197 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');