|
6 | 6 | import pygame.math
|
7 | 7 | from pygame.math import Vector2, Vector3
|
8 | 8 |
|
| 9 | +try: |
| 10 | + import numpy |
| 11 | +except ModuleNotFoundError: |
| 12 | + numpy = None |
| 13 | + |
9 | 14 | IS_PYPY = "PyPy" == platform.python_implementation()
|
10 | 15 |
|
11 | 16 |
|
@@ -255,6 +260,48 @@ def testConstructionVector2(self):
|
255 | 260 | self.assertEqual(v.x, 1.2)
|
256 | 261 | self.assertEqual(v.y, 3.4)
|
257 | 262 |
|
| 263 | + def testConstructionNumericSequence(self): |
| 264 | + class NumericSequence: |
| 265 | + # PyFloat_AsDouble will use this to convert to a float |
| 266 | + # so this is testing the implementation a bit |
| 267 | + def __float__(self): |
| 268 | + raise TypeError("Cannot convert to float") |
| 269 | + |
| 270 | + def __getitem__(self, index): |
| 271 | + return [1, 0][index] |
| 272 | + |
| 273 | + def __len__(self): |
| 274 | + return 2 |
| 275 | + |
| 276 | + v = Vector2(NumericSequence()) |
| 277 | + self.assertEqual(v.x, 1.0) |
| 278 | + self.assertEqual(v.y, 0.0) |
| 279 | + |
| 280 | + def testConstructionNumericNonFloat(self): |
| 281 | + class NumericNonFloat: |
| 282 | + # PyFloat_AsDouble will use this to convert to a float |
| 283 | + # so this is testing the implementation a bit |
| 284 | + def __float__(self): |
| 285 | + raise TypeError("Cannot convert to float") |
| 286 | + |
| 287 | + with self.assertRaises(TypeError): |
| 288 | + Vector2(NumericNonFloat()) |
| 289 | + |
| 290 | + with self.assertRaises(TypeError): |
| 291 | + Vector2(NumericNonFloat(), NumericNonFloat()) |
| 292 | + |
| 293 | + with self.assertRaises(TypeError): |
| 294 | + Vector2(1.0, NumericNonFloat()) |
| 295 | + |
| 296 | + @unittest.skipIf(numpy is None, "numpy not available") |
| 297 | + def testConstructionNumpyArray(self): |
| 298 | + assert numpy is not None |
| 299 | + |
| 300 | + arr = numpy.array([1.2, 3.4]) |
| 301 | + v = Vector2(arr) |
| 302 | + self.assertEqual(v.x, 1.2) |
| 303 | + self.assertEqual(v.y, 3.4) |
| 304 | + |
258 | 305 | def testAttributeAccess(self):
|
259 | 306 | tmp = self.v1.x
|
260 | 307 | self.assertEqual(tmp, self.v1.x)
|
@@ -1431,6 +1478,48 @@ def testConstructionMissing(self):
|
1431 | 1478 | self.assertRaises(ValueError, Vector3, 1, 2)
|
1432 | 1479 | self.assertRaises(ValueError, Vector3, x=1, y=2)
|
1433 | 1480 |
|
| 1481 | + def testConstructionNumericSequence(self): |
| 1482 | + class NumericSequence: |
| 1483 | + # PyFloat_AsDouble will use this to convert to a float |
| 1484 | + # so this is testing the implementation a bit |
| 1485 | + def __float__(self): |
| 1486 | + raise TypeError("Cannot convert to float") |
| 1487 | + |
| 1488 | + def __getitem__(self, index): |
| 1489 | + return [1, 0][index] |
| 1490 | + |
| 1491 | + def __len__(self): |
| 1492 | + return 2 |
| 1493 | + |
| 1494 | + v = Vector2(NumericSequence()) |
| 1495 | + self.assertEqual(v.x, 1.0) |
| 1496 | + self.assertEqual(v.y, 0.0) |
| 1497 | + |
| 1498 | + def testConstructionNumericNonFloat(self): |
| 1499 | + class NumericNonFloat: |
| 1500 | + # PyFloat_AsDouble will use this to convert to a float |
| 1501 | + # so this is testing the implementation a bit |
| 1502 | + def __float__(self): |
| 1503 | + raise TypeError("Cannot convert to float") |
| 1504 | + |
| 1505 | + with self.assertRaises(TypeError): |
| 1506 | + Vector2(NumericNonFloat()) |
| 1507 | + |
| 1508 | + with self.assertRaises(TypeError): |
| 1509 | + Vector2(NumericNonFloat(), NumericNonFloat()) |
| 1510 | + |
| 1511 | + with self.assertRaises(TypeError): |
| 1512 | + Vector2(1.0, NumericNonFloat()) |
| 1513 | + |
| 1514 | + @unittest.skipIf(numpy is None, "numpy not available") |
| 1515 | + def testConstructionNumpyArray(self): |
| 1516 | + assert numpy is not None |
| 1517 | + |
| 1518 | + arr = numpy.array([1.2, 3.4]) |
| 1519 | + v = Vector2(arr) |
| 1520 | + self.assertEqual(v.x, 1.2) |
| 1521 | + self.assertEqual(v.y, 3.4) |
| 1522 | + |
1434 | 1523 | def testAttributeAccess(self):
|
1435 | 1524 | tmp = self.v1.x
|
1436 | 1525 | self.assertEqual(tmp, self.v1.x)
|
|
0 commit comments