Example #1
0
        public void PlaceRobotInNotCorrectPositionTheRobotShouldntPlaced()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(10, 2), Direction.NORTH);

            Assert.IsNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);
        }
        public void Turnleft_TableTop5Into5RobotTurnedLeftFourTimesSameFaing_IsTrue()
        {
            //arrange
            TableTop     tt = new TableTop(5, 5);
            ToyRobot     tr = new ToyRobot();
            CommandModel cm = new CommandModel()
            {
                Command    = Command.PLACE,
                Coordinate = new Coordinate(3, 4),
                Facing     = Facing.NORTH
            };
            ICommandInterface placeCommand = new PlaceRobotCommand(cm, tr, tt);

            placeCommand.Execute();
            ICommandInterface leftCommand = new TurnLeftCommand(tr);

            //act
            leftCommand.Execute();
            leftCommand.Execute();
            leftCommand.Execute();
            leftCommand.Execute();

            //assert
            Assert.AreEqual(tr.robotDirection, Facing.NORTH);
        }
        public void TestPlacingAToyRobotShouldBePlacedCorrectly()
        {
            // Arrange
            uint width    = 5;
            uint height   = 5;
            var  table    = new Table(width, height);
            var  toyRobot = new ToyRobot(table);

            uint      xPosition      = 1;
            uint      yPosition      = 3;
            Direction robotDirection = Direction.EAST;

            // Assert
            // make sure robot is not placed
            Assert.True(!toyRobot.X.HasValue && !toyRobot.Y.HasValue && !toyRobot.Direction.HasValue && !toyRobot.IsPlaced);

            // ACT
            toyRobot.Place(xPosition, yPosition, robotDirection);

            // make sure robot is placed
            Assert.True(toyRobot.IsPlaced);
            Assert.True(toyRobot.X.HasValue && toyRobot.X.Value == xPosition);
            Assert.True(toyRobot.Y.HasValue && toyRobot.Y.Value == yPosition);
            Assert.True(toyRobot.Direction.HasValue && toyRobot.Direction.Value == robotDirection);
        }
Example #4
0
        public ToyRobot PlaceRobotOnTable(string command)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            string[] commandComponents = command.Split(' ');
            string   commandType       = commandComponents[0];

            if (commandType != Constants.Commands.PLACE)
            {
                throw new InvalidOperationException($"this is not a place command: {command}");
            }

            if (commandComponents.Length != 2)
            {
                throw new InvalidOperationException($"invalid place command: {command}");
            }

            string         commandArguments = commandComponents[1];
            PlaceArguments placeArguments   = GetPlaceArguments(commandArguments);
            IReportManager reportManager    = new ReportManager();
            var            robot            = new ToyRobot(placeArguments.Position, placeArguments.CardinalDirectionManager, reportManager);

            return(robot);
        }
Example #5
0
        public void InterpretCommandTest_Success()
        {
            // init
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(300, 500);
            ToyRobot robot = new ToyRobot(tb);

            // place report check
            robot.InterpretCommand("PLACE 99,230,EAST");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.EAST);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.EAST);
            robot.InterpretCommand("PLACE 99,230,WEST");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.WEST);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(99, 230), Direction.WEST);
            robot.InterpretCommand("PLACE 230,99,NORTH");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.NORTH);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.NORTH);
            robot.InterpretCommand("PLACE 230,99,SOUTH");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.SOUTH);
            robot.InterpretCommand("REPORT");
            TestLocation(robot, new CoordinateXY(230, 99), Direction.SOUTH);

            //move tests
            robot.InterpretCommand("MOVE");
            TestLocation(robot, new CoordinateXY(230, 98), Direction.SOUTH);
            robot.InterpretCommand("MOVE");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.SOUTH);
            robot.InterpretCommand("LEFT");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.EAST);
            robot.InterpretCommand("RIGHT");
            TestLocation(robot, new CoordinateXY(230, 97), Direction.SOUTH);
        }
 public PlaceCommand(ToyRobot toy, PlacementCoordinates placementVar)
 {
     toyRobot            = toy;
     xCoordinate         = placementVar.XPlacement;
     yCooridinate        = placementVar.YPlacement;
     startingOrientation = placementVar.orientations;
 }
Example #7
0
        public void CorrectListOfCommandWouldReturnCorrectOutput()
        {
            List <string> stringCommands = new List <string>()
            {
                "PLACE 0,0, North move ",
                "Move",
                "right",
                "Move",
                "left",
                "Report",
            };

            var listOfCommands = Commands.GenerateOutputListOfCommands(stringCommands);

            Assert.IsNotNull(listOfCommands);
            Assert.AreEqual(listOfCommands.Count, 7);

            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            foreach (var command in listOfCommands)
            {
                command.CommandAsType.Execute(robot);
                command.ReportOfRobot = (command.CommandAsType.GetType() == (new ReportCommand()).GetType()) ?
                                        ((ReportCommand)command.CommandAsType).LastReportOfRobot : "";
                Assert.IsNotNull(robot.RobotRoute);
                Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            }
            // report should be "OutPuT: 1,1, NORTH"
            Assert.AreEqual(listOfCommands[listOfCommands.Count - 1].ReportOfRobot, "OutPuT: 1,2, NORTH");
        }
Example #8
0
        public string Exec(ref ToyRobot toyRobot, TableTop tableTop)
        {
            log.Info(string.Format("Exec: {0} - {1}", toyRobot, tableTop));

            if (toyRobot.Facing == null)
            {
                throw new InvalidFacingException("The first command should be 'PLACE X,Y,F'");
            }

            else if (toyRobot.Facing.Equals(FacingEnum.NORTH) && (toyRobot.Y + 1) <= tableTop.Height)
            {
                toyRobot.Y++;
            }
            else if (toyRobot.Facing.Equals(FacingEnum.EAST) && (toyRobot.X + 1) <= tableTop.Width)
            {
                toyRobot.X++;
            }
            else if (toyRobot.Facing.Equals(FacingEnum.SOUTH) && (toyRobot.Y - 1) >= 0)
            {
                toyRobot.Y--;
            }
            else if (toyRobot.Facing.Equals(FacingEnum.WEST) && (toyRobot.X - 1) >= 0)
            {
                toyRobot.X--;
            }
            else
            {
                throw new InvalidCommandException("The toy robot can not falling from the table");
            }

            log.Info(string.Format("Exec - Result: {0}", toyRobot));

            return(null);
        }
Example #9
0
        public void TestMoveOnTable()
        {
            // Create an instance to test:
            ToyRobot robot = new ToyRobot();
            // Define a test input and output value:
            int    x      = 0;
            int    y      = 0;
            Facing facing = Facing.North;

            robot.Place(x, y, facing);

            // Run the method under test and verify test when facing North
            robot.Move();
            Assert.AreEqual(robot.X, 0);
            Assert.AreEqual(robot.Y, 1);
            Assert.AreEqual(robot.Facing, Facing.North);
            // Run the method under test and verify test when facing East
            robot.GetType().GetProperty("Facing").SetValue(robot, Facing.East);
            robot.Move();
            Assert.AreEqual(robot.X, 1);
            Assert.AreEqual(robot.Y, 1);
            Assert.AreEqual(robot.Facing, Facing.East);
            // Run the method under test and verify test when facing South
            robot.GetType().GetProperty("Facing").SetValue(robot, Facing.South);
            robot.Move();
            Assert.AreEqual(robot.X, 1);
            Assert.AreEqual(robot.Y, 0);
            Assert.AreEqual(robot.Facing, Facing.South);
            // Run the method under test and verify test when facing West
            robot.GetType().GetProperty("Facing").SetValue(robot, Facing.West);
            robot.Move();
            Assert.AreEqual(robot.X, 0);
            Assert.AreEqual(robot.Y, 0);
            Assert.AreEqual(robot.Facing, Facing.West);
        }
Example #10
0
        public void MoveRobot_MovingUpAndCheckBounds_ReturnsTrue()
        {
            var board    = new Board();
            var toyRobot = new ToyRobot();

            // placing robot at 1,2,NORTH
            toyRobot.Execute("PLACE 1,2,NORTH", board);

            // moves to 1,3
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 1);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 3);

            // moves to 1,4
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 1);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 4);

            // moves to 1,4
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 1);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 4);

            // moves to 1,4
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 1);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 4);
        }
Example #11
0
        public void MoveRobot_MovingRightAndCheckBounds_ReturnsTrue()
        {
            var board    = new Board();
            var toyRobot = new ToyRobot();

            // placing robot at 1,2,EAST
            toyRobot.Execute("PLACE 1,2,EAST", board);

            // moves to 2,2
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 2);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 2);

            // moves to 3,2
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 3);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 2);

            // moves to 4,2
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 4);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 2);

            // moves to 4,2
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 4);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 2);

            // moves to 4,2
            toyRobot.MoveForward(board);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.X, actual: 4);
            Assert.AreEqual(expected: toyRobot.CurrentPosition.Y, actual: 2);
        }
Example #12
0
        public void Move_TableTop5Into5Placed3Into3RobotMovedFiveTimesForward()
        {
            //arrange
            TableTop     tt = new TableTop(5, 5);
            ToyRobot     tr = new ToyRobot();
            CommandModel cm = new CommandModel()
            {
                Command    = Command.PLACE,
                Coordinate = new Coordinate(3, 3),
                Facing     = Facing.NORTH
            };
            ICommandInterface placeCommand = new PlaceRobotCommand(cm, tr, tt);

            placeCommand.Execute();

            ICommandInterface moveCommand = new MoveForwardCommand(tr, tt);

            //act
            moveCommand.Execute();
            moveCommand.Execute();
            moveCommand.Execute();
            moveCommand.Execute();
            moveCommand.Execute();


            //assert: actual/expected
            Assert.IsTrue(tr.RobotStillOnTableTop(tt));
        }
Example #13
0
        public void Move_TableTopFiveIntoFiveAlreadyPlacedMovedTwiceForward()
        {
            //arrange
            TableTop     tt = new TableTop(5, 5);
            ToyRobot     tr = new ToyRobot();
            CommandModel cm = new CommandModel()
            {
                Command    = Command.PLACE,
                Coordinate = new Coordinate(3, 3),
                Facing     = Facing.NORTH
            };
            ICommandInterface placeCommand = new PlaceRobotCommand(cm, tr, tt);

            placeCommand.Execute();

            ICommandInterface moveCommand = new MoveForwardCommand(tr, tt);

            //act
            moveCommand.Execute();
            moveCommand.Execute();


            //assert: actual/expected
            Assert.AreEqual(tr.GetCurrentRobotPosition(), new ToyRobot(new Coordinate(3, 5), Facing.NORTH).GetCurrentRobotPosition());
        }
Example #14
0
        public ToyRobot PlaceRobotInPosition(Position position, Direction direction)
        {
            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            robot.Place(new Route(position, direction));
            return(robot);
        }
        public void WrongCommandWouldBeIgnored()
        {
            List <string> stringCommands = new List <string>()
            {
                "Move",
                "PLACE 0,0, East  PLACE 0,20, East",
                "Turn Left",
                "Now stop",
                "Move",
                "PLACE 0,0West",
                "move right",
                "Report",
            };

            var listOfCommands = Commands.GenerateOutputListOfCommands(stringCommands);

            Assert.IsNotNull(listOfCommands);
            Assert.AreEqual(listOfCommands.Count, 7);

            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            Simulator simulator = new Simulator(robot);

            //All command with Report Result should be written in console
            simulator.Execute(listOfCommands);

            // report should be "OutPuT: 2,0, EAST"
            Assert.AreEqual(listOfCommands[listOfCommands.Count - 1].ReportOfRobot, "OutPuT: 0,2, EAST");
        }
        public void TestExecutingInTheCommandHandlerByCommandStringReturnCorrectPosAndDirectionInAToyRobot()
        {
            // Arrange
            uint width    = 5;
            uint height   = 5;
            var  table    = new Table(width, height);
            var  toyRobot = new ToyRobot(table)
            {
                X         = 0,
                Y         = 0,
                Direction = Direction.NORTH
            };
            var    commandStringSeparator = " ";
            var    ignoreCase             = true;
            string commandString          = "PLACE 1,2,NORTH MOVE MOVE RIGHT REPORT PLACE 3,2,EAST REPORT";

            // ACT
            var commandHandler = new CommandHandler(new CommandFactory(), toyRobot);

            Assert.NotNull(commandHandler);

            commandHandler.Handle(commandString, new[] { commandStringSeparator }, ignoreCase);

            var expectX         = 3;
            var expectY         = 2;
            var expectDirection = Direction.EAST;

            // Assert
            Assert.True(toyRobot.X == expectX && toyRobot.Y == expectY && toyRobot.Direction == expectDirection);
        }
Example #17
0
        public void WhenPlacingARobotInInvalidPositionsMakeSurePlaceCommandIsIgnored()
        {
            // create a robot that knows about it's environment
            var board = new Board(5, 5);
            var robot = new ToyRobot(board);

            Assert.IsNotNull(board);
            Assert.IsNotNull(robot);

            var direction = Direction.North;

            robot.Place(new Vector2d <ulong>(new Point2d <ulong>(0, 0), direction));
            Assert.IsTrue(robot.HasAPosition);
            Assert.IsTrue(ComparePosition(robot, 0, 0, direction));

            // get robot current position & direction
            var origRobotX   = robot.Vector.Position.X;
            var origRobotY   = robot.Vector.Position.Y;
            var origRobotDir = robot.Vector.Dir;
            var extraXDim    = (ulong)5;
            var extraYDim    = (ulong)5;

            // test some invalid positions
            for (var x = board.Width; x < board.Width + extraXDim; x++)
            {
                WhenPlacingARobotInInvalidPositionsMakeSurePlaceCommandIsIgnoredOnEachColumn(board, robot, direction, origRobotX, origRobotY, origRobotDir, extraYDim, x);
            }
        }
        public void TestValidationOfCommandHandlerIfNoPlaceCommandShouldBeReturnValidationFailed()
        {
            // Arrange
            uint width    = 5;
            uint height   = 5;
            var  table    = new Table(width, height);
            var  toyRobot = new ToyRobot(table)
            {
                // we didn't set up initial position and direction for the toy robot
            };
            var    commandStringSeparator = " ";
            var    ignoreCase             = true;
            string commandString          = "NORTH MOVE  REPORT"; // no PLACE command in there, which means robot IS NOT Placed

            // ACT
            var commandHandler = new CommandHandler(new CommandFactory(), toyRobot);

            Assert.NotNull(commandHandler);

            // Test key: when we use command handler it will auto validate  each command before executing command
            commandHandler.Handle(commandString, new[] { commandStringSeparator }, ignoreCase);

            // Assert
            Assert.False(toyRobot.IsPlaced);// no PLACE Command, so it will return false obviously
            Assert.False(toyRobot.X.HasValue);
            Assert.False(toyRobot.Y.HasValue);
            Assert.False(toyRobot.Direction.HasValue);
        }
Example #19
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);
        }
        public void TestExecutingACommandFileProcessingSimulatorByMultipleFilesReturnCorrectResult()
        {
            // Arrange
            uint width         = 5;
            uint height        = 5;
            var  table         = new Table(width, height);
            var  toyRobot      = new ToyRobot(table);
            var  fileExtension = ".txt";
            var  commandFileProcessingSimulator = new CommandFileProcessingSimulator(toyRobot, fileExtension);

            var commandStringSeparator = " ";
            var ignoreCase             = true;
            var fileCommandList        = new List <string>
            {
                "./TestData/example1.txt",
                "./TestData/example.txt" // TEST KEY: will always get robot position and direction following last file result
            };
            var fileCommandString = string.Join(commandStringSeparator, fileCommandList);

            //Assert
            Assert.NotNull(commandFileProcessingSimulator);

            //ACT
            commandFileProcessingSimulator.Execute(fileCommandString, new[] { commandStringSeparator }, ignoreCase);

            var expectX         = 0;
            var expectY         = 1;
            var expectDirection = Direction.NORTH;

            Assert.True(toyRobot.X == expectX && toyRobot.Y == expectY && toyRobot.Direction == expectDirection);
        }
Example #21
0
        public void WrongCommandWouldBeIgnored()
        {
            List <string> stringCommands = new List <string>()
            {
                "Move",
                "PLACE 0,0 East",
                "Turn Left",
                "Now stop",
                "PLACE 0,0, East Move",
                "PLACE 0,0West",
                "move right",
                "Report",
            };

            var listOfCommands = Commands.GenerateOutputListOfCommands(stringCommands);

            Assert.IsNotNull(listOfCommands);
            Assert.AreEqual(listOfCommands.Count, 5);

            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            foreach (var command in listOfCommands)
            {
                command.CommandAsType.Execute(robot);
                command.ReportOfRobot = (command.CommandAsType.GetType() == (new ReportCommand()).GetType()) ?
                                        ((ReportCommand)command.CommandAsType).LastReportOfRobot : "";
                Assert.IsNotNull(robot.RobotRoute);
                Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
                // report should console writeline "OutPuT: 2,0, EAST"
            }
            // report should be "OutPuT: 2,0, EAST"
            Assert.AreEqual(listOfCommands[listOfCommands.Count - 1].ReportOfRobot, "OutPuT: 2,0, SOUTH");
        }
        public void TestExecutingAnInteractiveSimulatorByCommandStringShouldReturnCorrectResult()
        {
            // Arrange
            uint width                = 5;
            uint height               = 5;
            var  table                = new Table(width, height);
            var  toyRobot             = new ToyRobot(table);
            var  interactiveSimulator = new InteractiveSimulator(toyRobot);

            var commandStringSeparator = " ";
            var ignoreCase             = true;
            var commandList            = new List <string>
            {
                "PLACE 1,2,NORTH MOVE",
                "MOVE1",          // (Test key) this command shouldn't execute
                "MOVE RIGHT",
                "PLACE 0,0,EAST", // using new place, ignoring all of previous actions
                "MOVE LEFT REPORT"
            };
            var commandString = string.Join(commandStringSeparator, commandList);

            //Assert
            Assert.NotNull(interactiveSimulator);

            //ACT
            interactiveSimulator.Execute(commandString, new[] { commandStringSeparator }, ignoreCase);

            var expectX         = 1;
            var expectY         = 0;
            var expectDirection = Direction.NORTH;

            Assert.True(toyRobot.X == expectX && toyRobot.Y == expectY && toyRobot.Direction == expectDirection);
        }
Example #23
0
        public void InterpretCommandTest_Throw()
        {
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(300, 500);
            ToyRobot robot = new ToyRobot(tb);

            // test in valid commands
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(""); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("blahblah asd ad 123e 123 312 ,1,1,,1"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE123"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 123"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 123,2,NROTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE X, ,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE ,,"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE 3xs2,xxa,SOUTH"); });
            //negative values
            AssertOutofBoundException(() => { robot.InterpretCommand("PLACE -123,-23,NORTH"); });
            //testing limits
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MaxValue},{int.MinValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MinValue},{int.MinValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MinValue},{int.MaxValue},NORTH"); });
            AssertOutofBoundException(() => { robot.InterpretCommand($"PLACE {int.MaxValue},{int.MaxValue},NORTH"); });
            //make sure interpreter throws for overflow
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand($"PLACE {int.MaxValue}3213,{int.MinValue}3213,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("PLACE     !@#!@#()_@!#(<<<mmm,,,,,,,"); });
            //out of bound
            AssertOutofBoundException(() => { robot.InterpretCommand("PLACE 4123,23,NORTH"); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("   PLACE      "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(" MOVE bassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("REPORT bassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("LEFTbassdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand(" LEFTba  s  sdasd "); });
            AssertInvalidRobotCommandException(() => { robot.InterpretCommand("RIGHT  sdsad "); });
        }
        public void TestExecutingAnInteractiveSimulatorUsingSensitiveByCommandStringShouldReturnRobotIsNotPlaced()
        {
            // Arrange
            uint width                = 5;
            uint height               = 5;
            var  table                = new Table(width, height);
            var  toyRobot             = new ToyRobot(table);
            var  interactiveSimulator = new InteractiveSimulator(toyRobot);

            var commandStringSeparator = " ";
            var ignoreCase             = false;
            var commandList            = new List <string>
            {
                "place 1,2,NORTH", // TEST KEY: place is sensitive, so it will always return validation is false before executing command
                "REPORT"
            };
            var commandString = string.Join(commandStringSeparator, commandList);

            //Assert
            Assert.NotNull(interactiveSimulator);

            //ACT
            interactiveSimulator.Execute(commandString, new[] { commandStringSeparator }, ignoreCase);

            // Assert
            Assert.False(toyRobot.IsPlaced);// no PLACE Command, so it will return false obviously
            Assert.False(toyRobot.X.HasValue);
            Assert.False(toyRobot.Y.HasValue);
            Assert.False(toyRobot.Direction.HasValue);
        }
Example #25
0
        public void ToyRobot_internal_TurnTest()
        {
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(100, 500);
            ToyRobot robot = new ToyRobot(tb);

            PlaceRobotTest(robot, new CoordinateXY(55, 45), Direction.NORTH);
            Assert.AreEqual(robot.Report(), "55,45,NORTH");
            TestLocation(robot, new CoordinateXY(55, 45), Direction.NORTH);
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.WEST);
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.SOUTH);
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.EAST);
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.NORTH);
            robot.Turn(false);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.EAST);
            Assert.AreEqual(robot.Report(), "55,45,EAST");
            robot.Turn(false);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.SOUTH);
            robot.Turn(false);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.WEST);
            robot.Turn(false);
            TestLocation(robot, new CoordinateXY(55, 45), Direction.NORTH);
            Assert.AreEqual(robot.Report(), "55,45,NORTH");
        }
        public void TestExecutingACommandFileProcessingSimulatorByOneFileReturnCorrectResult()
        {
            // Arrange
            uint width         = 5;
            uint height        = 5;
            var  table         = new Table(width, height);
            var  toyRobot      = new ToyRobot(table);
            var  fileExtension = ".txt";
            var  commandFileProcessingSimulator = new CommandFileProcessingSimulator(toyRobot, fileExtension);

            var commandStringSeparator = " ";
            var ignoreCase             = true;
            var fileCommandList        = new List <string>
            {
                "./TestData/example1.txt"
            };
            var fileCommandString = string.Join(commandStringSeparator, fileCommandList);

            //Assert
            Assert.NotNull(commandFileProcessingSimulator);

            //ACT
            commandFileProcessingSimulator.Execute(fileCommandString, new[] { commandStringSeparator }, ignoreCase);

            var expectX         = 4;
            var expectY         = 2;
            var expectDirection = Direction.SOUTH;

            Assert.True(toyRobot.X == expectX && toyRobot.Y == expectY && toyRobot.Direction == expectDirection);
        }
Example #27
0
        public SFMLSimulator()
        {
            window = new RenderWindow(new VideoMode(800, 600), "SFML Simulator");
            window.SetView(new View(new FloatRect(0, 0, size * width, size * height)));
            window.Closed += Window_Closed;
            window.MouseButtonReleased += Window_MouseButtonReleased;
            window.KeyReleased         += Window_KeyReleased;
            toyRobot = new ToyRobot();
            map      = new Map(width, height);
            grass    = new Sprite[width, height];

            Image   grassImage   = new Image("./Resources/grass.png");
            Texture grassTexture = new Texture(grassImage);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    grass[x, y] = new Sprite(grassTexture)
                    {
                        Position = new Vector2f((x * size), (y * size))
                    };
                }
            }

            Image   robotImage   = new Image("./Resources/robot.png");
            Texture robotTexture = new Texture(robotImage);

            robot = new Sprite(robotTexture)
            {
                Origin = new Vector2f(8, 8)
            };
        }
        public void CorrectListOfCommandWouldReturnCorrectOutput()
        {
            List <string> stringCommands = new List <string>()
            {
                "PLACE 0,0, North",
                "Move",
                "right PLACE 1,1, North right",
                "Move Move ",
                "left",
                "Report",
            };

            var listOfCommands = Commands.GenerateOutputListOfCommands(stringCommands);

            Assert.IsNotNull(listOfCommands);
            Assert.AreEqual(listOfCommands.Count, 9);

            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            Simulator simulator = new Simulator(robot);

            //All command with Report Result should be written in console
            simulator.Execute(listOfCommands);
            // report should be "OutPuT: 1,1, NORTH"
            Assert.AreEqual(listOfCommands[listOfCommands.Count - 1].ReportOfRobot, "OutPuT: 3,1, NORTH");
        }
        // combining all actions of robots to make sure it returns expect result
        public void TestAToyRobotWithAllActionsShouldBeReturnCorrectPosAndDirection()
        {
            // Arrange
            uint width  = 5;
            uint height = 5;
            var  table  = new Table(width, height);

            // initializing position for a toy robot
            var toyRobot = new ToyRobot(table);

            // add PLACE action
            toyRobot.Place(1, 1, Direction.NORTH);

            // add three move actions
            toyRobot.Move();
            toyRobot.Move();
            toyRobot.Move();

            // then turn right
            toyRobot.TurnRight();

            // then turn left
            toyRobot.TurnLeft();

            // Testing key point,  just move once again, should ignore the Move step, because robot position beyond table height
            toyRobot.Move();

            var expectXPostion  = 1;
            var expectYPostion  = 4;
            var expectDirection = Direction.NORTH;

            // Assert
            Assert.True(toyRobot.X == expectXPostion && toyRobot.Y == expectYPostion && toyRobot.Direction == expectDirection);
        }
Example #30
0
        public void TheRobotCouldWalkThroughTheBoardSurfaceFromSouthToNorthAndReturnAndReportLastPosition()
        {
            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

            //walk from start position 0,0 face to NORTH, move to the edge of NORTH
            //then turn Right and one move right,
            // agian turn Right to face back SOUTH and move to the edge of SOUTH
            // continue this pattern to seek all the board surface to position 5,0
            robot.Place(new Route(new Position(0, 0), Direction.NORTH));

            for (int horizontalStep = 0; horizontalStep <= 5; horizontalStep++)
            {
                Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
                for (int verticalStep = 1; verticalStep <= 5; verticalStep++)
                {
                    robot.Move();
                    Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
                }

                if (robot.RobotRoute.RobotFaceDirection == Direction.NORTH)
                {
                    robot.TurnRight(); robot.Move(); robot.TurnRight(); continue;
                }
                if (robot.RobotRoute.RobotFaceDirection == Direction.SOUTH)
                {
                    robot.TurnLeft(); robot.Move(); robot.TurnLeft();
                }
            }
            // the report should be "OutPuT: 5,0, NORTH"
            Assert.AreEqual(robot.Report(), "OutPuT: 5,0, NORTH");
            Console.WriteLine(robot.Report());
        }