Beispiel #1
0
        public void UnknownCommand_MustThrowInvalidCommandException()
        {
            // ARRANGE
            Terrain terrain    = new Terrain(default(uint), default(uint));
            Vehicle vehicle    = new Vehicle(terrain);
            char    commandKey = 'x';

            // ACT
            Func <VehicleCommand> actTodo = () => VehicleCommandFactory.Build(vehicle, commandKey);

            // ASSERT
            Assert.Throws <InvalidCommandException>(() => actTodo.Invoke());
        }
Beispiel #2
0
        public void BuildCommandTurnLeft_MustBeCorrectLoweCase()
        {
            // ARRANGE
            Terrain terrain    = new Terrain(default(uint), default(uint));
            Vehicle vehicle    = new Vehicle(terrain);
            char    commandKey = 'l';

            // ACT
            var command = VehicleCommandFactory.Build(vehicle, commandKey);

            // ASSERT
            Assert.NotNull(command);
            Assert.Equal <Type>(typeof(VehicleTurnLeftCommand), command.GetType());
        }
Beispiel #3
0
        public void TestCase00_StubVehicle()
        {
            // ARRANGE
            VehicleOperationsStub        roverStub = new VehicleOperationsStub();
            IEnumerable <VehicleCommand> commands  = "AAARAARA".Select(command => VehicleCommandFactory.Build(roverStub, command));
            MarsRoverEngine sut = new MarsRoverEngine(roverStub, commands);

            // ACT
            sut.ExecuteCommands();

            // ASSERT
            Assert.Equal <int>(6, roverStub.AdvanceInvocations);
            Assert.Equal <int>(2, roverStub.TurnRightInvocations);
            Assert.Equal <int>(0, roverStub.TurnLeftInvocations);
        }
Beispiel #4
0
        public void MoveVehicle(Plateau plateau, ExplorationVehicle vehicle, IList <Rotation> rotations, IList <IPoint> busyPoints)
        {
            var vehiclePoint = new Point(vehicle.Point.PositionX, vehicle.Point.PositionY);
            var direction    = vehicle.GetDirection();

            var virtualVehicle   = ExplorationVehicles.Rover.Factory(vehiclePoint, direction, plateau);
            var destinationPoint = VehicleHelperFunctions.CheckBusyPointsRoute(virtualVehicle, rotations, busyPoints);

            VehicleHelperFunctions.CheckBorderLimits(plateau, destinationPoint);

            var vehicleCommandInvoker = new VehicleCommandInvoker();

            foreach (var rotation in rotations)
            {
                var vehicleCommand = VehicleCommandFactory.GetVehicleCommand(rotation, vehicle);
                vehicleCommandInvoker.Execute(vehicleCommand);
            }
        }
Beispiel #5
0
        public static IPoint CheckBusyPointsRoute(ExplorationVehicle virtualVehicle, IList <Rotation> rotations, IList <IPoint> busyPoints)
        {
            var vehicleCommandInvoker = new VehicleCommandInvoker();

            foreach (var rotation in rotations)
            {
                var vehicleCommand = VehicleCommandFactory.GetVehicleCommand(rotation, virtualVehicle);
                vehicleCommandInvoker.Execute(vehicleCommand);

                if (busyPoints
                    .Where(x => x.PositionX == virtualVehicle.Point.PositionX)
                    .Where(x => x.PositionY == virtualVehicle.Point.PositionY)
                    .Count() > 0)
                {
                    throw new HandledException(BusinessConstants.BUSY_POINT);
                }
            }
            return(virtualVehicle.Point);
        }
Beispiel #6
0
        public void TestCase01()
        {
            // ARRANGE
            Terrain mars  = new Terrain(5, 5);
            Vehicle rover = new Vehicle(mars);

            rover.Initialize(0, 0, Orientation.N);
            IEnumerable <VehicleCommand> commands = "AAARAARAAAA".Select(command => VehicleCommandFactory.Build(rover, command));
            MarsRoverEngine sut = new MarsRoverEngine(rover, commands);

            // ACT
            sut.ExecuteCommands();
            var finalStatus = sut.GetCurrentVehicleStatus();

            // ASSERT
            Assert.Equal <int>(2, finalStatus.Position.X);
            Assert.Equal <int>(-1, finalStatus.Position.Y);
            Assert.Equal <Orientation>(Orientation.S, finalStatus.Orientation);
            Assert.False(finalStatus.InTerrainLimits);
        }
Beispiel #7
0
        /// <summary>
        /// The application entrypoint.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>
        ///         <ul>
        ///             <b>0</b> if all executed correctly.</returns>
        ///             <b>-1</b> if the arguments are missed.
        ///             <b>-2</b> if the orientation is incorrect.
        ///             <b>-3</b> if the command secuence contains an incorrect command.
        ///             <b>-4</b> if the Rover leaves Mars!!.
        ///         </ul>
        /// </returns>
        public static int Main(string[] args)
        {
            bool pause  = true;
            int  result = -1;

            try
            {
                Parser.Default.ParseArguments <CommandLineOptions>(args)
                .WithParsed <CommandLineOptions>(o =>
                {
                    bool.TryParse(o.Pause, out pause);

                    Orientation initialOrientation = o.RoverO.ToOrientation();
                    Terrain mars  = new Terrain(o.TerrainWidth, o.TerrainHeight);
                    Vehicle rover = new Vehicle(mars);
                    rover.Initialize((int)o.RoverX, (int)o.RoverY, initialOrientation);

                    IEnumerable <VehicleCommand> vehicleActions = o.Commands.Trim().Select(command => VehicleCommandFactory.Build(rover, command));

                    MarsRoverEngine marsRoverEngine = new MarsRoverEngine(rover, vehicleActions);
                    marsRoverEngine.ExecuteCommands();
                    var status = rover.GetCurrentStatus();
                    Console.WriteLine(status);

                    if (!status.InTerrainLimits)
                    {
                        result = -4;
                    }
                    else
                    {
                        result = 0;
                    }
                })
                .WithNotParsed <CommandLineOptions>(errors =>
                {
                    result = -1;
                    Console.WriteLine("Example of valid calls");
                    Console.WriteLine("dotnet CodingTest01.dll -w 5 -h 5 -x 0 -y 0 -o N -c AARALA -p false");
                    Console.WriteLine("dotnet CodingTest01.dll -w 2 -h 1 -x 1 -y 1 -o W -c RALALA -p true");
                });
            }
            catch (InvalidOrientationException)
            {
                result = -2;
            }
            catch (InvalidCommandException)
            {
                result = -3;
            }
            catch (InvalidPositionException)
            {
                result = -4;
            }

            Console.WriteLine($"Result value: {result}");
            if (pause)
            {
                Console.ReadLine();
            }

            return(result);
        }