Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 011_create_class.t
CommitLineData
cbd9f942 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 5use Test::Fatal;
cbd9f942 6
efd3d14c 7use Class::MOP;
cbd9f942 8
88dd563c 9my $Point = Class::MOP::Class->create('Point' => (
10 version => '0.01',
cbd9f942 11 attributes => [
1aeb4c53 12 Class::MOP::Attribute->new('x' => (
cbd9f942 13 reader => 'x',
14 init_arg => 'x'
15 )),
1aeb4c53 16 Class::MOP::Attribute->new('y' => (
cbd9f942 17 accessor => 'y',
18 init_arg => 'y'
86a4d873 19 )),
cbd9f942 20 ],
21 methods => {
22 'new' => sub {
23 my $class = shift;
d69fb6b3 24 my $instance = $class->meta->new_object(@_);
cbd9f942 25 bless $instance => $class;
26 },
27 'clear' => sub {
28 my $self = shift;
1aeb4c53 29 $self->{'x'} = 0;
86a4d873 30 $self->{'y'} = 0;
cbd9f942 31 }
32 }
33));
34
88dd563c 35my $Point3D = Class::MOP::Class->create('Point3D' => (
86a4d873 36 version => '0.01',
cbd9f942 37 superclasses => [ 'Point' ],
38 attributes => [
1aeb4c53 39 Class::MOP::Attribute->new('z' => (
cbd9f942 40 default => 123
41 )),
42 ],
43 methods => {
44 'clear' => sub {
45 my $self = shift;
1aeb4c53 46 $self->{'z'} = 0;
cbd9f942 47 $self->SUPER::clear();
48 }
49 }
50));
51
52isa_ok($Point, 'Class::MOP::Class');
53isa_ok($Point3D, 'Class::MOP::Class');
54
55# ... test the classes themselves
56
57my $point = Point->new('x' => 2, 'y' => 3);
58isa_ok($point, 'Point');
59
60can_ok($point, 'x');
61can_ok($point, 'y');
62can_ok($point, 'clear');
63
64{
65 my $meta = $point->meta;
66 is($meta, Point->meta(), '... got the meta from the instance too');
67}
68
1aeb4c53 69is($point->y, 3, '... the y attribute was initialized correctly through the metaobject');
cbd9f942 70
71$point->y(42);
1aeb4c53 72is($point->y, 42, '... the y attribute was set properly with the accessor');
cbd9f942 73
1aeb4c53 74is($point->x, 2, '... the x attribute was initialized correctly through the metaobject');
cbd9f942 75
871e9eb5 76isnt( exception {
b9dfbf78 77 $point->x(42);
871e9eb5 78}, undef, '... cannot write to a read-only accessor' );
1aeb4c53 79is($point->x, 2, '... the x attribute was not altered');
cbd9f942 80
81$point->clear();
82
1aeb4c53 83is($point->y, 0, '... the y attribute was cleared correctly');
84is($point->x, 0, '... the x attribute was cleared correctly');
cbd9f942 85
1aeb4c53 86my $point3d = Point3D->new('x' => 1, 'y' => 2, 'z' => 3);
cbd9f942 87isa_ok($point3d, 'Point3D');
88isa_ok($point3d, 'Point');
89
90{
91 my $meta = $point3d->meta;
92 is($meta, Point3D->meta(), '... got the meta from the instance too');
93}
94
95can_ok($point3d, 'x');
96can_ok($point3d, 'y');
97can_ok($point3d, 'clear');
98
1aeb4c53 99is($point3d->x, 1, '... the x attribute was initialized correctly through the metaobject');
100is($point3d->y, 2, '... the y attribute was initialized correctly through the metaobject');
101is($point3d->{'z'}, 3, '... the z attribute was initialized correctly through the metaobject');
cbd9f942 102
103{
104 my $point3d = Point3D->new();
105 isa_ok($point3d, 'Point3D');
86a4d873 106
1aeb4c53 107 is($point3d->x, undef, '... the x attribute was not initialized');
108 is($point3d->y, undef, '... the y attribute was not initialized');
86a4d873 109 is($point3d->{'z'}, 123, '... the z attribute was initialized correctly through the metaobject');
cbd9f942 110
86a4d873 111}
cbd9f942 112
86a4d873 113done_testing;