Skip to content

Commit

Permalink
improve docs of vec2d by moving most tests into doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
viblo committed Oct 22, 2024
1 parent e4a315f commit 4d5f4a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 107 deletions.
53 changes: 51 additions & 2 deletions pymunk/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,65 @@ def testNoStaticShape(self) -> None:
b3.position = -9, 0

space.add(b1, c1, b2, c2, b3, c3)
print("\nc1", c1)

def remove_first(arbiter: p.Arbiter, space: p.Space, data: Any) -> None:
print("SEP 1", arbiter.shapes)
print(" space.shapes", space.shapes)
print(" space._remove_later", space._remove_later)
first_shape = arbiter.shapes[0]
if c1 in space.shapes:
space.remove(c1)
print(" space.shapes", space.shapes)
print(" space._remove_later", space._remove_later)
print("SEP done")
# space.add_post_step_callback(space.remove, first_shape, first_shape.body)
space.remove(c1)
# space.remove(c1)

space.add_collision_handler(2, 0).separate = remove_first

print(1)
space.step(1.0 / 60)
print(2)
b2.position = 22, 0
space.step(1.0 / 60)
print(3)

def testX(self) -> None:
space = p.Space()

b1 = p.Body()
c1 = p.Circle(b1, 10)
c1.collision_type = 2

b2 = p.Body()
c2 = p.Circle(b2, 10)

b3 = p.Body()
c3 = p.Circle(b3, 10)

# b1.position = 0, 0
# b2.position = 9, 0
# b3.position = -9, 0

space.add(b1, c1, b2, c2, b3, c3)
print("\nc1", c1)

def separate(arbiter: p.Arbiter, space: p.Space, data: Any) -> None:
print("SEP 1", arbiter.shapes)
print(" space.shapes", space.shapes)
print(" space._remove_later", space._remove_later)
if c1 in space.shapes:
space.remove(c1)
print(" space.shapes", space.shapes)
print(" space._remove_later", space._remove_later)
print("SEP done")
# space.add_post_step_callback(space.remove, first_shape, first_shape.body)
# space.remove(c1)

space.add_collision_handler(2, 0).separate = separate
print(1)
space.step(1)
print(2)
b2.position = 22, 0
space.step(1)
print(3)
105 changes: 0 additions & 105 deletions pymunk/tests/test_vec2d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import math
import pickle
import unittest

import pymunk
Expand All @@ -8,103 +6,16 @@

class UnitTestVec2d(unittest.TestCase):
def testCreationAndAccess(self) -> None:
v = Vec2d(*(0, 0))
self.assertEqual(v.x, 0)
self.assertEqual(v[0], 0)
self.assertEqual(v.y, 0)
self.assertEqual(v[1], 0)

v = Vec2d(3, 5)
self.assertEqual(v.x, 3)
self.assertEqual(v[0], 3)
self.assertEqual(v.y, 5)
self.assertEqual(v[1], 5)

v = Vec2d(111, 222)
self.assertTrue(v.x == 111 and v.y == 222)
with self.assertRaises(AttributeError):
v.x = 333 # type: ignore
with self.assertRaises(TypeError):
v[1] = 444 # type: ignore

v = Vec2d(3, 5)
self.assertEqual(len(v), 2)
self.assertEqual(list(v), [3, 5])
self.assertEqual(tuple(v), (3, 5))

v = Vec2d.from_polar(3, 2)
self.assertEqual(v.length, 3)
self.assertEqual(v.angle, 2)

def testMath(self) -> None:
v = Vec2d(111, 222)
self.assertEqual(v + Vec2d(1, 2), Vec2d(112, 224))
self.assertEqual(v + (1, 2), Vec2d(112, 224))
self.assertEqual((1, 2) + v, Vec2d(112, 224))

self.assertEqual(v - Vec2d(1, 2), Vec2d(110, 220))
self.assertEqual(v - (1, 2), Vec2d(110, 220))
self.assertEqual((1, 2) - v, Vec2d(-110, -220))

self.assertEqual(v * 3, Vec2d(333, 666))
self.assertEqual(3 * v, Vec2d(333, 666))

self.assertEqual(v / 2, Vec2d(55.5, 111))
self.assertEqual(v // 2, Vec2d(55, 111))

def testUnary(self) -> None:
v = Vec2d(111, 222)
self.assertEqual(+v, v)
self.assertEqual(-v, Vec2d(-111, -222))
self.assertAlmostEqual(abs(v), 248.20354550247666)

def testLength(self) -> None:
v = Vec2d(3, 4)
self.assertTrue(v.length == 5)
self.assertTrue(v.get_length_sqrd() == 25)
with self.assertRaises(AttributeError):
v.length = 5 # type: ignore

def testAnglesDegrees(self) -> None:
v = Vec2d(0, 3)
self.assertEqual(v.angle_degrees, 90)
v2 = Vec2d(*v)
v = v.rotated_degrees(-90)
self.assertEqual(v.get_angle_degrees_between(v2), 90)
v2 = v2.rotated_degrees(-90)
self.assertEqual(v.length, v2.length)
self.assertAlmostEqual(v2.angle_degrees, 0, 10)
self.assertAlmostEqual(v2.x, 3)
self.assertAlmostEqual(v2.y, 0)
self.assertTrue((v - v2).length < 0.00001)
self.assertEqual(v.length, v2.length)
v2 = v2.rotated_degrees(300)
self.assertAlmostEqual(
v.get_angle_degrees_between(v2), -60
) # Allow a little more error than usual (floats..)
v2 = v2.rotated_degrees(v2.get_angle_degrees_between(v))
self.assertAlmostEqual(v.get_angle_degrees_between(v2), 0)

def testAnglesRadians(self) -> None:
v = Vec2d(0, 3)
self.assertEqual(v.angle, math.pi / 2.0)
v2 = Vec2d(*v)
v = v.rotated(-math.pi / 2.0)
self.assertEqual(v.get_angle_between(v2), math.pi / 2.0)
v2 = v2.rotated(-math.pi / 2.0)
self.assertEqual(v.length, v2.length)
self.assertAlmostEqual(v2.angle, 0)
self.assertEqual(v2.x, 3)
self.assertAlmostEqual(v2.y, 0)
self.assertTrue((v - v2).length < 0.00001)
self.assertEqual(v.length, v2.length)
v2 = v2.rotated(math.pi / 3.0 * 5.0)
self.assertAlmostEqual(
v.get_angle_between(v2), -math.pi / 3.0
) # Allow a little more error than usual (floats..)
v2 = v2.rotated(v2.get_angle_between(v))
self.assertAlmostEqual(v.get_angle_between(v2), 0)

def testComparison(self) -> None:
int_vec = Vec2d(3, -2)
flt_vec = Vec2d(3.0, -2.0)
Expand All @@ -117,19 +28,3 @@ def testComparison(self) -> None:
self.assertTrue(int_vec != (0, 0))
self.assertTrue(int_vec != 5) # type: ignore
self.assertTrue(int_vec != (3, -2, -5)) # type: ignore

def testImmuatable(self) -> None:
inplace_vec = Vec2d(5, 13)
inplace_ref = inplace_vec
inplace_vec *= 0.5
inplace_vec += Vec2d(0.5, 0.5)
inplace_vec -= Vec2d(3.5, 3.5)
inplace_vec /= 5
self.assertEqual(inplace_ref, Vec2d(5, 13))
self.assertEqual(inplace_vec, Vec2d(-0.1, 0.7))

def testPickle(self) -> None:
testvec = Vec2d(5, 0.3)
testvec_str = pickle.dumps(testvec)
loaded_vec = pickle.loads(testvec_str)
self.assertEqual(testvec, loaded_vec)

0 comments on commit 4d5f4a5

Please sign in to comment.