Ejemplo n.º 1
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");
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
0
        public void TheRobotCouldWalkThroughTheBoardSurfaceFromEastToWestAndReturnAndReportLastPosition()
        {
            ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5

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

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

                if (robot.RobotRoute.RobotFaceDirection == Direction.EAST)
                {
                    robot.TurnLeft(); robot.Move(); robot.TurnLeft(); continue;
                }
                if (robot.RobotRoute.RobotFaceDirection == Direction.WEST)
                {
                    robot.TurnRight(); robot.Move(); robot.TurnRight();
                }
            }
            // The report should be "OutPuT: 0,5, EAST"
            Assert.AreEqual(robot.Report(), "OutPuT: 0,5, EAST");
            Console.WriteLine(robot.Report());
        }
Ejemplo n.º 4
0
        public void IfRobotNotPlacedCorrectlyAtFirstTimeReportAnsweredIsNotPlacedProperly()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(10, 2), Direction.NORTH);

            Assert.IsNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);
            //The report should be empty
            Assert.AreEqual(robot.Report(), "");
            Console.WriteLine(robot.Report());
        }
Ejemplo n.º 5
0
        public void IfRobotPlaceCorrectlyTheReportAnswerTheCorrectPosition()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(1, 2), Direction.NORTH);

            Assert.IsNotNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            //The report should be "OutPuT: 1,2, NORTH"
            Assert.AreEqual(robot.Report(), "OutPuT: 1,2, NORTH");
            Console.WriteLine(robot.Report());
        }
Ejemplo n.º 6
0
        public void Discard_Report_command_when_the_robot_was_not_placed_on_the_table()
        {
            var toyRobot = new ToyRobot();

            var result = toyRobot.Report();

            result.Should().Be(null);
        }
Ejemplo n.º 7
0
        public void IfRobotPlacedCorrectlyAtFirstTimeButNotCorrectlyAfterReportShouldShowLastPosition()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(1, 2), Direction.NORTH);

            Assert.IsNotNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            robot.Move();
            //This wrong Place will not set
            robot.Place(new Route(new Position(10, 2), Direction.NORTH));
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);

            //The report should be Last Position "OutPuT: 1,3, NORTH"
            Assert.AreEqual(robot.Report(), "OutPuT: 1,3, NORTH");
            Console.WriteLine(robot.Report());
            robot.Move();
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            Assert.AreEqual(robot.Report(), "OutPuT: 1,4, NORTH");
            Console.WriteLine(robot.Report());
        }
Ejemplo n.º 8
0
        public void Return_position_when_it_is_valid(int x, int y, string direction, string report)
        {
            var toyRobot = new ToyRobot();

            toyRobot.Place(x, y, direction);

            var result = toyRobot.Report();

            result.Should().Be(report);
        }
Ejemplo n.º 9
0
        public void Execute(Command command, ToyRobot toyRobot)
        {
            if (_userInteractionService.ClearScreenIfToyRobotIsDeactive(toyRobot))
            {
                return;
            }

            string reportText = toyRobot.Report(); // Perform related action on the Toy Robot

            _userInteractionService.PrintText($"\n\n{reportText}\n");
        }
Ejemplo n.º 10
0
 public void RunAndValidate(ToyRobot robot)
 {
     foreach (string c in Commands)
     {
         try
         {
             robot.InterpretCommand(c);
         }
         // robot exceptions are count in the console app, therefore catch them here
         catch (RobotException) { }
     }
     Assert.AreEqual(robot.Report(), this.ExpectedResult);
 }
Ejemplo n.º 11
0
        public void TestReportNoValidPlaceCommandExecutedException()
        {
            // Create an instance to test:
            ToyRobot robot = new ToyRobot();

            // Run the method under test:
            try
            {
                robot.Report();
                Assert.Fail("A valid Place command needs to be executed first");
            }
            catch (Exception ex)
            {
                Assert.AreEqual(typeof(NoValidPlaceCommandExecutedException), ex.GetType(), ex.Message);
            }
        }
Ejemplo n.º 12
0
        public void ToyRobot_internal_MoveTest()
        {
            ToyTable tb    = ToyTableTests.ToyTableCreate_successtest(100, 500);
            ToyRobot robot = new ToyRobot(tb);

            robot.Place(new CoordinateXY(55, 45), Direction.NORTH);
            // test move in different directions
            robot.Move();
            TestLocation(robot, new CoordinateXY(55, 46), Direction.NORTH);
            robot.Turn(true);
            robot.Move();
            robot.Move();
            TestLocation(robot, new CoordinateXY(53, 46), Direction.WEST);
            robot.Turn(true);
            robot.Move();
            robot.Move();
            TestLocation(robot, new CoordinateXY(53, 44), Direction.SOUTH);
            robot.Turn(true);
            robot.Move();
            robot.Move();
            TestLocation(robot, new CoordinateXY(55, 44), Direction.EAST);
            Assert.AreEqual(robot.Report(), "55,44,EAST");

            // test out of bound movements
            robot.Place(new CoordinateXY(0, 0), Direction.NORTH);
            robot.Turn(true);
            AssertOutofBoundException(() => { robot.Move(); });
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(0, 0), Direction.SOUTH);
            AssertOutofBoundException(() => { robot.Move(); });
            robot.Turn(true);
            robot.Move();
            robot.Move();
            TestLocation(robot, new CoordinateXY(2, 0), Direction.EAST);
            robot.Turn(true);
            TestLocation(robot, new CoordinateXY(2, 0), Direction.NORTH);

            // move 499 times north to the edge.
            for (int i = 0; i < 499; i++)
            {
                robot.Move();
                TestLocation(robot, new CoordinateXY(2, i + 1), Direction.NORTH);
            }
            // next move sound be out of bound.
            AssertOutofBoundException(() => { robot.Move(); });
            TestLocation(robot, new CoordinateXY(2, 499), Direction.NORTH);
        }
Ejemplo n.º 13
0
        public void XmlTestCases()
        {
            // read data from row in data.xml
            string description    = (string)TestContext.DataRow["Description"];
            string expectedOutput = (string)TestContext.DataRow["Expected"];
            string allCommands    = (string)TestContext.DataRow["Commands"];

            // interpret user input - this avoids duplicating the XML
            // test data in a more explicit form
            foreach (string cmd in allCommands.Split(LineEnds, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!String.IsNullOrWhiteSpace(cmd))
                {
                    string normalisedCmd = cmd.Trim().ToUpperInvariant();
                    if (normalisedCmd.StartsWith("PLACE"))
                    {
                        string   args   = normalisedCmd.Substring(5).Trim();
                        string[] tokens = args.Split(CommaOrSpace, StringSplitOptions.RemoveEmptyEntries);
                        if (tokens != null && tokens.Length == 3)
                        {
                            if (Int32.TryParse(tokens[0], out int x) &&
                                Int32.TryParse(tokens[1], out int y) &&
                                Enum.TryParse <Direction>(tokens[2].ToUpperInvariant(), out Direction facing))
                            {
                                robot.Place(x, y, facing, tableTop);
                            }
                        }
                    }
                    else if (normalisedCmd.StartsWith("LEFT"))
                    {
                        robot.Left();
                    }
                    else if (normalisedCmd.StartsWith("RIGHT"))
                    {
                        robot.Right();
                    }
                    else if (normalisedCmd.StartsWith("MOVE"))
                    {
                        robot.Move();
                    }
                }
            }

            // check output
            Assert.AreEqual(expectedOutput, robot.Report(), description);
        }
Ejemplo n.º 14
0
        public void TestReportWhenCorrectlyPlaced()
        {
            // 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:
            string location = robot.Report();

            // Verify the result:
            Assert.AreEqual("0,0,NORTH", location);
        }
        public void Report_PrintPositionAndDirection_CheckConsoleWrapperIsCalled()
        {
            //Arrange
            var currentPosition = new Position(0, 0);
            ICardinalDirectionManager facingManager = new NorthManager();
            var reportManagerMock = new Mock <IReportManager>();

            reportManagerMock.Setup(reportManager => reportManager.Report(It.IsAny <ToyRobotState>()));

            var toyRobotManager = new ToyRobot(currentPosition, facingManager, reportManagerMock.Object);

            //Act
            toyRobotManager.Report();

            //Assert
            reportManagerMock.Verify(reportManager => reportManager.Report(It.IsAny <ToyRobotState>()), Times.Once());
        }
Ejemplo n.º 16
0
        public void ToyRobot_internal_tests()
        {
            try
            {
                // when table is null
                ToyRobot failbot = new ToyRobot(null);
            }
            catch (ArgumentException) { }

            // create table and robot instance
            ToyTable tb = ToyTableTests.ToyTableCreate_successtest(522, 522);

            Assert.IsNotNull(tb);
            ToyRobot robot = new ToyRobot(tb);

            Assert.IsNotNull(robot);

            // running commands before robot is placed
            AssertInvalidRobotCommandException(() => { robot.Report(); });
            AssertInvalidRobotCommandException(() => { robot.Move(); });
            AssertInvalidRobotCommandException(() => { robot.Turn(true); });
            AssertInvalidRobotCommandException(() => { robot.Turn(false); });
            // placing robot at invalid locations
            AssertOutofBoundException(() => { robot.Place(new CoordinateXY(0, -1)); });
            AssertOutofBoundException(() => { robot.Place(new CoordinateXY(0, 522)); });
            AssertOutofBoundException(() => { robot.Place(new CoordinateXY(521, 522)); });
            AssertOutofBoundException(() => { robot.Place(new CoordinateXY(524, 522)); });
            AssertOutofBoundException(() => { robot.Place(new CoordinateXY(-14, -1)); });

            // place robot tests
            PlaceRobotTest(robot, new CoordinateXY(0, 0), Direction.NORTH);
            PlaceRobotTest(robot, new CoordinateXY(0, 0), Direction.SOUTH);
            PlaceRobotTest(robot, new CoordinateXY(0, 0), Direction.EAST);
            robot.Report();
            PlaceRobotTest(robot, new CoordinateXY(0, 22), Direction.WEST);
            PlaceRobotTest(robot, new CoordinateXY(521, 521), Direction.WEST);
            robot.Report();
            PlaceRobotTest(robot, new CoordinateXY(520, 21), Direction.NORTH);
            PlaceRobotTest(robot, new CoordinateXY(0, 24), Direction.SOUTH);
            PlaceRobotTest(robot, new CoordinateXY(55, 0), Direction.EAST);
            Assert.AreEqual(robot.Report(), "55,0,EAST");
            Assert.AreEqual(robot.Report(), "55,0,EAST"); //report multiple times
            Assert.AreEqual(robot.Report(), "55,0,EAST"); //report multiple times
        }
Ejemplo n.º 17
0
 public void Execute(string commandText, ToyRobot toyRobot)
 {
     _userInteractionByTextService.PrintText($"   {toyRobot.Report()}\n");
 }
Ejemplo n.º 18
0
        public void Return_StateOfTheToyRobot()
        {
            var toyRobot = new ToyRobot(0, 0, Direction.East);

            Assert.That(toyRobot.Report(), Is.EqualTo("Output: 0,0,EAST"));
        }
Ejemplo n.º 19
0
        static void Main(string[] args)
        {
            try
            {
                ToyRobot tRobot1 = new ToyRobot(5, 5);

                Console.WriteLine("PLACE (0,0,NORTH)");
                tRobot1.Place(0, 0, Directions.North);

                Console.WriteLine(tRobot1.Report());

                Console.WriteLine("MOVE");
                tRobot1.Move();

                Console.WriteLine(tRobot1.Report());

                Console.WriteLine();
                Console.WriteLine();

                ToyRobot tRobot2 = new ToyRobot(5, 5);

                Console.WriteLine("PLACE (0,0,NORTH)");
                tRobot2.Place(0, 0, Directions.North);

                Console.WriteLine(tRobot2.Report());

                Console.WriteLine("MOVE");
                tRobot2.Left();

                Console.WriteLine(tRobot2.Report());

                Console.WriteLine();
                Console.WriteLine();

                ToyRobot tRobot3 = new ToyRobot(5, 5);

                Console.WriteLine("PLACE (1,2,EAST)");
                tRobot3.Place(1, 2, Directions.East);

                Console.WriteLine(tRobot3.Report());

                Console.WriteLine("MOVE");
                tRobot3.Move();

                Console.WriteLine(tRobot3.Report());

                Console.WriteLine("MOVE");
                tRobot3.Move();

                Console.WriteLine(tRobot3.Report());

                Console.WriteLine("LEFT");
                tRobot3.Left();

                Console.WriteLine(tRobot3.Report());

                Console.WriteLine("MOVE");
                tRobot3.Move();

                Console.WriteLine(tRobot3.Report());

                Console.WriteLine();
                Console.WriteLine();

                Console.ReadKey();
            }
            catch (Exception)
            {
            }
        }