Skip to content

Commit f262ef1

Browse files
committed
Add updated unit tests.
1 parent 02560ad commit f262ef1

8 files changed

+182
-119
lines changed

formation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Formation(object):
77
"""Formation that handles all units it manages. Used to handle unit
88
placement and waypoint updating.
99
"""
10-
def __init__(self, filename=None):
10+
def __init__(self, file=None):
1111
"""Create a formation. If a filename is specified, the formation
1212
is loaded from there, otherwise, the file has to be specified later.
1313
"""
@@ -16,8 +16,8 @@ def __init__(self, filename=None):
1616
self._direction = Vec2d(0, 0)
1717
self.positions = []
1818
self.waypoints = []
19-
if filename:
20-
self.parse_formation_file(filename)
19+
if file:
20+
self.parse_formation_file(file)
2121

2222
def parse_formation_file(self, open_file):
2323
"""Open and parse the given formation file. Set up all the required
@@ -44,9 +44,9 @@ def set_center(self, center_pair_or_x, y=None):
4444
specifying the x and y coordinates as separate arguments.
4545
"""
4646
if y is None:
47-
self.waypoint = center_pair_or_x
47+
self._center = center_pair_or_x
4848
else:
49-
self.waypoint = Vec2d(center_pair_or_x, y)
49+
self._center = Vec2d(center_pair_or_x, y)
5050

5151
def gen_and_get_boids(self):
5252
"""Generate all the units in the formation based on their relative

test_unit_tests.py

Lines changed: 0 additions & 114 deletions
This file was deleted.

tests/test_enemy.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from enemy import Enemy
3+
from vec2d import Vec2d
4+
5+
6+
class TestEnemy(unittest.TestCase):
7+
def test___init__(self):
8+
enemy = Enemy(Vec2d(0, 0), Vec2d(200, 200))
9+
self.assertIsNotNone(enemy)
10+
11+
def test_update_and_apply_velocity(self):
12+
enemy = Enemy(Vec2d(0, 0), Vec2d(200, 200))
13+
enemy_pos = Vec2d(enemy.position.x, enemy.position.y)
14+
enemy.update()
15+
self.assertNotEquals(enemy_pos, enemy.position)
16+
17+
if __name__ == '__main__':
18+
unittest.main()

tests/test_formation.fm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(291, 214)
2+
(-1, 91)
3+
(-33, 1)
4+
(21, -2)
5+
(-3, 40)
6+
(-49, 79)
7+
(46, 81)
8+
(4, 124)

tests/test_formation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import unittest
2+
from vec2d import Vec2d
3+
from formation import Formation
4+
5+
6+
class TestFormation(unittest.TestCase):
7+
def test___init__(self):
8+
formation = Formation(open('test_formation.fm'))
9+
self.assertIsNotNone(formation)
10+
11+
def test_center(self):
12+
formation = Formation(open('test_formation.fm'))
13+
self.assertEqual(Vec2d(291, 214), formation.center)
14+
15+
def test_direction(self):
16+
formation = Formation(open('test_formation.fm'))
17+
self.assertEqual(Vec2d(290, 305), formation.direction)
18+
19+
def test_facing(self):
20+
formation = Formation(open('test_formation.fm'))
21+
self.assertEqual(Vec2d(-1, 91), formation.facing)
22+
23+
def test_gen_and_get_boids(self):
24+
formation = Formation(open('test_formation.fm'))
25+
self.assertEqual(6, len(formation.gen_and_get_boids()))
26+
27+
def test_set_center(self):
28+
formation = Formation(open('test_formation.fm'))
29+
old_center = formation.center
30+
formation.set_center(300, 300)
31+
self.assertEqual(Vec2d(300, 300), formation.center)
32+
self.assertNotEqual(old_center, formation.center)
33+
34+
def test_set_waypoint(self):
35+
formation = Formation(open('test_formation.fm'))
36+
formation.set_waypoint(500, 500)
37+
self.assertEqual(Vec2d(500, 500), formation.waypoint)
38+
39+
if __name__ == '__main__':
40+
unittest.main()

tests/test_formation_designer.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from formation_designer import add_tuple
2+
import unittest
3+
from formation_designer import distance_to
4+
from formation_designer import close_to
5+
from formation_designer import get_closest
6+
from formation_designer import get_closest_and_dist
7+
8+
9+
class TestAddTuple(unittest.TestCase):
10+
def test_add_tuple_returns_tuple_for_t1_equal_tuple_and_t2_equal_tuple(self):
11+
self.assertEqual((3, 5), add_tuple((1, 2), (2, 3)))
12+
13+
14+
class TestDistanceTo(unittest.TestCase):
15+
def test_distance_to_returns_111803398875_for_point1_equal_tuple_and_point2_equal_tuple(self):
16+
self.assertEqual(111.80339887498948, distance_to((100, 50), (0, 0)))
17+
18+
def test_distance_to_returns_111803398875_for_point1_equal_tuple_and_point2_equal_tuple_case_2(self):
19+
self.assertEqual(111.80339887498948, distance_to((200, 50), (100, 100)))
20+
21+
def test_distance_to_returns_1500_for_point1_equal_tuple_and_point2_equal_tuple(self):
22+
self.assertEqual(150.0, distance_to((200, 50), (50, 50)))
23+
24+
def test_distance_to_returns_206155281281_for_point1_equal_tuple_and_point2_equal_tuple(self):
25+
self.assertEqual(206.15528128088303, distance_to((200, 50), (0, 0)))
26+
27+
def test_distance_to_returns_20_for_point1_equal_tuple_and_point2_equal_tuple(self):
28+
self.assertEqual(2.0, distance_to((0, 0), (2, 0)))
29+
30+
def test_distance_to_returns_269258240357_for_point1_equal_tuple_and_point2_equal_tuple(self):
31+
self.assertEqual(269.2582403567252, distance_to((200, 50), (300, 300)))
32+
33+
def test_distance_to_returns_320156211872_for_point1_equal_tuple_and_point2_equal_tuple(self):
34+
self.assertEqual(320.1562118716424, distance_to((100, 50), (300, 300)))
35+
36+
def test_distance_to_returns_500_for_point1_equal_tuple_and_point2_equal_tuple(self):
37+
self.assertEqual(50.0, distance_to((100, 50), (100, 100)))
38+
39+
def test_distance_to_returns_500_for_point1_equal_tuple_and_point2_equal_tuple_case_2(self):
40+
self.assertEqual(50.0, distance_to((100, 50), (50, 50)))
41+
42+
43+
class TestCloseTo(unittest.TestCase):
44+
def test_close_to_returns_list_for_placed_units_equal_list_and_point_equal_tuple(self):
45+
self.assertEqual([], close_to((200, 50), [(0, 0), (100, 100), (50, 50), (300, 300)]))
46+
47+
48+
class TestGetClosest(unittest.TestCase):
49+
def test_get_closest_returns_tuple_for_close_units_equal_list_and_point_equal_tuple(self):
50+
obj = (100, 100)
51+
self.assertEqual(obj, get_closest((100, 50), [(0, 0), obj, (50, 50), (300, 300)]))
52+
53+
54+
class TestGetClosestAndDist(unittest.TestCase):
55+
def test_get_closest_and_dist_returns_tuple_for_close_units_equal_list_and_point_equal_tuple(self):
56+
obj = (100, 100)
57+
self.assertEqual((obj, 50.0), get_closest_and_dist((100, 50), [(0, 0), obj, (50, 50), (300, 300)]))
58+
59+
if __name__ == '__main__':
60+
unittest.main()

tests/test_unit.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from unit import Unit
3+
from vec2d import Vec2d
4+
5+
6+
class TestUnit(unittest.TestCase):
7+
def test___init__(self):
8+
unit = Unit(Vec2d(0, 0), Vec2d(0, 0), None)
9+
self.assertIsNotNone(unit)
10+
11+
def test_contains(self):
12+
unit = Unit(Vec2d(0, 0), Vec2d(0, 0), None)
13+
self.assertTrue(unit.contains(Vec2d(5, 0)))
14+
15+
def test_get_center(self):
16+
unit = Unit(Vec2d(0, 0), Vec2d(0, 0), None)
17+
self.assertListEqual([0, 0], unit.get_center())
18+
19+
def test_get_inverse_square(self):
20+
unit = Unit(Vec2d(0, 0), Vec2d(0, 0), None)
21+
other_unit = Unit(Vec2d(10, 0), Vec2d(10, 0), None)
22+
self.assertEqual(1.0 / 100, unit.get_inverse_square(other_unit))
23+
24+
if __name__ == '__main__':
25+
unittest.main()

tests/test_waypoint.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from waypoint import Waypoint
2+
from vec2d import Vec2d
3+
import unittest
4+
5+
6+
class TestWaypoint(unittest.TestCase):
7+
def test_create_waypoint(self):
8+
waypoint = Waypoint(Vec2d(0, 0), Vec2d(100, 100))
9+
self.assertIsNotNone(waypoint)
10+
11+
def test_get_position(self):
12+
waypoint = Waypoint(Vec2d(0, 0), Vec2d(100, 100))
13+
self.assertEqual(Vec2d(100, 100), waypoint.get_position())
14+
15+
def test_update_position(self):
16+
waypoint = Waypoint(Vec2d(0, 0), Vec2d(100, 100))
17+
waypoint.update_position(Vec2d(50, 50))
18+
self.assertEqual(Vec2d(50, 50), waypoint.get_position())
19+
20+
def test_rotate(self):
21+
waypoint = Waypoint(Vec2d(10, 0), Vec2d(100, 0))
22+
waypoint.rotate(90)
23+
self.assertEqual(Vec2d(100, 10), waypoint.get_position())
24+
25+
if __name__ == '__main__':
26+
unittest.main()

0 commit comments

Comments
 (0)