Skip to content

Instantly share code, notes, and snippets.

@jasongorman
Created July 23, 2020 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasongorman/d61aa29ae8ff9ad5f0be33811a1d8400 to your computer and use it in GitHub Desktop.
Save jasongorman/d61aa29ae8ff9ad5f0be33811a1d8400 to your computer and use it in GitHub Desktop.
describe('Mars Rover', () => {
[
{facing: 'N', endsFacing: 'E'},
{facing: 'E', endsFacing: 'S'},
{facing: 'S', endsFacing: 'W'},
{facing: 'W', endsFacing: 'N'}
].forEach(({facing, endsFacing}) => {
it('turns right from ${facing} to ${endsFacing}', () => {
let rover = {facing: facing};
rover = exec(rover, 'R');
assert.equal(rover.facing, endsFacing);
});
});
[
{facing: 'N', endsFacing: 'W'},
{facing: 'W', endsFacing: 'S'},
{facing: 'S', endsFacing: 'E'},
{facing: 'E', endsFacing: 'N'}
].forEach(({facing, endsFacing}) => {
it('turns right from ${facing} to ${endsFacing}', () => {
let rover = {facing: facing};
rover = exec(rover, 'L');
assert.equal(rover.facing, endsFacing);
});
});
[
{facing: 'N', x: 5, y: 6},
{facing: 'E', x: 6, y: 5},
{facing: 'S', x: 5, y: 4},
{facing: 'W', x: 4, y: 5}
].forEach(({facing, x, y}) => {
it('moves forward in direction ${facing}', () => {
let rover = {facing: facing, position: {x: 5, y: 5}};
rover = exec(rover, 'F');
assert.deepEqual(rover.position, {x: x, y: y});
});
});
[
{facing: 'N', x: 5, y: 4},
{facing: 'E', x: 4, y: 5},
{facing: 'S', x: 5, y: 6},
{facing: 'W', x: 6, y: 5}
].forEach(({facing, x, y}) => {
it('moves forward in direction ${facing}', () => {
let rover = {facing: facing, position: {x: 5, y: 5}};
rover = exec(rover, 'B');
assert.deepEqual(rover.position, {x: x, y: y});
});
});
it('executes a sequence of instructions', () => {
let rover = {facing: 'N'};
rover = exec(rover, 'RR');
assert.equal(rover.facing, 'S');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment