コード例 #1
0
        public void ExecuteCommands(string[] commandTexts)
        {
            if (commandTexts.Length >= 3 && commandTexts.Length % 2 == 1)
            {
                _plateau = new CreatePlateauCommand().Execute(commandTexts[0]);

                Rover deployedRover = null;
                for (int i = 1; i < commandTexts.Length; i++)
                {
                    var modeOfCommand = (i - 1) % 2;
                    if (modeOfCommand == 0)
                    {
                        deployedRover = new RoverDeployCommand(_plateau).Execute(commandTexts[i]);
                    }
                    else
                    {
                        new RoverMoveCommand(deployedRover, _plateau).Execute(commandTexts[i]);
                    }
                }
            }
            else
            {
                throw new ArgumentException("It is not a valid command for execution.");
            }
        }
コード例 #2
0
            public void Returns_RoverDeployCommand_type()
            {
                var point = new Point(0, 0);
                var roverDeployCommand = new RoverDeployCommand(point, CardinalDirection.West);

                Assert.AreEqual(roverDeployCommand.GetCommandType(), CommandType.RoverDeployCommand);
            }
コード例 #3
0
        public void Return_RoverDeployCommand_Type_When_Get_Command_Type_Called()
        {
            var position           = new Position(0, 0);
            var roverDeployCommand = new RoverDeployCommand(position, CardinalDirection.West);

            Assert.AreEqual(roverDeployCommand.GetCommandType(), CommandType.RoverDeployCommand);
        }
コード例 #4
0
        public void DeployRoverExpectValidLocationOrThrows(string deployRoverCommandText)
        {
            var splittedRoverInfo = deployRoverCommandText.Split(' ');

            ICommand <Rover> roverDeployCommand = new RoverDeployCommand(Plateau);

            if (splittedRoverInfo.Length == 3)
            {
                var isRoverLocationXParsedSuccessfully = int.TryParse(splittedRoverInfo[0], out var roverX);
                var isRoverLocationYParsedSuccessfully = int.TryParse(splittedRoverInfo[1], out var roverY);
                if (!isRoverLocationXParsedSuccessfully)
                {
                    var ex = Assert.Throws <InvalidCastException>(() =>
                                                                  roverDeployCommand.Execute(deployRoverCommandText));
                    Assert.AreEqual("Invalid argument value for parsing upper value of x.", ex.Message);
                }
                else if (!isRoverLocationYParsedSuccessfully)
                {
                    var ex = Assert.Throws <InvalidCastException>(() =>
                                                                  roverDeployCommand.Execute(deployRoverCommandText));
                    Assert.That(ex.Message, Is.EqualTo("Invalid argument value for parsing upper value of y."));
                }
                else if (!Plateau.ContainsKey(new Block {
                    X = roverX, Y = roverY
                }) || Plateau[new Block {
                                  X = roverX, Y = roverY
                              }] != null)
                {
                    var ex = Assert.Throws <ArgumentException>(() => roverDeployCommand.Execute(deployRoverCommandText));
                    Assert.AreEqual("The location is not valid for deploying.", ex.Message);
                }
                else
                {
                    var roverDirection = EnumHelper.GetValueFromDescription <DirectionType>(splittedRoverInfo[2]);
                    if (roverDirection == DirectionType.None)
                    {
                        var ex = Assert.Throws <InvalidEnumArgumentException>(() => roverDeployCommand.Execute(deployRoverCommandText));
                        Assert.AreEqual("roverDirection", ex.ParamName);
                    }
                    else
                    {
                        var rover = roverDeployCommand.Execute(deployRoverCommandText);
                        Assert.NotNull(rover);
                        Assert.AreEqual(roverDirection, rover.Direction);
                        var roverLocation = new Block {
                            X = roverX, Y = roverY
                        };
                        Assert.AreSame(Plateau[roverLocation], rover);
                    }
                }
            }
            else
            {
                var ex = Assert.Throws <TargetParameterCountException>(() => roverDeployCommand.Execute(deployRoverCommandText));
                Assert.That(ex.Message, Is.EqualTo($"{deployRoverCommandText} is not a valid deploy command, please give the order in \"x(1) y(2) direction(N)\" format."));
            }
        }
コード例 #5
0
        public void Expose_Position_And_Direction(int expectedX, int expectedY, CardinalDirection expectedDirection)
        {
            var expectedPosition = new Position(expectedX, expectedY);

            var roverDeployCommand = new RoverDeployCommand(expectedPosition, expectedDirection);

            Assert.AreEqual(expectedPosition, roverDeployCommand.DeploymentPosition);
            Assert.AreEqual(expectedDirection, roverDeployCommand.DeployDirection);
        }
コード例 #6
0
            public void Given_size_and_direction_exposes_as_public_properties(int expectedX, int expectedY, CardinalDirection expectedDirection)
            {
                var expectedPoint = new Point(expectedX, expectedY);

                var roverDeployCommand = new RoverDeployCommand(expectedPoint, expectedDirection);

                Assert.AreEqual(expectedPoint, roverDeployCommand.DeployPoint);
                Assert.AreEqual(expectedDirection, roverDeployCommand.DeployDirection);
            }
コード例 #7
0
        public void Not_Throw_Exception_When_Provided_With_Recievers()
        {
            const CardinalDirection anyCardinalDirection = CardinalDirection.South;
            var position                  = new Position(0, 0);
            var mockRover                 = new Mock <IRover>();
            var mockLandingSurface        = new Mock <ILandingSurface>();
            var landingSurfaceSizeCommand = new RoverDeployCommand(position, anyCardinalDirection);

            Assert.DoesNotThrow(() =>
                                landingSurfaceSizeCommand.SetReceivers(mockRover.Object, mockLandingSurface.Object));
        }
コード例 #8
0
            public void Accepts_Receiver_arguments()
            {
                const CardinalDirection anyCardinalDirection = CardinalDirection.South;
                var anyPoint                  = new Point(0, 0);
                var mockRover                 = new Mock <IRover>();
                var mockLandingSurface        = new Mock <ILandingSurface>();
                var landingSurfaceSizeCommand = new RoverDeployCommand(anyPoint, anyCardinalDirection);

                Assert.DoesNotThrow(() =>
                                    landingSurfaceSizeCommand.SetReceivers(mockRover.Object, mockLandingSurface.Object));
            }
コード例 #9
0
        public void Call_Rover_Deploy_When_Command_Executed()
        {
            const CardinalDirection expectedCardinalDirection = CardinalDirection.North;
            var expectedPosition = new Position(0, 0);

            var mockRover          = new Mock <IRover>();
            var mockLandingSurface = new Mock <ILandingSurface>();
            var roverDeployCommand = new RoverDeployCommand(expectedPosition, expectedCardinalDirection);

            roverDeployCommand.SetReceivers(mockRover.Object, mockLandingSurface.Object);

            roverDeployCommand.Execute();

            mockRover.Verify(x =>
                             x.Deploy(mockLandingSurface.Object, expectedPosition, expectedCardinalDirection), Times.Once());
        }
コード例 #10
0
        public void MoveRoverExpectValidPositionOrThrows(string deployRoverCommandText, string moveRoverCommandText, string expectedPositionText)
        {
            ICommand <Rover> roverDeployCommand = new RoverDeployCommand(Plateau);
            var deployedRover = roverDeployCommand.Execute(deployRoverCommandText);
            ICommand <Rover> roverMoveCommand = new RoverMoveCommand(deployedRover, Plateau);

            var regexMoveCommandPattern = new Regex("^[LRM]+$");

            if (regexMoveCommandPattern.IsMatch(moveRoverCommandText))
            {
                var splittedPosition = expectedPositionText.Split(' ');
                int.TryParse(splittedPosition[0], out var expectedRoverXLocation);
                int.TryParse(splittedPosition[1], out var expectedRoverYLocation);
                var expectedRoverDirection = EnumHelper.GetValueFromDescription <DirectionType>(splittedPosition[2]);
                var expectedPosition       = new Block {
                    X = expectedRoverXLocation, Y = expectedRoverYLocation
                };
                if (Plateau.ContainsKey(expectedPosition) && Plateau[expectedPosition] == null)
                {
                    var movedRover = roverMoveCommand.Execute(moveRoverCommandText);
                    Assert.AreSame(deployedRover, movedRover);
                    var actualPosition = Plateau.GetRoverLocation(movedRover);

                    Assert.AreEqual(expectedRoverXLocation, actualPosition.X);
                    Assert.AreEqual(expectedRoverYLocation, actualPosition.Y);
                    Assert.AreEqual(expectedRoverDirection, movedRover.Direction);
                }
                else
                {
                    var ex = Assert.Throws <ArgumentException>(() => roverMoveCommand.Execute(moveRoverCommandText));
                    Assert.AreEqual($"Direction of {expectedRoverDirection} at order {deployedRover.Order} rover's move command is not valid!", ex.Message);
                }
            }
            else
            {
                var ex = Assert.Throws <ArgumentException>(() => roverMoveCommand.Execute(moveRoverCommandText));
                Assert.AreEqual($"Invalid character(s) in \"{moveRoverCommandText}\" text for move command.", ex.Message);
            }
        }