-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover.test.js
219 lines (193 loc) · 6.79 KB
/
rover.test.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
describe("Mars Rover", () => {
let rover;
beforeAll(async () => {
({ default: rover } = await import("./rover.js"));
});
it("should export a function as a default export", () => {
expect(rover).toBeInstanceOf(Function);
});
describe("given a single robot ", () => {
const origin = "0 0";
describe("and a valid world size", () => {
describe("of minimum viable dimension (single unit size)", () => {
const world = `${origin}`;
describe("with a valid start location (0 0)", () => {
const location = `${origin}`;
describe.each`
orientation
${"N"}
${"E"}
${"S"}
${"W"}
`("and orientation cardinal ($orientation)", ({ orientation }) => {
const state = `${location} ${orientation}`;
describe("and a forward instruction (F)", () => {
const instruction = "F";
it(`should return the starting location with an indication that the robot is lost`, () => {
expect(rover(`${world}\n${state}\n${instruction}`)).toEqual(
`${state} LOST`
);
});
});
});
});
});
describe.each`
right | top
${50} | ${0}
${0} | ${50}
`("of right top coordinate ($right $top)", ({ right, top }) => {
const world = `${right} ${top}`;
describe.each`
location
${`${right} 0`}
${`0 ${top}`}
`("with a valid start location ($location)", ({ location }) => {
describe.each`
orientation
${"N"}
${"E"}
${"S"}
${"W"}
`("and orientation cardinal ($orientation)", ({ orientation }) => {
const state = `${location} ${orientation}`;
describe("and no instructions", () => {
it(`should return the starting state ${state} unchanged`, () => {
expect(rover(`${world}\n${state}`)).toEqual(state);
});
});
describe.each`
instructions
${"R"}
${"L"}
${"RR"}
${"LL"}
${"RRR"}
${"LLL"}
${"LR"}
${"RL"}
${"RLR"}
`(
"and rotation instructions ($instructions)",
({ instructions }) => {
const cardinals = ["N", "E", "S", "W"];
let orientationIndex = cardinals.indexOf(orientation);
instructions.split("").forEach((instruction) => {
const instructionDeltaMap = {
L: -1,
R: 1,
};
const mod = (n, m) => ((n % m) + m) % m;
orientationIndex = mod(
orientationIndex + instructionDeltaMap[instruction],
4
);
});
const expectedOrientation = cardinals[orientationIndex];
it(`should return the starting location with an updated orientation (${expectedOrientation})`, () => {
expect(rover(`${world}\n${state}\n${instructions}`)).toEqual(
`${location} ${expectedOrientation}`
);
});
}
);
});
});
describe.each`
location
${`${right + 1} 0`}
${`0 ${top + 1}`}
${`-1 0`}
${`0 -1`}
`("with an invalid start location ($location)", ({ location }) => {
const state = `${location} N`;
it(`should throw an exception indicating that the coordinate is outside of accepted bounds`, () => {
expect(() => rover(`${world}\n${state}`)).toThrow(
`invalid start location ${location} received`
);
});
});
});
describe.each`
right | top | valid
${51} | ${0} | ${"an invalid"}
${50} | ${0} | ${"a valid"}
${-1} | ${0} | ${"an invalid"}
${0} | ${51} | ${"an invalid"}
${0} | ${50} | ${"a valid"}
${0} | ${-1} | ${"an invalid"}
`(
"and $valid world right top coordinate ($right $top)",
({ right, top, valid }) => {
const world = `${right} ${top}`;
const state = "0 0 N";
const isValid = valid === "a valid";
const expectation = isValid ? "not " : "";
it(`should ${expectation}throw an exception indicating that the coordinate is outside of accepted bounds`, () => {
let expectation = expect(() => rover(`${world}\n${state}`));
if (isValid) {
expectation = expectation.not;
}
expectation.toThrow(
`invalid world right top coordinate (${right} ${top}) received`
);
});
}
);
describe("and a world with with right top coordinate of (5 3)", () => {
const world = "5 3";
describe.each`
state | instructions | expectation
${"1 1 E"} | ${"RFRFRFRF"} | ${"1 1 E"}
${"3 2 N"} | ${"FRRFLLFFRRFLL"} | ${"3 3 N LOST"}
`(
"and a valid start state ($state) and instructions ($instructions)",
({ state, instructions, expectation }) => {
it(`should return a new state (${expectation})`, () => {
expect(rover(`${world}\n${state}\n${instructions}`)).toEqual(
expectation
);
});
}
);
});
});
});
describe("given multiple robots", () => {
const robots = [
{ state: "1 1 E", instructions: "RFRFRFRF" },
{ state: "3 2 N", instructions: "FRRFLLFFRRFLL" },
{ state: "0 3 W", instructions: "LLFFFLFLFL" },
{ state: "0 3 N", instructions: "F" },
{ state: "1 0 S", instructions: "F" },
{ state: "5 1 E", instructions: "F" },
{ state: "0 3 N", instructions: "F" },
];
const expectations = [
"1 1 E",
"3 3 N LOST",
"2 3 S",
"0 3 N LOST",
"1 0 S LOST",
"5 1 E LOST",
"0 3 N",
];
const stateDescription = robots
.map(
({ state, instructions }) =>
`state ${state} and instructions (${instructions})`
)
.join(" and ");
describe(`with ${stateDescription} and a world with right top coordinate of (5 3)`, () => {
const world = "5 3";
const formatRobot = ({ state, instructions }) =>
`${state}\n${instructions}`;
const input = `${world}\n${robots.map(formatRobot).join("\n \n")}`;
it(`should return the expected locations (${expectations.join(
" and "
)})`, () => {
expect(rover(input)).toEqual(expectations.join("\n"));
});
});
});
});