6 use Test::More tests => 55;
19 has 'x' => (isa => 'Int', is => 'ro');
20 has 'y' => (isa => 'Int', is => 'rw');
35 has 'z' => (isa => 'Int');
37 after 'clear' => sub {
44 my $point = Point->new(x => 1, y => 2);
45 isa_ok($point, 'Point');
46 isa_ok($point, 'Moose::Object');
48 is($point->x, 1, '... got the right value for x');
49 is($point->y, 2, '... got the right value for y');
52 is($point->y, 10, '... got the right (changed) value for y');
56 } '... cannot assign a non-Int to y';
59 is($point->x, 1, '... got the right (un-changed) value for x');
63 is($point->x, 0, '... got the right (cleared) value for x');
64 is($point->y, 0, '... got the right (cleared) value for y');
66 # check the type constraints on the constructor
69 Point->new(x => 0, y => 0);
70 } '... can assign a 0 to x and y';
73 Point->new(x => 10, y => 'Foo');
74 } '... cannot assign a non-Int to y';
77 Point->new(x => 'Foo', y => 10);
78 } '... cannot assign a non-Int to x';
82 my $point3d = Point3D->new(x => 10, y => 15, z => 3);
83 isa_ok($point3d, 'Point3D');
84 isa_ok($point3d, 'Point');
85 isa_ok($point3d, 'Moose::Object');
87 is($point3d->x, 10, '... got the right value for x');
88 is($point3d->y, 15, '... got the right value for y');
89 is($point3d->{'z'}, 3, '... got the right value for z');
93 } '... there is no method for z';
97 is($point3d->x, 0, '... got the right (cleared) value for x');
98 is($point3d->y, 0, '... got the right (cleared) value for y');
99 is($point3d->{'z'}, 0, '... got the right (cleared) value for z');
102 Point3D->new(x => 10, y => 'Foo', z => 3);
103 } '... cannot assign a non-Int to y';
106 Point3D->new(x => 'Foo', y => 10, z => 3);
107 } '... cannot assign a non-Int to x';
110 Point3D->new(x => 0, y => 10, z => 'Bar');
111 } '... cannot assign a non-Int to z';
113 # test some class introspection
115 can_ok('Point', 'meta');
116 isa_ok(Point->meta, 'Moose::Meta::Class');
118 can_ok('Point3D', 'meta');
119 isa_ok(Point3D->meta, 'Moose::Meta::Class');
121 isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
126 [ Point->meta->superclasses ],
128 '... Point got the automagic base class');
130 my @Point_methods = qw(meta x y clear);
131 my @Point_attrs = ('x', 'y');
134 [ sort @Point_methods ],
135 [ sort Point->meta->get_method_list() ],
136 '... we match the method list for Point');
139 [ sort @Point_attrs ],
140 [ sort Point->meta->get_attribute_list() ],
141 '... we match the attribute list for Point');
143 foreach my $method (@Point_methods) {
144 ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"');
147 foreach my $attr_name (@Point_attrs ) {
148 ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"');
149 my $attr = Point->meta->get_attribute($attr_name);
150 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
151 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
152 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');
158 [ Point3D->meta->superclasses ],
160 '... Point3D gets the parent given to it');
162 my @Point3D_methods = qw(meta clear);
163 my @Point3D_attrs = ('z');
166 [ sort @Point3D_methods ],
167 [ sort Point3D->meta->get_method_list() ],
168 '... we match the method list for Point3D');
171 [ sort @Point3D_attrs ],
172 [ sort Point3D->meta->get_attribute_list() ],
173 '... we match the attribute list for Point3D');
175 foreach my $method (@Point3D_methods) {
176 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
179 foreach my $attr_name (@Point3D_attrs ) {
180 ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"');
181 my $attr = Point3D->meta->get_attribute($attr_name);
182 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
183 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
184 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');