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.
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.this.position
to position
this.mode
to 'NORMAL'
generatorWatts
to 110receiveMessage(message)
message
is a Message
objectmessage
: the name of the original Message
objectresults
: an array of results. Each element in the array is an
object that corresponds to one Command
in message.commands
.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.
"constructor sets position and default values for mode and generatorWatts". Refer to the Rover Class description above for these default values.
"response returned by receiveMessage contains name of message"
"response returned by receiveMessage includes two results if two commands are sent in the message"
"responds correctly to status check command"
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."responds correctly to mode change command".
completed
property and rover mode for accuracy."responds with false completed value when attempting to move in LOW_POWER mode".
completed
property for accuracy and confirm
that the rover position did not change."responds with position for move command".
MOVE
command will update the rover's position with the position value in
the command.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.
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.