Skip to content

Commit

Permalink
use switch instead of if/else if/else; add tests for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SillyFreak committed Feb 20, 2019
1 parent 3efa0b7 commit 2ef2870
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 25 deletions.
84 changes: 61 additions & 23 deletions hedgehog/protocol/messages/motor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Action extends Message {

@RequestMsg.message(motor_pb.MotorConfigAction, PayloadCase.MOTOR_CONFIG_ACTION)
export class ConfigAction extends Message {
constructor(public port: number, public config) {
constructor(public port: number, public config: MotorConfig) {
super();
}

Expand All @@ -62,19 +62,53 @@ export class ConfigAction extends Message {
public static parseFrom(containerMsg: ProtoContainerMessage): Message {
let msg = (containerMsg as any).getMotorConfigAction();
let port = msg.getPort();
// <default GSL customizable: ConfigAction-parse-config>
// TODO parse custom field 'config'
let config = msg.getConfig();
// <GSL customizable: ConfigAction-parse-config>
let config: MotorConfig;
switch(msg.getConfigCase()) {
case motor_pb.MotorConfigAction.ConfigCase.DC:
config = {
kind: ConfigKind.DC,
};
break;
case motor_pb.MotorConfigAction.ConfigCase.ENCODER:
config = {
kind: ConfigKind.ENCODER,
encoderAPort: msg.getEncoder().getEncoderAPort(),
encoderBPort: msg.getEncoder().getEncoderBPort(),
};
break;
case motor_pb.MotorConfigAction.ConfigCase.STEPPER:
config = {
kind: ConfigKind.STEPPER,
};
break;
default:
throw new Error("unreachable");
}
// </GSL customizable: ConfigAction-parse-config>
return new ConfigAction(port, config);
}

public serializeTo(containerMsg: ProtoContainerMessage): void {
let msg = new motor_pb.MotorConfigAction();
msg.setPort(this.port);
// <default GSL customizable: ConfigAction-serialize-config>
// TODO serialize custom field 'config'
msg.setConfig(this.config);
// <GSL customizable: ConfigAction-serialize-config>
switch(this.config.kind) {
case ConfigKind.DC:
msg.setDc(new motor_pb.Dummy());
break;
case ConfigKind.ENCODER:
let config = new motor_pb.EncoderConfig();
config.setEncoderAPort(this.config.encoderAPort);
config.setEncoderBPort(this.config.encoderBPort);
msg.setEncoder(config);
break;
case ConfigKind.STEPPER:
msg.setStepper(new motor_pb.Dummy());
break;
default:
throw new Error("unreachable");
}
// </GSL customizable: ConfigAction-serialize-config>
(containerMsg as any).setMotorConfigAction(msg);
}
Expand Down Expand Up @@ -306,22 +340,26 @@ ReplyMsg.parser(PayloadCase.MOTOR_COMMAND_MESSAGE)(
let subscription = msg.hasSubscription()? msg.getSubscription() : undefined;
// <GSL customizable: parseMotorCommandMessageReplyFrom-return>
let config: MotorConfig;
if(dc !== undefined) {
config = {
kind: ConfigKind.DC,
}
} else if(encoder !== undefined) {
config = {
kind: ConfigKind.ENCODER,
encoderAPort: encoder.getEncoderAPort(),
encoderBPort: encoder.getEncoderBPort(),
}
} else if(encoder !== undefined) {
config = {
kind: ConfigKind.STEPPER,
}
} else {
throw new Error("unreachable");
switch(msg.getConfigCase()) {
case motor_pb.MotorCommandMessage.ConfigCase.DC:
config = {
kind: ConfigKind.DC,
};
break;
case motor_pb.MotorCommandMessage.ConfigCase.ENCODER:
config = {
kind: ConfigKind.ENCODER,
encoderAPort: encoder.getEncoderAPort(),
encoderBPort: encoder.getEncoderBPort(),
};
break;
case motor_pb.MotorCommandMessage.ConfigCase.STEPPER:
config = {
kind: ConfigKind.STEPPER,
};
break;
default:
throw new Error("unreachable");
}

if(subscription === undefined)
Expand Down
129 changes: 127 additions & 2 deletions test/protocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,51 @@ describe('Protocol', () => {
});
});

describe('MotorConfigAction', () => {
it("should translate `motor.ConfigAction` for DC successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorConfigAction();
proto.setPort(0);
proto.setDc(new motor_pb.Dummy());
_wire.setMotorConfigAction(proto);
});

let msg = new motor.ConfigAction(0, { kind: motor.ConfigKind.DC });
testMessage(msg, wire, protocol.RequestMsg);
});

it("should translate `motor.ConfigAction` for ENCODER successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorConfigAction();
proto.setPort(0);
let config = new motor_pb.EncoderConfig();
config.setEncoderAPort(0);
config.setEncoderBPort(1);
proto.setEncoder(config);
_wire.setMotorConfigAction(proto);
});

let msg = new motor.ConfigAction(0, {
kind: motor.ConfigKind.ENCODER,
encoderAPort: 0,
encoderBPort: 1,
});
testMessage(msg, wire, protocol.RequestMsg);
});

it("should translate `motor.ConfigAction` for STEPPER successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorConfigAction();
proto.setPort(0);
proto.setStepper(new motor_pb.Dummy());
_wire.setMotorConfigAction(proto);
});

let msg = new motor.ConfigAction(0, { kind: motor.ConfigKind.STEPPER });
testMessage(msg, wire, protocol.RequestMsg);
});
});

describe('MotorSetPositionAction', () => {
it("should translate `motor.SetPositionAction` successfully", () => {
let wire = makeWire(_wire => {
Expand Down Expand Up @@ -293,7 +338,7 @@ describe('Protocol', () => {
testMessage(msg, wire, protocol.RequestMsg);
});

it("should translate `motor.CommandReply` successfully", () => {
it("should translate `motor.CommandReply` for DC successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorCommandMessage();
proto.setPort(0);
Expand All @@ -307,7 +352,42 @@ describe('Protocol', () => {
testMessage(msg, wire, protocol.ReplyMsg);
});

it("should translate `motor.CommandUpdate` successfully", () => {
it("should translate `motor.CommandReply` for ENCODER successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorCommandMessage();
proto.setPort(0);
let config = new motor_pb.EncoderConfig();
config.setEncoderAPort(0);
config.setEncoderBPort(1);
proto.setEncoder(config);
proto.setState(motor.MotorState.POWER);
proto.setAmount(1000);
_wire.setMotorCommandMessage(proto);
});

let msg = new motor.CommandReply(0, {
kind: motor.ConfigKind.ENCODER,
encoderAPort: 0,
encoderBPort: 1,
}, motor.MotorState.POWER, 1000);
testMessage(msg, wire, protocol.ReplyMsg);
});

it("should translate `motor.CommandReply` for STEPPER successfully", () => {
let wire = makeWire(_wire => {
let proto = new motor_pb.MotorCommandMessage();
proto.setPort(0);
proto.setStepper(new motor_pb.Dummy());
proto.setState(motor.MotorState.POWER);
proto.setAmount(1000);
_wire.setMotorCommandMessage(proto);
});

let msg = new motor.CommandReply(0, { kind: motor.ConfigKind.STEPPER }, motor.MotorState.POWER, 1000);
testMessage(msg, wire, protocol.ReplyMsg);
});

it("should translate `motor.CommandUpdate` for DC successfully", () => {
let sub = new subscription_pb.Subscription();
sub.setSubscribe(true);
sub.setTimeout(10);
Expand All @@ -325,6 +405,51 @@ describe('Protocol', () => {
let msg = new motor.CommandUpdate(0, { kind: motor.ConfigKind.DC }, motor.MotorState.POWER, 1000, sub);
testMessage(msg, wire, protocol.ReplyMsg);
});

it("should translate `motor.CommandUpdate` for ENCODER successfully", () => {
let sub = new subscription_pb.Subscription();
sub.setSubscribe(true);
sub.setTimeout(10);

let wire = makeWire(_wire => {
let proto = new motor_pb.MotorCommandMessage();
proto.setPort(0);
let config = new motor_pb.EncoderConfig();
config.setEncoderAPort(0);
config.setEncoderBPort(1);
proto.setEncoder(config);
proto.setState(motor.MotorState.POWER);
proto.setAmount(1000);
proto.setSubscription(sub);
_wire.setMotorCommandMessage(proto);
});

let msg = new motor.CommandUpdate(0, {
kind: motor.ConfigKind.ENCODER,
encoderAPort: 0,
encoderBPort: 1,
}, motor.MotorState.POWER, 1000, sub);
testMessage(msg, wire, protocol.ReplyMsg);
});

it("should translate `motor.CommandUpdate` for STEPPER successfully", () => {
let sub = new subscription_pb.Subscription();
sub.setSubscribe(true);
sub.setTimeout(10);

let wire = makeWire(_wire => {
let proto = new motor_pb.MotorCommandMessage();
proto.setPort(0);
proto.setStepper(new motor_pb.Dummy());
proto.setState(motor.MotorState.POWER);
proto.setAmount(1000);
proto.setSubscription(sub);
_wire.setMotorCommandMessage(proto);
});

let msg = new motor.CommandUpdate(0, { kind: motor.ConfigKind.STEPPER }, motor.MotorState.POWER, 1000, sub);
testMessage(msg, wire, protocol.ReplyMsg);
});
});

describe('MotorStateMessage', () => {
Expand Down

0 comments on commit 2ef2870

Please sign in to comment.