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