Exemplo n.º 1
0
        public void IfRobotPlacedCorrectlyAtFirstTimeButNotCorrectlyAfterReportShouldShowLastPosition()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(1, 2), Direction.NORTH);

            Assert.IsNotNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);

            MoveCommand moveCommand = new MoveCommand();

            moveCommand.Execute(robot);

            PlaceCommand placeCommand = new PlaceCommand(new Route(new Position(10, 2), Direction.NORTH));

            //This wrong Place will not set
            placeCommand.Execute(robot);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);

            ReportCommand reportCommand = new ReportCommand();

            //The report should be Last Position "OutPuT: 1,3, NORTH"
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "OutPuT: 1,3, NORTH");
            Console.WriteLine(reportCommand.LastReportOfRobot);
            moveCommand.Execute(robot);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "OutPuT: 1,4, NORTH");
            Console.WriteLine(reportCommand.LastReportOfRobot);
        }
Exemplo n.º 2
0
        private string InterpredCommand(string[] data, string commandName)
        {
            Command addCmd    = new AddCommand(data, repository, unitFactory);
            Command reportcmd = new ReportCommand(data, repository, unitFactory);
            Command removecmd = new RemoveCommand(data, repository, unitFactory);
            string  result    = string.Empty;

            if (commandName == "fight")
            {
                Environment.Exit(0);
            }
            switch (commandName)
            {
            case "add":
                return(addCmd.Execute());

            case "report":
                return(reportcmd.Execute());

            case "fight":
                Environment.Exit(0);
                break;

            case "retire":
                return(removecmd.Execute());

            default:
                throw new InvalidOperationException("Invalid command!");
            }
            return(result);
        }
        public void Robot_Report_Command_Without_Place_Should_Ignore()
        {
            Robot   toyRobot = new Robot(table);
            Command command  = new ReportCommand();

            var report = command.Execute(toyRobot);

            Assert.Equal(string.Empty, report);
        }
Exemplo n.º 4
0
        public void TestCommandReportExecuteValidateParameters()
        {
            Robot         robot   = new Robot(2);
            StringBuilder sb      = new StringBuilder();
            StringWriter  writer  = new StringWriter(sb);
            var           command = new ReportCommand(writer);

            command.Execute(robot, "A,B,C");
        }
Exemplo n.º 5
0
        public void ShouldOutputNullForInvalidRobot()
        {
            var command = new ReportCommand();

            var robot  = new Robot();
            var result = command.Execute(robot);

            Assert.Null(result);
        }
Exemplo n.º 6
0
        public void ShouldExecuteOnValidRobot()
        {
            var command = new ReportCommand();

            var robot = new Robot();

            robot.PlaceAt(new Coordinate(1, 2), Direction.North);
            var result = command.Execute(robot);

            Assert.Equal("1,2,NORTH", result);
        }
Exemplo n.º 7
0
        public void TestCommandReportExecuteValidNewRobot()
        {
            Robot robot = new Robot(2);

            StringBuilder sb      = new StringBuilder();
            StringWriter  writer  = new StringWriter(sb);
            var           command = new ReportCommand(writer);

            command.Execute(robot, "");

            Assert.AreEqual("None,None,None\r\n", sb.ToString());
        }
Exemplo n.º 8
0
        public void ReportCommand_WithoutSetCurrentPosition_Reports_NotPlacedMessage()
        {
            Position           currentPosition   = null;
            IPositionValidator positionValidator = new PositionValidator(5, 5);
            ICommand           reportCommand     = new ReportCommand(new string[] { });
            StringWriter       reportOutput      = new StringWriter();

            Console.SetOut(reportOutput);

            Position newPosition = reportCommand.Execute(currentPosition, positionValidator);

            Assert.That(reportOutput.ToString() == $"Output: {ErrorMessages.NotPlaced}\r\n");
        }
Exemplo n.º 9
0
        public void IfRobotPlacedNotCorrectlyAtFirstTimeReportCommandAnsweredIsNotPlacedProperly()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(10, 2), Direction.NORTH);

            Assert.IsNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);
            ReportCommand reportCommand = new ReportCommand();

            //The report should be empty
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "");
            Console.WriteLine(reportCommand.LastReportOfRobot);
        }
Exemplo n.º 10
0
        public void IfRobotPlaceCorrectlyTheReportCommandAnswerTheCorrectPosition()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(1, 2), Direction.NORTH);

            Assert.IsNotNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);

            ReportCommand reportCommand = new ReportCommand();

            //The report should be  "OutPuT: 1,2, NORTH"
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "OutPuT: 1,2, NORTH");
            Console.WriteLine(reportCommand.LastReportOfRobot);
        }
Exemplo n.º 11
0
        public void Execute_RobotAndOutputGiven_ShouldCallRobotReportMethod()
        {
            // Arrange
            var robotMock  = new Mock <IRobot>();
            var outputMock = new Mock <IOutput>();

            var reportCommand = new ReportCommand(outputMock.Object);

            // Act
            reportCommand.Execute(robotMock.Object);

            // Assert
            robotMock.Verify(robot => robot.Report(outputMock.Object), Times.Once());
        }
        public void Robot_Report_Command_With_Place_Should_Success()
        {
            Robot toyRobot = new Robot(table);

            Command command = new PlaceCommand();

            command.Execute(toyRobot, "2,2,NORTH");
            var afterPlacePosition = toyRobot.CurrentPosition;

            command = new ReportCommand();
            var report = command.Execute(toyRobot);

            Assert.Equal("Output: 2,2,NORTH", report);
        }
Exemplo n.º 13
0
        public void TestCommandReportExecuteValidRobot()
        {
            Robot robot = new Robot(2);

            robot.SetPosition(1, 2);
            robot.SetBearing("SOUTH");

            StringBuilder sb      = new StringBuilder();
            StringWriter  writer  = new StringWriter(sb);
            var           command = new ReportCommand(writer);

            command.Execute(robot, "");

            Assert.AreEqual("1,2,SOUTH\r\n", sb.ToString());
        }
Exemplo n.º 14
0
        public void TestReportCommand()
        {
            var rover = new Rover();

            rover.Stand(new Point(1, 1), Direction.NORTH);

            var cmd = new ReportCommand(rover);

            Assert.IsTrue(cmd.Validate());

            cmd.Execute();

            Assert.AreEqual(cmd.Result.Item1.X, 1);
            Assert.AreEqual(cmd.Result.Item1.Y, 1);
            Assert.AreEqual(cmd.Result.Item2, Direction.NORTH);
        }
Exemplo n.º 15
0
        public void ReportsWarningWhenNotPlaced()
        {
            string  output = null;
            ILogger logger = new CustomisableTextLogger(delegate(string message)
            {
                output = message;
            });
            var   grid  = new Grid(5, 5);
            State?state = null;

            var command = new ReportCommand();

            state = command.Execute(logger, state, grid);

            Assert.IsTrue(output != null && output.StartsWith("Warning:"));
        }
Exemplo n.º 16
0
        public void Execute_RobotNotPlacedOnTable_OK()
        {
            // Arrange
            var robot         = new Robot();
            var textWriter    = new StringWriter();
            var reportCommand = new ReportCommand(textWriter);

            // Act
            reportCommand.Execute(robot);

            // Assert
            Assert.Null(robot.Heading);
            Assert.Null(robot.X);
            Assert.Null(robot.Y);
            Assert.Equal("Not roaming upon a table", textWriter.ToString().TrimEnd());
        }
Exemplo n.º 17
0
        public void ReportCommand_Returns_UnchangedPosition()
        {
            Position currentPosition = new Position
            {
                Facing = Direction.NORTH,
                X      = 0,
                Y      = 0
            };
            IPositionValidator positionValidator = new PositionValidator(5, 5);
            ICommand           reportCommand     = new ReportCommand(new string[] { });

            Position newPosition = reportCommand.Execute(currentPosition, positionValidator);

            Assert.That(newPosition.X == currentPosition.X);
            Assert.That(newPosition.Y == currentPosition.Y);
            Assert.That(newPosition.Facing == currentPosition.Facing);
        }
Exemplo n.º 18
0
        public void ReportCommand_SetCurrentPosition_Reports_CorrectPosition()
        {
            Position currentPosition = new Position
            {
                Facing = Direction.NORTH,
                X      = 1,
                Y      = 1
            };
            IPositionValidator positionValidator = new PositionValidator(5, 5);
            ICommand           reportCommand     = new ReportCommand(new string[] { });
            StringWriter       reportOutput      = new StringWriter();

            Console.SetOut(reportOutput);

            Position newPosition = reportCommand.Execute(currentPosition, positionValidator);

            Assert.That(reportOutput.ToString() == $"Output: 1,1,NORTH\r\n");
        }
Exemplo n.º 19
0
        public void ReportsPositionWhenPlaced()
        {
            string  output = null;
            ILogger logger = new CustomisableTextLogger(delegate(string message)
            {
                output = message;
            });
            var   grid  = new Grid(5, 5);
            State?state = null;

            Command command = new PlaceCommand(3, 3, MoveDirection.East);

            state = command.Execute(logger, state, grid);

            command = new ReportCommand();
            state   = command.Execute(logger, state, grid);

            Assert.AreEqual("3,3,East", output);
        }
Exemplo n.º 20
0
        public void Execute_RobotPlacedOnTable_OK()
        {
            var robot   = new Robot();
            var table   = new Table();
            var x       = 0;
            var y       = 1;
            var heading = (int)CompassPoint.North;

            robot.PlaceOnTable(table, x, y, heading);
            var textWriter = new StringWriter();

            var reportCommand = new ReportCommand(textWriter);

            // Act
            reportCommand.Execute(robot);

            // Assert
            Assert.Equal(x, robot.X);
            Assert.Equal(y, robot.Y);
            Assert.Equal((int)CompassPoint.North, robot.Heading);
            Assert.Same(table, robot.Table);
            Assert.Equal("0,1,North", textWriter.ToString().TrimEnd());
        }