8 if (eval "require Class::Method::Modifiers; 1") {
12 plan skip_all => "Class::Method::Modifiers required for this test";
23 has 'x' => (isa => 'Int', is => 'ro');
24 has 'y' => (isa => 'Int', is => 'rw');
32 __PACKAGE__->meta->make_immutable();
39 has 'z' => (isa => 'Int');
41 after 'clear' => sub {
46 __PACKAGE__->meta->make_immutable();
49 my $point = Point->new(x => 1, y => 2);
50 isa_ok($point, 'Point');
51 isa_ok($point, 'Mouse::Object');
53 is($point->x, 1, '... got the right value for x');
54 is($point->y, 2, '... got the right value for y');
57 is($point->y, 10, '... got the right (changed) value for y');
61 } '... cannot assign a non-Int to y';
65 } '... cannot assign to a read-only method';
66 is($point->x, 1, '... got the right (un-changed) value for x');
70 is($point->x, 0, '... got the right (cleared) value for x');
71 is($point->y, 0, '... got the right (cleared) value for y');
73 # check the type constraints on the constructor
76 Point->new(x => 0, y => 0);
77 } '... can assign a 0 to x and y';
80 Point->new(x => 10, y => 'Foo');
81 } '... cannot assign a non-Int to y';
84 Point->new(x => 'Foo', y => 10);
85 } '... cannot assign a non-Int to x';
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');
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');
100 } '... there is no method for z';
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');
109 Point3D->new(x => 10, y => 'Foo', z => 3);
110 } '... cannot assign a non-Int to y';
113 Point3D->new(x => 'Foo', y => 10, z => 3);
114 } '... cannot assign a non-Int to x';
117 Point3D->new(x => 0, y => 10, z => 'Bar');
118 } '... cannot assign a non-Int to z';
120 # test some class introspection
122 can_ok('Point', 'meta');
123 isa_ok(Point->meta, 'Mouse::Meta::Class');
125 can_ok('Point3D', 'meta');
126 isa_ok(Point3D->meta, 'Mouse::Meta::Class');
128 isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
133 [ Point->meta->superclasses ],
135 '... Point got the automagic base class');
137 my @Point_methods = qw(meta new x y clear);
138 my @Point_attrs = ('x', 'y');
141 [ sort @Point_methods ],
142 [ sort Point->meta->get_method_list() ],
143 '... we match the method list for Point');
146 skip "Mouse has no method introspection", 1 + @Point_methods;
149 [ sort @Point_attrs ],
150 [ sort Point->meta->get_attribute_list() ],
151 '... we match the attribute list for Point');
153 foreach my $method (@Point_methods) {
154 ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"');
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');
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');
173 [ Point3D->meta->superclasses ],
175 '... Point3D gets the parent given to it');
177 my @Point3D_methods = qw(new meta clear);
178 my @Point3D_attrs = ('z');
181 skip "Mouse has no method introspection", 2 + @Point3D_methods;
184 [ sort @Point3D_methods ],
185 [ sort Point3D->meta->get_method_list() ],
186 '... we match the method list for Point3D');
189 [ sort @Point3D_attrs ],
190 [ sort Point3D->meta->get_attribute_list() ],
191 '... we match the attribute list for Point3D');
193 foreach my $method (@Point3D_methods) {
194 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
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');
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');