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