uploadin
[gitmo/Moose.git] / t / 001_basic.t
CommitLineData
fcd84ca9 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
bc1e29b5 6use Test::More tests => 32;
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;
18
19 has '$.x' => (reader => 'x');
20 has '$.y' => (accessor => 'y');
21
22 sub clear {
23 my $self = shift;
24 $self->{'$.x'} = 0;
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
35 has '$:z';
36
3c7278fb 37 after 'clear' => sub {
fcd84ca9 38 my $self = shift;
fcd84ca9 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
bc1e29b5 54$point->x(1000);
55is($point->x, 1, '... got the right (un-changed) value for x');
56
fcd84ca9 57$point->clear();
58
59is($point->x, 0, '... got the right (cleared) value for x');
60is($point->y, 0, '... got the right (cleared) value for y');
61
62my $point3d = Point3D->new(x => 10, y => 15, z => 3);
63isa_ok($point3d, 'Point3D');
64isa_ok($point3d, 'Point');
bc1e29b5 65isa_ok($point3d, 'Moose::Object');
fcd84ca9 66
67is($point3d->x, 10, '... got the right value for x');
68is($point3d->y, 15, '... got the right value for y');
69is($point3d->{'$:z'}, 3, '... got the right value for z');
70
bc1e29b5 71dies_ok {
72 $point3d->z;
73} '... there is no method for z';
74
fcd84ca9 75$point3d->clear();
76
77is($point3d->x, 0, '... got the right (cleared) value for x');
78is($point3d->y, 0, '... got the right (cleared) value for y');
79is($point3d->{'$:z'}, 0, '... got the right (cleared) value for z');
bc1e29b5 80
81# test some class introspection
82
83can_ok('Point', 'meta');
84isa_ok(Point->meta, 'Moose::Meta::Class');
85
86can_ok('Point3D', 'meta');
87isa_ok(Point3D->meta, 'Moose::Meta::Class');
88
89isnt(Point->meta, Point3D->meta, '... they are different metaclasses as well');
90
91# poke at Point
92
93is_deeply(
94 [ Point->meta->superclasses ],
95 [ 'Moose::Object' ],
96 '... Point got the automagic base class');
97
98my @Point_methods = qw(x y clear);
99
100is_deeply(
101 [ sort @Point_methods ],
102 [ sort Point->meta->get_method_list() ],
103 '... we match the method list for Point');
104
105foreach my $method (@Point_methods) {
106 ok(Point->meta->has_method($method), '... Point has the method "' . $method . '"');
107}
108
109# poke at Point3D
110
111is_deeply(
112 [ Point3D->meta->superclasses ],
113 [ 'Point' ],
114 '... Point3D gets the parent given to it');
115
116my @Point3D_methods = qw(clear);
117
118is_deeply(
119 [ sort @Point3D_methods ],
120 [ sort Point3D->meta->get_method_list() ],
121 '... we match the method list for Point3D');
122
123foreach my $method (@Point3D_methods) {
124 ok(Point3D->meta->has_method($method), '... Point3D has the method "' . $method . '"');
125}
126
127