rect_test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. import unittest
  2. from pygame import Rect
  3. class RectTypeTest(unittest.TestCase):
  4. def testConstructionXYWidthHeight(self):
  5. r = Rect(1, 2, 3, 4)
  6. self.assertEqual(1, r.left)
  7. self.assertEqual(2, r.top)
  8. self.assertEqual(3, r.width)
  9. self.assertEqual(4, r.height)
  10. def testConstructionTopLeftSize(self):
  11. r = Rect((1, 2), (3, 4))
  12. self.assertEqual(1, r.left)
  13. self.assertEqual(2, r.top)
  14. self.assertEqual(3, r.width)
  15. self.assertEqual(4, r.height)
  16. def testCalculatedAttributes(self):
  17. r = Rect(1, 2, 3, 4)
  18. self.assertEqual(r.left + r.width, r.right)
  19. self.assertEqual(r.top + r.height, r.bottom)
  20. self.assertEqual((r.width, r.height), r.size)
  21. self.assertEqual((r.left, r.top), r.topleft)
  22. self.assertEqual((r.right, r.top), r.topright)
  23. self.assertEqual((r.left, r.bottom), r.bottomleft)
  24. self.assertEqual((r.right, r.bottom), r.bottomright)
  25. midx = r.left + r.width // 2
  26. midy = r.top + r.height // 2
  27. self.assertEqual(midx, r.centerx)
  28. self.assertEqual(midy, r.centery)
  29. self.assertEqual((r.centerx, r.centery), r.center)
  30. self.assertEqual((r.centerx, r.top), r.midtop)
  31. self.assertEqual((r.centerx, r.bottom), r.midbottom)
  32. self.assertEqual((r.left, r.centery), r.midleft)
  33. self.assertEqual((r.right, r.centery), r.midright)
  34. def test_normalize(self):
  35. r = Rect(1, 2, -3, -6)
  36. r2 = Rect(r)
  37. r2.normalize()
  38. self.assertTrue(r2.width >= 0)
  39. self.assertTrue(r2.height >= 0)
  40. self.assertEqual((abs(r.width), abs(r.height)), r2.size)
  41. self.assertEqual((-2, -4), r2.topleft)
  42. def test_left(self):
  43. """Changing the left attribute moves the rect and does not change
  44. the rect's width
  45. """
  46. r = Rect(1, 2, 3, 4)
  47. new_left = 10
  48. r.left = new_left
  49. self.assertEqual(new_left, r.left)
  50. self.assertEqual(Rect(new_left, 2, 3, 4), r)
  51. def test_right(self):
  52. """Changing the right attribute moves the rect and does not change
  53. the rect's width
  54. """
  55. r = Rect(1, 2, 3, 4)
  56. new_right = r.right + 20
  57. expected_left = r.left + 20
  58. old_width = r.width
  59. r.right = new_right
  60. self.assertEqual(new_right, r.right)
  61. self.assertEqual(expected_left, r.left)
  62. self.assertEqual(old_width, r.width)
  63. def test_top(self):
  64. """Changing the top attribute moves the rect and does not change
  65. the rect's width
  66. """
  67. r = Rect(1, 2, 3, 4)
  68. new_top = 10
  69. r.top = new_top
  70. self.assertEqual(Rect(1, new_top, 3, 4), r)
  71. self.assertEqual(new_top, r.top)
  72. def test_bottom(self):
  73. """Changing the bottom attribute moves the rect and does not change
  74. the rect's height
  75. """
  76. r = Rect(1, 2, 3, 4)
  77. new_bottom = r.bottom + 20
  78. expected_top = r.top + 20
  79. old_height = r.height
  80. r.bottom = new_bottom
  81. self.assertEqual(new_bottom, r.bottom)
  82. self.assertEqual(expected_top, r.top)
  83. self.assertEqual(old_height, r.height)
  84. def test_centerx(self):
  85. """Changing the centerx attribute moves the rect and does not change
  86. the rect's width
  87. """
  88. r = Rect(1, 2, 3, 4)
  89. new_centerx = r.centerx + 20
  90. expected_left = r.left + 20
  91. old_width = r.width
  92. r.centerx = new_centerx
  93. self.assertEqual(new_centerx, r.centerx)
  94. self.assertEqual(expected_left, r.left)
  95. self.assertEqual(old_width, r.width)
  96. def test_centery(self):
  97. """Changing the centerx attribute moves the rect and does not change
  98. the rect's width
  99. """
  100. r = Rect(1, 2, 3, 4)
  101. new_centery = r.centery + 20
  102. expected_top = r.top + 20
  103. old_height = r.height
  104. r.centery = new_centery
  105. self.assertEqual(new_centery, r.centery)
  106. self.assertEqual(expected_top, r.top)
  107. self.assertEqual(old_height, r.height)
  108. def test_topleft(self):
  109. """Changing the topleft attribute moves the rect and does not change
  110. the rect's size
  111. """
  112. r = Rect(1, 2, 3, 4)
  113. new_topleft = (r.left + 20, r.top + 30)
  114. old_size = r.size
  115. r.topleft = new_topleft
  116. self.assertEqual(new_topleft, r.topleft)
  117. self.assertEqual(old_size, r.size)
  118. def test_bottomleft(self):
  119. """Changing the bottomleft attribute moves the rect and does not change
  120. the rect's size
  121. """
  122. r = Rect(1, 2, 3, 4)
  123. new_bottomleft = (r.left + 20, r.bottom + 30)
  124. expected_topleft = (r.left + 20, r.top + 30)
  125. old_size = r.size
  126. r.bottomleft = new_bottomleft
  127. self.assertEqual(new_bottomleft, r.bottomleft)
  128. self.assertEqual(expected_topleft, r.topleft)
  129. self.assertEqual(old_size, r.size)
  130. def test_topright(self):
  131. """Changing the bottomleft attribute moves the rect and does not change
  132. the rect's size
  133. """
  134. r = Rect(1, 2, 3, 4)
  135. new_topright = (r.right + 20, r.top + 30)
  136. expected_topleft = (r.left + 20, r.top + 30)
  137. old_size = r.size
  138. r.topright = new_topright
  139. self.assertEqual(new_topright, r.topright)
  140. self.assertEqual(expected_topleft, r.topleft)
  141. self.assertEqual(old_size, r.size)
  142. def test_bottomright(self):
  143. """Changing the bottomright attribute moves the rect and does not change
  144. the rect's size
  145. """
  146. r = Rect(1, 2, 3, 4)
  147. new_bottomright = (r.right + 20, r.bottom + 30)
  148. expected_topleft = (r.left + 20, r.top + 30)
  149. old_size = r.size
  150. r.bottomright = new_bottomright
  151. self.assertEqual(new_bottomright, r.bottomright)
  152. self.assertEqual(expected_topleft, r.topleft)
  153. self.assertEqual(old_size, r.size)
  154. def test_center(self):
  155. """Changing the center attribute moves the rect and does not change
  156. the rect's size
  157. """
  158. r = Rect(1, 2, 3, 4)
  159. new_center = (r.centerx + 20, r.centery + 30)
  160. expected_topleft = (r.left + 20, r.top + 30)
  161. old_size = r.size
  162. r.center = new_center
  163. self.assertEqual(new_center, r.center)
  164. self.assertEqual(expected_topleft, r.topleft)
  165. self.assertEqual(old_size, r.size)
  166. def test_midleft(self):
  167. """Changing the midleft attribute moves the rect and does not change
  168. the rect's size
  169. """
  170. r = Rect(1, 2, 3, 4)
  171. new_midleft = (r.left + 20, r.centery + 30)
  172. expected_topleft = (r.left + 20, r.top + 30)
  173. old_size = r.size
  174. r.midleft = new_midleft
  175. self.assertEqual(new_midleft, r.midleft)
  176. self.assertEqual(expected_topleft, r.topleft)
  177. self.assertEqual(old_size, r.size)
  178. def test_midright(self):
  179. """Changing the midright attribute moves the rect and does not change
  180. the rect's size
  181. """
  182. r = Rect(1, 2, 3, 4)
  183. new_midright= (r.right + 20, r.centery + 30)
  184. expected_topleft = (r.left + 20, r.top + 30)
  185. old_size = r.size
  186. r.midright = new_midright
  187. self.assertEqual(new_midright, r.midright)
  188. self.assertEqual(expected_topleft, r.topleft)
  189. self.assertEqual(old_size, r.size)
  190. def test_midtop(self):
  191. """Changing the midtop attribute moves the rect and does not change
  192. the rect's size
  193. """
  194. r = Rect(1, 2, 3, 4)
  195. new_midtop= (r.centerx + 20, r.top + 30)
  196. expected_topleft = (r.left + 20, r.top + 30)
  197. old_size = r.size
  198. r.midtop = new_midtop
  199. self.assertEqual(new_midtop, r.midtop)
  200. self.assertEqual(expected_topleft, r.topleft)
  201. self.assertEqual(old_size, r.size)
  202. def test_midbottom(self):
  203. """Changing the midbottom attribute moves the rect and does not change
  204. the rect's size
  205. """
  206. r = Rect(1, 2, 3, 4)
  207. new_midbottom = (r.centerx + 20, r.bottom + 30)
  208. expected_topleft = (r.left + 20, r.top + 30)
  209. old_size = r.size
  210. r.midbottom = new_midbottom
  211. self.assertEqual(new_midbottom, r.midbottom)
  212. self.assertEqual(expected_topleft, r.topleft)
  213. self.assertEqual(old_size, r.size)
  214. def test_width(self):
  215. """Changing the width resizes the rect from the top-left corner
  216. """
  217. r = Rect(1, 2, 3, 4)
  218. new_width = 10
  219. old_topleft = r.topleft
  220. old_height = r.height
  221. r.width = new_width
  222. self.assertEqual(new_width, r.width)
  223. self.assertEqual(old_height, r.height)
  224. self.assertEqual(old_topleft, r.topleft)
  225. def test_height(self):
  226. """Changing the height resizes the rect from the top-left corner
  227. """
  228. r = Rect(1, 2, 3, 4)
  229. new_height = 10
  230. old_topleft = r.topleft
  231. old_width = r.width
  232. r.height = new_height
  233. self.assertEqual(new_height, r.height)
  234. self.assertEqual(old_width, r.width)
  235. self.assertEqual(old_topleft, r.topleft)
  236. def test_size(self):
  237. """Changing the size resizes the rect from the top-left corner
  238. """
  239. r = Rect(1, 2, 3, 4)
  240. new_size = (10, 20)
  241. old_topleft = r.topleft
  242. r.size = new_size
  243. self.assertEqual(new_size, r.size)
  244. self.assertEqual(old_topleft, r.topleft)
  245. def test_contains(self):
  246. r = Rect(1, 2, 3, 4)
  247. self.assertTrue(r.contains(Rect(2, 3, 1, 1)),
  248. "r does not contain Rect(2, 3, 1, 1)")
  249. self.assertTrue(r.contains(Rect(r)),
  250. "r does not contain the same rect as itself")
  251. self.assertTrue(r.contains(Rect(2, 3, 0, 0)),
  252. "r does not contain an empty rect within its bounds")
  253. self.assertFalse(r.contains(Rect(0, 0, 1, 2)),
  254. "r contains Rect(0, 0, 1, 2)")
  255. self.assertFalse(r.contains(Rect(4, 6, 1, 1)),
  256. "r contains Rect(4, 6, 1, 1)")
  257. self.assertFalse(r.contains(Rect(4, 6, 0, 0)),
  258. "r contains Rect(4, 6, 0, 0)")
  259. def test_collidepoint(self):
  260. r = Rect(1, 2, 3, 4)
  261. self.assertTrue(r.collidepoint(r.left, r.top),
  262. "r does not collide with point (left, top)")
  263. self.assertFalse(r.collidepoint(r.left - 1, r.top),
  264. "r collides with point (left - 1, top)")
  265. self.assertFalse(r.collidepoint(r.left, r.top - 1),
  266. "r collides with point (left, top - 1)")
  267. self.assertFalse(r.collidepoint(r.left - 1, r.top - 1),
  268. "r collides with point (left - 1, top - 1)")
  269. self.assertTrue(r.collidepoint(r.right - 1, r.bottom - 1),
  270. "r does not collide with point (right - 1, bottom - 1)")
  271. self.assertFalse(r.collidepoint(r.right, r.bottom),
  272. "r collides with point (right, bottom)")
  273. self.assertFalse(r.collidepoint(r.right - 1, r.bottom),
  274. "r collides with point (right - 1, bottom)")
  275. self.assertFalse(r.collidepoint(r.right, r.bottom - 1),
  276. "r collides with point (right, bottom - 1)")
  277. def test_inflate__larger(self):
  278. """The inflate method inflates around the center of the rectangle
  279. """
  280. r = Rect(2, 4, 6, 8)
  281. r2 = r.inflate(4, 6)
  282. self.assertEqual(r.center, r2.center)
  283. self.assertEqual(r.left - 2, r2.left)
  284. self.assertEqual(r.top - 3, r2.top)
  285. self.assertEqual(r.right + 2, r2.right)
  286. self.assertEqual(r.bottom + 3, r2.bottom)
  287. self.assertEqual(r.width + 4, r2.width)
  288. self.assertEqual(r.height + 6, r2.height)
  289. def test_inflate__smaller(self):
  290. """The inflate method inflates around the center of the rectangle
  291. """
  292. r = Rect(2, 4, 6, 8)
  293. r2 = r.inflate(-4, -6)
  294. self.assertEqual(r.center, r2.center)
  295. self.assertEqual(r.left + 2, r2.left)
  296. self.assertEqual(r.top + 3, r2.top)
  297. self.assertEqual(r.right - 2, r2.right)
  298. self.assertEqual(r.bottom - 3, r2.bottom)
  299. self.assertEqual(r.width - 4, r2.width)
  300. self.assertEqual(r.height - 6, r2.height)
  301. def test_inflate_ip__larger(self):
  302. """The inflate_ip method inflates around the center of the rectangle
  303. """
  304. r = Rect(2, 4, 6, 8)
  305. r2 = Rect(r)
  306. r2.inflate_ip(-4, -6)
  307. self.assertEqual(r.center, r2.center)
  308. self.assertEqual(r.left + 2, r2.left)
  309. self.assertEqual(r.top + 3, r2.top)
  310. self.assertEqual(r.right - 2, r2.right)
  311. self.assertEqual(r.bottom - 3, r2.bottom)
  312. self.assertEqual(r.width - 4, r2.width)
  313. self.assertEqual(r.height - 6, r2.height)
  314. def test_inflate_ip__smaller(self):
  315. """The inflate method inflates around the center of the rectangle
  316. """
  317. r = Rect(2, 4, 6, 8)
  318. r2 = Rect(r)
  319. r2.inflate_ip(-4, -6)
  320. self.assertEqual(r.center, r2.center)
  321. self.assertEqual(r.left + 2, r2.left)
  322. self.assertEqual(r.top + 3, r2.top)
  323. self.assertEqual(r.right - 2, r2.right)
  324. self.assertEqual(r.bottom - 3, r2.bottom)
  325. self.assertEqual(r.width - 4, r2.width)
  326. self.assertEqual(r.height - 6, r2.height)
  327. def test_clamp(self):
  328. r = Rect(10, 10, 10, 10)
  329. c = Rect(19, 12, 5, 5).clamp(r)
  330. self.assertEqual(c.right, r.right)
  331. self.assertEqual(c.top, 12)
  332. c = Rect(1, 2, 3, 4).clamp(r)
  333. self.assertEqual(c.topleft, r.topleft)
  334. c = Rect(5, 500, 22, 33).clamp(r)
  335. self.assertEqual(c.center, r.center)
  336. def test_clamp_ip(self):
  337. r = Rect(10, 10, 10, 10)
  338. c = Rect(19, 12, 5, 5)
  339. c.clamp_ip(r)
  340. self.assertEqual(c.right, r.right)
  341. self.assertEqual(c.top, 12)
  342. c = Rect(1, 2, 3, 4)
  343. c.clamp_ip(r)
  344. self.assertEqual(c.topleft, r.topleft)
  345. c = Rect(5, 500, 22, 33)
  346. c.clamp_ip(r)
  347. self.assertEqual(c.center, r.center)
  348. def test_clip(self):
  349. r1 = Rect(1, 2, 3, 4)
  350. self.assertEqual(Rect(1, 2, 2, 2), r1.clip( Rect(0, 0, 3, 4)))
  351. self.assertEqual(Rect(2, 2, 2, 4), r1.clip( Rect(2, 2, 10, 20)))
  352. self.assertEqual(Rect(2, 3, 1, 2), r1.clip(Rect(2, 3, 1, 2)))
  353. self.assertEqual((0, 0), r1.clip(20, 30, 5, 6).size)
  354. self.assertEqual(r1, r1.clip(Rect(r1)),
  355. "r1 does not clip an identical rect to itself")
  356. def test_move(self):
  357. r = Rect(1, 2, 3, 4)
  358. move_x = 10
  359. move_y = 20
  360. r2 = r.move(move_x, move_y)
  361. expected_r2 = Rect(r.left + move_x, r.top + move_y, r.width, r.height)
  362. self.assertEqual(expected_r2, r2)
  363. def test_move_ip(self):
  364. r = Rect(1, 2, 3, 4)
  365. r2 = Rect(r)
  366. move_x = 10
  367. move_y = 20
  368. r2.move_ip(move_x, move_y)
  369. expected_r2 = Rect(r.left + move_x, r.top + move_y, r.width, r.height)
  370. self.assertEqual(expected_r2, r2)
  371. def test_union(self):
  372. r1 = Rect(1, 1, 1, 2)
  373. r2 = Rect(-2, -2, 1, 2)
  374. self.assertEqual(Rect(-2, -2, 4, 5), r1.union(r2))
  375. def test_union__with_identical_Rect(self):
  376. r1 = Rect(1, 2, 3, 4)
  377. self.assertEqual(r1, r1.union(Rect(r1)))
  378. def test_union_ip(self):
  379. r1 = Rect(1, 1, 1, 2)
  380. r2 = Rect(-2, -2, 1, 2)
  381. r1.union_ip(r2)
  382. self.assertEqual(Rect(-2, -2, 4, 5), r1)
  383. def test_unionall(self):
  384. r1 = Rect(0, 0, 1, 1)
  385. r2 = Rect(-2, -2, 1, 1)
  386. r3 = Rect(2, 2, 1, 1)
  387. r4 = r1.unionall([r2, r3])
  388. self.assertEqual(Rect(-2, -2, 5, 5), r4)
  389. def test_unionall_ip(self):
  390. r1 = Rect(0, 0, 1, 1)
  391. r2 = Rect(-2, -2, 1, 1)
  392. r3 = Rect(2, 2, 1, 1)
  393. r1.unionall_ip([r2, r3])
  394. self.assertEqual(Rect(-2, -2, 5, 5), r1)
  395. # Bug for an empty list. Would return a Rect instead of None.
  396. self.assertTrue(r1.unionall_ip([]) is None)
  397. def test_colliderect(self):
  398. r1 = Rect(1, 2, 3, 4)
  399. self.assertTrue(r1.colliderect(Rect(0, 0, 2, 3)),
  400. "r1 does not collide with Rect(0, 0, 2, 3)")
  401. self.assertFalse(r1.colliderect(Rect(0, 0, 1, 2)),
  402. "r1 collides with Rect(0, 0, 1, 2)")
  403. self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 2, 2)),
  404. "r1 collides with Rect(r1.right, r1.bottom, 2, 2)")
  405. self.assertTrue(r1.colliderect(Rect(r1.left + 1, r1.top + 1,
  406. r1.width - 2, r1.height - 2)),
  407. "r1 does not collide with Rect(r1.left + 1, r1.top + 1, "+
  408. "r1.width - 2, r1.height - 2)")
  409. self.assertTrue(r1.colliderect(Rect(r1.left - 1, r1.top - 1,
  410. r1.width + 2, r1.height + 2)),
  411. "r1 does not collide with Rect(r1.left - 1, r1.top - 1, "+
  412. "r1.width + 2, r1.height + 2)")
  413. self.assertTrue(r1.colliderect(Rect(r1)),
  414. "r1 does not collide with an identical rect")
  415. self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 0, 0)),
  416. "r1 collides with Rect(r1.right, r1.bottom, 0, 0)")
  417. self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 1, 1)),
  418. "r1 collides with Rect(r1.right, r1.bottom, 1, 1)")
  419. def testEquals(self):
  420. """ check to see how the rect uses __eq__
  421. """
  422. r1 = Rect(1, 2, 3, 4)
  423. r2 = Rect(10, 20, 30, 40)
  424. r3 = (10, 20, 30, 40)
  425. r4 = Rect(10, 20, 30, 40)
  426. class foo (Rect):
  427. def __eq__(self, other):
  428. return id(self) == id(other)
  429. def __ne__(self, other):
  430. return id(self) != id(other)
  431. class foo2 (Rect):
  432. pass
  433. r5 = foo(10, 20, 30, 40)
  434. r6 = foo2(10, 20, 30, 40)
  435. self.assertNotEqual(r5, r2)
  436. # because we define equality differently for this subclass.
  437. self.assertEqual(r6, r2)
  438. rect_list = [r1, r2, r3, r4, r6]
  439. # see if we can remove 4 of these.
  440. rect_list.remove(r2)
  441. rect_list.remove(r2)
  442. rect_list.remove(r2)
  443. rect_list.remove(r2)
  444. self.assertRaises(ValueError, rect_list.remove, r2)
  445. def test_collidedict(self):
  446. # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidedict:
  447. # Rect.collidedict(dict): return (key, value)
  448. # test if one rectangle in a dictionary intersects
  449. #
  450. # Returns the key and value of the first dictionary value that
  451. # collides with the Rect. If no collisions are found, None is
  452. # returned.
  453. #
  454. # Rect objects are not hashable and cannot be used as keys in a
  455. # dictionary, only as values.
  456. r = Rect(1, 1, 10, 10)
  457. r1 = Rect(1, 1, 10, 10)
  458. r2 = Rect(50, 50, 10, 10)
  459. r3 = Rect(70, 70, 10, 10)
  460. r4 = Rect(61, 61, 10, 10)
  461. d = {1: r1, 2: r2, 3: r3}
  462. rects_values = 1
  463. val = r.collidedict(d, rects_values)
  464. self.assertTrue(val)
  465. self.assertEqual(len(val), 2)
  466. self.assertEqual(val[0], 1)
  467. self.assertEqual(val[1], r1)
  468. none_d = {2: r2, 3: r3}
  469. none_val = r.collidedict(none_d, rects_values)
  470. self.assertFalse(none_val)
  471. barely_d = {1: r1, 2: r2, 3: r3}
  472. k3, v3 = r4.collidedict(barely_d, rects_values)
  473. self.assertEqual(k3, 3)
  474. self.assertEqual(v3, r3)
  475. def test_collidedictall(self):
  476. # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidedictall:
  477. # Rect.collidedictall(dict): return [(key, value), ...]
  478. # test if all rectangles in a dictionary intersect
  479. #
  480. # Returns a list of all the key and value pairs that intersect with
  481. # the Rect. If no collisions are found an empty dictionary is
  482. # returned.
  483. #
  484. # Rect objects are not hashable and cannot be used as keys in a
  485. # dictionary, only as values.
  486. r = Rect(1, 1, 10, 10)
  487. r2 = Rect(1, 1, 10, 10)
  488. r3 = Rect(5, 5, 10, 10)
  489. r4 = Rect(10, 10, 10, 10)
  490. r5 = Rect(50, 50, 10, 10)
  491. rects_values = 1
  492. d = {2: r2}
  493. l = r.collidedictall(d, rects_values)
  494. self.assertEqual(l, [(2, r2)])
  495. d2 = {2: r2, 3: r3, 4: r4, 5: r5}
  496. l2 = r.collidedictall(d2, rects_values)
  497. self.assertEqual(l2, [(2, r2), (3, r3), (4, r4)])
  498. def test_collidelist(self):
  499. # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelist:
  500. # Rect.collidelist(list): return index
  501. # test if one rectangle in a list intersects
  502. #
  503. # Test whether the rectangle collides with any in a sequence of
  504. # rectangles. The index of the first collision found is returned. If
  505. # no collisions are found an index of -1 is returned.
  506. r = Rect(1, 1, 10, 10)
  507. l = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)]
  508. self.assertEqual(r.collidelist(l), 1)
  509. f = [Rect(50, 50, 1, 1), (100, 100, 4, 4)]
  510. self.assertEqual(r.collidelist(f), -1)
  511. def test_collidelistall(self):
  512. # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelistall:
  513. # Rect.collidelistall(list): return indices
  514. # test if all rectangles in a list intersect
  515. #
  516. # Returns a list of all the indices that contain rectangles that
  517. # collide with the Rect. If no intersecting rectangles are found, an
  518. # empty list is returned.
  519. r = Rect(1, 1, 10, 10)
  520. l = [
  521. Rect(1, 1, 10, 10),
  522. Rect(5, 5, 10, 10),
  523. Rect(15, 15, 1, 1),
  524. Rect(2, 2, 1, 1),
  525. ]
  526. self.assertEqual(r.collidelistall(l), [0, 1, 3])
  527. f = [Rect(50, 50, 1, 1), Rect(20, 20, 5, 5)]
  528. self.assertFalse(r.collidelistall(f))
  529. def test_fit(self):
  530. # __doc__ (as of 2008-08-02) for pygame.rect.Rect.fit:
  531. # Rect.fit(Rect): return Rect
  532. # resize and move a rectangle with aspect ratio
  533. #
  534. # Returns a new rectangle that is moved and resized to fit another.
  535. # The aspect ratio of the original Rect is preserved, so the new
  536. # rectangle may be smaller than the target in either width or height.
  537. r = Rect(10, 10, 30, 30)
  538. r2 = Rect(30, 30, 15, 10)
  539. f = r.fit(r2)
  540. self.assertTrue(r2.contains(f))
  541. f2 = r2.fit(r)
  542. self.assertTrue(r.contains(f2))
  543. def test_copy(self):
  544. r = Rect(1, 2, 10, 20)
  545. c = r.copy()
  546. self.assertEqual(c, r)
  547. def test_subscript(self):
  548. r = Rect(1, 2, 3, 4)
  549. self.assertEqual(r[0], 1)
  550. self.assertEqual(r[1], 2)
  551. self.assertEqual(r[2], 3)
  552. self.assertEqual(r[3], 4)
  553. self.assertEqual(r[-1], 4)
  554. self.assertEqual(r[-2], 3)
  555. self.assertEqual(r[-4], 1)
  556. self.assertRaises(IndexError, r.__getitem__, 5)
  557. self.assertRaises(IndexError, r.__getitem__, -5)
  558. self.assertEqual(r[0:2], [1, 2])
  559. self.assertEqual(r[0:4], [1, 2, 3, 4])
  560. self.assertEqual(r[0:-1], [1, 2, 3])
  561. self.assertEqual(r[:], [1, 2, 3, 4])
  562. self.assertEqual(r[...], [1, 2, 3, 4])
  563. self.assertEqual(r[0:4:2], [1, 3])
  564. self.assertEqual(r[0:4:3], [1, 4])
  565. self.assertEqual(r[3::-1], [4, 3, 2, 1])
  566. self.assertRaises(TypeError, r.__getitem__, None)
  567. def test_ass_subscript(self):
  568. r = Rect(0, 0, 0, 0)
  569. r[...] = 1, 2, 3, 4
  570. self.assertEqual(r, [1, 2, 3, 4])
  571. self.assertRaises(TypeError, r.__setitem__, None, 0)
  572. self.assertEqual(r, [1, 2, 3, 4])
  573. self.assertRaises(TypeError, r.__setitem__, 0, '')
  574. self.assertEqual(r, [1, 2, 3, 4])
  575. self.assertRaises(IndexError, r.__setitem__, 4, 0)
  576. self.assertEqual(r, [1, 2, 3, 4])
  577. self.assertRaises(IndexError, r.__setitem__, -5, 0)
  578. self.assertEqual(r, [1, 2, 3, 4])
  579. r[0] = 10
  580. self.assertEqual(r, [10, 2, 3, 4])
  581. r[3] = 40
  582. self.assertEqual(r, [10, 2, 3, 40])
  583. r[-1] = 400
  584. self.assertEqual(r, [10, 2, 3, 400])
  585. r[-4] = 100
  586. self.assertEqual(r, [100, 2, 3, 400])
  587. r[1:3] = 0
  588. self.assertEqual(r, [100, 0, 0, 400])
  589. r[...] = 0
  590. self.assertEqual(r, [0, 0, 0, 0])
  591. r[:] = 9
  592. self.assertEqual(r, [9, 9, 9, 9])
  593. r[:] = 11, 12, 13, 14
  594. self.assertEqual(r, [11, 12, 13, 14])
  595. r[::-1] = r
  596. self.assertEqual(r, [14, 13, 12, 11])
  597. class SubclassTest(unittest.TestCase):
  598. class MyRect(Rect):
  599. def __init__(self, *args, **kwds):
  600. super(SubclassTest.MyRect, self).__init__(*args, **kwds)
  601. self.an_attribute = True
  602. def test_copy(self):
  603. mr1 = self.MyRect(1, 2, 10, 20)
  604. self.assertTrue(mr1.an_attribute)
  605. mr2 = mr1.copy()
  606. self.assertTrue(isinstance(mr2, self.MyRect))
  607. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  608. def test_move(self):
  609. mr1 = self.MyRect(1, 2, 10, 20)
  610. self.assertTrue(mr1.an_attribute)
  611. mr2 = mr1.move(1, 2)
  612. self.assertTrue(isinstance(mr2, self.MyRect))
  613. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  614. def test_inflate(self):
  615. mr1 = self.MyRect(1, 2, 10, 20)
  616. self.assertTrue(mr1.an_attribute)
  617. mr2 = mr1.inflate(2, 4)
  618. self.assertTrue(isinstance(mr2, self.MyRect))
  619. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  620. def test_clamp(self):
  621. mr1 = self.MyRect(19, 12, 5, 5)
  622. self.assertTrue(mr1.an_attribute)
  623. mr2 = mr1.clamp(Rect(10, 10, 10, 10))
  624. self.assertTrue(isinstance(mr2, self.MyRect))
  625. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  626. def test_clip(self):
  627. mr1 = self.MyRect(1, 2, 3, 4)
  628. self.assertTrue(mr1.an_attribute)
  629. mr2 = mr1.clip(Rect(0, 0, 3, 4))
  630. self.assertTrue(isinstance(mr2, self.MyRect))
  631. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  632. def test_union(self):
  633. mr1 = self.MyRect(1, 1, 1, 2)
  634. self.assertTrue(mr1.an_attribute)
  635. mr2 = mr1.union(Rect(-2, -2, 1, 2))
  636. self.assertTrue(isinstance(mr2, self.MyRect))
  637. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  638. def test_unionall(self):
  639. mr1 = self.MyRect(0, 0, 1, 1)
  640. self.assertTrue(mr1.an_attribute)
  641. mr2 = mr1.unionall([Rect(-2, -2, 1, 1), Rect(2, 2, 1, 1)])
  642. self.assertTrue(isinstance(mr2, self.MyRect))
  643. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  644. def test_fit(self):
  645. mr1 = self.MyRect(10, 10, 30, 30)
  646. self.assertTrue(mr1.an_attribute)
  647. mr2 = mr1.fit(Rect(30, 30, 15, 10))
  648. self.assertTrue(isinstance(mr2, self.MyRect))
  649. self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
  650. if __name__ == '__main__':
  651. unittest.main()