1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
|
import math
import sys
import unittest
import platform
from pygame import Rect, Vector2
from pygame.tests import test_utils
IS_PYPY = "PyPy" == platform.python_implementation()
class RectTypeTest(unittest.TestCase):
def _assertCountEqual(self, *args, **kwargs):
self.assertCountEqual(*args, **kwargs)
def testConstructionXYWidthHeight(self):
r = Rect(1, 2, 3, 4)
self.assertEqual(1, r.left)
self.assertEqual(2, r.top)
self.assertEqual(3, r.width)
self.assertEqual(4, r.height)
def testConstructionTopLeftSize(self):
r = Rect((1, 2), (3, 4))
self.assertEqual(1, r.left)
self.assertEqual(2, r.top)
self.assertEqual(3, r.width)
self.assertEqual(4, r.height)
def testCalculatedAttributes(self):
r = Rect(1, 2, 3, 4)
self.assertEqual(r.left + r.width, r.right)
self.assertEqual(r.top + r.height, r.bottom)
self.assertEqual((r.width, r.height), r.size)
self.assertEqual((r.left, r.top), r.topleft)
self.assertEqual((r.right, r.top), r.topright)
self.assertEqual((r.left, r.bottom), r.bottomleft)
self.assertEqual((r.right, r.bottom), r.bottomright)
midx = r.left + r.width // 2
midy = r.top + r.height // 2
self.assertEqual(midx, r.centerx)
self.assertEqual(midy, r.centery)
self.assertEqual((r.centerx, r.centery), r.center)
self.assertEqual((r.centerx, r.top), r.midtop)
self.assertEqual((r.centerx, r.bottom), r.midbottom)
self.assertEqual((r.left, r.centery), r.midleft)
self.assertEqual((r.right, r.centery), r.midright)
def test_normalize(self):
"""Ensures normalize works when width and height are both negative."""
test_rect = Rect((1, 2), (-3, -6))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y + test_rect.h),
(-test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_normalize__positive_height(self):
"""Ensures normalize works with a negative width and a positive height."""
test_rect = Rect((1, 2), (-3, 6))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y),
(-test_rect.w, test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_normalize__positive_width(self):
"""Ensures normalize works with a positive width and a negative height."""
test_rect = Rect((1, 2), (3, -6))
expected_normalized_rect = (
(test_rect.x, test_rect.y + test_rect.h),
(test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_normalize__zero_height(self):
"""Ensures normalize works with a negative width and a zero height."""
test_rect = Rect((1, 2), (-3, 0))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y),
(-test_rect.w, test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_normalize__zero_width(self):
"""Ensures normalize works with a zero width and a negative height."""
test_rect = Rect((1, 2), (0, -6))
expected_normalized_rect = (
(test_rect.x, test_rect.y + test_rect.h),
(test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
@unittest.skipIf(IS_PYPY, "fails on pypy")
def test_normalize__non_negative(self):
"""Ensures normalize works when width and height are both non-negative.
Tests combinations of positive and zero values for width and height.
The normalize method has no impact when both width and height are
non-negative.
"""
for size in ((3, 6), (3, 0), (0, 6), (0, 0)):
test_rect = Rect((1, 2), size)
expected_normalized_rect = Rect(test_rect)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_x(self):
"""Ensures changing the x attribute moves the rect and does not change
the rect's size.
"""
expected_x = 10
expected_y = 2
expected_size = (3, 4)
r = Rect((1, expected_y), expected_size)
r.x = expected_x
self.assertEqual(r.x, expected_x)
self.assertEqual(r.x, r.left)
self.assertEqual(r.y, expected_y)
self.assertEqual(r.size, expected_size)
def test_x__invalid_value(self):
"""Ensures the x attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.x = value
def test_x__del(self):
"""Ensures the x attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.x
def test_y(self):
"""Ensures changing the y attribute moves the rect and does not change
the rect's size.
"""
expected_x = 1
expected_y = 20
expected_size = (3, 4)
r = Rect((expected_x, 2), expected_size)
r.y = expected_y
self.assertEqual(r.y, expected_y)
self.assertEqual(r.y, r.top)
self.assertEqual(r.x, expected_x)
self.assertEqual(r.size, expected_size)
def test_y__invalid_value(self):
"""Ensures the y attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.y = value
def test_y__del(self):
"""Ensures the y attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.y
def test_left(self):
"""Changing the left attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_left = 10
r.left = new_left
self.assertEqual(new_left, r.left)
self.assertEqual(Rect(new_left, 2, 3, 4), r)
def test_left__invalid_value(self):
"""Ensures the left attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.left = value
def test_left__del(self):
"""Ensures the left attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.left
def test_right(self):
"""Changing the right attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_right = r.right + 20
expected_left = r.left + 20
old_width = r.width
r.right = new_right
self.assertEqual(new_right, r.right)
self.assertEqual(expected_left, r.left)
self.assertEqual(old_width, r.width)
def test_right__invalid_value(self):
"""Ensures the right attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.right = value
def test_right__del(self):
"""Ensures the right attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.right
def test_top(self):
"""Changing the top attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_top = 10
r.top = new_top
self.assertEqual(Rect(1, new_top, 3, 4), r)
self.assertEqual(new_top, r.top)
def test_top__invalid_value(self):
"""Ensures the top attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.top = value
def test_top__del(self):
"""Ensures the top attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.top
def test_bottom(self):
"""Changing the bottom attribute moves the rect and does not change
the rect's height
"""
r = Rect(1, 2, 3, 4)
new_bottom = r.bottom + 20
expected_top = r.top + 20
old_height = r.height
r.bottom = new_bottom
self.assertEqual(new_bottom, r.bottom)
self.assertEqual(expected_top, r.top)
self.assertEqual(old_height, r.height)
def test_bottom__invalid_value(self):
"""Ensures the bottom attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottom = value
def test_bottom__del(self):
"""Ensures the bottom attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottom
def test_centerx(self):
"""Changing the centerx attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_centerx = r.centerx + 20
expected_left = r.left + 20
old_width = r.width
r.centerx = new_centerx
self.assertEqual(new_centerx, r.centerx)
self.assertEqual(expected_left, r.left)
self.assertEqual(old_width, r.width)
def test_centerx__invalid_value(self):
"""Ensures the centerx attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.centerx = value
def test_centerx__del(self):
"""Ensures the centerx attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.centerx
def test_centery(self):
"""Changing the centery attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_centery = r.centery + 20
expected_top = r.top + 20
old_height = r.height
r.centery = new_centery
self.assertEqual(new_centery, r.centery)
self.assertEqual(expected_top, r.top)
self.assertEqual(old_height, r.height)
def test_centery__invalid_value(self):
"""Ensures the centery attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.centery = value
def test_centery__del(self):
"""Ensures the centery attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.centery
def test_topleft(self):
"""Changing the topleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.topleft = new_topleft
self.assertEqual(new_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_topleft__invalid_value(self):
"""Ensures the topleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.topleft = value
def test_topleft__del(self):
"""Ensures the topleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.topleft
def test_bottomleft(self):
"""Changing the bottomleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_bottomleft = (r.left + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.bottomleft = new_bottomleft
self.assertEqual(new_bottomleft, r.bottomleft)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_bottomleft__invalid_value(self):
"""Ensures the bottomleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottomleft = value
def test_bottomleft__del(self):
"""Ensures the bottomleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottomleft
def test_topright(self):
"""Changing the topright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_topright = (r.right + 20, r.top + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.topright = new_topright
self.assertEqual(new_topright, r.topright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_topright__invalid_value(self):
"""Ensures the topright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.topright = value
def test_topright__del(self):
"""Ensures the topright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.topright
def test_bottomright(self):
"""Changing the bottomright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_bottomright = (r.right + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.bottomright = new_bottomright
self.assertEqual(new_bottomright, r.bottomright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_bottomright__invalid_value(self):
"""Ensures the bottomright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottomright = value
def test_bottomright__del(self):
"""Ensures the bottomright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottomright
def test_center(self):
"""Changing the center attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_center = (r.centerx + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.center = new_center
self.assertEqual(new_center, r.center)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_center__invalid_value(self):
"""Ensures the center attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.center = value
def test_center__del(self):
"""Ensures the center attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.center
def test_midleft(self):
"""Changing the midleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midleft = (r.left + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midleft = new_midleft
self.assertEqual(new_midleft, r.midleft)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midleft__invalid_value(self):
"""Ensures the midleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midleft = value
def test_midleft__del(self):
"""Ensures the midleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midleft
def test_midright(self):
"""Changing the midright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midright = (r.right + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midright = new_midright
self.assertEqual(new_midright, r.midright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midright__invalid_value(self):
"""Ensures the midright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midright = value
def test_midright__del(self):
"""Ensures the midright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midright
def test_midtop(self):
"""Changing the midtop attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midtop = (r.centerx + 20, r.top + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midtop = new_midtop
self.assertEqual(new_midtop, r.midtop)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midtop__invalid_value(self):
"""Ensures the midtop attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midtop = value
def test_midtop__del(self):
"""Ensures the midtop attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midtop
def test_midbottom(self):
"""Changing the midbottom attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midbottom = (r.centerx + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midbottom = new_midbottom
self.assertEqual(new_midbottom, r.midbottom)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midbottom__invalid_value(self):
"""Ensures the midbottom attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midbottom = value
def test_midbottom__del(self):
"""Ensures the midbottom attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midbottom
def test_width(self):
"""Changing the width resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_width = 10
old_topleft = r.topleft
old_height = r.height
r.width = new_width
self.assertEqual(new_width, r.width)
self.assertEqual(old_height, r.height)
self.assertEqual(old_topleft, r.topleft)
def test_width__invalid_value(self):
"""Ensures the width attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.width = value
def test_width__del(self):
"""Ensures the width attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.width
def test_height(self):
"""Changing the height resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_height = 10
old_topleft = r.topleft
old_width = r.width
r.height = new_height
self.assertEqual(new_height, r.height)
self.assertEqual(old_width, r.width)
self.assertEqual(old_topleft, r.topleft)
def test_height__invalid_value(self):
"""Ensures the height attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.height = value
def test_height__del(self):
"""Ensures the height attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.height
def test_size(self):
"""Changing the size resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_size = (10, 20)
old_topleft = r.topleft
r.size = new_size
self.assertEqual(new_size, r.size)
self.assertEqual(old_topleft, r.topleft)
def test_size__invalid_value(self):
"""Ensures the size attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.size = value
def test_size__del(self):
"""Ensures the size attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.size
def test_contains(self):
r = Rect(1, 2, 3, 4)
self.assertTrue(
r.contains(Rect(2, 3, 1, 1)), "r does not contain Rect(2, 3, 1, 1)"
)
self.assertTrue(Rect(2, 3, 1, 1) in r, "r does not contain Rect(2, 3, 1, 1) 2")
self.assertTrue(
r.contains(Rect(r)), "r does not contain the same rect as itself"
)
self.assertTrue(r in Rect(r), "r does not contain the same rect as itself")
self.assertTrue(
r.contains(Rect(2, 3, 0, 0)),
"r does not contain an empty rect within its bounds",
)
self.assertTrue(
Rect(2, 3, 0, 0) in r,
"r does not contain an empty rect within its bounds",
)
self.assertFalse(r.contains(Rect(0, 0, 1, 2)), "r contains Rect(0, 0, 1, 2)")
self.assertFalse(r.contains(Rect(4, 6, 1, 1)), "r contains Rect(4, 6, 1, 1)")
self.assertFalse(r.contains(Rect(4, 6, 0, 0)), "r contains Rect(4, 6, 0, 0)")
self.assertFalse(Rect(0, 0, 1, 2) in r, "r contains Rect(0, 0, 1, 2)")
self.assertFalse(Rect(4, 6, 1, 1) in r, "r contains Rect(4, 6, 1, 1)")
self.assertFalse(Rect(4, 6, 0, 0) in r, "r contains Rect(4, 6, 0, 0)")
self.assertTrue(2 in Rect(0, 0, 1, 2), "r does not contain 2")
self.assertFalse(3 in Rect(0, 0, 1, 2), "r contains 3")
def test_collidepoint(self):
r = Rect(1, 2, 3, 4)
self.assertTrue(
r.collidepoint(r.left, r.top), "r does not collide with point (left, top)"
)
self.assertFalse(
r.collidepoint(r.left - 1, r.top), "r collides with point (left - 1, top)"
)
self.assertFalse(
r.collidepoint(r.left, r.top - 1), "r collides with point (left, top - 1)"
)
self.assertFalse(
r.collidepoint(r.left - 1, r.top - 1),
"r collides with point (left - 1, top - 1)",
)
self.assertTrue(
r.collidepoint(r.right - 1, r.bottom - 1),
"r does not collide with point (right - 1, bottom - 1)",
)
self.assertFalse(
r.collidepoint(r.right, r.bottom), "r collides with point (right, bottom)"
)
self.assertFalse(
r.collidepoint(r.right - 1, r.bottom),
"r collides with point (right - 1, bottom)",
)
self.assertFalse(
r.collidepoint(r.right, r.bottom - 1),
"r collides with point (right, bottom - 1)",
)
def test_inflate__larger(self):
"""The inflate method inflates around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r2 = r.inflate(4, 6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 2, r2.left)
self.assertEqual(r.top - 3, r2.top)
self.assertEqual(r.right + 2, r2.right)
self.assertEqual(r.bottom + 3, r2.bottom)
self.assertEqual(r.width + 4, r2.width)
self.assertEqual(r.height + 6, r2.height)
def test_inflate__smaller(self):
"""The inflate method inflates around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r2 = r.inflate(-4, -6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 3, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 3, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 6, r2.height)
def test_inflate_ip__larger(self):
"""The inflate_ip method inflates around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r2 = Rect(r)
r2.inflate_ip(-4, -6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 3, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 3, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 6, r2.height)
def test_inflate_ip__smaller(self):
"""The inflate method inflates around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r2 = Rect(r)
r2.inflate_ip(-4, -6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 3, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 3, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 6, r2.height)
def test_clamp(self):
r = Rect(10, 10, 10, 10)
c = Rect(19, 12, 5, 5).clamp(r)
self.assertEqual(c.right, r.right)
self.assertEqual(c.top, 12)
c = Rect(1, 2, 3, 4).clamp(r)
self.assertEqual(c.topleft, r.topleft)
c = Rect(5, 500, 22, 33).clamp(r)
self.assertEqual(c.center, r.center)
def test_clamp_ip(self):
r = Rect(10, 10, 10, 10)
c = Rect(19, 12, 5, 5)
c.clamp_ip(r)
self.assertEqual(c.right, r.right)
self.assertEqual(c.top, 12)
c = Rect(1, 2, 3, 4)
c.clamp_ip(r)
self.assertEqual(c.topleft, r.topleft)
c = Rect(5, 500, 22, 33)
c.clamp_ip(r)
self.assertEqual(c.center, r.center)
def test_clip(self):
r1 = Rect(1, 2, 3, 4)
self.assertEqual(Rect(1, 2, 2, 2), r1.clip(Rect(0, 0, 3, 4)))
self.assertEqual(Rect(2, 2, 2, 4), r1.clip(Rect(2, 2, 10, 20)))
self.assertEqual(Rect(2, 3, 1, 2), r1.clip(Rect(2, 3, 1, 2)))
self.assertEqual((0, 0), r1.clip(20, 30, 5, 6).size)
self.assertEqual(
r1, r1.clip(Rect(r1)), "r1 does not clip an identical rect to itself"
)
def test_clipline(self):
"""Ensures clipline handles four int parameters.
Tests the clipline(x1, y1, x2, y2) format.
"""
rect = Rect((1, 2), (35, 40))
x1 = 5
y1 = 6
x2 = 11
y2 = 19
expected_line = ((x1, y1), (x2, y2))
clipped_line = rect.clipline(x1, y1, x2, y2)
self.assertIsInstance(clipped_line, tuple)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__two_sequences(self):
"""Ensures clipline handles a sequence of two sequences.
Tests the clipline((x1, y1), (x2, y2)) format.
Tests the sequences as different types.
"""
rect = Rect((1, 2), (35, 40))
pt1 = (5, 6)
pt2 = (11, 19)
INNER_SEQUENCES = (list, tuple, Vector2)
expected_line = (pt1, pt2)
for inner_seq1 in INNER_SEQUENCES:
endpt1 = inner_seq1(pt1)
for inner_seq2 in INNER_SEQUENCES:
clipped_line = rect.clipline((endpt1, inner_seq2(pt2)))
self.assertIsInstance(clipped_line, tuple)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__sequence_of_four_ints(self):
"""Ensures clipline handles a sequence of four ints.
Tests the clipline((x1, y1, x2, y2)) format.
Tests the sequence as different types.
"""
rect = Rect((1, 2), (35, 40))
line = (5, 6, 11, 19)
expected_line = ((line[0], line[1]), (line[2], line[3]))
for outer_seq in (list, tuple):
clipped_line = rect.clipline(outer_seq(line))
self.assertIsInstance(clipped_line, tuple)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__sequence_of_two_sequences(self):
"""Ensures clipline handles a sequence of two sequences.
Tests the clipline(((x1, y1), (x2, y2))) format.
Tests the sequences as different types.
"""
rect = Rect((1, 2), (35, 40))
pt1 = (5, 6)
pt2 = (11, 19)
INNER_SEQUENCES = (list, tuple, Vector2)
expected_line = (pt1, pt2)
for inner_seq1 in INNER_SEQUENCES:
endpt1 = inner_seq1(pt1)
for inner_seq2 in INNER_SEQUENCES:
endpt2 = inner_seq2(pt2)
for outer_seq in (list, tuple):
clipped_line = rect.clipline(outer_seq((endpt1, endpt2)))
self.assertIsInstance(clipped_line, tuple)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__floats(self):
"""Ensures clipline handles float parameters."""
rect = Rect((1, 2), (35, 40))
x1 = 5.9
y1 = 6.9
x2 = 11.9
y2 = 19.9
# Floats are truncated.
expected_line = (
(math.floor(x1), math.floor(y1)),
(math.floor(x2), math.floor(y2)),
)
clipped_line = rect.clipline(x1, y1, x2, y2)
self.assertIsInstance(clipped_line, tuple)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__no_overlap(self):
"""Ensures lines that do not overlap the rect are not clipped."""
rect = Rect((10, 25), (15, 20))
# Use a bigger rect to help create test lines.
big_rect = rect.inflate(2, 2)
lines = (
(big_rect.bottomleft, big_rect.topleft), # Left edge.
(big_rect.topleft, big_rect.topright), # Top edge.
(big_rect.topright, big_rect.bottomright), # Right edge.
(big_rect.bottomright, big_rect.bottomleft),
) # Bottom edge.
expected_line = ()
# Test lines outside rect.
for line in lines:
clipped_line = rect.clipline(line)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__both_endpoints_outside(self):
"""Ensures lines that overlap the rect are clipped.
Testing lines with both endpoints outside the rect.
"""
rect = Rect((0, 0), (20, 20))
# Use a bigger rect to help create test lines.
big_rect = rect.inflate(2, 2)
# Create a dict of lines and expected results.
line_dict = {
(big_rect.midleft, big_rect.midright): (
rect.midleft,
(rect.midright[0] - 1, rect.midright[1]),
),
(big_rect.midtop, big_rect.midbottom): (
rect.midtop,
(rect.midbottom[0], rect.midbottom[1] - 1),
),
# Diagonals.
(big_rect.topleft, big_rect.bottomright): (
rect.topleft,
(rect.bottomright[0] - 1, rect.bottomright[1] - 1),
),
# This line needs a small adjustment to make sure it intersects
# the rect correctly.
(
(big_rect.topright[0] - 1, big_rect.topright[1]),
(big_rect.bottomleft[0], big_rect.bottomleft[1] - 1),
): (
(rect.topright[0] - 1, rect.topright[1]),
(rect.bottomleft[0], rect.bottomleft[1] - 1),
),
}
for line, expected_line in line_dict.items():
clipped_line = rect.clipline(line)
self.assertTupleEqual(clipped_line, expected_line)
# Swap endpoints to test for symmetry.
expected_line = (expected_line[1], expected_line[0])
clipped_line = rect.clipline((line[1], line[0]))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__both_endpoints_inside(self):
"""Ensures lines that overlap the rect are clipped.
Testing lines with both endpoints inside the rect.
"""
rect = Rect((-10, -5), (20, 20))
# Use a smaller rect to help create test lines.
small_rect = rect.inflate(-2, -2)
lines = (
(small_rect.midleft, small_rect.midright),
(small_rect.midtop, small_rect.midbottom),
# Diagonals.
(small_rect.topleft, small_rect.bottomright),
(small_rect.topright, small_rect.bottomleft),
)
for line in lines:
expected_line = line
clipped_line = rect.clipline(line)
self.assertTupleEqual(clipped_line, expected_line)
# Swap endpoints to test for symmetry.
expected_line = (expected_line[1], expected_line[0])
clipped_line = rect.clipline((line[1], line[0]))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__endpoints_inside_and_outside(self):
"""Ensures lines that overlap the rect are clipped.
Testing lines with one endpoint outside the rect and the other is
inside the rect.
"""
rect = Rect((0, 0), (21, 21))
# Use a bigger rect to help create test lines.
big_rect = rect.inflate(2, 2)
# Create a dict of lines and expected results.
line_dict = {
(big_rect.midleft, rect.center): (rect.midleft, rect.center),
(big_rect.midtop, rect.center): (rect.midtop, rect.center),
(big_rect.midright, rect.center): (
(rect.midright[0] - 1, rect.midright[1]),
rect.center,
),
(big_rect.midbottom, rect.center): (
(rect.midbottom[0], rect.midbottom[1] - 1),
rect.center,
),
# Diagonals.
(big_rect.topleft, rect.center): (rect.topleft, rect.center),
(big_rect.topright, rect.center): (
(rect.topright[0] - 1, rect.topright[1]),
rect.center,
),
(big_rect.bottomright, rect.center): (
(rect.bottomright[0] - 1, rect.bottomright[1] - 1),
rect.center,
),
# This line needs a small adjustment to make sure it intersects
# the rect correctly.
((big_rect.bottomleft[0], big_rect.bottomleft[1] - 1), rect.center): (
(rect.bottomleft[0], rect.bottomleft[1] - 1),
rect.center,
),
}
for line, expected_line in line_dict.items():
clipped_line = rect.clipline(line)
self.assertTupleEqual(clipped_line, expected_line)
# Swap endpoints to test for symmetry.
expected_line = (expected_line[1], expected_line[0])
clipped_line = rect.clipline((line[1], line[0]))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__edges(self):
"""Ensures clipline properly clips line that are along the rect edges."""
rect = Rect((10, 25), (15, 20))
# Create a dict of edges and expected results.
edge_dict = {
# Left edge.
(rect.bottomleft, rect.topleft): (
(rect.bottomleft[0], rect.bottomleft[1] - 1),
rect.topleft,
),
# Top edge.
(rect.topleft, rect.topright): (
rect.topleft,
(rect.topright[0] - 1, rect.topright[1]),
),
# Right edge.
(rect.topright, rect.bottomright): (),
# Bottom edge.
(rect.bottomright, rect.bottomleft): (),
}
for edge, expected_line in edge_dict.items():
clipped_line = rect.clipline(edge)
self.assertTupleEqual(clipped_line, expected_line)
# Swap endpoints to test for symmetry.
if expected_line:
expected_line = (expected_line[1], expected_line[0])
clipped_line = rect.clipline((edge[1], edge[0]))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__equal_endpoints_with_overlap(self):
"""Ensures clipline handles lines with both endpoints the same.
Testing lines that overlap the rect.
"""
rect = Rect((10, 25), (15, 20))
# Test all the points in and on a rect.
pts = (
(x, y)
for x in range(rect.left, rect.right)
for y in range(rect.top, rect.bottom)
)
for pt in pts:
expected_line = (pt, pt)
clipped_line = rect.clipline((pt, pt))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__equal_endpoints_no_overlap(self):
"""Ensures clipline handles lines with both endpoints the same.
Testing lines that do not overlap the rect.
"""
expected_line = ()
rect = Rect((10, 25), (15, 20))
# Test points outside rect.
for pt in test_utils.rect_perimeter_pts(rect.inflate(2, 2)):
clipped_line = rect.clipline((pt, pt))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__zero_size_rect(self):
"""Ensures clipline handles zero sized rects correctly."""
expected_line = ()
for size in ((0, 15), (15, 0), (0, 0)):
rect = Rect((10, 25), size)
clipped_line = rect.clipline(rect.topleft, rect.topleft)
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__negative_size_rect(self):
"""Ensures clipline handles negative sized rects correctly."""
expected_line = ()
for size in ((-15, 20), (15, -20), (-15, -20)):
rect = Rect((10, 25), size)
norm_rect = rect.copy()
norm_rect.normalize()
# Use a bigger rect to help create test lines.
big_rect = norm_rect.inflate(2, 2)
# Create a dict of lines and expected results. Some line have both
# endpoints outside the rect and some have one inside and one
# outside.
line_dict = {
(big_rect.midleft, big_rect.midright): (
norm_rect.midleft,
(norm_rect.midright[0] - 1, norm_rect.midright[1]),
),
(big_rect.midtop, big_rect.midbottom): (
norm_rect.midtop,
(norm_rect.midbottom[0], norm_rect.midbottom[1] - 1),
),
(big_rect.midleft, norm_rect.center): (
norm_rect.midleft,
norm_rect.center,
),
(big_rect.midtop, norm_rect.center): (
norm_rect.midtop,
norm_rect.center,
),
(big_rect.midright, norm_rect.center): (
(norm_rect.midright[0] - 1, norm_rect.midright[1]),
norm_rect.center,
),
(big_rect.midbottom, norm_rect.center): (
(norm_rect.midbottom[0], norm_rect.midbottom[1] - 1),
norm_rect.center,
),
}
for line, expected_line in line_dict.items():
clipped_line = rect.clipline(line)
# Make sure rect wasn't normalized.
self.assertNotEqual(rect, norm_rect)
self.assertTupleEqual(clipped_line, expected_line)
# Swap endpoints to test for symmetry.
expected_line = (expected_line[1], expected_line[0])
clipped_line = rect.clipline((line[1], line[0]))
self.assertTupleEqual(clipped_line, expected_line)
def test_clipline__invalid_line(self):
"""Ensures clipline handles invalid lines correctly."""
rect = Rect((0, 0), (10, 20))
invalid_lines = (
(),
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 4, 5),
((1, 2),),
((1, 2), (3,)),
((1, 2), 3),
((1, 2, 5), (3, 4)),
((1, 2), (3, 4, 5)),
((1, 2), (3, 4), (5, 6)),
)
for line in invalid_lines:
with self.assertRaises(TypeError):
clipped_line = rect.clipline(line)
with self.assertRaises(TypeError):
clipped_line = rect.clipline(*line)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_move(self):
r = Rect(1, 2, 3, 4)
move_x = 10
move_y = 20
r2 = r.move(move_x, move_y)
expected_r2 = Rect(r.left + move_x, r.top + move_y, r.width, r.height)
self.assertEqual(expected_r2, r2)
@unittest.skipIf(IS_PYPY, "fails on pypy sometimes")
def test_move_ip(self):
r = Rect(1, 2, 3, 4)
r2 = Rect(r)
move_x = 10
move_y = 20
r2.move_ip(move_x, move_y)
expected_r2 = Rect(r.left + move_x, r.top + move_y, r.width, r.height)
self.assertEqual(expected_r2, r2)
def test_update_XYWidthHeight(self):
"""Test update with 4 int values(x, y, w, h)"""
rect = Rect(0, 0, 1, 1)
rect.update(1, 2, 3, 4)
self.assertEqual(1, rect.left)
self.assertEqual(2, rect.top)
self.assertEqual(3, rect.width)
self.assertEqual(4, rect.height)
def test_update__TopLeftSize(self):
"""Test update with 2 tuples((x, y), (w, h))"""
rect = Rect(0, 0, 1, 1)
rect.update((1, 2), (3, 4))
self.assertEqual(1, rect.left)
self.assertEqual(2, rect.top)
self.assertEqual(3, rect.width)
self.assertEqual(4, rect.height)
def test_update__List(self):
"""Test update with list"""
rect = Rect(0, 0, 1, 1)
rect2 = [1, 2, 3, 4]
rect.update(rect2)
self.assertEqual(1, rect.left)
self.assertEqual(2, rect.top)
self.assertEqual(3, rect.width)
self.assertEqual(4, rect.height)
def test_update__RectObject(self):
"""Test update with other rect object"""
rect = Rect(0, 0, 1, 1)
rect2 = Rect(1, 2, 3, 4)
rect.update(rect2)
self.assertEqual(1, rect.left)
self.assertEqual(2, rect.top)
self.assertEqual(3, rect.width)
self.assertEqual(4, rect.height)
def test_union(self):
r1 = Rect(1, 1, 1, 2)
r2 = Rect(-2, -2, 1, 2)
self.assertEqual(Rect(-2, -2, 4, 5), r1.union(r2))
def test_union__with_identical_Rect(self):
r1 = Rect(1, 2, 3, 4)
self.assertEqual(r1, r1.union(Rect(r1)))
def test_union_ip(self):
r1 = Rect(1, 1, 1, 2)
r2 = Rect(-2, -2, 1, 2)
r1.union_ip(r2)
self.assertEqual(Rect(-2, -2, 4, 5), r1)
def test_unionall(self):
r1 = Rect(0, 0, 1, 1)
r2 = Rect(-2, -2, 1, 1)
r3 = Rect(2, 2, 1, 1)
r4 = r1.unionall([r2, r3])
self.assertEqual(Rect(-2, -2, 5, 5), r4)
def test_unionall__invalid_rect_format(self):
"""Ensures unionall correctly handles invalid rect parameters."""
numbers = [0, 1.2, 2, 3.3]
strs = ["a", "b", "c"]
nones = [None, None]
for invalid_rects in (numbers, strs, nones):
with self.assertRaises(TypeError):
Rect(0, 0, 1, 1).unionall(invalid_rects)
def test_unionall_ip(self):
r1 = Rect(0, 0, 1, 1)
r2 = Rect(-2, -2, 1, 1)
r3 = Rect(2, 2, 1, 1)
r1.unionall_ip([r2, r3])
self.assertEqual(Rect(-2, -2, 5, 5), r1)
# Bug for an empty list. Would return a Rect instead of None.
self.assertTrue(r1.unionall_ip([]) is None)
def test_unionall__invalid_rect_format(self):
"""Ensures unionall_ip correctly handles invalid rect parameters."""
numbers = [0, 1.2, 2, 3.3]
strs = ["a", "b", "c"]
nones = [None, None]
for invalid_rects in (numbers, strs, nones):
with self.assertRaises(TypeError):
Rect(0, 0, 1, 1).unionall_ip(invalid_rects)
def test_colliderect(self):
r1 = Rect(1, 2, 3, 4)
self.assertTrue(
r1.colliderect(Rect(0, 0, 2, 3)),
"r1 does not collide with Rect(0, 0, 2, 3)",
)
self.assertFalse(
r1.colliderect(Rect(0, 0, 1, 2)), "r1 collides with Rect(0, 0, 1, 2)"
)
self.assertFalse(
r1.colliderect(Rect(r1.right, r1.bottom, 2, 2)),
"r1 collides with Rect(r1.right, r1.bottom, 2, 2)",
)
self.assertTrue(
r1.colliderect(Rect(r1.left + 1, r1.top + 1, r1.width - 2, r1.height - 2)),
"r1 does not collide with Rect(r1.left + 1, r1.top + 1, "
+ "r1.width - 2, r1.height - 2)",
)
self.assertTrue(
r1.colliderect(Rect(r1.left - 1, r1.top - 1, r1.width + 2, r1.height + 2)),
"r1 does not collide with Rect(r1.left - 1, r1.top - 1, "
+ "r1.width + 2, r1.height + 2)",
)
self.assertTrue(
r1.colliderect(Rect(r1)), "r1 does not collide with an identical rect"
)
self.assertFalse(
r1.colliderect(Rect(r1.right, r1.bottom, 0, 0)),
"r1 collides with Rect(r1.right, r1.bottom, 0, 0)",
)
self.assertFalse(
r1.colliderect(Rect(r1.right, r1.bottom, 1, 1)),
"r1 collides with Rect(r1.right, r1.bottom, 1, 1)",
)
@unittest.skipIf(IS_PYPY, "fails on pypy3 sometimes")
def testEquals(self):
"""check to see how the rect uses __eq__"""
r1 = Rect(1, 2, 3, 4)
r2 = Rect(10, 20, 30, 40)
r3 = (10, 20, 30, 40)
r4 = Rect(10, 20, 30, 40)
class foo(Rect):
def __eq__(self, other):
return id(self) == id(other)
def __ne__(self, other):
return id(self) != id(other)
class foo2(Rect):
pass
r5 = foo(10, 20, 30, 40)
r6 = foo2(10, 20, 30, 40)
self.assertNotEqual(r5, r2)
# because we define equality differently for this subclass.
self.assertEqual(r6, r2)
rect_list = [r1, r2, r3, r4, r6]
# see if we can remove 4 of these.
rect_list.remove(r2)
rect_list.remove(r2)
rect_list.remove(r2)
rect_list.remove(r2)
self.assertRaises(ValueError, rect_list.remove, r2)
def test_collidedict(self):
"""Ensures collidedict detects collisions."""
rect = Rect(1, 1, 10, 10)
collide_item1 = ("collide 1", rect.copy())
collide_item2 = ("collide 2", Rect(5, 5, 10, 10))
no_collide_item1 = ("no collide 1", Rect(60, 60, 10, 10))
no_collide_item2 = ("no collide 2", Rect(70, 70, 10, 10))
# Dict to check collisions with values.
rect_values = dict(
(collide_item1, collide_item2, no_collide_item1, no_collide_item2)
)
value_collide_items = (collide_item1, collide_item2)
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
key_collide_items = tuple((tuple(v), k) for k, v in value_collide_items)
for use_values in (True, False):
if use_values:
expected_items = value_collide_items
d = rect_values
else:
expected_items = key_collide_items
d = rect_keys
collide_item = rect.collidedict(d, use_values)
# The detected collision could be any of the possible items.
self.assertIn(collide_item, expected_items)
def test_collidedict__no_collision(self):
"""Ensures collidedict returns None when no collisions."""
rect = Rect(1, 1, 10, 10)
no_collide_item1 = ("no collide 1", Rect(50, 50, 10, 10))
no_collide_item2 = ("no collide 2", Rect(60, 60, 10, 10))
no_collide_item3 = ("no collide 3", Rect(70, 70, 10, 10))
# Dict to check collisions with values.
rect_values = dict((no_collide_item1, no_collide_item2, no_collide_item3))
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
for use_values in (True, False):
d = rect_values if use_values else rect_keys
collide_item = rect.collidedict(d, use_values)
self.assertIsNone(collide_item)
def test_collidedict__barely_touching(self):
"""Ensures collidedict works correctly for rects that barely touch."""
rect = Rect(1, 1, 10, 10)
# Small rect to test barely touching collisions.
collide_rect = Rect(0, 0, 1, 1)
collide_item1 = ("collide 1", collide_rect)
no_collide_item1 = ("no collide 1", Rect(50, 50, 10, 10))
no_collide_item2 = ("no collide 2", Rect(60, 60, 10, 10))
no_collide_item3 = ("no collide 3", Rect(70, 70, 10, 10))
# Dict to check collisions with values.
no_collide_rect_values = dict(
(no_collide_item1, no_collide_item2, no_collide_item3)
)
# Dict to check collisions with keys.
no_collide_rect_keys = {tuple(v): k for k, v in no_collide_rect_values.items()}
# Tests the collide_rect on each of the rect's corners.
for attr in ("topleft", "topright", "bottomright", "bottomleft"):
setattr(collide_rect, attr, getattr(rect, attr))
for use_values in (True, False):
if use_values:
expected_item = collide_item1
d = dict(no_collide_rect_values)
else:
expected_item = (tuple(collide_item1[1]), collide_item1[0])
d = dict(no_collide_rect_keys)
d.update((expected_item,)) # Add in the expected item.
collide_item = rect.collidedict(d, use_values)
self.assertTupleEqual(collide_item, expected_item)
def test_collidedict__zero_sized_rects(self):
"""Ensures collidedict works correctly with zero sized rects.
There should be no collisions with zero sized rects.
"""
zero_rect1 = Rect(1, 1, 0, 0)
zero_rect2 = Rect(1, 1, 1, 0)
zero_rect3 = Rect(1, 1, 0, 1)
zero_rect4 = Rect(1, 1, -1, 0)
zero_rect5 = Rect(1, 1, 0, -1)
no_collide_item1 = ("no collide 1", zero_rect1.copy())
no_collide_item2 = ("no collide 2", zero_rect2.copy())
no_collide_item3 = ("no collide 3", zero_rect3.copy())
no_collide_item4 = ("no collide 4", zero_rect4.copy())
no_collide_item5 = ("no collide 5", zero_rect5.copy())
no_collide_item6 = ("no collide 6", Rect(0, 0, 10, 10))
no_collide_item7 = ("no collide 7", Rect(0, 0, 2, 2))
# Dict to check collisions with values.
rect_values = dict(
(
no_collide_item1,
no_collide_item2,
no_collide_item3,
no_collide_item4,
no_collide_item5,
no_collide_item6,
no_collide_item7,
)
)
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
for use_values in (True, False):
d = rect_values if use_values else rect_keys
for zero_rect in (
zero_rect1,
zero_rect2,
zero_rect3,
zero_rect4,
zero_rect5,
):
collide_item = zero_rect.collidedict(d, use_values)
self.assertIsNone(collide_item)
def test_collidedict__zero_sized_rects_as_args(self):
"""Ensures collidedict works correctly with zero sized rects as args.
There should be no collisions with zero sized rects.
"""
rect = Rect(0, 0, 10, 10)
no_collide_item1 = ("no collide 1", Rect(1, 1, 0, 0))
no_collide_item2 = ("no collide 2", Rect(1, 1, 1, 0))
no_collide_item3 = ("no collide 3", Rect(1, 1, 0, 1))
# Dict to check collisions with values.
rect_values = dict((no_collide_item1, no_collide_item2, no_collide_item3))
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
for use_values in (True, False):
d = rect_values if use_values else rect_keys
collide_item = rect.collidedict(d, use_values)
self.assertIsNone(collide_item)
def test_collidedict__negative_sized_rects(self):
"""Ensures collidedict works correctly with negative sized rects."""
neg_rect = Rect(1, 1, -1, -1)
collide_item1 = ("collide 1", neg_rect.copy())
collide_item2 = ("collide 2", Rect(0, 0, 10, 10))
no_collide_item1 = ("no collide 1", Rect(1, 1, 10, 10))
# Dict to check collisions with values.
rect_values = dict((collide_item1, collide_item2, no_collide_item1))
value_collide_items = (collide_item1, collide_item2)
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
key_collide_items = tuple((tuple(v), k) for k, v in value_collide_items)
for use_values in (True, False):
if use_values:
collide_items = value_collide_items
d = rect_values
else:
collide_items = key_collide_items
d = rect_keys
collide_item = neg_rect.collidedict(d, use_values)
# The detected collision could be any of the possible items.
self.assertIn(collide_item, collide_items)
def test_collidedict__negative_sized_rects_as_args(self):
"""Ensures collidedict works correctly with negative sized rect args."""
rect = Rect(0, 0, 10, 10)
collide_item1 = ("collide 1", Rect(1, 1, -1, -1))
no_collide_item1 = ("no collide 1", Rect(1, 1, -1, 0))
no_collide_item2 = ("no collide 2", Rect(1, 1, 0, -1))
# Dict to check collisions with values.
rect_values = dict((collide_item1, no_collide_item1, no_collide_item2))
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
for use_values in (True, False):
if use_values:
expected_item = collide_item1
d = rect_values
else:
expected_item = (tuple(collide_item1[1]), collide_item1[0])
d = rect_keys
collide_item = rect.collidedict(d, use_values)
self.assertTupleEqual(collide_item, expected_item)
def test_collidedict__invalid_dict_format(self):
"""Ensures collidedict correctly handles invalid dict parameters."""
rect = Rect(0, 0, 10, 10)
invalid_value_dict = ("collide", rect.copy())
invalid_key_dict = tuple(invalid_value_dict[1]), invalid_value_dict[0]
for use_values in (True, False):
d = invalid_value_dict if use_values else invalid_key_dict
with self.assertRaises(TypeError):
collide_item = rect.collidedict(d, use_values)
def test_collidedict__invalid_dict_value_format(self):
"""Ensures collidedict correctly handles dicts with invalid values."""
rect = Rect(0, 0, 10, 10)
rect_keys = {tuple(rect): "collide"}
with self.assertRaises(TypeError):
collide_item = rect.collidedict(rect_keys, 1)
def test_collidedict__invalid_dict_key_format(self):
"""Ensures collidedict correctly handles dicts with invalid keys."""
rect = Rect(0, 0, 10, 10)
rect_values = {"collide": rect.copy()}
with self.assertRaises(TypeError):
collide_item = rect.collidedict(rect_values)
def test_collidedict__invalid_use_values_format(self):
"""Ensures collidedict correctly handles invalid use_values parameters."""
rect = Rect(0, 0, 1, 1)
d = {}
for invalid_param in (None, d, 1.1):
with self.assertRaises(TypeError):
collide_item = rect.collidedict(d, invalid_param)
def test_collidedictall(self):
"""Ensures collidedictall detects collisions."""
rect = Rect(1, 1, 10, 10)
collide_item1 = ("collide 1", rect.copy())
collide_item2 = ("collide 2", Rect(5, 5, 10, 10))
no_collide_item1 = ("no collide 1", Rect(60, 60, 20, 20))
no_collide_item2 = ("no collide 2", Rect(70, 70, 20, 20))
# Dict to check collisions with values.
rect_values = dict(
(collide_item1, collide_item2, no_collide_item1, no_collide_item2)
)
value_collide_items = [collide_item1, collide_item2]
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
key_collide_items = [(tuple(v), k) for k, v in value_collide_items]
for use_values in (True, False):
if use_values:
expected_items = value_collide_items
d = rect_values
else:
expected_items = key_collide_items
d = rect_keys
collide_items = rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__no_collision(self):
"""Ensures collidedictall returns an empty list when no collisions."""
rect = Rect(1, 1, 10, 10)
no_collide_item1 = ("no collide 1", Rect(50, 50, 20, 20))
no_collide_item2 = ("no collide 2", Rect(60, 60, 20, 20))
no_collide_item3 = ("no collide 3", Rect(70, 70, 20, 20))
# Dict to check collisions with values.
rect_values = dict((no_collide_item1, no_collide_item2, no_collide_item3))
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
expected_items = []
for use_values in (True, False):
d = rect_values if use_values else rect_keys
collide_items = rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__barely_touching(self):
"""Ensures collidedictall works correctly for rects that barely touch."""
rect = Rect(1, 1, 10, 10)
# Small rect to test barely touching collisions.
collide_rect = Rect(0, 0, 1, 1)
collide_item1 = ("collide 1", collide_rect)
no_collide_item1 = ("no collide 1", Rect(50, 50, 20, 20))
no_collide_item2 = ("no collide 2", Rect(60, 60, 20, 20))
no_collide_item3 = ("no collide 3", Rect(70, 70, 20, 20))
# Dict to check collisions with values.
no_collide_rect_values = dict(
(no_collide_item1, no_collide_item2, no_collide_item3)
)
# Dict to check collisions with keys.
no_collide_rect_keys = {tuple(v): k for k, v in no_collide_rect_values.items()}
# Tests the collide_rect on each of the rect's corners.
for attr in ("topleft", "topright", "bottomright", "bottomleft"):
setattr(collide_rect, attr, getattr(rect, attr))
for use_values in (True, False):
if use_values:
expected_items = [collide_item1]
d = dict(no_collide_rect_values)
else:
expected_items = [(tuple(collide_item1[1]), collide_item1[0])]
d = dict(no_collide_rect_keys)
d.update(expected_items) # Add in the expected items.
collide_items = rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__zero_sized_rects(self):
"""Ensures collidedictall works correctly with zero sized rects.
There should be no collisions with zero sized rects.
"""
zero_rect1 = Rect(2, 2, 0, 0)
zero_rect2 = Rect(2, 2, 2, 0)
zero_rect3 = Rect(2, 2, 0, 2)
zero_rect4 = Rect(2, 2, -2, 0)
zero_rect5 = Rect(2, 2, 0, -2)
no_collide_item1 = ("no collide 1", zero_rect1.copy())
no_collide_item2 = ("no collide 2", zero_rect2.copy())
no_collide_item3 = ("no collide 3", zero_rect3.copy())
no_collide_item4 = ("no collide 4", zero_rect4.copy())
no_collide_item5 = ("no collide 5", zero_rect5.copy())
no_collide_item6 = ("no collide 6", Rect(0, 0, 10, 10))
no_collide_item7 = ("no collide 7", Rect(0, 0, 2, 2))
# Dict to check collisions with values.
rect_values = dict(
(
no_collide_item1,
no_collide_item2,
no_collide_item3,
no_collide_item4,
no_collide_item5,
no_collide_item6,
no_collide_item7,
)
)
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
expected_items = []
for use_values in (True, False):
d = rect_values if use_values else rect_keys
for zero_rect in (
zero_rect1,
zero_rect2,
zero_rect3,
zero_rect4,
zero_rect5,
):
collide_items = zero_rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__zero_sized_rects_as_args(self):
"""Ensures collidedictall works correctly with zero sized rects
as args.
There should be no collisions with zero sized rects.
"""
rect = Rect(0, 0, 20, 20)
no_collide_item1 = ("no collide 1", Rect(2, 2, 0, 0))
no_collide_item2 = ("no collide 2", Rect(2, 2, 2, 0))
no_collide_item3 = ("no collide 3", Rect(2, 2, 0, 2))
# Dict to check collisions with values.
rect_values = dict((no_collide_item1, no_collide_item2, no_collide_item3))
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
expected_items = []
for use_values in (True, False):
d = rect_values if use_values else rect_keys
collide_items = rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__negative_sized_rects(self):
"""Ensures collidedictall works correctly with negative sized rects."""
neg_rect = Rect(2, 2, -2, -2)
collide_item1 = ("collide 1", neg_rect.copy())
collide_item2 = ("collide 2", Rect(0, 0, 20, 20))
no_collide_item1 = ("no collide 1", Rect(2, 2, 20, 20))
# Dict to check collisions with values.
rect_values = dict((collide_item1, collide_item2, no_collide_item1))
value_collide_items = [collide_item1, collide_item2]
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
key_collide_items = [(tuple(v), k) for k, v in value_collide_items]
for use_values in (True, False):
if use_values:
expected_items = value_collide_items
d = rect_values
else:
expected_items = key_collide_items
d = rect_keys
collide_items = neg_rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__negative_sized_rects_as_args(self):
"""Ensures collidedictall works correctly with negative sized rect
args.
"""
rect = Rect(0, 0, 10, 10)
collide_item1 = ("collide 1", Rect(1, 1, -1, -1))
no_collide_item1 = ("no collide 1", Rect(1, 1, -1, 0))
no_collide_item2 = ("no collide 2", Rect(1, 1, 0, -1))
# Dict to check collisions with values.
rect_values = dict((collide_item1, no_collide_item1, no_collide_item2))
value_collide_items = [collide_item1]
# Dict to check collisions with keys.
rect_keys = {tuple(v): k for k, v in rect_values.items()}
key_collide_items = [(tuple(v), k) for k, v in value_collide_items]
for use_values in (True, False):
if use_values:
expected_items = value_collide_items
d = rect_values
else:
expected_items = key_collide_items
d = rect_keys
collide_items = rect.collidedictall(d, use_values)
self._assertCountEqual(collide_items, expected_items)
def test_collidedictall__invalid_dict_format(self):
"""Ensures collidedictall correctly handles invalid dict parameters."""
rect = Rect(0, 0, 10, 10)
invalid_value_dict = ("collide", rect.copy())
invalid_key_dict = tuple(invalid_value_dict[1]), invalid_value_dict[0]
for use_values in (True, False):
d = invalid_value_dict if use_values else invalid_key_dict
with self.assertRaises(TypeError):
collide_item = rect.collidedictall(d, use_values)
def test_collidedictall__invalid_dict_value_format(self):
"""Ensures collidedictall correctly handles dicts with invalid values."""
rect = Rect(0, 0, 10, 10)
rect_keys = {tuple(rect): "collide"}
with self.assertRaises(TypeError):
collide_items = rect.collidedictall(rect_keys, 1)
def test_collidedictall__invalid_dict_key_format(self):
"""Ensures collidedictall correctly handles dicts with invalid keys."""
rect = Rect(0, 0, 10, 10)
rect_values = {"collide": rect.copy()}
with self.assertRaises(TypeError):
collide_items = rect.collidedictall(rect_values)
def test_collidedictall__invalid_use_values_format(self):
"""Ensures collidedictall correctly handles invalid use_values
parameters.
"""
rect = Rect(0, 0, 1, 1)
d = {}
for invalid_param in (None, d, 1.1):
with self.assertRaises(TypeError):
collide_items = rect.collidedictall(d, invalid_param)
def test_collidelist(self):
# __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelist:
# Rect.collidelist(list): return index
# test if one rectangle in a list intersects
#
# Test whether the rectangle collides with any in a sequence of
# rectangles. The index of the first collision found is returned. If
# no collisions are found an index of -1 is returned.
r = Rect(1, 1, 10, 10)
l = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)]
self.assertEqual(r.collidelist(l), 1)
f = [Rect(50, 50, 1, 1), (100, 100, 4, 4)]
self.assertEqual(r.collidelist(f), -1)
def test_collidelistall(self):
# __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelistall:
# Rect.collidelistall(list): return indices
# test if all rectangles in a list intersect
#
# Returns a list of all the indices that contain rectangles that
# collide with the Rect. If no intersecting rectangles are found, an
# empty list is returned.
r = Rect(1, 1, 10, 10)
l = [
Rect(1, 1, 10, 10),
Rect(5, 5, 10, 10),
Rect(15, 15, 1, 1),
Rect(2, 2, 1, 1),
]
self.assertEqual(r.collidelistall(l), [0, 1, 3])
f = [Rect(50, 50, 1, 1), Rect(20, 20, 5, 5)]
self.assertFalse(r.collidelistall(f))
def test_fit(self):
# __doc__ (as of 2008-08-02) for pygame.rect.Rect.fit:
# Rect.fit(Rect): return Rect
# resize and move a rectangle with aspect ratio
#
# Returns a new rectangle that is moved and resized to fit another.
# The aspect ratio of the original Rect is preserved, so the new
# rectangle may be smaller than the target in either width or height.
r = Rect(10, 10, 30, 30)
r2 = Rect(30, 30, 15, 10)
f = r.fit(r2)
self.assertTrue(r2.contains(f))
f2 = r2.fit(r)
self.assertTrue(r.contains(f2))
def test_copy(self):
r = Rect(1, 2, 10, 20)
c = r.copy()
self.assertEqual(c, r)
def test_subscript(self):
r = Rect(1, 2, 3, 4)
self.assertEqual(r[0], 1)
self.assertEqual(r[1], 2)
self.assertEqual(r[2], 3)
self.assertEqual(r[3], 4)
self.assertEqual(r[-1], 4)
self.assertEqual(r[-2], 3)
self.assertEqual(r[-4], 1)
self.assertRaises(IndexError, r.__getitem__, 5)
self.assertRaises(IndexError, r.__getitem__, -5)
self.assertEqual(r[0:2], [1, 2])
self.assertEqual(r[0:4], [1, 2, 3, 4])
self.assertEqual(r[0:-1], [1, 2, 3])
self.assertEqual(r[:], [1, 2, 3, 4])
self.assertEqual(r[...], [1, 2, 3, 4])
self.assertEqual(r[0:4:2], [1, 3])
self.assertEqual(r[0:4:3], [1, 4])
self.assertEqual(r[3::-1], [4, 3, 2, 1])
self.assertRaises(TypeError, r.__getitem__, None)
def test_ass_subscript(self):
r = Rect(0, 0, 0, 0)
r[...] = 1, 2, 3, 4
self.assertEqual(r, [1, 2, 3, 4])
self.assertRaises(TypeError, r.__setitem__, None, 0)
self.assertEqual(r, [1, 2, 3, 4])
self.assertRaises(TypeError, r.__setitem__, 0, "")
self.assertEqual(r, [1, 2, 3, 4])
self.assertRaises(IndexError, r.__setitem__, 4, 0)
self.assertEqual(r, [1, 2, 3, 4])
self.assertRaises(IndexError, r.__setitem__, -5, 0)
self.assertEqual(r, [1, 2, 3, 4])
r[0] = 10
self.assertEqual(r, [10, 2, 3, 4])
r[3] = 40
self.assertEqual(r, [10, 2, 3, 40])
r[-1] = 400
self.assertEqual(r, [10, 2, 3, 400])
r[-4] = 100
self.assertEqual(r, [100, 2, 3, 400])
r[1:3] = 0
self.assertEqual(r, [100, 0, 0, 400])
r[...] = 0
self.assertEqual(r, [0, 0, 0, 0])
r[:] = 9
self.assertEqual(r, [9, 9, 9, 9])
r[:] = 11, 12, 13, 14
self.assertEqual(r, [11, 12, 13, 14])
r[::-1] = r
self.assertEqual(r, [14, 13, 12, 11])
@unittest.skipIf(IS_PYPY, "fails on pypy")
class SubclassTest(unittest.TestCase):
class MyRect(Rect):
def __init__(self, *args, **kwds):
super(SubclassTest.MyRect, self).__init__(*args, **kwds)
self.an_attribute = True
def test_copy(self):
mr1 = self.MyRect(1, 2, 10, 20)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.copy()
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_move(self):
mr1 = self.MyRect(1, 2, 10, 20)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.move(1, 2)
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_inflate(self):
mr1 = self.MyRect(1, 2, 10, 20)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.inflate(2, 4)
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_clamp(self):
mr1 = self.MyRect(19, 12, 5, 5)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.clamp(Rect(10, 10, 10, 10))
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_clip(self):
mr1 = self.MyRect(1, 2, 3, 4)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.clip(Rect(0, 0, 3, 4))
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_union(self):
mr1 = self.MyRect(1, 1, 1, 2)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.union(Rect(-2, -2, 1, 2))
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_unionall(self):
mr1 = self.MyRect(0, 0, 1, 1)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.unionall([Rect(-2, -2, 1, 1), Rect(2, 2, 1, 1)])
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
def test_fit(self):
mr1 = self.MyRect(10, 10, 30, 30)
self.assertTrue(mr1.an_attribute)
mr2 = mr1.fit(Rect(30, 30, 15, 10))
self.assertTrue(isinstance(mr2, self.MyRect))
self.assertRaises(AttributeError, getattr, mr2, "an_attribute")
if __name__ == "__main__":
unittest.main()
|