Skip to content

Commit 13c5887

Browse files
committed
add tests to prevent a regression
1 parent ec30d0b commit 13c5887

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

test/math_test.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import pygame.math
77
from pygame.math import Vector2, Vector3
88

9+
try:
10+
import numpy
11+
except ModuleNotFoundError:
12+
numpy = None
13+
914
IS_PYPY = "PyPy" == platform.python_implementation()
1015

1116

@@ -255,6 +260,48 @@ def testConstructionVector2(self):
255260
self.assertEqual(v.x, 1.2)
256261
self.assertEqual(v.y, 3.4)
257262

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+
258305
def testAttributeAccess(self):
259306
tmp = self.v1.x
260307
self.assertEqual(tmp, self.v1.x)
@@ -1431,6 +1478,48 @@ def testConstructionMissing(self):
14311478
self.assertRaises(ValueError, Vector3, 1, 2)
14321479
self.assertRaises(ValueError, Vector3, x=1, y=2)
14331480

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+
14341523
def testAttributeAccess(self):
14351524
tmp = self.v1.x
14361525
self.assertEqual(tmp, self.v1.x)

0 commit comments

Comments
 (0)