public async Task NorthSuccessTestAsync() { var rover = await roverParser.ParseAsync("1 2 N"); Assert.AreEqual(1, rover.XCoordinate); Assert.AreEqual(2, rover.YCoordinate); Assert.AreEqual(Direction.North, rover.Heading); }
/// <summary> /// Parse from text /// </summary> /// <param name="text">Input text</param> /// <returns>Parsed result</returns> public override async Task <Input> ParseAsync(string text) { if (string.IsNullOrWhiteSpace(text)) { throw new ArgumentNullException(nameof(text)); } try { Plateau plateau = null; InputLine currentLine = null; List <Rover> rovers = new List <Rover>(); List <CommandSet> roverCommandSets = new List <CommandSet>(); // parse lines var lines = await inputLinesParser.ParseAsync(text); var linesQueue = new Queue <InputLine>(lines); // parse plateau if (linesQueue.Any()) { try { currentLine = linesQueue.Dequeue(); plateau = await plateauParser.ParseAsync(currentLine.Text); } catch (Exception exception) { throw new PlateauParserException($"Expected valid plateau initialization at line {currentLine.Number} e.g. 5 5", exception); } } // parse rovers and command sets while (linesQueue.Any()) { Rover rover; CommandSet roverCommandSet; // parse rover try { currentLine = linesQueue.Dequeue(); rover = await roverParser.ParseAsync(currentLine.Text); } catch (Exception exception) { throw new RoverParserException($"Expected valid rover initialization at line {currentLine.Number} e.g. 1 2 N", exception); } rover.SetPlateau(plateau); rovers.Add(rover); // parse rover command set if (linesQueue.Any()) { try { currentLine = linesQueue.Dequeue(); roverCommandSet = await roverCommandSetParser.ParseAsync(currentLine.Text); } catch (Exception exception) { throw new CommandSetParserException($"Expected valid rover command set at line {currentLine.Number} e.g. LMLMLMLMM", exception); } roverCommandSet.SetRover(rover); roverCommandSets.Add(roverCommandSet); } } // create new input var input = new Input(plateau, rovers, roverCommandSets); // return new input return(input); } catch (Exception exception) { throw new InputParserException("Failed to parse input", exception); } }