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()); }
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()); }
// 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); }
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); }
public void TestMoveRobotWillFallException() { // Create an instance to test: ToyRobot robot = new ToyRobot(); // Define a test input and output value: int x = 4; int y = 4; Facing facing = Facing.North; robot.Place(x, y, facing); // Run the method under test and verify test when facing North try { robot.Move(); Assert.Fail("Robot will fall"); } catch (Exception ex) { Assert.AreEqual(typeof(RobotWillFallException), ex.GetType(), ex.Message); } // Run the method under test and verify test when facing East robot.GetType().GetProperty("Facing").SetValue(robot, Facing.East); try { robot.Move(); Assert.Fail("Robot will fall"); } catch (Exception ex) { Assert.AreEqual(typeof(RobotWillFallException), ex.GetType(), ex.Message); } // Run the method under test and verify test when facing South robot.GetType().GetProperty("Facing").SetValue(robot, Facing.South); robot.GetType().GetProperty("Y").SetValue(robot, 0); try { robot.Move(); Assert.Fail("Robot will fall"); } catch (Exception ex) { Assert.AreEqual(typeof(RobotWillFallException), ex.GetType(), ex.Message); } // Run the method under test and verify test when facing West robot.GetType().GetProperty("Facing").SetValue(robot, Facing.West); robot.GetType().GetProperty("X").SetValue(robot, 0); try { robot.Move(); Assert.Fail("Robot will fall"); } catch (Exception ex) { Assert.AreEqual(typeof(RobotWillFallException), ex.GetType(), ex.Message); } }
public void WhenSpanningARobotLeftRightBottomTopOverTheBoardDimensionsMakeSureMovesAreGood() { // 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); // lets do a left to right bottom to top, top to bottom zig zag of the board robot.Place(new Vector2d <ulong>(new Point2d <ulong>(0, 0), Direction.North)); Assert.IsTrue(ComparePosition(robot, 0, 0, Direction.North)); // left to right for (var x = (ulong)0; x < board.Width; x++) { MoveRobotUpTheBoardFromTheBottomPosition(board, robot); if (x == board.Width - 1) { // at the end, can't do a u turn break; } if (robot.Vector.Dir == Direction.North) { // u turn to next row down robot.TurnRight(); robot.Move(); robot.TurnRight(); continue; } if (robot.Vector.Dir == Direction.South) { // u turn to next row down robot.TurnLeft(); robot.Move(); robot.TurnLeft(); continue; } } Assert.IsTrue(ComparePosition(robot, board.Width - 1, board.Width % 2 == 0 ? 0 : board.Height - 1, board.Width % 2 == 0 ? Direction.South : Direction.North)); }
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()); }
public void Discard_Move_command_when_the_robot_was_not_placed_on_the_table() { var toyRobot = new ToyRobot(); toyRobot.Move(); toyRobot.X.Should().Be(null); toyRobot.Y.Should().Be(null); toyRobot.Facing.Should().Be(null); }
public void Move_West_when_facing_West() { var toyRobot = new ToyRobot(); toyRobot.Place(2, 2, Direction.West); toyRobot.Move(); toyRobot.X.Should().Be(1); toyRobot.Y.Should().Be(2); }
public void TheRobotMovementToWrongXPositionWillBeIgnored() { ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5 //Place Robot At the edge of east, face to EAST robot.Place(new Route(new Position(5, 0), Direction.EAST)); robot.Move(); Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot); //Robot stayed at the last correct position Assert.AreEqual(robot.RobotRoute.RobotPosition.X, 5); }
public void Not_cause_the_robot_to_fall_outside_the_table(int x, int y, string direction) { var toyRobot = new ToyRobot(); toyRobot.Place(x, y, direction); toyRobot.Move(); toyRobot.X.Should().Be(x); toyRobot.Y.Should().Be(y); }
public void Move_North_when_facing_North() { var toyRobot = new ToyRobot(); toyRobot.Place(2, 2, Direction.North); toyRobot.Move(); toyRobot.X.Should().Be(2); toyRobot.Y.Should().Be(3); }
public void Execute(Command command, ToyRobot toyRobot) { if (_userInteractionService.ClearScreenIfToyRobotIsDeactive(toyRobot)) { return; } toyRobot.Move(); // Perform related action on the Toy Robot _userInteractionService.PrintCommandExecuted(nameof(Command.Move)); }
public void MoveToyRobotFacingNorth_OneUnitForwardInNorth() { var toyRobot = new ToyRobot( xCoordinate: Boundary.XMin, yCoordinate: Boundary.YMin, facing: Direction.North ); toyRobot.Move(); Assert.That(toyRobot.Position.YCoordinate, Is.EqualTo(Boundary.YMin + 1)); Assert.That(toyRobot.Position.XCoordinate, Is.EqualTo(Boundary.XMin)); Assert.That(toyRobot.Position.Facing, Is.EqualTo(Direction.North)); }
public void TheRobotMovementToWrongYPositionWillBeIgnored() { Board board = new Board();//Default Borad is 5 x 5 ToyRobot robot = new ToyRobot(board); //Place Robot At the edge of NORTH, face to NORTH robot.Place(new Route(new Position(0, 5), Direction.NORTH)); robot.Move(); Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot); //Robot stayed at the last correct position Assert.AreEqual(robot.RobotRoute.RobotPosition.Y, 5); Assert.IsTrue(board.IsValidPosition(robot.RobotRoute.RobotPosition)); }
public void NotMoveToyRobotFacingWest_IfNextStepIsOutOfBounds() { var toyRobot = new ToyRobot( xCoordinate: Boundary.XMin, yCoordinate: Boundary.YMin, facing: Direction.West ); toyRobot.Move(); Assert.That(toyRobot.Position.XCoordinate, Is.EqualTo(Boundary.XMin)); Assert.That(toyRobot.Position.YCoordinate, Is.EqualTo(Boundary.YMin)); Assert.That(toyRobot.Position.Facing, Is.EqualTo(Direction.West)); }
public void MoveToyRobotFacingWest_OneUnitForwardInWest() { var toyRobot = new ToyRobot( xCoordinate: Boundary.XMax, yCoordinate: Boundary.YMin, facing: Direction.West ); toyRobot.Move(); Assert.That(toyRobot.Position.XCoordinate, Is.EqualTo(Boundary.XMax - 1)); Assert.That(toyRobot.Position.YCoordinate, Is.EqualTo(Boundary.YMin)); Assert.That(toyRobot.Position.Facing, Is.EqualTo(Direction.West)); }
public void Move_ToNorth_IncreaseYPosition() { //Arrange var currentPosition = new Position(0, 0); var facingManagerMock = new Mock <ICardinalDirectionManager>(); facingManagerMock.Setup(facingManager => facingManager.TryMoveAhead(currentPosition)).Returns(new Position(0, 1)); var toyRobotManager = new ToyRobot(currentPosition, facingManagerMock.Object, new ReportManager()); //Act Position finalPosition = toyRobotManager.Move(); //Assert Assert.AreEqual(0, finalPosition.X); Assert.AreEqual(1, finalPosition.Y); }
public void TestMoveNoValidPlaceCommandExecutedException() { // Create an instance to test: ToyRobot robot = new ToyRobot(); // Run the method under test: try { robot.Move(); Assert.Fail("A valid Place command needs to be executed first"); } catch (Exception ex) { Assert.AreEqual(typeof(NoValidPlaceCommandExecutedException), ex.GetType(), ex.Message); } }
public void TheRobotMovementFromAllCorrectXPositionToNorth() { ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5 for (int x = 0; x <= 5; x++) { //Place the Robot on button SOUTH, Face to NORTH robot.Place(new Route(new Position(x, 0), Direction.NORTH)); for (int y = 1; y <= 5; y++) { robot.Move(); Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand); Assert.AreEqual(robot.RobotRoute.RobotPosition.Y, y); } Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand); } }
[InlineData(0, 0, Direction.SOUTH, 0, 0)] // this is key, because robot has no way can be move to public void TestMovingAToyRobotShouldBeMoveToCorrectPosition(uint xPosition, uint yPosition, Direction direction, uint expectXPosition, uint expectYPosition) { // Arrange uint width = 5; uint height = 5; var table = new Table(width, height); var toyRobot = new ToyRobot(table); toyRobot.Place(xPosition, yPosition, direction); // ACT toyRobot.Move(); // make sure robot moved correctly Assert.True(toyRobot.X == expectXPosition); Assert.True(toyRobot.Y == expectYPosition); }
public void TheRobotMovementFromAllCorrectYPositionToWest() { ToyRobot robot = new ToyRobot(new Board());//Default Borad is 5 x 5 for (int y = 0; y <= 5; y++) { //Place the Robot on Right EAST, Face to WEST robot.Place(new Route(new Position(5, y), Direction.WEST)); for (int x = 4; x >= 0; x--) { robot.Move(); Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand); Assert.AreEqual(robot.RobotRoute.RobotPosition.X, x); } Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand); } }
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); }
private void Window_KeyReleased(object sender, KeyEventArgs e) { if (e.Code == Keyboard.Key.Right) { toyRobot.Right(); } if (e.Code == Keyboard.Key.Left) { toyRobot.Left(); } if (e.Code == Keyboard.Key.Up) { toyRobot.Move(); } }
public void TestValidMoveCommand() { // Arrange ToyRobot tRobot = new ToyRobot(5, 5); tRobot.Place(0, 0, Directions.North); // Act tRobot.Move(); // Assert Assert.AreEqual(Directions.North, tRobot.F); Assert.AreEqual(0, tRobot.X); Assert.AreEqual(1, tRobot.Y); }
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 }
public void TestPlaceAsFirstCommand() { // Arrange ToyRobot tRobot = new ToyRobot(5, 5); try { // Act tRobot.Move(); } catch (InvalidCommandException) { } // Assert Assert.AreEqual(false, tRobot.Placed); }
public void TestInvalidMoveCommand() { // Arrange ToyRobot tRobot = new ToyRobot(5, 5); tRobot.Place(0, 4, Directions.North); try { // Act tRobot.Move(); } catch (InvalidCommandException) { } // Assert Assert.AreEqual(Directions.North, tRobot.F); Assert.AreEqual(0, tRobot.X); Assert.AreEqual(4, tRobot.Y); }
public void WhenMovingARobotUpAndDownWithinTheBoardDimensionsMakeSureMovesAreGood() { // 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); // lets place it in the middle facing north robot.Place(new Vector2d <ulong>(new Point2d <ulong>(2, 2), Direction.North)); Assert.IsTrue(ComparePosition(robot, 2, 2, Direction.North)); // lets move it up, turn around, move down to edge, turn around and move back up // move to 2,3 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 3, Direction.North)); // move to 2,4 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 4, Direction.North)); // turn 90 degrees right robot.TurnRight(); Assert.IsTrue(ComparePosition(robot, 2, 4, Direction.East)); // turn 90 degrees right again robot.TurnRight(); Assert.IsTrue(ComparePosition(robot, 2, 4, Direction.South)); // move to 2,3 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 3, Direction.South)); // move to 2,2 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 2, Direction.South)); // move to 2,1 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 1, Direction.South)); // move to 2,0 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 0, Direction.South)); // turn 90 degrees left robot.TurnLeft(); Assert.IsTrue(ComparePosition(robot, 2, 0, Direction.East)); // turn 90 degrees left again robot.TurnLeft(); Assert.IsTrue(ComparePosition(robot, 2, 0, Direction.North)); // move to 2,1 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 1, Direction.North)); // move to 2,2 robot.Move(); Assert.IsTrue(ComparePosition(robot, 2, 2, Direction.North)); }
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); }