Commit | Line | Data |
fcd84ca9 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d7f17ebb |
6 | use Test::More tests => 56; |
bc1e29b5 |
7 | use Test::Exception; |
fcd84ca9 |
8 | |
9 | BEGIN { |
10 | use_ok('Moose'); |
11 | } |
12 | |
13 | { |
14 | package Point; |
e5ebe4ce |
15 | use strict; |
16 | use warnings; |
fcd84ca9 |
17 | use Moose; |
29db16a9 |
18 | |
182134e8 |
19 | has 'x' => (isa => 'Int', is => 'ro'); |
20 | has 'y' => (isa => 'Int', is => 'rw'); |
fcd84ca9 |
21 | |
22 | sub clear { |
23 | my $self = shift; |
a15dff8d |
24 | $self->{x} = 0; |
fcd84ca9 |
25 | $self->y(0); |
26 | } |
27 | |
28 | package Point3D; |
e5ebe4ce |
29 | use strict; |
30 | use warnings; |
fcd84ca9 |
31 | use Moose; |
32 | |
bc1e29b5 |
33 | extends 'Point'; |
fcd84ca9 |
34 | |
182134e8 |
35 | has 'z' => (isa => 'Int'); |
fcd84ca9 |
36 | |
3c7278fb |
37 | after 'clear' => sub { |
fcd84ca9 |
38 | my $self = shift; |
a15dff8d |
39 | $self->{z} = 0; |
3c7278fb |
40 | }; |
fcd84ca9 |
41 | |
42 | } |
43 | |
44 | my $point = Point->new(x => 1, y => 2); |
45 | isa_ok($point, 'Point'); |
bc1e29b5 |
46 | isa_ok($point, 'Moose::Object'); |
fcd84ca9 |
47 | |
48 | is($point->x, 1, '... got the right value for x'); |
49 | is($point->y, 2, '... got the right value for y'); |
50 | |
51 | $point->y(10); |
fcd84ca9 |
52 | is($point->y, 10, '... got the right (changed) value for y'); |
53 | |
a15dff8d |
54 | dies_ok { |
55 | $point->y('Foo'); |
56 | } '... cannot assign a non-Int to y'; |
57 | |
d7f17ebb |
58 | dies_ok { |
59 | $point->x(1000); |
60 | } '... cannot assign to a read-only method'; |
bc1e29b5 |
61 | is($point->x, 1, '... got the right (un-changed) value for x'); |
62 | |
fcd84ca9 |
63 | $point->clear(); |
64 | |
65 | is($point->x, 0, '... got the right (cleared) value for x'); |
66 | is($point->y, 0, '... got the right (cleared) value for y'); |
67 | |
a15dff8d |
68 | # check the type constraints on the constructor |
69 | |
70 | lives_ok { |
71 | Point->new(x => 0, y => 0); |
72 | } '... can assign a 0 to x and y'; |
73 | |
74 | dies_ok { |
75 | Point->new(x => 10, y => 'Foo'); |
76 | } '... cannot assign a non-Int to y'; |
77 | |
78 | dies_ok { |
79 | Point->new(x => 'Foo', y => 10); |
80 | } '... cannot assign a non-Int to x'; |
81 | |
82 | # Point3D |
83 | |
fcd84ca9 |
84 | my $point3d = Point3D->new(x => 10, y => 15, z => 3); |
85 | isa_ok($point3d, 'Point3D'); |
86 | isa_ok($point3d, 'Point'); |
bc1e29b5 |
87 | isa_ok($point3d, 'Moose::Object'); |
fcd84ca9 |
88 | |
89 | is($point3d->x, 10, '... got the right value for x'); |
90 | is($point3d->y, 15, '... got the right value for y'); |
a15dff8d |
91 | is($point3d->{'z'}, 3, '... got the right value for z'); |
fcd84ca9 |
92 | |
bc1e29b5 |
93 | dies_ok { |
94 | $point3d->z; |
95 | } '... there is no method for z'; |
96 | |
fcd84ca9 |
97 | $point3d->clear(); |
98 | |
99 | is($point3d->x, 0, '... got the right (cleared) value for x'); |
100 | is($point3d->y, 0, '... got the right (cleared) value for y'); |
a15dff8d |
101 | is($point3d->{'z'}, 0, '... got the right (cleared) value for z'); |
102 | |
103 | dies_ok { |
104 | Point3D->new(x => 10, y => 'Foo', z => 3); |
105 | } '... cannot assign a non-Int to y'; |
106 | |
107 | dies_ok { |
108 | Point3D->new(x => 'Foo', y => 10, z => 3); |
109 | } '... cannot assign a non-Int to x'; |
110 | |
111 | dies_ok { |
112 | Point3D->new(x => 0, y => 10, z => 'Bar'); |
113 | } '... cannot assign a non-Int to z'; |
bc1e29b5 |
114 | |
115 | # test some class introspection |
116 | |
117 | can_ok('Point', 'meta'); |
118 | isa_ok(Point->meta, 'Moose::Meta::Class'); |
119 | |
120 | can_ok('Point3D', 'meta'); |
121 | isa_ok(Point3D->meta, 'Moose::Meta::Class'); |
122 | |
123 | isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well'); |
124 | |
125 | # poke at Point |
126 | |
127 | is_deeply( |
128 | [ Point->meta->superclasses ], |
129 | [ 'Moose::Object' ], |
130 | '... Point got the automagic base class'); |
131 | |
e522431d |
132 | my @Point_methods = qw(meta x y clear); |
a15dff8d |
133 | my @Point_attrs = ('x', 'y'); |
bc1e29b5 |
134 | |
135 | is_deeply( |
136 | [ sort @Point_methods ], |
137 | [ sort Point->meta->get_method_list() ], |
138 | '... we match the method list for Point'); |
a15dff8d |
139 | |
140 | is_deeply( |
141 | [ sort @Point_attrs ], |
142 | [ sort Point->meta->get_attribute_list() ], |
143 | '... we match the attribute list for Point'); |
bc1e29b5 |
144 | |
145 | foreach my $method (@Point_methods) { |
146 | ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"'); |
147 | } |
148 | |
7415b2cb |
149 | foreach my $attr_name (@Point_attrs ) { |
150 | ok(Point->meta->has_attribute($attr_name), '... Point has the attribute "' . $attr_name . '"'); |
151 | my $attr = Point->meta->get_attribute($attr_name); |
152 | ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint'); |
153 | isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint'); |
154 | is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint'); |
155 | } |
156 | |
bc1e29b5 |
157 | # poke at Point3D |
158 | |
159 | is_deeply( |
160 | [ Point3D->meta->superclasses ], |
161 | [ 'Point' ], |
162 | '... Point3D gets the parent given to it'); |
163 | |
e522431d |
164 | my @Point3D_methods = qw(meta clear); |
a15dff8d |
165 | my @Point3D_attrs = ('z'); |
bc1e29b5 |
166 | |
167 | is_deeply( |
168 | [ sort @Point3D_methods ], |
169 | [ sort Point3D->meta->get_method_list() ], |
170 | '... we match the method list for Point3D'); |
a15dff8d |
171 | |
172 | is_deeply( |
173 | [ sort @Point3D_attrs ], |
174 | [ sort Point3D->meta->get_attribute_list() ], |
175 | '... we match the attribute list for Point3D'); |
bc1e29b5 |
176 | |
177 | foreach my $method (@Point3D_methods) { |
178 | ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"'); |
179 | } |
7415b2cb |
180 | |
181 | foreach my $attr_name (@Point3D_attrs ) { |
182 | ok(Point3D->meta->has_attribute($attr_name), '... Point3D has the attribute "' . $attr_name . '"'); |
183 | my $attr = Point3D->meta->get_attribute($attr_name); |
184 | ok($attr->has_type_constraint, '... Attribute ' . $attr_name . ' has a type constraint'); |
185 | isa_ok($attr->type_constraint, 'Moose::Meta::TypeConstraint'); |
186 | is($attr->type_constraint->name, 'Int', '... Attribute ' . $attr_name . ' has an Int type constraint'); |
187 | } |