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