public void ParseCommandTestValidCommand() { try { Command expected = Command.LEFT; Command actual = InputParser.GetCommand(Command.LEFT.ToString()); Assert.IsTrue(expected == actual); } catch (ArgumentException) { Assert.Fail(); } }
public void ParseCommandTestInvalidCommand() { try { string expectedCmdString = "TESTING"; Command result = InputParser.GetCommand("TESTING 123 435"); Assert.IsTrue(expectedCmdString == result.ToString()); } catch (ArgumentException ex) { string expected = "Requested value 'TESTING 123 435' was not found."; Assert.AreEqual(ex.Message, expected); } }
public void ProcessInput(string commandString) { log.Info("Command: " + commandString); string[] args = commandString.Split(new[] { Constants.SPACECHAR }, 2); try { Command cmd = InputParser.GetCommand(args[0]); switch (cmd) { case Command.PLACE: if (args.Length > 1) { (int x, int y, Direction dir) = InputParser.ProcessPlaceCommand(args[1]); Robot.Place(x, y, dir); } else { log.Error("PLACE command issued without location and direction arguments. please provide the following arguments: PLACE X,Y,F"); } break; case Command.LEFT: Robot.Left(); break; case Command.MOVE: Robot.Move(); break; case Command.RIGHT: Robot.Right(); break; default: //anything apart from the handled commands need to be ignored. log.Error(string.Format("Command {0} is invalid and will be ignored.", commandString)); break; } } catch (Exception ex) when(ex is ArgumentException || ex is FormatException) { //parsing errors will cause these exceptions so handle them as invalid commands log.Error(string.Format("Command {0} is invalid and will be ignored.", commandString)); } }