added some tests for SDL::Game::Rect
[sdlgit/SDL_perl.git] / t / sdlgamerect.t
1 use Test::More tests => 87;
2 use strict;
3
4 use_ok( 'SDL::Game::Rect' ); 
5   
6 can_ok ('SDL::Game::Rect', qw/
7         new
8         x 
9         y 
10         width 
11         height
12         w
13         h
14         top
15         left
16         centerx
17         centery
18          /);
19
20 my $rect = SDL::Game::Rect->new();
21
22 isa_ok ($rect, 'SDL::Game::Rect','new went ok');
23
24 foreach my $attr (qw(x y top    left  width   height 
25                      w h bottom right centerx centery)
26                  ) {
27     is ($rect->$attr, 0, "$attr is 0");
28 }
29
30 # set and get at the same time (and testing method aliases)
31 is ($rect->left(15), 15, 'left is now 15');
32 is ($rect->x, 15, 'x and left point to the same place');
33 is ($rect->x(12), 12, 'x is now 12');
34 is ($rect->left, 12, 'left is an alias to x');
35
36 is ($rect->top(132), 132, 'top is now 132');
37 is ($rect->y, 132, 'y and top point to the same place');
38 is ($rect->y(123), 123, 'y is now 123');
39 is ($rect->top, 123, 'top is an alias to y');
40
41 is ($rect->w(54), 54, 'w is now 54');
42 is ($rect->width, 54, 'w and width point to the same place');
43 is ($rect->width(45), 45, 'w is now 45');
44 is ($rect->w, 45, 'w is an alias to width');
45
46 is ($rect->h(76), 76, 'h is now 76');
47 is ($rect->height, 76, 'h and height point to the same place');
48 is ($rect->height(67), 67, 'h is now 67');
49 is ($rect->h, 67, 'h is an alias to height');
50
51 # get alone
52 is ($rect->x(), 12, 'x is 12');
53 is ($rect->left(), 12, 'left is 12');
54 is ($rect->y(), 123, 'y is 123');
55 is ($rect->top(), 123, 'top is 123');
56 is ($rect->width(), 45, 'width is 45');
57 is ($rect->w(), 45, 'w is 45');
58 is ($rect->height(), 67, 'height is 67');
59 is ($rect->h(), 67, 'h is 67');
60
61 # other helpers
62 is ($rect->bottom, 190, 'bottom should be relative to heigth and top');
63 is ($rect->bottom(189), 189, 'changing bottom value');
64 is ($rect->bottom, 189, 'checking bottom value again');
65 is ($rect->top, 122, 'top value should have been updated after bottom change');
66 is ($rect->height, 67, 'height should have stayed the same');
67
68 is ($rect->centery, 155, 'checking vertical center');
69 is ($rect->centery(154), 154, 'changing centery value');
70 is ($rect->centery, 154, 'checking centery value again');
71 is ($rect->top, 121, 'top value should have been updated after centery change');
72 is ($rect->height, 67, 'height should have stayed the same');
73
74 is ($rect->right, 57, 'right should be relative to width and left');
75 is ($rect->right(56), 56, 'changing right value');
76 is ($rect->right, 56, 'checking right value again');
77 is ($rect->left, 11, 'left value should have been updated after bottom change');
78 is ($rect->width, 45, 'width should have stayed the same');
79
80 is ($rect->centerx, 33, 'checking horizontal center');
81 is ($rect->centerx(32), 32, 'changing centerx value');
82 is ($rect->centerx, 32, 'checking centerx value again');
83 is ($rect->left, 10, 'left value should have been updated after bottom change');
84 is ($rect->width, 45, 'width should have stayed the same');
85
86 # checking two-valued accessors
87 can_ok ('SDL::Game::Rect', qw/
88     size
89         center
90         topleft
91         midleft
92         bottomleft
93         topright
94         midright
95         bottomright
96         midtop
97         midbottom
98          /);
99
100
101 is_deeply ( [$rect->center], [32, 154], 'checking center pair');
102 $rect->center(undef, undef);
103 is($rect->centerx, 32, 'center() does nothing when passed undef');
104 is($rect->centery, 154, 'center() does nothing when passed undef');
105 $rect->center(undef, 200);
106 is($rect->centerx, 32, 'center() does nothing for X when passed undef');
107 is($rect->centery, 200, 'center() works on one-parameter (y)');
108 $rect->center(7, undef);
109 is($rect->centerx, 7, 'center() works on one-parameter (x)');
110 is($rect->centery, 200, 'center() does nothing for Y when passed undef');
111 $rect->center(32, 154);
112 is($rect->centerx, 32, 'center() can be used as an acessor for x');
113 is($rect->centery, 154, 'center() can be used as an acessor for y');
114
115 is_deeply ( [$rect->topleft], [121, 10], 'checking topleft pair');
116 $rect->topleft(undef, undef);
117 is($rect->top, 121, 'topleft() does nothing when passed undef');
118 is($rect->left, 10, 'topleft() does nothing when passed undef');
119 $rect->topleft(undef, 200);
120 is($rect->top, 121, 'topleft() does nothing for Y when passed undef');
121 is($rect->left, 200, 'topleft() works on one-parameter (x)');
122 $rect->topleft(7, undef);
123 is($rect->top, 7, 'topleft() works on one-parameter (y)');
124 is($rect->left, 200, 'topleft() does nothing for X when passed undef');
125 $rect->topleft(121, 10);
126 is($rect->top, 121, 'topleft() can be used as an acessor for y');
127 is($rect->left, 10, 'topleft() can be used as an acessor for x');
128
129 is_deeply ( [$rect->midleft], [154, 10], 'checking midleft pair');
130 $rect->midleft(undef, undef);
131 is($rect->centery, 154, 'midleft() does nothing when passed undef');
132 is($rect->left, 10, 'midleft() does nothing when passed undef');
133 $rect->midleft(undef, 200);
134 is($rect->centery, 154, 'midleft() does nothing for Y when passed undef');
135 is($rect->left, 200, 'midleft() works on one-parameter (x)');
136 $rect->midleft(7, undef);
137 is($rect->centery, 7, 'midleft() works on one-parameter (y)');
138 is($rect->left, 200, 'midleft() does nothing for X when passed undef');
139 $rect->midleft(154, 10);
140 is($rect->centery, 154, 'midleft() can be used as an acessor for y');
141 is($rect->left, 10, 'midleft() can be used as an acessor for x');