Mars Rover, Part 3

With Part 2 completed, let's turn our attention to the final class, the Rover class.

Rover

Rover receives a message object, updates its properties from the message, and returns the results.

Remember to use TDD by first reading the class description, writing tests, and then coding the class.

Rover Class Description

This class builds a rover object with a few properties, and it also contains a function outside of constructor to handle updates to its properties.

  1. constructor(position)
    1. position is a number representing the rover's position.
    2. Sets this.position to position
    3. Sets this.mode to 'NORMAL'
    4. Sets default value for generatorWatts to 110
  2. receiveMessage(message)
    1. message is a Message object
    2. Returns an object containing at least two properties:
      1. message: the name of the original Message object
      2. results: an array of results. Each element in the array is an object that corresponds to one Command in message.commands.
    3. Updates certain properties of the rover object
      1. Details about how to respond to different commands are in the Command Types table.

Example

let commands = [new Command('MODE_CHANGE', 'LOW_POWER'), new Command('STATUS_CHECK')];
let message = new Message('Test message with two commands', commands);
let rover = new Rover(98382);    // Passes 98382 as the rover's position.
let response = rover.receiveMessage(message);

console.log(response);

Output

{
   message: 'Test message with two commands',
   results: [
      {
         completed: true
      },
      {
         completed: true,
         roverStatus: { mode: 'LOW_POWER', generatorWatts: 110, position: 98382 }
      }
   ]
}

Rover Tests

Create spec/rover.spec.js and write the following tests. Write the code to make them pass in rover.js. Remember to use the given phrase as the test description.

Test 7

"constructor sets position and default values for mode and generatorWatts". Refer to the Rover Class description above for these default values.

Test 8

"response returned by receiveMessage contains name of message"

Test 9

"response returned by receiveMessage includes two results if two commands are sent in the message"

Test 10

"responds correctly to status check command"

  1. For the STATUS_CHECK command, receiveMessage(message).results includes a roverStatus object describing the current state of the rover object --- mode, generatorWatts, and position. The test should check each of these for accuracy.
  2. See the Rover Command Types table for more details.

Test 11

"responds correctly to mode change command".

  1. The test should check the completed property and rover mode for accuracy.
  2. The rover has two modes that can be passed a values to a mode change command, 'LOW_POWER' and 'NORMAL'.

Test 12

"responds with false completed value when attempting to move in LOW_POWER mode".

  1. The test should check the completed property for accuracy and confirm that the rover position did not change.
  2. Use the Rover Modes table for guidance on how to handle move commands in different modes.

Test 13

"responds with position for move command".

  1. A MOVE command will update the rover's position with the position value in the command.

Rover Command Types

Command Value sent with command Updates to Rover object Result returned
MOVE Number representing the position the rover should move to. position {completed: true}
STATUS_CHECK No values sent with this command. No updates {completed: true, roverStatus: {mode: 'NORMAL', generatorWatts: 110, position: 87382098}} Values for mode, generatorWatts, position will depend on current state of rover.
MODE_CHANGE String representing rover mode (see modes) mode {completed: true}

Note

The response value for completed will be false if the command could NOT be completed.

Rover Modes

Mode Restrictions
LOW_POWER Can't be moved in this state.
NORMAL None

Once your Rover class is complete, make sure to submit your work.