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