Skip to content

Commit cc92b1f

Browse files
committed
Fix failing build
This isn't all tests passing but fixes the compile errors and adds some tests. Should (hopefully) mean the documentation actually compiles * pygorithm/geometry/rect2.py - def -> class (brainfart), spacing * tests/test_geometry.py - def -> class (brainfart), start implementing rect2 tests
1 parent 1bb2269 commit cc92b1f

File tree

2 files changed

+162
-10
lines changed

2 files changed

+162
-10
lines changed

pygorithm/geometry/rect2.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pygorithm.geometry import (vector2, line2, axisall, polygon2)
1616

17-
def Rect2(object):
17+
class Rect2(object):
1818
"""
1919
A rectangle. Uses SAT collision against polygons and
2020
broad-phase collision against other rectangles.
@@ -41,7 +41,7 @@ def Rect2(object):
4141
:vartype mincorner: :class:`pygorithm.geometry.vector2.Vector2`
4242
"""
4343

44-
def __init__(self, width, height, mincorner=None):
44+
def __init__(self, width, height, mincorner = None):
4545
"""
4646
Create a new rectangle of width and height.
4747
@@ -53,6 +53,8 @@ def __init__(self, width, height, mincorner=None):
5353
:type height: :class:`numbers.Number`
5454
:param mincorner: the position of this rect
5555
:type mincorner: :class:`pygorithm.geometry.vector2.Vector2` or None
56+
57+
:raises ValueError: if width or height are not strictly positive
5658
"""
5759
pass
5860

@@ -85,10 +87,12 @@ def width(self):
8587
8688
:returns: width of this rect
8789
:rtype: :class:`numbers.Number`
90+
91+
:raises ValueError: if trying to set width <= 0
8892
"""
8993
pass
9094

91-
@property.setter
95+
@width.setter
9296
def width(self, value):
9397
pass
9498

@@ -104,10 +108,12 @@ def height(self):
104108
105109
:returns: height of this rect
106110
:rtype: :class:`numbers.Number`
111+
112+
:raises ValueError: if trying to set height <= 0
107113
"""
108114
pass
109115

110-
@property.setter
116+
@height.setter
111117
def height(self, value):
112118
pass
113119

tests/test_geometry.py

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -862,15 +862,161 @@ def test_find_intersection_overlapping(self):
862862
self._find_intersection_fuzzer(poly1, poly2, False, True, (0.5, vector2.Vector2(0, -1)))
863863
self._find_intersection_fuzzer(poly1, poly3, False, True, (0.70710678118, vector2.Vector2(0.70710678118, -0.70710678118)))
864864

865-
def TestRect2(unittest.TestCase):
866-
def test_constructor(self):
867-
pass
865+
class TestRect2(unittest.TestCase):
866+
def test_constructor_defaults(self):
867+
_rect = rect2.Rect2(1, 1)
868+
869+
self.assertIsNotNone(_rect)
870+
self.assertIsEqual(1, _rect.width)
871+
self.assertIsEqual(1, _rect.height)
872+
self.assertIsNotNone(_rect.mincorner)
873+
self.assertIsEqual(0, _rect.mincorner.x)
874+
self.assertIsEqual(0, _rect.mincorner.y)
875+
876+
def test_constructor_specified(self):
877+
_rect = rect2.Rect2(1, 3, vector2.Vector2(-1, -1))
868878

869-
def test_polygon(self):
870-
pass
879+
self.assertEqual(1, _rect.width)
880+
self.assertEqual(3, _rect.height)
881+
self.assertIsNotNone(_rect.mincorner)
882+
self.assertEqual(-1, _rect.mincorner.x)
883+
self.assertEqual(-1, _rect.mincorner.y)
884+
885+
def test_constructor_errors(self):
886+
with self.assertRaises(ValueError):
887+
_rect = rect2.Rect2(-1, 1)
888+
889+
with self.assertRaises(ValueError):
890+
_rect = rect2.Rect2(1, -1)
891+
892+
with self.assertRaises(ValueError):
893+
_rect = rect2.Rect2(0, 1)
894+
895+
with self.assertRaises(ValueError):
896+
_rect = rect2.Rect2(5, 0)
897+
898+
with self.assertRaises(ValueError):
899+
_rect = rect2.Rect2(0, 0)
900+
901+
with self.assertRaises(ValueError):
902+
_rect = rect2.Rect2(-3, -3)
903+
904+
def test_width(self):
905+
_rect = rect2.Rect2(1, 1)
906+
907+
self.assertEqual(1, _rect.width)
908+
909+
_rect.width = 3
910+
911+
self.assertEqual(3, _rect.width)
912+
913+
with self.assertRaises(ValueError):
914+
_rect.width = 0
915+
916+
_rect = rect2.Rect2(1, 1)
917+
with self.assertRaises(ValueError):
918+
_rect.width = -3
871919

920+
def test_height(self):
921+
_rect = rect2.Rect2(7, 11)
922+
923+
self.assertEqual(11, _rect.height)
924+
925+
_rect.height = 5
926+
927+
self.assertEqual(5, _rect.height)
928+
929+
with self.assertRaises(ValueError):
930+
_rect.height = 0
931+
932+
_rect = rect2.Rect2(1, 1)
933+
with self.assertRaises(ValueError):
934+
_rect.height = -15
935+
936+
_rect = rect2.Rect2(1, 1)
937+
with self.assertRaises(ValueError):
938+
_rect.height = 1e-09
939+
940+
def test_polygon_unshifted(self):
941+
_rect = rect2.Rect2(1, 1)
942+
943+
self.assertIsNotNone(_rect.polygon)
944+
self.assertEqual(0, _rect.polygon.points[0].x)
945+
self.assertEqual(0, _rect.polygon.points[0].y)
946+
self.assertEqual(0, _rect.polygon.points[1].x)
947+
self.assertEqual(1, _rect.polygon.points[1].y)
948+
self.assertEqual(1, _rect.polygon.points[2].x)
949+
self.assertEqual(1, _rect.polygon.points[2].y)
950+
self.assertEqual(1, _rect.polygon.points[3].x)
951+
self.assertEqual(0, _rect.polygon.points[3].y)
952+
self.assertEqual(4, len(_rect.polygon.points))
953+
954+
def test_polygon_shifted(self):
955+
_rect = rect2.Rect2(1, 1, vector2.Vector2(1, 1))
956+
957+
self.assertIsNotNone(_rect.polygon)
958+
self.assertEqual(0, _rect.polygon.points[0].x)
959+
self.assertEqual(0, _rect.polygon.points[0].y)
960+
self.assertEqual(0, _rect.polygon.points[1].x)
961+
self.assertEqual(1, _rect.polygon.points[1].y)
962+
self.assertEqual(1, _rect.polygon.points[2].x)
963+
self.assertEqual(1, _rect.polygon.points[2].y)
964+
self.assertEqual(1, _rect.polygon.points[3].x)
965+
self.assertEqual(0, _rect.polygon.points[3].y)
966+
self.assertEqual(4, len(_rect.polygon.points))
967+
968+
def test_polygon_resized(self):
969+
_rect = rect2.Rect2(1, 1)
970+
971+
self.assertIsNotNone(_rect.polygon)
972+
self.assertEqual(0, _rect.polygon.points[0].x)
973+
self.assertEqual(0, _rect.polygon.points[0].y)
974+
self.assertEqual(0, _rect.polygon.points[1].x)
975+
self.assertEqual(1, _rect.polygon.points[1].y)
976+
self.assertEqual(1, _rect.polygon.points[2].x)
977+
self.assertEqual(1, _rect.polygon.points[2].y)
978+
self.assertEqual(1, _rect.polygon.points[3].x)
979+
self.assertEqual(0, _rect.polygon.points[3].y)
980+
self.assertEqual(4, len(_rect.polygon.points))
981+
982+
_rect.width = 3
983+
984+
self.assertIsNotNone(_rect.polygon)
985+
self.assertEqual(0, _rect.polygon.points[0].x)
986+
self.assertEqual(0, _rect.polygon.points[0].y)
987+
self.assertEqual(0, _rect.polygon.points[1].x)
988+
self.assertEqual(1, _rect.polygon.points[1].y)
989+
self.assertEqual(3, _rect.polygon.points[2].x)
990+
self.assertEqual(1, _rect.polygon.points[2].y)
991+
self.assertEqual(3, _rect.polygon.points[3].x)
992+
self.assertEqual(0, _rect.polygon.points[3].y)
993+
self.assertEqual(4, len(_rect.polygon.points))
994+
995+
_rect.height = 0.5
996+
997+
self.assertIsNotNone(_rect.polygon)
998+
self.assertEqual(0, _rect.polygon.points[0].x)
999+
self.assertEqual(0, _rect.polygon.points[0].y)
1000+
self.assertEqual(0, _rect.polygon.points[1].x)
1001+
self.assertEqual(0.5, _rect.polygon.points[1].y)
1002+
self.assertEqual(3, _rect.polygon.points[2].x)
1003+
self.assertEqual(0.5, _rect.polygon.points[2].y)
1004+
self.assertEqual(3, _rect.polygon.points[3].x)
1005+
self.assertEqual(0, _rect.polygon.points[3].y)
1006+
self.assertEqual(4, len(_rect.polygon.points))
1007+
8721008
def test_area(self):
873-
pass
1009+
_rect = rect2.Rect2(1, 1)
1010+
1011+
self.assertEqual(1, _rect.area)
1012+
1013+
_rect.width = 3
1014+
1015+
self.assertEqual(3, _rect.area)
1016+
1017+
_rect.height = 7
1018+
1019+
self.assertEqual(21, _rect.area)
8741020

8751021
def test_project_onto_axis(self):
8761022
pass

0 commit comments

Comments
 (0)