-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover_spec.rb
139 lines (126 loc) · 6.27 KB
/
rover_spec.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
load 'rover.rb'
describe "Rover" do
before(:each) do
@max_size_of_plateau = {:x => 5, :y => 5}
@initial_position_of_rover = {:x => 1, :y => 2 ,:direction => 'N'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
end
describe "given the max_x and max_y and the initial position of the rover" do
context "and given the instructions to the rover" do
it "should return the final position of the rover" do
instruction_set = "LMLMLMLMM"
@mars_rover.calculate_final_position(instruction_set).should eql({:x => 1, :y => 3, :direction => 'N'})
@initial_position_of_rover = {:x => 3, :y => 3 ,:direction => 'E'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
instruction_set = "MMRMMRMRRM"
@mars_rover.calculate_final_position(instruction_set).should eql({:x => 5, :y => 1, :direction => 'E'})
end
end
end
describe "Given the current position and the instruction should return the next position" do
before(:each) do
end
describe "Instruction is L" do
context "if current position is 0, 0, N" do
it "should return the next position as 0, 0, W" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'N'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('L')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'W'})
end
end
context "if current position is 0, 0, W" do
it "should return the next position as 0, 0, S" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'W'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('L')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'S'})
end
end
context "if current position is 0, 0, S" do
it "should return the next position as 0, 0, E" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'S'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('L')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'E'})
end
end
context "if current position is 0, 0, E" do
it "should return the next position as 0, 0, N" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'E'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('L')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'N'})
end
end
end
describe "Instruction is R" do
context "if current position is 0, 0, N" do
it "should return the next position as 0, 0, E" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'N'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('R')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'E'})
end
end
context "if current position is 0, 0, W" do
it "should return the next position as 0, 0, N" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'W'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('R')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'N'})
end
end
context "if current position is 0, 0, S" do
it "should return the next position as 0, 0, W" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'S'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('R')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'W'})
end
end
context "if current position is 0, 0, E" do
it "should return the next position as 0, 0, S" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'E'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('R')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 0, :direction => 'S'})
end
end
end
describe "Instruction is M" do
context "if current position is 0, 0, N" do
it "should return the next position as 0, 1, N" do
@initial_position_of_rover = {:x => 0, :y => 0 ,:direction => 'N'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('M')
@mars_rover.current_position_of_rover.should eql({:x => 0, :y => 1, :direction => 'N'})
end
end
context "if current position is 2, 3, W" do
it "should return the next position as 1, 3, W" do
@initial_position_of_rover = {:x => 2, :y => 3 ,:direction => 'W'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('M')
@mars_rover.current_position_of_rover.should eql({:x => 1, :y => 3, :direction => 'W'})
end
end
context "if current position is 2, 3, S" do
it "should return the next position as 2, 2, S" do
@initial_position_of_rover = {:x => 2, :y => 3 ,:direction => 'S'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('M')
@mars_rover.current_position_of_rover.should eql({:x => 2, :y => 2, :direction => 'S'})
end
end
context "if current position is 2, 3, E" do
it "should return the next position as 2, 4, E" do
@initial_position_of_rover = {:x => 2, :y => 3 ,:direction => 'E'}
@mars_rover = Rover.new(@max_size_of_plateau, @initial_position_of_rover)
@mars_rover.update_position('M')
@mars_rover.current_position_of_rover.should eql({:x => 3, :y => 3, :direction => 'E'})
end
end
end
end
end