public void Run()
        {
            CleanningSession session = _reader.ReadAllCommands();
            int placesClean          = _robot.ExecuteClean(session);

            _view.WriteLine(string.Format(Resources.ResultLabel, placesClean));
        }
        public void ReadAllCommand_text_sequence_input_should_return_CleanningSession()
        {
            CommandReader SUT = new CommandReader(_mockView.Object);

            _mockView.SetupSequence(x => x.ReadLine()).Returns("4")
            .Returns("-100 -101")
            .Returns("N 100")
            .Returns("E 1000")
            .Returns("S 500")
            .Returns("W 10");
            CleanningSession result = SUT.ReadAllCommands();

            Assert.AreEqual(-100, result.StartingCoordinate.X);
            Assert.AreEqual(-101, result.StartingCoordinate.Y);
            Assert.AreEqual(4, result.Commands.Count);

            Assert.AreEqual(Direction.N, result.Commands[0].Direction);
            Assert.AreEqual(100, result.Commands[0].Steps);

            Assert.AreEqual(Direction.E, result.Commands[1].Direction);
            Assert.AreEqual(1000, result.Commands[1].Steps);

            Assert.AreEqual(Direction.S, result.Commands[2].Direction);
            Assert.AreEqual(500, result.Commands[2].Steps);

            Assert.AreEqual(Direction.W, result.Commands[3].Direction);
            Assert.AreEqual(10, result.Commands[3].Steps);
        }
示例#3
0
        public void ExecuteClean_Session_One_Line_should_return_4001()
        {
            List <MoveCommand> commands = new List <MoveCommand>();

            commands.Add(new MoveCommand(Direction.E, 4000));
            CleanningSession session = new CleanningSession(new Coordinate(-2000, 2000), commands);

            Robot SUT    = new Robot();
            int   places = SUT.ExecuteClean(session);

            Assert.AreEqual(2000, SUT.CurrentPosition.X);
            Assert.AreEqual(2000, SUT.CurrentPosition.Y);
            Assert.AreEqual(4001, places);
        }
示例#4
0
        public CleanningSession ReadAllCommands()
        {
            int                amountOfCommands   = this.ReadAmountOfCommands();
            Coordinate         startingCoordinate = this.ReadStartingCoordinate();
            List <MoveCommand> commands           = new List <MoveCommand>();

            while (commands.Count < amountOfCommands)
            {
                commands.Add(this.ReadCommand());
            }
            CleanningSession session = new CleanningSession(startingCoordinate, commands);

            return(session);
        }
示例#5
0
        public void ExecuteClean_Session_Repeating_line_should_return_3001()
        {
            Robot SUT = new Robot();

            List <MoveCommand> commands = new List <MoveCommand>();

            commands.Add(new MoveCommand(Direction.S, 3000));
            commands.Add(new MoveCommand(Direction.N, 3000));
            CleanningSession session = new CleanningSession(new Coordinate(-20000, -20000), commands);
            int places = SUT.ExecuteClean(session);

            Assert.AreEqual(-20000, SUT.CurrentPosition.X);
            Assert.AreEqual(-20000, SUT.CurrentPosition.Y);
            Assert.AreEqual(3001, places);
        }
        public CleanningSession ReadAllCommands()
        {
            string mapLimit         = String.Empty;
            String startingPosition = String.Empty;
            string directions       = String.Empty;

            string[] splittedCommands = this.ReadFormattedInput();
            if (splittedCommands != null && splittedCommands.Count() == 3)
            {
                foreach (var splitedCommand in splittedCommands)
                {
                    if (splitedCommand.StartsWith("M"))
                    {
                        mapLimit = splitedCommand.Substring(2, splitedCommand.Length - 2);
                    }
                    else if (splitedCommand.StartsWith("S"))
                    {
                        startingPosition = splitedCommand.Substring(2, splitedCommand.Length - 2);
                    }
                    else
                    {
                        directions = splitedCommand.Substring(1, splitedCommand.Length - 2);
                    }
                }
            }
            else
            {
                CleanningSession emptySession = new CleanningSession(new Coordinate(0, 0), new List <MoveCommand>());
                emptySession.ErrorMessage = "Input data is not in a correct format";
                return(emptySession);
            }

            Coordinate startingCoordinate = this.ReadStartingCoordinate(startingPosition);

            SetMapBoundaries(startingCoordinate, mapLimit);
            List <MoveCommand> commands = new List <MoveCommand>();

            string[] details = directions.Split(',');
            foreach (var direction in details)
            {
                commands.Add(this.ReadDirection(direction));
            }
            CleanningSession session = new CleanningSession(startingCoordinate, commands);

            return(session);
        }
示例#7
0
        public void ExecuteClean_Session_Square_should_return_800000()
        {
            Robot SUT = new Robot();

            List <MoveCommand> commands = new List <MoveCommand>();

            commands.Add(new MoveCommand(Direction.E, 200000));
            commands.Add(new MoveCommand(Direction.S, 200000));
            commands.Add(new MoveCommand(Direction.W, 200000));
            commands.Add(new MoveCommand(Direction.N, 200000));
            CleanningSession session = new CleanningSession(new Coordinate(-100000, 100000), commands);
            int places = SUT.ExecuteClean(session);

            Assert.AreEqual(-100000, SUT.CurrentPosition.X);
            Assert.AreEqual(100000, SUT.CurrentPosition.Y);
            Assert.AreEqual(800000, places);
        }
示例#8
0
        public void Run_should_display_places_cleaned()
        {
            Controller       SUT         = new Controller(_mockView.Object, _mockReader.Object, _mockRobot.Object);
            CleanningSession mockSession = new CleanningSession(new Coordinate(0, 0), new List <MoveCommand>());

            mockSession.coordinates = new List <Coordinate>();
            _mockReader.Setup(x => x.ReadAllCommands()).Returns(mockSession);
            Coordinate cordinate = new Coordinate(4, 6);
            IDictionary <Coordinate, bool> placesClean = new Dictionary <Coordinate, bool>();

            placesClean[cordinate] = true;
            _mockRobot.Setup(x => x.ExecuteClean(It.IsAny <CleanningSession>())).Returns(placesClean);
            _mockView.Setup(x => x.WriteLine(It.IsAny <String>()));

            SUT.Run();

            _mockView.Verify(w => w.WriteLine(It.Is <string>(s => s == "4,6")));
        }
示例#9
0
        public void ExecuteClean_Session_3_Intersections_should_avoid_repeated()
        {
            Robot SUT = new Robot();

            List <MoveCommand> commands = new List <MoveCommand>();

            commands.Add(new MoveCommand(Direction.E, 100));
            commands.Add(new MoveCommand(Direction.S, 20));
            commands.Add(new MoveCommand(Direction.W, 20));
            commands.Add(new MoveCommand(Direction.N, 24));
            commands.Add(new MoveCommand(Direction.E, 10));
            commands.Add(new MoveCommand(Direction.S, 30));
            CleanningSession session = new CleanningSession(new Coordinate(0, 0), commands);
            int places = SUT.ExecuteClean(session);

            Assert.AreEqual(100 - 20 + 10, SUT.CurrentPosition.X);
            Assert.AreEqual(-20 + 24 - 30, SUT.CurrentPosition.Y);
            Assert.AreEqual(1 + 100 + 20 + 20 + 24 + 10 + 30 - 3, places);//Expected: initial place + total steps - intersections
        }
        public void ExecuteClean_Session_3_Intersections()
        {
            Robot SUT = new Robot();

            List <MoveCommand> commands = new List <MoveCommand>();

            commands.Add(new MoveCommand(Direction.E, 100));
            commands.Add(new MoveCommand(Direction.S, 20));
            commands.Add(new MoveCommand(Direction.W, 20));
            commands.Add(new MoveCommand(Direction.N, 24));
            commands.Add(new MoveCommand(Direction.E, 10));
            commands.Add(new MoveCommand(Direction.S, 30));
            CleanningSession session = new CleanningSession(new Coordinate(0, 0), commands);
            var places = SUT.ExecuteClean(session);

            Assert.AreEqual(0, SUT.CurrentPosition.X);
            Assert.AreEqual(0, SUT.CurrentPosition.Y);
            Assert.IsNotNull(places);
        }
        public void Run()
        {
            CleanningSession session = _reader.ReadAllCommands();

            try
            {
                IDictionary <Coordinate, bool> placesClean = _robot.ExecuteClean(session);
                String uniqueCoordinates = String.Empty;
                String allCoordinates    = String.Empty;
                foreach (var places in placesClean)
                {
                    var coordinateToPrint = places.Key.X + "," + places.Key.Y;
                    uniqueCoordinates = String.IsNullOrEmpty(uniqueCoordinates)? (uniqueCoordinates + coordinateToPrint) : (uniqueCoordinates + ";" + coordinateToPrint);
                }
                foreach (var places in session.coordinates)
                {
                    var coordinateToPrint = places.X + "," + places.Y;
                    allCoordinates = String.IsNullOrEmpty(allCoordinates) ? (allCoordinates + coordinateToPrint) : (allCoordinates + ";" + coordinateToPrint);
                }
                if (!String.IsNullOrEmpty(session.ErrorMessage))
                {
                    _display.WriteLine(string.Format(Resources.ErrorLabel, session.ErrorMessage));
                }
                if (!String.IsNullOrEmpty(allCoordinates))
                {
                    _display.WriteLine(string.Format(Resources.ResultLabel2));
                    _display.WriteLine(allCoordinates);
                }
                if (!String.IsNullOrEmpty(uniqueCoordinates))
                {
                    _display.WriteLine(string.Format(Resources.ResultLabel1));
                    _display.WriteLine(uniqueCoordinates);
                }
                _display.ReadLine();
            }
            catch (Exception ex)
            {
                _display.WriteLine(string.Format(Resources.ErrorLabel, ex.Message));
            }
        }