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