method modifiers
[gitmo/Class-MOP.git] / t / 011_create_class.t
CommitLineData
cbd9f942 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
1a7ebbb3 6use Test::More tests => 27;
cbd9f942 7use Test::Exception;
8
9BEGIN {
aa448b16 10 use_ok('Class::MOP');
cbd9f942 11}
12
13my $Point = Class::MOP::Class->create('Point' => '0.01' => (
14 attributes => [
15 Class::MOP::Attribute->new('$.x' => (
16 reader => 'x',
17 init_arg => 'x'
18 )),
19 Class::MOP::Attribute->new('$.y' => (
20 accessor => 'y',
21 init_arg => 'y'
22 )),
23 ],
24 methods => {
25 'new' => sub {
26 my $class = shift;
27 my $instance = $class->meta->construct_instance(@_);
28 bless $instance => $class;
29 },
30 'clear' => sub {
31 my $self = shift;
32 $self->{'$.x'} = 0;
33 $self->{'$.y'} = 0;
34 }
35 }
36));
37
38my $Point3D = Class::MOP::Class->create('Point3D' => '0.01' => (
39 superclasses => [ 'Point' ],
40 attributes => [
41 Class::MOP::Attribute->new('$:z' => (
42 default => 123
43 )),
44 ],
45 methods => {
46 'clear' => sub {
47 my $self = shift;
48 $self->{'$:z'} = 0;
49 $self->SUPER::clear();
50 }
51 }
52));
53
54isa_ok($Point, 'Class::MOP::Class');
55isa_ok($Point3D, 'Class::MOP::Class');
56
57# ... test the classes themselves
58
59my $point = Point->new('x' => 2, 'y' => 3);
60isa_ok($point, 'Point');
61
62can_ok($point, 'x');
63can_ok($point, 'y');
64can_ok($point, 'clear');
65
66{
67 my $meta = $point->meta;
68 is($meta, Point->meta(), '... got the meta from the instance too');
69}
70
71is($point->y, 3, '... the $.y attribute was initialized correctly through the metaobject');
72
73$point->y(42);
74is($point->y, 42, '... the $.y attribute was set properly with the accessor');
75
76is($point->x, 2, '... the $.x attribute was initialized correctly through the metaobject');
77
78$point->x(42);
79is($point->x, 2, '... the $.x attribute was not altered');
80
81$point->clear();
82
83is($point->y, 0, '... the $.y attribute was cleared correctly');
84is($point->x, 0, '... the $.x attribute was cleared correctly');
85
86my $point3d = Point3D->new('x' => 1, 'y' => 2, '$:z' => 3);
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
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');
102
103{
104 my $point3d = Point3D->new();
105 isa_ok($point3d, 'Point3D');
106
107 is($point3d->x, undef, '... the $.x attribute was not initialized');
108 is($point3d->y, undef, '... the $.y attribute was not initialized');
109 is($point3d->{'$:z'}, 123, '... the $:z attribute was initialized correctly through the metaobject');
110
111}
112
113