Ejemplo n.º 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());
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }