6 use Test::More tests => 57;
13 has 'x' => (isa => 'Int', is => 'rw', required => 1);
14 has 'y' => (isa => 'Int', is => 'rw', required => 1);
22 __PACKAGE__->meta->make_immutable( debug => 0 );
31 has 'z' => (isa => 'Int', is => 'rw', required => 1);
33 after 'clear' => sub {
38 __PACKAGE__->meta->make_immutable( debug => 0 );
41 my $point = Point->new(x => 1, y => 2);
42 isa_ok($point, 'Point');
43 isa_ok($point, 'Moose::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 } '... must provide required attributes to new';
61 is($point->x, 0, '... got the right (cleared) value for x');
62 is($point->y, 0, '... got the right (cleared) value for y');
64 # check the type constraints on the constructor
67 Point->new(x => 0, y => 0);
68 } '... can assign a 0 to x and y';
71 Point->new(x => 10, y => 'Foo');
72 } '... cannot assign a non-Int to y';
75 Point->new(x => 'Foo', y => 10);
76 } '... cannot assign a non-Int to x';
80 my $point3d = Point3D->new({ x => 10, y => 15, z => 3 });
81 isa_ok($point3d, 'Point3D');
82 isa_ok($point3d, 'Point');
83 isa_ok($point3d, 'Moose::Object');
85 is($point3d->x, 10, '... got the right value for x');
86 is($point3d->y, 15, '... got the right value for y');
87 is($point3d->{'z'}, 3, '... got the right value for z');
91 is($point3d->x, 0, '... got the right (cleared) value for x');
92 is($point3d->y, 0, '... got the right (cleared) value for y');
93 is($point3d->z, 0, '... got the right (cleared) value for z');
96 Point3D->new(x => 10, y => 'Foo', z => 3);
97 } '... cannot assign a non-Int to y';
100 Point3D->new(x => 'Foo', y => 10, z => 3);
101 } '... cannot assign a non-Int to x';
104 Point3D->new(x => 0, y => 10, z => 'Bar');
105 } '... cannot assign a non-Int to z';
108 Point3D->new(x => 10, y => 3);
109 } '... z is a required attribute for Point3D';
111 # test some class introspection
113 can_ok('Point', 'meta');
114 isa_ok(Point->meta, 'Moose::Meta::Class');
116 can_ok('Point3D', 'meta');
117 isa_ok(Point3D->meta, 'Moose::Meta::Class');
119 isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
124 [ Point->meta->superclasses ],
126 '... Point got the automagic base class');
128 my @Point_methods = qw(meta new x y clear);
129 my @Point_attrs = ('x', 'y');
132 [ sort @Point_methods ],
133 [ sort Point->meta->get_method_list() ],
134 '... we match the method list for Point');
137 [ sort @Point_attrs ],
138 [ sort Point->meta->get_attribute_list() ],
139 '... we match the attribute list for Point');
141 foreach my $method (@Point_methods) {
142 ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"');
145 foreach my $attr_name (@Point_attrs ) {
146 ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"');
147 my $attr = Point->meta->get_attribute($attr_name);
148 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
149 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
150 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');
156 [ Point3D->meta->superclasses ],
158 '... Point3D gets the parent given to it');
160 my @Point3D_methods = qw(new meta z clear);
161 my @Point3D_attrs = ('z');
164 [ sort @Point3D_methods ],
165 [ sort Point3D->meta->get_method_list() ],
166 '... we match the method list for Point3D');
169 [ sort @Point3D_attrs ],
170 [ sort Point3D->meta->get_attribute_list() ],
171 '... we match the attribute list for Point3D');
173 foreach my $method (@Point3D_methods) {
174 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
177 foreach my $attr_name (@Point3D_attrs ) {
178 ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"');
179 my $attr = Point3D->meta->get_attribute($attr_name);
180 ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint');
181 isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint');
182 is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint');