-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_simulator_test.rb
163 lines (133 loc) · 3.66 KB
/
robot_simulator_test.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
require 'minitest/autorun'
require_relative 'simulator'
class RobotTurningTest < MiniTest::Unit::TestCase
attr_reader :robot
def setup
@robot = Robot.new
end
def test_robot_bearing
[:east, :west, :north, :south].each do |direction|
robot.orient(direction)
assert_equal direction, robot.bearing
end
end
def test_invalid_robot_bearing # 15
assert_raises ArgumentError do
robot.orient(:crood)
end
end
def test_turn_right_from_north
robot.orient(:north)
robot.turn_right
assert_equal :east, robot.bearing
end
def test_turn_right_from_east
robot.orient(:east)
robot.turn_right
assert_equal :south, robot.bearing
end
def test_turn_right_from_south
robot.orient(:south)
robot.turn_right
assert_equal :west, robot.bearing
end
def test_turn_right_from_west
robot.orient(:west)
robot.turn_right
assert_equal :north, robot.bearing
end
def test_turn_left_from_north # 10
robot.orient(:north)
robot.turn_left
assert_equal :west, robot.bearing
end
def test_turn_left_from_east
robot.orient(:east)
robot.turn_left
assert_equal :north, robot.bearing
end
def test_turn_left_from_south
robot.orient(:south)
robot.turn_left
assert_equal :east, robot.bearing
end
def test_turn_left_from_west
robot.orient(:west)
robot.turn_left
assert_equal :south, robot.bearing
end
def test_robot_coordinates
robot.at(3, 0)
assert_equal [3, 0], robot.coordinates
end
def test_other_robot_coordinates # 5
robot.at(-2, 5)
assert_equal [-2, 5], robot.coordinates
end
def test_advance_when_facing_north
robot.at(0, 0)
robot.orient(:north)
robot.advance
assert_equal [0, 1], robot.coordinates
end
def test_advance_when_facing_east
robot.at(0, 0)
robot.orient(:east)
robot.advance
assert_equal [1, 0], robot.coordinates
end
def test_advance_when_facing_south
robot.at(0, 0)
robot.orient(:south)
robot.advance
assert_equal [0, -1], robot.coordinates
end
def test_advance_when_facing_west
robot.at(0, 0)
robot.orient(:west)
robot.advance
assert_equal [-1, 0], robot.coordinates
end
end
class RobotSimulatorTest < MiniTest::Unit::TestCase
def simulator
@simulator ||= Simulator.new
end
def test_instructions_for_turning_left
assert_equal [:turn_left], simulator.instructions('L')
end
def test_instructions_for_turning_right
assert_equal [:turn_right], simulator.instructions('R')
end
def test_instructions_for_advancing
assert_equal [:advance], simulator.instructions('A')
end
def test_series_of_instructions
commands = [:turn_right, :advance, :advance, :turn_left]
assert_equal commands, simulator.instructions('RAAL')
end
def test_instruct_robot
robot = Robot.new
simulator.place(robot, x: -2, y: 1, direction: :east)
simulator.evaluate(robot, 'RLAALAL')
assert_equal [0, 2], robot.coordinates
assert_equal :west, robot.bearing
end
def test_instruct_many_robots
robot1 = Robot.new
robot2 = Robot.new
robot3 = Robot.new
simulator.place(robot1, x: 0, y: 0, direction: :north)
simulator.place(robot2, x: 2, y: -7, direction: :east)
simulator.place(robot3, x: 8, y: 4, direction: :south)
simulator.evaluate(robot1, 'LAAARALA')
simulator.evaluate(robot2, 'RRAAAAALA')
simulator.evaluate(robot3, 'LAAARRRALLLL')
assert_equal [-4, 1], robot1.coordinates
assert_equal :west, robot1.bearing
assert_equal [-3, -8], robot2.coordinates
assert_equal :south, robot2.bearing
assert_equal [11, 5], robot3.coordinates
assert_equal :north, robot3.bearing
end
end