Exemple #1
0
        public void Movement_Service_Start_With_Initialize_Should_Return_Correct_Result2()
        {
            var moveAreaMock = new Mock <IMoveArea <int> >();

            moveAreaMock.SetupGet(s => s.Width).Returns(10);
            moveAreaMock.SetupGet(s => s.Height).Returns(10);
            var robo    = new Robo <int, int>(moveAreaMock.Object);
            var service = new MovementService <int, int>(robo);

            robo.Step = 1;

            service.Initialize("PLACE 1,2,EAST");
            service.ProcessCommand("MOVE");
            service.ProcessCommand("MOVE");
            service.ProcessCommand("LEFT");
            service.ProcessCommand("MOVE");

            var expectedDirection = DirectionFactory <int, int> .Create("NORTH");

            robo.Direction.GetType().Should().BeSameAs(expectedDirection.GetType());
            robo.Position.X.Should().Be(3);
            robo.Position.Y.Should().Be(3);

            service.ProcessCommand("MOVE");
            robo.Position.X.Should().Be(3);
            robo.Position.Y.Should().Be(4);
        }
Exemple #2
0
        public void Action_Compute_Cordinate_Should_Return_Same_Cordinate(string currentDirection, string command, string expectedDirection)
        {
            var currDirection = DirectionFactory <int, int> .Create(currentDirection);

            var action = ActionFactory <int, int> .Create(command);

            var expectDirection = DirectionFactory <int, int> .Create(expectedDirection);

            var result = currDirection.NextFacing(action);

            result.GetType().Should().BeSameAs(expectDirection.GetType());
        }
Exemple #3
0
        public Robot Parse(string inputString)
        {
            Match match = regex.Match(inputString);

            if (!match.Success)
            {
                throw new ArgumentException("The robot position should be '<X> <Y> <direction>' (ex: '1 2 N')");
            }
            Robot initRobot = new Robot(generateId(),
                                        new Coordinate(int.Parse(match.Groups["x"].Value), int.Parse(match.Groups["y"].Value)),
                                        DirectionFactory.Create(match.Groups["direction"].Value), getArena(), true);

            return(initRobot);
        }
Exemple #4
0
        public void Movement_Service_Initialize_Should_Initialize_Correctly(string command, int x, int y, string direction)
        {
            var moveAreaMock = new Mock <IMoveArea <int> >();

            moveAreaMock.SetupGet(s => s.Width).Returns(10);
            moveAreaMock.SetupGet(s => s.Height).Returns(10);
            var robo              = new Robo <int, int>(moveAreaMock.Object);
            var service           = new MovementService <int, int>(robo);
            var expectedDirection = DirectionFactory <int, int> .Create(direction);

            service.Initialize(command);

            robo.Direction.GetType().Should().BeSameAs(expectedDirection.GetType());
            robo.Position.X.Should().Be(x);
            robo.Position.Y.Should().Be(y);
        }
Exemple #5
0
        public void Robo_Process_Action_Should_Return_Correct_Result(string command, int x, int y, string direction)
        {
            var moveAreaMock = new Mock <IMoveArea <int> >();

            moveAreaMock.SetupGet(s => s.Width).Returns(10);
            moveAreaMock.SetupGet(s => s.Height).Returns(10);

            var robo = new Robo <int, int>(moveAreaMock.Object);

            robo.ProcessAction(command);

            var expetedDirection = DirectionFactory <int, int> .Create(direction);

            robo.Direction.GetType().Should().BeSameAs(expetedDirection.GetType());
            robo.Position.X.Should().Be(x);
            robo.Position.Y.Should().Be(y);
        }
Exemple #6
0
        private void ProcessInitialPlacement(string command)
        {
            var commandSegments = RoboHelper.GetCommandSegments(command);

            //this will throw exception if the commads are invalid
            //This is done this way so that the detail error messages are captured for ease of user
            RoboHelper.IsCommandLineArgumentsValid(commandSegments);

            //Check if the original cordinate is valid
            RoboHelper.IsCordinateValid <T>(commandSegments, MoveArea);

            //set the initial position
            Position = new Cordinate <T>
            {
                X = (T)Convert.ChangeType(commandSegments.X, typeof(T)),
                Y = (T)Convert.ChangeType(commandSegments.Y, typeof(T))
            };
            //set the initial direction
            Direction = DirectionFactory <T, U> .Create(commandSegments.Direction);
        }
Exemple #7
0
        public void Horizontal_East_Move_Action_Compute_Cordinate_Should_Return_Correct_New_Cordinate(string command, string directi, int x, int y, int exX, int exY, int step)
        {
            var moveAreaMock = new Mock <IMoveArea <int> >();

            moveAreaMock.SetupGet(s => s.Width).Returns(10);
            moveAreaMock.SetupGet(s => s.Height).Returns(10);

            var direction = DirectionFactory <int, int> .Create(directi);

            var action = ActionFactory <int, int> .Create(command);

            var cordinate = new Cordinate <int>
            {
                X = x,
                Y = y
            };

            var result = action.ComputeNewCordinate(cordinate, step, moveAreaMock.Object, direction);

            result.X.Should().Be(exX);
            result.Y.Should().Be(exY);
        }
        public void Direction_Factory_Should_Throw_ArgumentException_Exception(string command, string exceptionMessage)
        {
            Action act = () => DirectionFactory <int, int> .Create(command);

            act.Should().Throw <ArgumentException>().WithMessage(exceptionMessage);
        }
        public void Direction_Factory_Should_Create_Correct_Direction_Object(string command, Type type)
        {
            var direction = DirectionFactory <int, int> .Create(command);

            direction.Should().BeOfType(type);
        }
Exemple #10
0
        public string ProcessAction(string command)
        {
            var commandSegments = RoboHelper.GetCommandSegments(command);

            //check
            if (commandSegments != null)
            {
                //this will throw exception if the commads are invalid
                //This is done this way so that the detail error messages are captured for ease of user
                RoboHelper.IsCommandLineArgumentsValid(commandSegments);

                //Check if the original cordinate is valid
                RoboHelper.IsCordinateValid <T>(commandSegments, MoveArea);

                //set the new position
                Position = new Cordinate <T>
                {
                    X = (T)Convert.ChangeType(commandSegments.X, typeof(T)),
                    Y = (T)Convert.ChangeType(commandSegments.Y, typeof(T))
                };
                //set the new direction
                Direction = DirectionFactory <T, U> .Create(commandSegments.Direction);
            }
            else
            {
                if (RoboHelper.IsValidReportAction(command))
                {
                    return(new Report <T, U>().GetOutput(Position, Direction));
                }

                if (RoboHelper.IsPlaceExist(new string[] { command }))
                {
                    throw new ArgumentException($"PLACE command missing arguments");
                }

                if (!RoboHelper.IsValidMoveAction(command))
                {
                    throw new ArgumentException($"Invalid command line arguments");
                }

                if (Direction == null)
                {
                    throw new InvalidOperationException("Robo unable to perform this command without initial direction");
                }

                if (Position == null)
                {
                    throw new InvalidOperationException("Robo unable to perform this command without initial position");
                }

                //create the action
                var action = ActionFactory <T, U> .Create(command);

                //get the new direction
                Direction = Direction.NextFacing(action);
                //calculate the new position
                Position = action.ComputeNewCordinate(Position, Step, MoveArea, Direction);
            }

            return(string.Empty);
        }
 public void Create_should_throw_ArgumentException_when_directions_is_unknown()
 {
     Assert.Throws <ArgumentException>(() => DirectionFactory.Create("k"));
 }
 public void Create_should_create_the_right_Direction(string direction, Type expectedType)
 {
     DirectionFactory.Create(direction).GetType()
     .Should().Be.EqualTo(expectedType);
 }