public void TaskGrid_AddGridItem_NullGridItem_ReturnsSuccessFalse()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            // Act
            var result = taskGrid.AddGridItem <Robot>(null);

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void TaskGrid_AddValidGridItem_ReturnsSuccessTrue()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            // Act
            var result = taskGrid.AddGridItem <Robot>(gridItemTestName);

            // Assert
            Assert.IsTrue(result.Success);
        }
        public void TaskGrid_PlaceItemOutsideGridBoundsX_ReturnsSuccessFalse()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);

            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Place 5,1,North");

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void RunCommand_ValidPlaceCommand_ReturnsSuccessTrue()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);

            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Place 1,1,EAST");

            // Assert
            Assert.IsTrue(result.Success);
        }
        public void RunCommand_InValidPlaceWithMissingParamsCommand_ReturnsSuccessFalse()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);

            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Place 1,NORTH");

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void RunCommand_InValidCommandFirst_ReturnsSuccessFalse(string command)
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);

            // Act
            var result = taskGrid.RunCommand(gridItemTestName, command);

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void RunCommand_ValidPlaceThenReportCommand_ReturnsCorrectOutput()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);
            taskGrid.RunCommand(gridItemTestName, "Place 1,1,EAST");

            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Report");

            // Assert
            Assert.AreEqual(result.Output, "1,1,EAST");
        }
        public void RunCommand_MoveOffBoard_ReturnsSuccessFalse()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);
            taskGrid.RunCommand(gridItemTestName, "Place 1,1,WEST");
            taskGrid.RunCommand(gridItemTestName, "Move");
            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Move");

            // Assert
            Assert.IsFalse(result.Success);
        }
        public void RunCommand_ValidMoveInDirectionCommand_ReturnsCorrectCoordinates(string direction, string correctResult)
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);
            taskGrid.RunCommand(gridItemTestName, $"Place 1,1,{direction}");
            taskGrid.RunCommand(gridItemTestName, "Move");
            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Report");

            // Assert
            Assert.IsTrue(result.Output.StartsWith(correctResult));
        }
        public void RunCommand_ValidRightThenReportCommand_ReturnsCorrectDirection()
        {
            // Arrange
            var taskGrid = new TaskGrid();

            taskGrid.AddGridItem <Robot>(gridItemTestName);
            taskGrid.RunCommand(gridItemTestName, "Place 1,1,EAST");
            taskGrid.RunCommand(gridItemTestName, "Right");
            // Act
            var result = taskGrid.RunCommand(gridItemTestName, "Report");

            // Assert
            Assert.IsTrue(result.Output.EndsWith("SOUTH", StringComparison.InvariantCultureIgnoreCase));
        }
Exemple #11
0
        /// <summary>
        /// App entry method
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            List <string> commandsFromFile = new List <string>();
            StringBuilder outputLog        = new StringBuilder();

            bool IsInputFromFile = false;
            bool IsOutputToFile  = false;

            string outputFilename = "Results.txt";

            // Instanciate a TaskGrid
            var gridTask = new TaskGrid();

            // Add a Grid Item
            gridTask.AddGridItem <Robot>(gridItemName);

            // Display title as the C# console toy robot app.
            Console.WriteLine("Welcome to the Toy Robot Task");
            Console.WriteLine("-----------------------------");

            // Ask the user to type the first number.
            Console.WriteLine(instructions);
            Console.WriteLine();
            Console.WriteLine(validCommands);
            Console.WriteLine();

            if (args.Length > 0)
            {
                // An input file has been specified
                var inputFilePath = args[0];
                if (File.Exists(inputFilePath))
                {
                    // Load all commands. Ignore any empty lines.
                    commandsFromFile = File.ReadAllLines(inputFilePath).Where(x => !string.IsNullOrEmpty(x)).ToList();
                    if (commandsFromFile.Count > 0)
                    {
                        IsInputFromFile = true;

                        // If there is an input file then automatically save the results to an output file.
                        IsOutputToFile = true;

                        // Base the default output filename in the input filename.
                        var fileInfo = new FileInfo(inputFilePath);
                        outputFilename = $"{fileInfo.Name.Replace(fileInfo.Extension, "")}_Results.txt";
                    }
                }
            }

            bool exitApp      = false;
            bool showComments = false;

            while (!exitApp)
            {
                // Set input text colour to white.
                Console.ForegroundColor = ConsoleColor.White;
                var inputtedCommand = string.Empty;

                if (IsInputFromFile)
                {
                    // Get the command at the top of the list.
                    inputtedCommand = commandsFromFile.FirstOrDefault();
                    if (string.IsNullOrEmpty(inputtedCommand))
                    {
                        // No commands left to process, so break out.
                        break;
                    }

                    // Remove this command from the list
                    commandsFromFile.RemoveAt(0);

                    // Output the command to the console too.
                    Console.WriteLine(inputtedCommand);
                }
                else
                {
                    inputtedCommand = Console.ReadLine();
                }

                if (string.IsNullOrEmpty(inputtedCommand))
                {
                    // Show output in red.
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(instructions);
                    continue;
                }
                else if (inputtedCommand.Trim().ToLower() == CMD_SHOW_COMMENTS)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"Type '{CMD_HIDE_COMMENTS}' to stop showing comments.");
                    showComments = true;
                    continue;
                }
                else if (inputtedCommand.Trim().ToLower() == CMD_HIDE_COMMENTS)
                {
                    showComments = false;
                    continue;
                }
                else if (inputtedCommand.Trim().ToLower() == CMD_SAVE_OUTPUTS)
                {
                    IsOutputToFile = true;
                    continue;
                }
                else if (inputtedCommand.Trim().ToLower() == CMD_EXIT)
                {
                    break;
                }

                // Store all outputs.
                outputLog.AppendLine(inputtedCommand);

                var commandResult = gridTask.RunCommand(gridItemName, inputtedCommand);

                // If there is an output then show it.
                if (!string.IsNullOrEmpty(commandResult.Output))
                {
                    var output = $"Output: {commandResult.Output}";

                    outputLog.AppendLine(output);

                    // Show output in green.
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(output);
                }

                if (showComments && !string.IsNullOrEmpty(commandResult.Comment))
                {
                    var comment = $"Comment: {commandResult.Comment}";

                    outputLog.AppendLine(comment);

                    // Show output in green.
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(comment);
                }
            } // While

            if (IsOutputToFile)
            {
                if (outputLog.Length > 0 && !string.IsNullOrEmpty(outputFilename))
                {
                    // Save output to file.
                    File.WriteAllText(outputFilename, outputLog.ToString());
                    Console.WriteLine($"Results file saved: {outputFilename}");
                }
            }
        }