-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
151 lines (132 loc) · 4.61 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import mars_rover
import unittest
from typings import Coordinate, RoverPosition, RoverMission
from exceptions import BoundsError, CollisionError, CommandError
class TestMarsRover(unittest.TestCase):
def test_given_example(self):
ending_positions = mars_rover.execute_mission(
Coordinate(5, 5),
[
RoverMission(RoverPosition(1, 2, "N"), "LMLMLMLMM"),
RoverMission(RoverPosition(3, 3, "E"), "MMRMMRMRRM")
]
)
self.assertEqual(ending_positions, [
RoverPosition(1, 3, "N"),
RoverPosition(5, 1, "E")
])
def test_turns_rover_left(self):
ending_positions = mars_rover.execute_mission(
Coordinate(1, 1),
[
RoverMission(RoverPosition(0, 0, "N"), "L"),
RoverMission(RoverPosition(0, 1, "E"), "L"),
RoverMission(RoverPosition(1, 0, "S"), "L"),
RoverMission(RoverPosition(1, 1, "W"), "L")
]
)
self.assertEqual(ending_positions, [
RoverPosition(0, 0, "W"),
RoverPosition(0, 1, "N"),
RoverPosition(1, 0, "E"),
RoverPosition(1, 1, "S")
])
def test_turns_rover_right(self):
ending_positions = mars_rover.execute_mission(
Coordinate(1, 1),
[
RoverMission(RoverPosition(0, 0, "N"), "R"),
RoverMission(RoverPosition(0, 1, "E"), "R"),
RoverMission(RoverPosition(1, 0, "S"), "R"),
RoverMission(RoverPosition(1, 1, "W"), "R")
]
)
self.assertEqual(ending_positions, [
RoverPosition(0, 0, "E"),
RoverPosition(0, 1, "S"),
RoverPosition(1, 0, "W"),
RoverPosition(1, 1, "N")
])
def test_move_directions(self):
ending_positions = mars_rover.execute_mission(
Coordinate(2, 2),
[
RoverMission(RoverPosition(0, 1, "N"), "M"),
RoverMission(RoverPosition(0, 0, "E"), "M"),
RoverMission(RoverPosition(2, 1, "S"), "M"),
RoverMission(RoverPosition(1, 1, "W"), "M")
]
)
self.assertEqual(ending_positions, [
RoverPosition(0, 2, "N"),
RoverPosition(1, 0, "E"),
RoverPosition(2, 0, "S"),
RoverPosition(0, 1, "W")
])
def test_out_of_bounds_east_raises(self):
self.assertRaises(
BoundsError,
mars_rover.execute_mission,
Coordinate(1, 1),
[RoverMission(RoverPosition(1, 0, "E"), "M")]
)
def test_out_of_bounds_north_raises(self):
self.assertRaises(
BoundsError,
mars_rover.execute_mission,
Coordinate(1, 1),
[RoverMission(RoverPosition(0, 1, "N"), "M")]
)
def test_out_of_bounds_west_raises(self):
self.assertRaises(
BoundsError,
mars_rover.execute_mission,
Coordinate(1, 1),
[RoverMission(RoverPosition(0, 1, "W"), "M")]
)
def test_out_of_bounds_south_raises(self):
self.assertRaises(
BoundsError,
mars_rover.execute_mission,
Coordinate(1, 1),
[RoverMission(RoverPosition(1, 0, "S"), "M")]
)
def test_rovers_ending_in_same_position_raises(self):
self.assertRaises(
CollisionError,
mars_rover.execute_mission,
Coordinate(1, 1),
[
RoverMission(RoverPosition(0, 1, "S"), "M"),
RoverMission(RoverPosition(1, 0, "W"), "M")
]
)
def test_collision_with_finished_rover(self):
self.assertRaises(
CollisionError,
mars_rover.execute_mission,
Coordinate(1, 1),
[
RoverMission(RoverPosition(0, 1, "S"), "M"),
RoverMission(RoverPosition(1, 0, "W"), "MRM")
]
)
def test_collision_with_not_started_rover(self):
self.assertRaises(
CollisionError,
mars_rover.execute_mission,
Coordinate(1, 1),
[
RoverMission(RoverPosition(0, 1, "S"), "M"),
RoverMission(RoverPosition(0, 0, "N"), "M")
]
)
def test_invalid_command_raises(self):
self.assertRaises(
CommandError,
mars_rover.execute_mission,
Coordinate(1, 1),
[RoverMission(RoverPosition(0, 0, "N"), "I")],
)
if __name__ == '__main__':
unittest.main()