6 use Test::More tests => 56;
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';
60 } '... cannot assign to a read-only method';
61 is($point->x, 1, '... got the right (un-changed) value for x');
65 is($point->x, 0, '... got the right (cleared) value for x');
66 is($point->y, 0, '... got the right (cleared) value for y');
68 # check the type constraints on the constructor
71 Point->new(x => 0, y => 0);
72 } '... can assign a 0 to x and y';
75 Point->new(x => 10, y => 'Foo');
76 } '... cannot assign a non-Int to y';
79 Point->new(x => 'Foo', y => 10);
80 } '... cannot assign a non-Int to x';
84 my $point3d = Point3D->new({ x => 10, y => 15, z => 3 });
85 isa_ok($point3d, 'Point3D');
86 isa_ok($point3d, 'Point');
87 isa_ok($point3d, 'Moose::Object');
89 is($point3d->x, 10, '... got the right value for x');
90 is($point3d->y, 15, '... got the right value for y');
91 is($point3d->{'z'}, 3, '... got the right value for z');
95 } '... there is no method for z';
99 is($point3d->x, 0, '... got the right (cleared) value for x');
100 is($point3d->y, 0, '... got the right (cleared) value for y');
101 is($point3d->{'z'}, 0, '... got the right (cleared) value for z');
104 Point3D->new(x => 10, y => 'Foo', z => 3);
105 } '... cannot assign a non-Int to y';
108 Point3D->new(x => 'Foo', y => 10, z => 3);
109 } '... cannot assign a non-Int to x';
112 Point3D->new(x => 0, y => 10, z => 'Bar');
113 } '... cannot assign a non-Int to z';
115 # test some class introspection
117 can_ok('Point', 'meta');
118 isa_ok(Point->meta, 'Moose::Meta::Class');
120 can_ok('Point3D', 'meta');
121 isa_ok(Point3D->meta, 'Moose::Meta::Class');
123 isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
128 [ Point->meta->superclasses ],
130 '... Point got the automagic base class');
132 my @Point_methods = qw(meta x y clear);
133 my @Point_attrs = ('x', 'y');
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 . '"');
149 foreach my $attr_name (@Point_attrs ) {
150 ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"');
151 my $attr = Point->meta->get_attribute($attr_name);
152 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
153 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
154 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');
160 [ Point3D->meta->superclasses ],
162 '... Point3D gets the parent given to it');
164 my @Point3D_methods = qw(meta clear);
165 my @Point3D_attrs = ('z');
168 [ sort @Point3D_methods ],
169 [ sort Point3D->meta->get_method_list() ],
170 '... we match the method list for Point3D');
173 [ sort @Point3D_attrs ],
174 [ sort Point3D->meta->get_attribute_list() ],
175 '... we match the attribute list for Point3D');
177 foreach my $method (@Point3D_methods) {
178 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
181 foreach my $attr_name (@Point3D_attrs ) {
182 ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"');
183 my $attr = Point3D->meta->get_attribute($attr_name);
184 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
185 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
186 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');