Пример #1
0
        public void TestCommandPlaceCanExecuteValid()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            Assert.IsTrue(command.CanExecute(robot));
        }
Пример #2
0
        public override ICommand Create(params string[] args)
        {
            ICommand result = null;

            if (!args.Length.Equals(4))
            {
                throw new ArgumentException("invalid number of arguments");
            }

            var x         = 0;
            var y         = 0;
            var direction = Direction.NORTH;

            if (!int.TryParse(args[1], out x))
            {
                throw new ArgumentException("invalid position coordinates (x)");
            }


            if (!int.TryParse(args[2], out y))
            {
                throw new ArgumentException("invalid position coordinates (y)");
            }

            if (!Enum.TryParse <Direction>(args[3], out direction))
            {
                throw new ArgumentException("invalid direction");
            }

            result = new PlaceCommand(Rover, new Point(x, y), direction);

            return(result);
        }
Пример #3
0
        public void Execute_AlreadyPlacedOnTable_OK()
        {
            // Arrange
            // Place robot on table
            var robot        = new Robot();
            var table        = new Table();
            var x            = 0;
            var y            = 0;
            var heading      = CompassPoint.North;
            var placeCommand = new PlaceCommand(table, x, y, heading);

            placeCommand.Execute(robot);

            // Act
            // Replace robot on table in different location and heading.
            x       = 1;
            y       = 1;
            heading = CompassPoint.West;
            var replaceCommand = new PlaceCommand(table, x, y, heading);

            replaceCommand.Execute(robot);

            // Assert
            Assert.NotNull(placeCommand);
            Assert.Equal(x, robot.X);
            Assert.Equal(y, robot.Y);
            Assert.Equal(heading, (CompassPoint)robot.Heading);
            Assert.Same(table, robot.Table);
        }
Пример #4
0
        public void Execute_PlacedDifferentTable_OK()
        {
            // Arrange
            // Place robot on table1
            var robot        = new Robot();
            var table1       = new Table();
            var x1           = 0;
            var y1           = 0;
            var heading1     = CompassPoint.North;
            var placeCommand = new PlaceCommand(table1, x1, y1, heading1);

            placeCommand.Execute(robot);

            // Act
            // Replace robot on table2
            var table2         = new Table();
            var x2             = 0;
            var y2             = 0;
            var heading2       = CompassPoint.North;
            var replaceCommand = new PlaceCommand(table2, x2, y2, heading2);

            replaceCommand.Execute(robot);

            // Assert
            Assert.NotNull(placeCommand);
            Assert.Equal(x2, robot.X);
            Assert.Equal(y2, robot.Y);
            Assert.Equal(heading2, (CompassPoint)robot.Heading);
            Assert.Same(table2, robot.Table);
        }
Пример #5
0
        public void TestCommandPlaceExecuteHasParameters()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "");
        }
Пример #6
0
    private void CheckCommand(Commands command)
    {
        switch (command)
        {
        case Commands.Place:
            placementCoordinates = testCase.ReturnPlacementCoordinates(PlacementCoordinatesIndex);
            PlaceCommand placeCommand = new PlaceCommand(toyRobot, placementCoordinates);
            commandExucute.ExcutePlace(placeCommand);
            PlacementCoordinatesIndex++;
            break;

        case Commands.Move:
            MoveCommand moveCommand = new MoveCommand(toyRobot);
            commandExucute.ExcuteMovement(moveCommand);
            break;

        case Commands.Left:
        case Commands.Right:
            RotateCommand rotateCommand = new RotateCommand(toyRobot, command);
            commandExucute.ExcuteRotate(rotateCommand);
            break;

        case Commands.Report:
            ReportCommand reportCommand = new ReportCommand(toyRobot);
            commandExucute.ExcuteReport(reportCommand);
            break;
        }
    }
Пример #7
0
        public void TestCommandPlaceExecuteYParameterOverflow()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "1,23456789012,NORTH");
        }
Пример #8
0
        public void PlaceRobotInMap()
        {
            /*
             * Should test each of the cases where a robot can be placed in a map
             * 1. Test initial state
             * 2. Out of bounds
             * 3. Between (0,0) and (4,4)
             */

            //1
            Assert.Throws <ToyRobotHasNotBeenPlacedException>(() => toyRobot.GetCurrentPosition());
            Assert.IsFalse(toyRobot.HasBeenPlaced());

            //2
            var          args         = "-1,0,WEST";
            PlaceCommand placeCommand = new PlaceCommand(map, args);

            Assert.Throws <InvalidPlaceCommandException>(() => toyRobot.Execute(placeCommand));

            //2.1
            args         = "2,5,WEST";
            placeCommand = new PlaceCommand(map, args);
            Assert.Throws <InvalidPlaceCommandException>(() => toyRobot.Execute(placeCommand));

            //3
            args         = "0,0,NORTH";
            placeCommand = new PlaceCommand(map, args);
            toyRobot.Execute(placeCommand);

            //3.1
            args         = "3,4,NORTH";
            placeCommand = new PlaceCommand(map, args);
            toyRobot.Execute(placeCommand);
        }
        public void Command_Manager_should_return_place_command_when_place_0_1_north_is_given()
        {
            var          coordinateValidator = new SurfaceCoordinateValidator();
            var          directionValidator  = new DirectionValidator();
            var          paramValidator      = new CommandParamValidator(coordinateValidator, directionValidator);
            var          mgr = new CommandManager(paramValidator, null);
            PlaceCommand cmd = (PlaceCommand)mgr.ChooseCommand("PLACE 0,1 NORTH");

            var coordinate = new SurfaceCoordinate {
                X_Position = 0, Y_Position = 1
            };

            PlaceCommandParam actualParam = (PlaceCommandParam)cmd.CommandParam;

            Assert.AreEqual <CommandType>(CommandType.PLACE, actualParam.CommandParamType);
            Assert.AreEqual <Direction>(Direction.NORTH, actualParam.Direction);
            Assert.AreEqual <SurfaceCoordinate>(coordinate, actualParam.SurfaceCoordinate);

            RobotPosition actualPosition = null;
            var           actual         = cmd.GetCommandResult(null, out actualPosition);

            Assert.AreEqual <bool>(true, actual);

            Assert.AreEqual <Direction>(Direction.NORTH, actualPosition.Direction);
            Assert.AreEqual <SurfaceCoordinate>(coordinate, actualPosition.Coordinate);
        }
Пример #10
0
        private static IPositionService GetPositionService()
        {
            var placeCommand  = new PlaceCommand();
            var reportCommand = new ReportCommand();

            return(new PositionService(placeCommand, reportCommand));
        }
Пример #11
0
 public void PlaceRobotInvalidDirection()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var placeCommand = new PlaceCommand("PLACE 1,1,Nth");
     });
 }
Пример #12
0
        public void Should_Set_Correct_Indexes()
        {
            var sut = new PlaceCommand(new ToyRobot.misc.PointsTo(ToyRobot.misc.Cardinal.Est), 3, 4);

            Assert.True(sut.XPosition == 3);
            Assert.True(sut.YPosition == 4);
        }
Пример #13
0
        private static Robot GetRobot()
        {
            if (_robot != null)
            {
                return(_robot);
            }

            var callStack         = new Stack <Call>();
            var boundsEvaluator   = new BoundsEvaluator(new Table());
            var positionTracker   = new PositionTracker();
            var movementProcessor = new MovementProcessor(boundsEvaluator, positionTracker);
            var placeCommand      = new PlaceCommand(callStack, movementProcessor);
            var moveCommand       = new MoveCommand(callStack, movementProcessor);
            var turnCommand       = new TurnCommand(callStack, movementProcessor);
            var positionReporter  = new PositionReporter(positionTracker);
            var reportCommand     = new ReportCommand(callStack, positionReporter);
            var commandExecutor   = new CommandExecutor(
                callStack,
                placeCommand,
                moveCommand,
                turnCommand,
                reportCommand);

            _robot = new Robot(commandExecutor);

            return(_robot);
        }
Пример #14
0
        public void Robot_TestUndoWhenWhenRobotIsStuckAgainstSouthWall_RobotReportsOrignalPosition()
        {
            Robot          robot     = new Robot();
            Tabletop       table     = new Tabletop(5, 5);
            RobotCommander commander = new RobotCommander();

            PlaceCommand place = new PlaceCommand(robot, table);

            place.Direction = "North";
            MoveCommand  move  = new MoveCommand(robot, table);
            RightCommand right = new RightCommand(robot);
            LeftCommand  left  = new LeftCommand(robot);


            commander.Commands.Enqueue(place);
            commander.Commands.Enqueue(right);
            commander.Commands.Enqueue(right);
            commander.Commands.Enqueue(move);

            commander.ExecuteCommands();
            commander.UndoCommands(1);

            Assert.Equal(0, robot.Position.Y);
            Assert.Equal(0, robot.Position.X);
        }
Пример #15
0
        public void TestCommandPlaceExecuteXParameter()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "A,2,NORTH");
        }
Пример #16
0
        public void TestCommandPlaceExecuteYParameterFloat()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "1,2.0,NORTH");
        }
Пример #17
0
        public void TestCommandPlaceExecuteTooManyParameters()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "1,2,NORTH,SHOE");
        }
Пример #18
0
        public void RotateRobotToRight()
        {
            /*
             * Should rotate the Robot without moving it
             * 1. Test initial state
             * 2. Place the robot
             * 3. 360 degrees rotation
             */

            //1
            Assert.Throws <ToyRobotHasNotBeenPlacedException>(() => toyRobot.GetCurrentPosition());
            Assert.IsFalse(toyRobot.HasBeenPlaced());

            //2
            var          args         = "0,0,WEST";
            PlaceCommand placeCommand = new PlaceCommand(map, args);

            toyRobot.Execute(placeCommand);

            //3
            RightCommand rightCommand = new RightCommand(map, string.Empty);

            toyRobot.Execute(rightCommand);
            Assert.IsTrue(toyRobot.GetCurrentPosition().Item3 == FacesEnum.NORTH);
            toyRobot.Execute(rightCommand);
            Assert.IsTrue(toyRobot.GetCurrentPosition().Item3 == FacesEnum.EAST);
            toyRobot.Execute(rightCommand);
            Assert.IsTrue(toyRobot.GetCurrentPosition().Item3 == FacesEnum.SOUTH);
            toyRobot.Execute(rightCommand);
            Assert.IsTrue(toyRobot.GetCurrentPosition().Item3 == FacesEnum.WEST);
        }
Пример #19
0
        public void Robot_TestMultipleMovement_RobotReportsCorrectLocation()
        {
            Robot          robot     = new Robot();
            Tabletop       table     = new Tabletop(5, 5);
            RobotCommander commander = new RobotCommander();

            PlaceCommand place = new PlaceCommand(robot, table);

            place.Direction = "North";
            MoveCommand  move  = new MoveCommand(robot, table);
            RightCommand right = new RightCommand(robot);
            LeftCommand  left  = new LeftCommand(robot);


            commander.Commands.Enqueue(place);
            commander.Commands.Enqueue(move);
            commander.Commands.Enqueue(move);
            commander.Commands.Enqueue(right);
            commander.Commands.Enqueue(move);
            commander.Commands.Enqueue(left);
            commander.Commands.Enqueue(left);

            commander.ExecuteCommands();

            Assert.Equal(Facing.West, robot.Direction);
            Assert.Equal(2, robot.Position.Y);
            Assert.Equal(1, robot.Position.X);
        }
Пример #20
0
        public void IfRobotPlacedCorrectlyAtFirstTimeButNotCorrectlyAfterReportShouldShowLastPosition()
        {
            ToyRobot robot = PlaceRobotInPosition(new Position(1, 2), Direction.NORTH);

            Assert.IsNotNull(robot.RobotRoute);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);

            MoveCommand moveCommand = new MoveCommand();

            moveCommand.Execute(robot);

            PlaceCommand placeCommand = new PlaceCommand(new Route(new Position(10, 2), Direction.NORTH));

            //This wrong Place will not set
            placeCommand.Execute(robot);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.InvalidPositionForRobot);

            ReportCommand reportCommand = new ReportCommand();

            //The report should be Last Position "OutPuT: 1,3, NORTH"
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "OutPuT: 1,3, NORTH");
            Console.WriteLine(reportCommand.LastReportOfRobot);
            moveCommand.Execute(robot);
            Assert.AreEqual(robot.RobotStatus, RobotStatus.CorrectPlaceToStand);
            reportCommand.Execute(robot);
            Assert.AreEqual(reportCommand.LastReportOfRobot, "OutPuT: 1,4, NORTH");
            Console.WriteLine(reportCommand.LastReportOfRobot);
        }
Пример #21
0
        public void TestCommandPlaceExecuteFParameter()
        {
            Robot robot   = new Robot(2);
            var   command = new PlaceCommand();

            command.Execute(robot, "2,1,ANA");
        }
Пример #22
0
        public void GivenTheRobotIsCurrentlyOnTheTableAtAndFacing(int x, int y, string orientation)
        {
            var scene = this.scenarioContext.Get <Scene>("scene");

            var status = new PlaceCommand(new Bearing(x, y, orientation.ToEnum <Orientation>())).Execute(scene);

            this.scenarioContext.Set(status.Data, "scene");
        }
Пример #23
0
        public void TestInvalidPlaceCommand()
        {
            var rover = new Rover();

            var cmd = new PlaceCommand(rover, new Point(10, 12), Direction.NORTH);

            Assert.IsFalse(cmd.Validate());
        }
Пример #24
0
        public CommandController()
        {
            var grid = GridService.CreateGrid(GridUnitsX, GridUnitsY);

            _moveCommand  = new MoveCommand(grid);
            _placeCommand = new PlaceCommand(grid);
            _rotateLeft   = new RotationCommand("LEFT", RotationalDirection.Left);
            _rotateRight  = new RotationCommand("RIGHT", RotationalDirection.Right);
        }
Пример #25
0
        private ToyRobot PlaceRobotInPosition(Position position, Direction direction)
        {
            ToyRobot     robot        = new ToyRobot(new Board());//Default Borad is 5 x 5
            Route        route        = new Route(position, direction);
            PlaceCommand placeCommand = new PlaceCommand(route);

            placeCommand.Execute(robot);
            return(robot);
        }
Пример #26
0
        public void WhenIReaddTheRobotAtAndFacing(int x, int y, string orientation)
        {
            var scene = this.scenarioContext.Get <Scene>("scene");

            var status = new PlaceCommand(new Bearing(x, y, orientation.ToEnum <Orientation>())).Execute(scene);

            this.scenarioContext.Set(status, "status");
            this.scenarioContext.Set(status.Data, "scene");
        }
Пример #27
0
        public void ShouldParseCommand(string input, int expectedX, int expectedY, Direction expecteDirection)
        {
            var command = PlaceCommand.Parse(input);

            Assert.NotNull(command);
            Assert.Equal(expectedX, command.X);
            Assert.Equal(expectedY, command.Y);
            Assert.Equal(expecteDirection, command.Direction);
        }
Пример #28
0
        public async Task <CommandResult> ValidateAsync(PlaceCommand command)
        {
            if (!_table.IsOnTheTable(command.Position))
            {
                return(await RejectCommand(command));
            }

            return(SuccessResult);
        }
Пример #29
0
        public void IsNotPlacedOutOfBoundsOfGrid()
        {
            ILogger logger  = new NullLogger <object>();
            var     grid    = new Grid(5, 5);
            State?  state   = null;
            var     command = new PlaceCommand(5, 3, MoveDirection.East);

            state = command.Execute(logger, state, grid);
            Assert.IsFalse(state.HasValue);
        }
Пример #30
0
 public Form1()
 {
     InitializeComponent();
     _robot        = new Robot();
     _grid         = new Grid(GridUnits, GridUnits);
     _moveCommand  = new MoveCommand(_grid);
     _placeCommand = new PlaceCommand(_grid);
     _rotateLeft   = new RotationCommand("LEFT", RotationalDirection.Left);
     _rotateRight  = new RotationCommand("RIGHT", RotationalDirection.Right);
 }
Пример #31
0
        private static Message Convert(string exchangeCode, PlaceCommand placeCommand)
        {
            XmlNode transactionNode = placeCommand.Content["Transaction"];

            Transaction[] transactions;
            Order[] orders;
            OrderRelation[] orderRelations;

            CommandConvertor.Parse(exchangeCode,transactionNode, out transactions, out orders, out orderRelations);
            PlaceMessage placeMessage = new PlaceMessage(exchangeCode,transactions, orders, orderRelations);
            placeMessage.AccountID = placeCommand.AccountID;
            placeMessage.InstrumentID = placeCommand.InstrumentID;
            return placeMessage;
        }
        private void PlaceOpenCommandBtn_Click(object sender, RoutedEventArgs e)
        {
            if (DQIndex > 7) return;
            string xmlPath = this.GetCommandXmlPath("PlaceCommand_DQ_Buy");

            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            XmlNode xmlTran = doc.ChildNodes[1].ChildNodes[DQIndex];

            PlaceCommand placeCommand;
            placeCommand = new PlaceCommand(DQIndex);
            placeCommand.InstrumentID = XmlConvert.ToGuid(xmlTran.Attributes["InstrumentID"].Value);
            placeCommand.AccountID = XmlConvert.ToGuid(xmlTran.Attributes["AccountID"].Value);

            XmlDocument xmlDoc = new XmlDocument();
            XmlNode content = xmlDoc.CreateElement("Place");
            xmlDoc.AppendChild(content);
            placeCommand.Content = content;

            content.AppendChild(xmlDoc.ImportNode(xmlTran, true));

            ManagerClient.AddCommand(placeCommand);
            DQIndex++;
        }
        //PlaceCommand /Order Task For MooMoc Order
        private void MooMocPlaceCommandBtn_Click(object sender, RoutedEventArgs e)
        {
            string xmlPath = string.Empty;
            ComboBoxItem item = (ComboBoxItem)this.MooMocComboBox.SelectedItem;
            string seletName = item.Content.ToString();
            switch (seletName)
            {
                case "MOOBuy":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_MOO_Buy");
                    break;
                case "MOOSell":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_MOO_Buy");
                    break;
                case "MOCBuy":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_MOO_Buy");
                    break;
                case "MOCSell":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_MOO_Buy");
                    break;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            XmlNode xmlTran = doc.ChildNodes[1].ChildNodes[0];

            PlaceCommand placeCommand;
            placeCommand = new PlaceCommand(1);
            placeCommand.InstrumentID = XmlConvert.ToGuid(xmlTran.Attributes["InstrumentID"].Value);
            placeCommand.AccountID = XmlConvert.ToGuid(xmlTran.Attributes["AccountID"].Value);

            XmlDocument xmlDoc = new XmlDocument();
            XmlNode content = xmlDoc.CreateElement("Place");
            xmlDoc.AppendChild(content);
            placeCommand.Content = content;

            content.AppendChild(xmlDoc.ImportNode(xmlTran, true));

            ManagerClient.AddCommand(placeCommand);
        }
        private void LMTCommandBtn_Click(object sender, RoutedEventArgs e)
        {
            if (LMTIndex > 7) return;
            string xmlPath = string.Empty;
            ComboBoxItem item = (ComboBoxItem)this.LmtComboBox.SelectedItem;
            string seletName = item.Content.ToString();
            switch (seletName)
            {
                case "LMTBuy":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_LMT_Sell");
                    break;
                case "LMTSell":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_LMT_Sell");
                    break;
                case "StopBuy":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_LMT_Sell");
                    break;
                case "StopSell":
                    xmlPath = this.GetCommandXmlPath("PlaceCommand_LMT_Sell");
                    break;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            XmlNode xmlTran = doc.ChildNodes[1].ChildNodes[LMTIndex];

            PlaceCommand placeCommand;
            placeCommand = new PlaceCommand(LMTIndex);
            placeCommand.InstrumentID = XmlConvert.ToGuid(xmlTran.Attributes["InstrumentID"].Value);
            placeCommand.AccountID = XmlConvert.ToGuid(xmlTran.Attributes["AccountID"].Value);

            XmlDocument xmlDoc = new XmlDocument();
            XmlNode content = xmlDoc.CreateElement("Place");
            xmlDoc.AppendChild(content);
            placeCommand.Content = content;

            content.AppendChild(xmlDoc.ImportNode(xmlTran, true));

            ManagerClient.AddCommand(placeCommand);
            LMTIndex++;
        }