4 use Test::More 'no_plan';
10 # =begin testing SETUP
16 has 'x' => (isa => 'Int', is => 'rw', required => 1);
17 has 'y' => (isa => 'Int', is => 'rw', required => 1);
30 has 'z' => (isa => 'Int', is => 'rw', required => 1);
32 after 'clear' => sub {
39 # hash or hashrefs are ok for the constructor
40 my $point1 = Point->new(x => 5, y => 7);
41 my $point2 = Point->new({x => 5, y => 7});
43 my $point3d = Point3D->new(x => 5, y => 42, z => -5);
50 my $point = Point->new( x => 1, y => 2 );
51 isa_ok( $point, 'Point' );
52 isa_ok( $point, 'Mouse::Object' );
54 is( $point->x, 1, '... got the right value for x' );
55 is( $point->y, 2, '... got the right value for y' );
58 is( $point->y, 10, '... got the right (changed) value for y' );
63 '... cannot assign a non-Int to y';
68 '... must provide required attributes to new';
72 is( $point->x, 0, '... got the right (cleared) value for x' );
73 is( $point->y, 0, '... got the right (cleared) value for y' );
75 # check the type constraints on the constructor
78 Point->new( x => 0, y => 0 );
80 '... can assign a 0 to x and y';
83 Point->new( x => 10, y => 'Foo' );
85 '... cannot assign a non-Int to y';
88 Point->new( x => 'Foo', y => 10 );
90 '... cannot assign a non-Int to x';
94 my $point3d = Point3D->new( { x => 10, y => 15, z => 3 } );
95 isa_ok( $point3d, 'Point3D' );
96 isa_ok( $point3d, 'Point' );
97 isa_ok( $point3d, 'Mouse::Object' );
99 is( $point3d->x, 10, '... got the right value for x' );
100 is( $point3d->y, 15, '... got the right value for y' );
101 is( $point3d->{'z'}, 3, '... got the right value for z' );
105 is( $point3d->x, 0, '... got the right (cleared) value for x' );
106 is( $point3d->y, 0, '... got the right (cleared) value for y' );
107 is( $point3d->z, 0, '... got the right (cleared) value for z' );
110 Point3D->new( x => 10, y => 'Foo', z => 3 );
112 '... cannot assign a non-Int to y';
115 Point3D->new( x => 'Foo', y => 10, z => 3 );
117 '... cannot assign a non-Int to x';
120 Point3D->new( x => 0, y => 10, z => 'Bar' );
122 '... cannot assign a non-Int to z';
125 Point3D->new( x => 10, y => 3 );
127 '... z is a required attribute for Point3D';
129 # test some class introspection
131 can_ok( 'Point', 'meta' );
132 isa_ok( Point->meta, 'Mouse::Meta::Class' );
134 can_ok( 'Point3D', 'meta' );
135 isa_ok( Point3D->meta, 'Mouse::Meta::Class' );
137 isnt( Point->meta, Point3D->meta,
138 '... they are different metaclasses as well' );
143 [ Point->meta->superclasses ],
145 '... Point got the automagic base class'
148 my @Point_methods = qw(meta x y clear);
149 my @Point_attrs = ( 'x', 'y' );
152 [ sort @Point_methods ],
153 [ sort Point->meta->get_method_list() ],
154 '... we match the method list for Point'
158 [ sort @Point_attrs ],
159 [ sort Point->meta->get_attribute_list() ],
160 '... we match the attribute list for Point'
163 foreach my $method (@Point_methods) {
164 ok( Point->meta->has_method($method),
165 '... Point has the method "' . $method . '"' );
168 foreach my $attr_name (@Point_attrs) {
169 ok( Point->meta->has_attribute($attr_name),
170 '... Point has the attribute "' . $attr_name . '"' );
171 my $attr = Point->meta->get_attribute($attr_name);
172 ok( $attr->has_type_constraint,
173 '... Attribute ' . $attr_name . ' has a type constraint' );
174 isa_ok( $attr->type_constraint, 'Mouse::Meta::TypeConstraint' );
175 is( $attr->type_constraint->name, 'Int',
176 '... Attribute ' . $attr_name . ' has an Int type constraint' );
182 [ Point3D->meta->superclasses ],
184 '... Point3D gets the parent given to it'
187 my @Point3D_methods = qw( meta z clear );
188 my @Point3D_attrs = ('z');
191 [ sort @Point3D_methods ],
192 [ sort Point3D->meta->get_method_list() ],
193 '... we match the method list for Point3D'
197 [ sort @Point3D_attrs ],
198 [ sort Point3D->meta->get_attribute_list() ],
199 '... we match the attribute list for Point3D'
202 foreach my $method (@Point3D_methods) {
203 ok( Point3D->meta->has_method($method),
204 '... Point3D has the method "' . $method . '"' );
207 foreach my $attr_name (@Point3D_attrs) {
208 ok( Point3D->meta->has_attribute($attr_name),
209 '... Point3D has the attribute "' . $attr_name . '"' );
210 my $attr = Point3D->meta->get_attribute($attr_name);
211 ok( $attr->has_type_constraint,
212 '... Attribute ' . $attr_name . ' has a type constraint' );
213 isa_ok( $attr->type_constraint, 'Mouse::Meta::TypeConstraint' );
214 is( $attr->type_constraint->name, 'Int',
215 '... Attribute ' . $attr_name . ' has an Int type constraint' );