| 1 |
import unittest |
| 2 |
import pygame |
| 3 |
from pgzero.rect import ZRect as Rect |
| 4 |
|
| 5 |
|
| 6 |
class RectTypeTest( unittest.TestCase ): |
| 7 |
def testConstructionXYWidthHeight( self ): |
| 8 |
r = Rect(1,2,3,4) |
| 9 |
self.assertEqual( 1, r.left ) |
| 10 |
self.assertEqual( 2, r.top ) |
| 11 |
self.assertEqual( 3, r.width ) |
| 12 |
self.assertEqual( 4, r.height ) |
| 13 |
|
| 14 |
def testConstructionTopLeftSize( self ): |
| 15 |
r = Rect( (1,2), (3,4) ) |
| 16 |
self.assertEqual( 1, r.left ) |
| 17 |
self.assertEqual( 2, r.top ) |
| 18 |
self.assertEqual( 3, r.width ) |
| 19 |
self.assertEqual( 4, r.height ) |
| 20 |
|
| 21 |
def testCalculatedAttributes( self ): |
| 22 |
r = Rect( 1, 2, 3, 4 ) |
| 23 |
|
| 24 |
self.assertEqual( r.left+r.width, r.right ) |
| 25 |
self.assertEqual( r.top+r.height, r.bottom ) |
| 26 |
self.assertEqual( (r.width,r.height), r.size ) |
| 27 |
self.assertEqual( (r.left,r.top), r.topleft ) |
| 28 |
self.assertEqual( (r.right,r.top), r.topright ) |
| 29 |
self.assertEqual( (r.left,r.bottom), r.bottomleft ) |
| 30 |
self.assertEqual( (r.right,r.bottom), r.bottomright ) |
| 31 |
|
| 32 |
midx = r.left + r.width / 2 |
| 33 |
midy = r.top + r.height / 2 |
| 34 |
|
| 35 |
self.assertEqual( midx, r.centerx ) |
| 36 |
self.assertEqual( midy, r.centery ) |
| 37 |
self.assertEqual( (r.centerx,r.centery), r.center ) |
| 38 |
self.assertEqual( (r.centerx,r.top), r.midtop ) |
| 39 |
self.assertEqual( (r.centerx,r.bottom), r.midbottom ) |
| 40 |
self.assertEqual( (r.left,r.centery), r.midleft ) |
| 41 |
self.assertEqual( (r.right,r.centery), r.midright ) |
| 42 |
|
| 43 |
def test_normalize( self ): |
| 44 |
r = Rect( 1, 2, -3, -6 ) |
| 45 |
r2 = Rect(r) |
| 46 |
r2.normalize() |
| 47 |
self.assertTrue( r2.width >= 0 ) |
| 48 |
self.assertTrue( r2.height >= 0 ) |
| 49 |
self.assertEqual( (abs(r.width),abs(r.height)), r2.size ) |
| 50 |
self.assertEqual( (-2,-4), r2.topleft ) |
| 51 |
|
| 52 |
def test_left( self ): |
| 53 |
"""Changing the left attribute moves the rect and does not change |
| 54 |
the rect's width |
| 55 |
""" |
| 56 |
r = Rect( 1, 2, 3, 4 ) |
| 57 |
new_left = 10 |
| 58 |
|
| 59 |
r.left = new_left |
| 60 |
self.assertEqual( new_left, r.left ) |
| 61 |
self.assertEqual( Rect(new_left,2,3,4), r ) |
| 62 |
|
| 63 |
def test_right( self ): |
| 64 |
"""Changing the right attribute moves the rect and does not change |
| 65 |
the rect's width |
| 66 |
""" |
| 67 |
r = Rect( 1, 2, 3, 4 ) |
| 68 |
new_right = r.right + 20 |
| 69 |
expected_left = r.left + 20 |
| 70 |
old_width = r.width |
| 71 |
|
| 72 |
r.right = new_right |
| 73 |
self.assertEqual( new_right, r.right ) |
| 74 |
self.assertEqual( expected_left, r.left ) |
| 75 |
self.assertEqual( old_width, r.width ) |
| 76 |
|
| 77 |
def test_top( self ): |
| 78 |
"""Changing the top attribute moves the rect and does not change |
| 79 |
the rect's width |
| 80 |
""" |
| 81 |
r = Rect( 1, 2, 3, 4 ) |
| 82 |
new_top = 10 |
| 83 |
|
| 84 |
r.top = new_top |
| 85 |
self.assertEqual( Rect(1,new_top,3,4), r ) |
| 86 |
self.assertEqual( new_top, r.top ) |
| 87 |
|
| 88 |
def test_bottom( self ): |
| 89 |
"""Changing the bottom attribute moves the rect and does not change |
| 90 |
the rect's height |
| 91 |
""" |
| 92 |
r = Rect( 1, 2, 3, 4 ) |
| 93 |
new_bottom = r.bottom + 20 |
| 94 |
expected_top = r.top + 20 |
| 95 |
old_height = r.height |
| 96 |
|
| 97 |
r.bottom = new_bottom |
| 98 |
self.assertEqual( new_bottom, r.bottom ) |
| 99 |
self.assertEqual( expected_top, r.top ) |
| 100 |
self.assertEqual( old_height, r.height ) |
| 101 |
|
| 102 |
def test_centerx( self ): |
| 103 |
"""Changing the centerx attribute moves the rect and does not change |
| 104 |
the rect's width |
| 105 |
""" |
| 106 |
r = Rect( 1, 2, 3, 4 ) |
| 107 |
new_centerx = r.centerx + 20 |
| 108 |
expected_left = r.left + 20 |
| 109 |
old_width = r.width |
| 110 |
|
| 111 |
r.centerx = new_centerx |
| 112 |
self.assertEqual( new_centerx, r.centerx ) |
| 113 |
self.assertEqual( expected_left, r.left ) |
| 114 |
self.assertEqual( old_width, r.width ) |
| 115 |
|
| 116 |
def test_centery( self ): |
| 117 |
"""Changing the centerx attribute moves the rect and does not change |
| 118 |
the rect's width |
| 119 |
""" |
| 120 |
r = Rect( 1, 2, 3, 4 ) |
| 121 |
new_centery = r.centery + 20 |
| 122 |
expected_top = r.top + 20 |
| 123 |
old_height = r.height |
| 124 |
|
| 125 |
r.centery = new_centery |
| 126 |
self.assertEqual( new_centery, r.centery ) |
| 127 |
self.assertEqual( expected_top, r.top ) |
| 128 |
self.assertEqual( old_height, r.height ) |
| 129 |
|
| 130 |
def test_topleft( self ): |
| 131 |
"""Changing the topleft attribute moves the rect and does not change |
| 132 |
the rect's size |
| 133 |
""" |
| 134 |
r = Rect( 1, 2, 3, 4 ) |
| 135 |
new_topleft = (r.left+20,r.top+30) |
| 136 |
old_size = r.size |
| 137 |
|
| 138 |
r.topleft = new_topleft |
| 139 |
self.assertEqual( new_topleft, r.topleft ) |
| 140 |
self.assertEqual( old_size, r.size ) |
| 141 |
|
| 142 |
def test_bottomleft( self ): |
| 143 |
"""Changing the bottomleft attribute moves the rect and does not change |
| 144 |
the rect's size |
| 145 |
""" |
| 146 |
r = Rect( 1, 2, 3, 4 ) |
| 147 |
new_bottomleft = (r.left+20,r.bottom+30) |
| 148 |
expected_topleft = (r.left+20,r.top+30) |
| 149 |
old_size = r.size |
| 150 |
|
| 151 |
r.bottomleft = new_bottomleft |
| 152 |
self.assertEqual( new_bottomleft, r.bottomleft ) |
| 153 |
self.assertEqual( expected_topleft, r.topleft ) |
| 154 |
self.assertEqual( old_size, r.size ) |
| 155 |
|
| 156 |
def test_topright( self ): |
| 157 |
"""Changing the bottomleft attribute moves the rect and does not change |
| 158 |
the rect's size |
| 159 |
""" |
| 160 |
r = Rect( 1, 2, 3, 4 ) |
| 161 |
new_topright = (r.right+20,r.top+30) |
| 162 |
expected_topleft = (r.left+20,r.top+30) |
| 163 |
old_size = r.size |
| 164 |
|
| 165 |
r.topright = new_topright |
| 166 |
self.assertEqual( new_topright, r.topright ) |
| 167 |
self.assertEqual( expected_topleft, r.topleft ) |
| 168 |
self.assertEqual( old_size, r.size ) |
| 169 |
|
| 170 |
def test_bottomright( self ): |
| 171 |
"""Changing the bottomright attribute moves the rect and does not change |
| 172 |
the rect's size |
| 173 |
""" |
| 174 |
r = Rect( 1, 2, 3, 4 ) |
| 175 |
new_bottomright = (r.right+20,r.bottom+30) |
| 176 |
expected_topleft = (r.left+20,r.top+30) |
| 177 |
old_size = r.size |
| 178 |
|
| 179 |
r.bottomright = new_bottomright |
| 180 |
self.assertEqual( new_bottomright, r.bottomright ) |
| 181 |
self.assertEqual( expected_topleft, r.topleft ) |
| 182 |
self.assertEqual( old_size, r.size ) |
| 183 |
|
| 184 |
def test_center( self ): |
| 185 |
"""Changing the center attribute moves the rect and does not change |
| 186 |
the rect's size |
| 187 |
""" |
| 188 |
r = Rect( 1, 2, 3, 4 ) |
| 189 |
new_center = (r.centerx+20,r.centery+30) |
| 190 |
expected_topleft = (r.left+20,r.top+30) |
| 191 |
old_size = r.size |
| 192 |
|
| 193 |
r.center = new_center |
| 194 |
self.assertEqual( new_center, r.center ) |
| 195 |
self.assertEqual( expected_topleft, r.topleft ) |
| 196 |
self.assertEqual( old_size, r.size ) |
| 197 |
|
| 198 |
def test_midleft( self ): |
| 199 |
"""Changing the midleft attribute moves the rect and does not change |
| 200 |
the rect's size |
| 201 |
""" |
| 202 |
r = Rect( 1, 2, 3, 4 ) |
| 203 |
new_midleft = (r.left+20,r.centery+30) |
| 204 |
expected_topleft = (r.left+20,r.top+30) |
| 205 |
old_size = r.size |
| 206 |
|
| 207 |
r.midleft = new_midleft |
| 208 |
self.assertEqual( new_midleft, r.midleft ) |
| 209 |
self.assertEqual( expected_topleft, r.topleft ) |
| 210 |
self.assertEqual( old_size, r.size ) |
| 211 |
|
| 212 |
def test_midright( self ): |
| 213 |
"""Changing the midright attribute moves the rect and does not change |
| 214 |
the rect's size |
| 215 |
""" |
| 216 |
r = Rect( 1, 2, 3, 4 ) |
| 217 |
new_midright= (r.right+20,r.centery+30) |
| 218 |
expected_topleft = (r.left+20,r.top+30) |
| 219 |
old_size = r.size |
| 220 |
|
| 221 |
r.midright = new_midright |
| 222 |
self.assertEqual( new_midright, r.midright ) |
| 223 |
self.assertEqual( expected_topleft, r.topleft ) |
| 224 |
self.assertEqual( old_size, r.size ) |
| 225 |
|
| 226 |
def test_midtop( self ): |
| 227 |
"""Changing the midtop attribute moves the rect and does not change |
| 228 |
the rect's size |
| 229 |
""" |
| 230 |
r = Rect( 1, 2, 3, 4 ) |
| 231 |
new_midtop= (r.centerx+20,r.top+30) |
| 232 |
expected_topleft = (r.left+20,r.top+30) |
| 233 |
old_size = r.size |
| 234 |
|
| 235 |
r.midtop = new_midtop |
| 236 |
self.assertEqual( new_midtop, r.midtop ) |
| 237 |
self.assertEqual( expected_topleft, r.topleft ) |
| 238 |
self.assertEqual( old_size, r.size ) |
| 239 |
|
| 240 |
def test_midbottom( self ): |
| 241 |
"""Changing the midbottom attribute moves the rect and does not change |
| 242 |
the rect's size |
| 243 |
""" |
| 244 |
r = Rect( 1, 2, 3, 4 ) |
| 245 |
new_midbottom = (r.centerx+20,r.bottom+30) |
| 246 |
expected_topleft = (r.left+20,r.top+30) |
| 247 |
old_size = r.size |
| 248 |
|
| 249 |
r.midbottom = new_midbottom |
| 250 |
self.assertEqual( new_midbottom, r.midbottom ) |
| 251 |
self.assertEqual( expected_topleft, r.topleft ) |
| 252 |
self.assertEqual( old_size, r.size ) |
| 253 |
|
| 254 |
def test_width( self ): |
| 255 |
"Changing the width resizes the rect from the top-left corner" |
| 256 |
r = Rect( 1, 2, 3, 4 ) |
| 257 |
new_width = 10 |
| 258 |
old_topleft = r.topleft |
| 259 |
old_height = r.height |
| 260 |
|
| 261 |
r.width = new_width |
| 262 |
self.assertEqual( new_width, r.width ) |
| 263 |
self.assertEqual( old_height, r.height ) |
| 264 |
self.assertEqual( old_topleft, r.topleft ) |
| 265 |
|
| 266 |
def test_height( self ): |
| 267 |
"Changing the height resizes the rect from the top-left corner" |
| 268 |
r = Rect( 1, 2, 3, 4 ) |
| 269 |
new_height = 10 |
| 270 |
old_topleft = r.topleft |
| 271 |
old_width = r.width |
| 272 |
|
| 273 |
r.height = new_height |
| 274 |
self.assertEqual( new_height, r.height ) |
| 275 |
self.assertEqual( old_width, r.width ) |
| 276 |
self.assertEqual( old_topleft, r.topleft ) |
| 277 |
|
| 278 |
def test_size( self ): |
| 279 |
"Changing the size resizes the rect from the top-left corner" |
| 280 |
r = Rect( 1, 2, 3, 4 ) |
| 281 |
new_size = (10,20) |
| 282 |
old_topleft = r.topleft |
| 283 |
|
| 284 |
r.size = new_size |
| 285 |
self.assertEqual( new_size, r.size ) |
| 286 |
self.assertEqual( old_topleft, r.topleft ) |
| 287 |
|
| 288 |
def test_contains( self ): |
| 289 |
r = Rect( 1, 2, 3, 4 ) |
| 290 |
|
| 291 |
self.assertTrue( r.contains( Rect( 2, 3, 1, 1 ) ), |
| 292 |
"r does not contain Rect(2,3,1,1)" ) |
| 293 |
self.assertTrue( r.contains( Rect(r) ), |
| 294 |
"r does not contain the same rect as itself" ) |
| 295 |
self.assertTrue( r.contains( Rect(2,3,0,0) ), |
| 296 |
"r does not contain an empty rect within its bounds" ) |
| 297 |
self.assertFalse( r.contains( Rect(0,0,1,2) ), |
| 298 |
"r contains Rect(0,0,1,2)" ) |
| 299 |
self.assertFalse( r.contains( Rect(4,6,1,1) ), |
| 300 |
"r contains Rect(4,6,1,1)" ) |
| 301 |
self.assertFalse( r.contains( Rect(4,6,0,0) ), |
| 302 |
"r contains Rect(4,6,0,0)" ) |
| 303 |
|
| 304 |
def test_collidepoint( self ): |
| 305 |
r = Rect( 1, 2, 3, 4 ) |
| 306 |
|
| 307 |
self.assertTrue( r.collidepoint( r.left, r.top ), |
| 308 |
"r does not collide with point (left,top)" ) |
| 309 |
self.assertFalse( r.collidepoint( r.left-1, r.top ), |
| 310 |
"r collides with point (left-1,top)" ) |
| 311 |
self.assertFalse( r.collidepoint( r.left, r.top-1 ), |
| 312 |
"r collides with point (left,top-1)" ) |
| 313 |
self.assertFalse( r.collidepoint( r.left-1,r.top-1 ), |
| 314 |
"r collides with point (left-1,top-1)" ) |
| 315 |
|
| 316 |
self.assertTrue( r.collidepoint( r.right-1, r.bottom-1 ), |
| 317 |
"r does not collide with point (right-1,bottom-1)") |
| 318 |
self.assertFalse( r.collidepoint( r.right, r.bottom ), |
| 319 |
"r collides with point (right,bottom)" ) |
| 320 |
self.assertFalse( r.collidepoint( r.right-1, r.bottom ), |
| 321 |
"r collides with point (right-1,bottom)" ) |
| 322 |
self.assertFalse( r.collidepoint( r.right, r.bottom-1 ), |
| 323 |
"r collides with point (right,bottom-1)" ) |
| 324 |
|
| 325 |
def test_inflate__larger( self ): |
| 326 |
"The inflate method inflates around the center of the rectangle" |
| 327 |
r = Rect( 2, 4, 6, 8 ) |
| 328 |
r2 = r.inflate( 4, 6 ) |
| 329 |
|
| 330 |
self.assertEqual( r.center, r2.center ) |
| 331 |
self.assertEqual( r.left-2, r2.left ) |
| 332 |
self.assertEqual( r.top-3, r2.top ) |
| 333 |
self.assertEqual( r.right+2, r2.right ) |
| 334 |
self.assertEqual( r.bottom+3, r2.bottom ) |
| 335 |
self.assertEqual( r.width+4, r2.width ) |
| 336 |
self.assertEqual( r.height+6, r2.height ) |
| 337 |
|
| 338 |
def test_inflate__smaller( self ): |
| 339 |
"The inflate method inflates around the center of the rectangle" |
| 340 |
r = Rect( 2, 4, 6, 8 ) |
| 341 |
r2 = r.inflate( -4, -6 ) |
| 342 |
|
| 343 |
self.assertEqual( r.center, r2.center ) |
| 344 |
self.assertEqual( r.left+2, r2.left ) |
| 345 |
self.assertEqual( r.top+3, r2.top ) |
| 346 |
self.assertEqual( r.right-2, r2.right ) |
| 347 |
self.assertEqual( r.bottom-3, r2.bottom ) |
| 348 |
self.assertEqual( r.width-4, r2.width ) |
| 349 |
self.assertEqual( r.height-6, r2.height ) |
| 350 |
|
| 351 |
def test_inflate_ip__larger( self ): |
| 352 |
"The inflate_ip method inflates around the center of the rectangle" |
| 353 |
r = Rect( 2, 4, 6, 8 ) |
| 354 |
r2 = Rect( r ) |
| 355 |
r2.inflate_ip( -4, -6 ) |
| 356 |
|
| 357 |
self.assertEqual( r.center, r2.center ) |
| 358 |
self.assertEqual( r.left+2, r2.left ) |
| 359 |
self.assertEqual( r.top+3, r2.top ) |
| 360 |
self.assertEqual( r.right-2, r2.right ) |
| 361 |
self.assertEqual( r.bottom-3, r2.bottom ) |
| 362 |
self.assertEqual( r.width-4, r2.width ) |
| 363 |
self.assertEqual( r.height-6, r2.height ) |
| 364 |
|
| 365 |
def test_inflate_ip__smaller( self ): |
| 366 |
"The inflate method inflates around the center of the rectangle" |
| 367 |
r = Rect( 2, 4, 6, 8 ) |
| 368 |
r2 = Rect( r ) |
| 369 |
r2.inflate_ip( -4, -6 ) |
| 370 |
|
| 371 |
self.assertEqual( r.center, r2.center ) |
| 372 |
self.assertEqual( r.left+2, r2.left ) |
| 373 |
self.assertEqual( r.top+3, r2.top ) |
| 374 |
self.assertEqual( r.right-2, r2.right ) |
| 375 |
self.assertEqual( r.bottom-3, r2.bottom ) |
| 376 |
self.assertEqual( r.width-4, r2.width ) |
| 377 |
self.assertEqual( r.height-6, r2.height ) |
| 378 |
|
| 379 |
def test_clamp( self ): |
| 380 |
r = Rect(10, 10, 10, 10) |
| 381 |
c = Rect(19, 12, 5, 5).clamp(r) |
| 382 |
self.assertEqual(c.right, r.right) |
| 383 |
self.assertEqual(c.top, 12) |
| 384 |
c = Rect(1, 2, 3, 4).clamp(r) |
| 385 |
self.assertEqual(c.topleft, r.topleft) |
| 386 |
c = Rect(5, 500, 22, 33).clamp(r) |
| 387 |
self.assertEqual(c.center, r.center) |
| 388 |
|
| 389 |
def test_clamp_ip( self ): |
| 390 |
r = Rect(10, 10, 10, 10) |
| 391 |
c = Rect(19, 12, 5, 5) |
| 392 |
c.clamp_ip(r) |
| 393 |
self.assertEqual(c.right, r.right) |
| 394 |
self.assertEqual(c.top, 12) |
| 395 |
c = Rect(1, 2, 3, 4) |
| 396 |
c.clamp_ip(r) |
| 397 |
self.assertEqual(c.topleft, r.topleft) |
| 398 |
c = Rect(5, 500, 22, 33) |
| 399 |
c.clamp_ip(r) |
| 400 |
self.assertEqual(c.center, r.center) |
| 401 |
|
| 402 |
def test_clip( self ): |
| 403 |
r1 = Rect( 1, 2, 3, 4 ) |
| 404 |
self.assertEqual( Rect( 1, 2, 2, 2 ), r1.clip( Rect(0,0,3,4) ) ) |
| 405 |
self.assertEqual( Rect( 2, 2, 2, 4 ), r1.clip( Rect(2,2,10,20) ) ) |
| 406 |
self.assertEqual( Rect(2,3,1,2), r1.clip( Rect(2,3,1,2) ) ) |
| 407 |
self.assertEqual( (0,0), r1.clip(20,30,5,6).size ) |
| 408 |
self.assertEqual( r1, r1.clip( Rect(r1) ), |
| 409 |
"r1 does not clip an identical rect to itself" ) |
| 410 |
|
| 411 |
def test_move( self ): |
| 412 |
r = Rect( 1, 2, 3, 4 ) |
| 413 |
move_x = 10 |
| 414 |
move_y = 20 |
| 415 |
r2 = r.move( move_x, move_y ) |
| 416 |
expected_r2 = Rect(r.left+move_x,r.top+move_y,r.width,r.height) |
| 417 |
self.assertEqual( expected_r2, r2 ) |
| 418 |
|
| 419 |
def test_move_ip( self ): |
| 420 |
r = Rect( 1, 2, 3, 4 ) |
| 421 |
r2 = Rect( r ) |
| 422 |
move_x = 10 |
| 423 |
move_y = 20 |
| 424 |
r2.move_ip( move_x, move_y ) |
| 425 |
expected_r2 = Rect(r.left+move_x,r.top+move_y,r.width,r.height) |
| 426 |
self.assertEqual( expected_r2, r2 ) |
| 427 |
|
| 428 |
def test_union( self ): |
| 429 |
r1 = Rect( 1, 1, 1, 2 ) |
| 430 |
r2 = Rect( -2, -2, 1, 2 ) |
| 431 |
self.assertEqual( Rect( -2, -2, 4, 5 ), r1.union(r2) ) |
| 432 |
|
| 433 |
def test_union__with_identical_Rect( self ): |
| 434 |
r1 = Rect( 1, 2, 3, 4 ) |
| 435 |
self.assertEqual( r1, r1.union( Rect(r1) ) ) |
| 436 |
|
| 437 |
def test_union_ip( self ): |
| 438 |
r1 = Rect( 1, 1, 1, 2 ) |
| 439 |
r2 = Rect( -2, -2, 1, 2 ) |
| 440 |
r1.union_ip(r2) |
| 441 |
self.assertEqual( Rect( -2, -2, 4, 5 ), r1 ) |
| 442 |
|
| 443 |
def test_unionall( self ): |
| 444 |
r1 = Rect( 0, 0, 1, 1 ) |
| 445 |
r2 = Rect( -2, -2, 1, 1 ) |
| 446 |
r3 = Rect( 2, 2, 1, 1 ) |
| 447 |
|
| 448 |
r4 = r1.unionall( [r2,r3] ) |
| 449 |
self.assertEqual( Rect(-2, -2, 5, 5), r4 ) |
| 450 |
|
| 451 |
def test_unionall_ip( self ): |
| 452 |
r1 = Rect( 0, 0, 1, 1 ) |
| 453 |
r2 = Rect( -2, -2, 1, 1 ) |
| 454 |
r3 = Rect( 2, 2, 1, 1 ) |
| 455 |
|
| 456 |
r1.unionall_ip( [r2,r3] ) |
| 457 |
self.assertEqual( Rect(-2, -2, 5, 5), r1 ) |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
def test_colliderect( self ): |
| 462 |
r1 = Rect(1,2,3,4) |
| 463 |
self.assertTrue( r1.colliderect( Rect(0,0,2,3) ), |
| 464 |
"r1 does not collide with Rect(0,0,2,3)" ) |
| 465 |
self.assertFalse( r1.colliderect( Rect(0,0,1,2) ), |
| 466 |
"r1 collides with Rect(0,0,1,2)" ) |
| 467 |
self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,2,2) ), |
| 468 |
"r1 collides with Rect(r1.right,r1.bottom,2,2)" ) |
| 469 |
self.assertTrue( r1.colliderect( Rect(r1.left+1,r1.top+1, |
| 470 |
r1.width-2,r1.height-2) ), |
| 471 |
"r1 does not collide with Rect(r1.left+1,r1.top+1,"+ |
| 472 |
"r1.width-2,r1.height-2)" ) |
| 473 |
self.assertTrue( r1.colliderect( Rect(r1.left-1,r1.top-1, |
| 474 |
r1.width+2,r1.height+2) ), |
| 475 |
"r1 does not collide with Rect(r1.left-1,r1.top-1,"+ |
| 476 |
"r1.width+2,r1.height+2)" ) |
| 477 |
self.assertTrue( r1.colliderect( Rect(r1) ), |
| 478 |
"r1 does not collide with an identical rect" ) |
| 479 |
self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,0,0) ), |
| 480 |
"r1 collides with Rect(r1.right,r1.bottom,0,0)" ) |
| 481 |
self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,1,1) ), |
| 482 |
"r1 collides with Rect(r1.right,r1.bottom,1,1)" ) |
| 483 |
|
| 484 |
def testEquals( self ): |
| 485 |
""" check to see how the rect uses __eq__ |
| 486 |
""" |
| 487 |
r1 = Rect(1,2,3,4) |
| 488 |
r2 = Rect(10,20,30,40) |
| 489 |
r3 = (10,20,30,40) |
| 490 |
r4 = Rect(10,20,30,40) |
| 491 |
|
| 492 |
class foo (Rect): |
| 493 |
def __eq__(self,other): |
| 494 |
return id(self) == id(other) |
| 495 |
def __ne__(self,other): |
| 496 |
return id(self) != id(other) |
| 497 |
|
| 498 |
class foo2 (Rect): |
| 499 |
pass |
| 500 |
|
| 501 |
r5 = foo(10,20,30,40) |
| 502 |
r6 = foo2(10,20,30,40) |
| 503 |
|
| 504 |
self.assertNotEqual(r5, r2) |
| 505 |
|
| 506 |
|
| 507 |
self.assertEqual(r6, r2) |
| 508 |
|
| 509 |
|
| 510 |
rect_list = [r1,r2,r3,r4,r6] |
| 511 |
|
| 512 |
|
| 513 |
rect_list.remove(r2) |
| 514 |
rect_list.remove(r2) |
| 515 |
rect_list.remove(r2) |
| 516 |
rect_list.remove(r2) |
| 517 |
self.assertRaises(ValueError, rect_list.remove, r2) |
| 518 |
|
| 519 |
def test_collidedict(self): |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
r = Rect(1, 1, 10, 10) |
| 534 |
r1 = Rect(1, 1, 10, 10) |
| 535 |
r2 = Rect(50, 50, 10, 10) |
| 536 |
r3 = Rect(70, 70, 10, 10) |
| 537 |
r4 = Rect(61, 61, 10, 10) |
| 538 |
|
| 539 |
d = {1: r1, 2: r2, 3: r3} |
| 540 |
|
| 541 |
rects_values = 1 |
| 542 |
val = r.collidedict(d, rects_values) |
| 543 |
self.assertTrue(val) |
| 544 |
self.assertEqual(len(val), 2) |
| 545 |
self.assertEqual(val[0], 1) |
| 546 |
self.assertEqual(val[1], r1) |
| 547 |
|
| 548 |
none_d = {2: r2, 3: r3} |
| 549 |
none_val = r.collidedict(none_d, rects_values) |
| 550 |
self.assertFalse(none_val) |
| 551 |
|
| 552 |
barely_d = {1: r1, 2: r2, 3: r3} |
| 553 |
k3, v3 = r4.collidedict(barely_d, rects_values) |
| 554 |
self.assertEqual(k3, 3) |
| 555 |
self.assertEqual(v3, r3) |
| 556 |
|
| 557 |
|
| 558 |
def test_collidedictall(self): |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
r = Rect(1, 1, 10, 10) |
| 573 |
|
| 574 |
r2 = Rect(1, 1, 10, 10) |
| 575 |
r3 = Rect(5, 5, 10, 10) |
| 576 |
r4 = Rect(10, 10, 10, 10) |
| 577 |
r5 = Rect(50, 50, 10, 10) |
| 578 |
|
| 579 |
rects_values = 1 |
| 580 |
d = {2: r2} |
| 581 |
l = r.collidedictall(d, rects_values) |
| 582 |
self.assertEqual(l, [(2, r2)]) |
| 583 |
|
| 584 |
d2 = {2: r2, 3: r3, 4: r4, 5: r5} |
| 585 |
l2 = r.collidedictall(d2, rects_values) |
| 586 |
self.assertEqual(l2, [(2, r2), (3, r3), (4, r4)]) |
| 587 |
|
| 588 |
def test_collidelist(self): |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
r = Rect(1, 1, 10, 10) |
| 600 |
l = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)] |
| 601 |
|
| 602 |
self.assertEqual(r.collidelist(l), 1) |
| 603 |
|
| 604 |
f = [Rect(50, 50, 1, 1), (100, 100, 4, 4)] |
| 605 |
self.assertEqual(r.collidelist(f), -1) |
| 606 |
|
| 607 |
|
| 608 |
def test_collidelistall(self): |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
r = Rect(1, 1, 10, 10) |
| 620 |
|
| 621 |
l = [ |
| 622 |
Rect(1, 1, 10, 10), |
| 623 |
Rect(5, 5, 10, 10), |
| 624 |
Rect(15, 15, 1, 1), |
| 625 |
Rect(2, 2, 1, 1), |
| 626 |
] |
| 627 |
self.assertEqual(r.collidelistall(l), [0, 1, 3]) |
| 628 |
|
| 629 |
f = [Rect(50, 50, 1, 1), Rect(20, 20, 5, 5)] |
| 630 |
self.assertFalse(r.collidelistall(f)) |
| 631 |
|
| 632 |
|
| 633 |
def test_fit(self): |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
r = Rect(10, 10, 30, 30) |
| 645 |
|
| 646 |
r2 = Rect(30, 30, 15, 10) |
| 647 |
|
| 648 |
f = r.fit(r2) |
| 649 |
self.assertTrue(r2.contains(f)) |
| 650 |
|
| 651 |
f2 = r2.fit(r) |
| 652 |
self.assertTrue(r.contains(f2)) |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
def test_copy(self): |
| 657 |
r = Rect(1, 2, 10, 20) |
| 658 |
c = r.copy() |
| 659 |
self.assertEqual(c, r) |
| 660 |
|
| 661 |
class PGZeroRectText(unittest.TestCase): |
| 662 |
|
| 663 |
def test_constructor_from_tuple(self): |
| 664 |
"Build a Rect from a 4-item tuple" |
| 665 |
r = Rect((1, 2, 3, 4)) |
| 666 |
self.assertEqual(r.x, 1) |
| 667 |
self.assertEqual(r.y, 2) |
| 668 |
self.assertEqual(r.w, 3) |
| 669 |
self.assertEqual(r.h, 4) |
| 670 |
|
| 671 |
def test_constructor_from_PGZeroRect(self): |
| 672 |
"Build a rect from another pgzero Rect" |
| 673 |
r = Rect(1, 2, 3, 4) |
| 674 |
r1 = Rect(r) |
| 675 |
self.assertEqual(r, r1) |
| 676 |
self.assertIsNot(r, r1) |
| 677 |
|
| 678 |
def test_constructor_from_PygameRect(self): |
| 679 |
"Build a rect from a pygame Rect" |
| 680 |
r = pygame.Rect(1, 2, 3, 4) |
| 681 |
r1 = Rect(r) |
| 682 |
self.assertEqual(r, r1) |
| 683 |
self.assertIsNot(r, r1) |
| 684 |
|
| 685 |
def test_constructor_from_rect_tuple(self): |
| 686 |
"Build a rect from an object with a rect attribute which is a tuple" |
| 687 |
class Obj: pass |
| 688 |
obj = Obj() |
| 689 |
obj.rect = 1, 2, 3, 4 |
| 690 |
r = Rect(obj) |
| 691 |
self.assertEqual(r, Rect(1, 2, 3, 4)) |
| 692 |
|
| 693 |
def test_constructor_from_rect_object(self): |
| 694 |
"Build a rect from an object with a rect attribute which is an object" |
| 695 |
class Obj: pass |
| 696 |
obj = Obj() |
| 697 |
obj.rect = Rect(1, 2, 3, 4) |
| 698 |
r = Rect(obj) |
| 699 |
self.assertEqual(r, Rect(1, 2, 3, 4)) |
| 700 |
|
| 701 |
def test_constructor_from_rect_indirect_tuple(self): |
| 702 |
"""Build a rect from an object with a rect attribute which |
| 703 |
is an object which has a rect attribute which is a tuple |
| 704 |
""" |
| 705 |
class Obj: pass |
| 706 |
obj = Obj() |
| 707 |
obj1 = Obj() |
| 708 |
obj1.rect = Rect(1, 2, 3, 4) |
| 709 |
obj.rect = obj1 |
| 710 |
r = Rect(obj) |
| 711 |
self.assertEqual(r, Rect(1, 2, 3, 4)) |
| 712 |
|
| 713 |
def test_constructor_from_rect_callable(self): |
| 714 |
"""Build a rect from an object with a rect attribute which |
| 715 |
is called and returns a tuple |
| 716 |
""" |
| 717 |
class Obj: |
| 718 |
def rect(self): |
| 719 |
return 1, 2, 3, 4 |
| 720 |
obj = Obj() |
| 721 |
r = Rect(obj) |
| 722 |
self.assertEqual(r, Rect(1, 2, 3, 4)) |
| 723 |
|
| 724 |
def test_float_instance(self): |
| 725 |
"Create an instance with floating-point co-ordinates" |
| 726 |
r = Rect(1.2, 3.4, 5.6, 7.8) |
| 727 |
self.assertEqual(r.x, 1.2) |
| 728 |
self.assertEqual(r.y, 3.4) |
| 729 |
self.assertEqual(r.w, 5.6) |
| 730 |
self.assertEqual(r.h, 7.8) |
| 731 |
|
| 732 |
def test_float_instance_as_PygameRect(self): |
| 733 |
"Create a Pygame Rect from a pgzero floating-point Rect" |
| 734 |
r = Rect(1.2, 3.4, 5.6, 7.8) |
| 735 |
r2 = pygame.Rect(r) |
| 736 |
self.assertEqual([int(i) for i in r], list(r2)) |
| 737 |
|
| 738 |
def test_float_center(self): |
| 739 |
r = Rect(0, 0, 5, 5) |
| 740 |
self.assertEqual(r.center, (2.5, 2.5)) |
| 741 |
|
| 742 |
def test_float_inflated(self): |
| 743 |
r = Rect(0, 0, 5, 5) |
| 744 |
self.assertEqual(r.inflate(1.5, 1.5), (-0.75, -0.75, 6.5, 6.5)) |
| 745 |
|
| 746 |
def test_contains_point(self): |
| 747 |
r = Rect(0, 0, 100, 100) |
| 748 |
self.assertTrue((50, 50) in r) |
| 749 |
self.assertFalse((150, 150) in r) |
| 750 |
|
| 751 |
def test_contains_rect(self): |
| 752 |
r = Rect(0, 0, 100, 100) |
| 753 |
self.assertTrue((20, 20, 50, 50) in r) |
| 754 |
self.assertFalse((20, 20, 100, 100) in r) |
| 755 |
|
| 756 |
def test_not_hashable(self): |
| 757 |
r1 = Rect(1, 2, 3, 4) |
| 758 |
r2 = Rect(r1) |
| 759 |
self.assertEqual(r1, r2) |
| 760 |
self.assertIsNot(r1, r2) |
| 761 |
with self.assertRaises(TypeError): |
| 762 |
self.assertEqual({r1}, {r2}) |
| 763 |
|
| 764 |
def test_as_tuple(self): |
| 765 |
t = 1, 2, 3, 4 |
| 766 |
r1 = Rect(t) |
| 767 |
self.assertEqual(tuple(r1), t) |
| 768 |
|
| 769 |
def test_collidepoint_tuple(self): |
| 770 |
r = Rect(0, 0, 1, 1) |
| 771 |
self.assertTrue(r.collidepoint((0.5, 0.5))) |
| 772 |
|
| 773 |
if __name__ == '__main__': |
| 774 |
unittest.main() |