Task 4: Rover Class
With the Message Class section
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.
constructor(position)
position
is a number representing the rover’s position.- Sets
this.position
toposition
- Sets
this.mode
to'NORMAL'
- Sets the default value for
generatorWatts
to 110
receiveMessage(message)
message
is aMessage
object- Returns an object containing at least two properties:
message
: the name of the originalMessage
objectresults
: an array of results. Each element in the array is an object that corresponds to oneCommand
inmessage.commands
.
- Updates certain properties of the rover object
- Details about how to respond to different commands are in the Command Types table .
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
Within the spec/rover.spec.js
file, write the tests below. Make sure to 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 the name of the message”
Test 9
“response returned by receiveMessage includes two results if two commands are sent in the message”
Test 10
“responds correctly to the status check command”
- For the
STATUS_CHECK
command,receiveMessage(message).results
includes aroverStatus
object describing the current state of the rover object —mode
,generatorWatts
, andposition
. The test should check each of these for accuracy. - See the Rover Command Types table for more details.
Test 11
“responds correctly to the mode change command”
- The test should check the
completed
property and rover mode for accuracy. - The rover has two modes that can be passed as values to a mode change command: ‘LOW_POWER’ and ‘NORMAL’.
Test 12
“responds with a false completed value when attempting to move in LOW_POWER mode”
- The test should check the
completed
property for accuracy and confirm that the rover’s position did not change. - Use the Rover Modes table for guidance on how to handle move commands in different modes.
Test 13
“responds with the position for the move command”
- 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 the current state of the 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
.