Ejemplo n.º 1
0
        public void Move_ShouldDoNothing_WhenInvalidMovement(int initX, int initY, int orientation)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(5, 5);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);

            var initialPosition = new Position {
                x = initX, y = initY
            };

            positionField.SetValue(robot, initialPosition);
            orientationField.SetValue(robot, (Orientation)orientation);

            //Act
            robot.Move();

            //Assert
            Assert.Equal((Orientation)orientation, orientationField.GetValue(robot));
            Assert.NotNull(positionField.GetValue(robot));
            Assert.IsType <Position>(positionField.GetValue(robot));
            Assert.Equal(initX, (positionField.GetValue(robot) as Position).x);
            Assert.Equal(initY, (positionField.GetValue(robot) as Position).y);
        }
Ejemplo n.º 2
0
        public void Move_ShouldSucceed_WhenValidMovement(int initX, int initY, int orientation, int expectedX, int expectedY)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(5, 5);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);
            var    isPlacedProperty = robot.GetType().GetProperty("IsPlaced", BindingFlags.Public | BindingFlags.Instance);

            positionField.SetValue(robot, new Position {
                x = initX, y = initY
            });
            orientationField.SetValue(robot, (Orientation)orientation);
            isPlacedProperty.SetValue(robot, true);

            //Act
            robot.Move();

            //Assert
            Assert.Equal((Orientation)orientation, orientationField.GetValue(robot));

            //TODO: Implement an overload of the Equals method to simplify these assertions.
            Assert.NotNull(positionField.GetValue(robot));
            Assert.IsType <Position>(positionField.GetValue(robot));
            Assert.Equal(expectedX, (positionField.GetValue(robot) as Position).x);
            Assert.Equal(expectedY, (positionField.GetValue(robot) as Position).y);
        }
Ejemplo n.º 3
0
        public void GetOrientation_ShouldReturnNullIfNotPlaced()
        {
            //Arrange
            var    mockTable = new Mock <RectangularTable>(5, 5);
            IRobot robot     = new ToyRobotLibrary.Robot.Robot(mockTable.Object);

            //Act
            var result = robot.GetOrientation();

            //Assert
            Assert.Null(result);
        }
Ejemplo n.º 4
0
        public void Constructor_PositionAndOrientationShouldBeNull_WhenFirstCreated()
        {
            //Arrange
            IRobot robot;

            //Act
            Exception ex = Record.Exception(() => robot = new ToyRobotLibrary.Robot.Robot(null));

            //Assert
            Assert.NotNull(ex);
            Assert.IsType <ArgumentNullException>(ex);
            Assert.Contains("Can't use a robot without a table instance.", ex.Message);
        }
Ejemplo n.º 5
0
        public void Rotate_ShouldDoNothingBeforeRobotIsPlaced(int spin)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(1, 1);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);

            //Act
            robot.Rotate((SpinDirection)spin);

            //Assert
            Assert.Null(orientationField.GetValue(robot));
        }
Ejemplo n.º 6
0
        public void Constructor_ShouldSucceed_WhenPassedValidTable()
        {
            //Arrange
            var mockTable = new Mock <RectangularTable>(1, 1);

            //Act
            IRobot robot = new ToyRobotLibrary.Robot.Robot(mockTable.Object);

            //Assert
            Assert.NotNull(robot);
            Assert.IsAssignableFrom <IRobot>(robot);
            Assert.IsType <ToyRobotLibrary.Robot.Robot>(robot);
        }
Ejemplo n.º 7
0
        public void Move_ShouldDoNothing_IfNotYetPlaced()
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(5, 5);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);

            //Act
            robot.Move();

            //Assert
            Assert.Null(orientationField.GetValue(robot));
            Assert.Null(positionField.GetValue(robot));
        }
Ejemplo n.º 8
0
        public void GetOrientation_ShouldReturnOrientation(Orientation orientation)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(5, 5);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);
            var    isPlacedProperty = robot.GetType().GetProperty("IsPlaced", BindingFlags.Public | BindingFlags.Instance);

            orientationField.SetValue(robot, orientation);
            isPlacedProperty.SetValue(robot, true);

            //Act
            var result = robot.GetOrientation();

            //Assert
            Assert.NotNull(result);
            Assert.IsType <Orientation>(result);
            Assert.Equal(orientation, result);
        }
Ejemplo n.º 9
0
        public void Constructor_ShouldThrowException_WhenPassedInvalidTable()
        {
            //Arrange
            var    mockTable = new Mock <RectangularTable>(1, 1);
            IRobot robot;

            //Act
            robot = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);
            var tableField       = robot.GetType().GetField("_table", BindingFlags.NonPublic | BindingFlags.Instance);


            //Assert
            Assert.Null(orientationField.GetValue(robot));
            Assert.Null(positionField.GetValue(robot));
            Assert.NotNull(tableField.GetValue(robot));
            Assert.False(robot.IsPlaced);
        }
Ejemplo n.º 10
0
        public void GetPosition_ShouldReturnPosition(int x, int y)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(5, 5);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var    isPlacedProperty = robot.GetType().GetProperty("IsPlaced", BindingFlags.Public | BindingFlags.Instance);

            positionField.SetValue(robot, new Position(x, y));
            isPlacedProperty.SetValue(robot, true);

            //Act
            var result = robot.GetPosition();

            //Assert
            Assert.IsType <Position>(result);
            Assert.Equal(x, result.x);
            Assert.Equal(y, result.y);
        }
Ejemplo n.º 11
0
        public void Rotate_ShouldSucceed_WithSingleSpin(int startingOrientation, int spin, int expectedOrientation)
        {
            //Arrange
            var    mockTable        = new Mock <RectangularTable>(1, 1);
            IRobot robot            = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var    orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);
            var    isPlacedProperty = robot.GetType().GetProperty("IsPlaced", BindingFlags.Public | BindingFlags.Instance);

            positionField.SetValue(robot, new Position {
                x = 0, y = 0
            });
            orientationField.SetValue(robot, (Orientation)startingOrientation);
            isPlacedProperty.SetValue(robot, true);

            //Act
            robot.Rotate((SpinDirection)spin);

            //Assert
            Assert.Equal((Orientation)expectedOrientation, orientationField.GetValue(robot));
        }
Ejemplo n.º 12
0
        public void Place_ShouldSucceed_WhenPassedValidCoordinatesAndOrientation(int numRows, int numCols, int xPlacement, int yPlacement, int orientationVal)
        {
            //Arrange
            var    mockTable         = new Mock <RectangularTable>(numRows, numCols);
            IRobot robot             = new ToyRobotLibrary.Robot.Robot(mockTable.Object);
            var    placementPosition = new Position {
                x = xPlacement, y = yPlacement
            };
            var positionField    = robot.GetType().GetField("_position", BindingFlags.NonPublic | BindingFlags.Instance);
            var orientationField = robot.GetType().GetField("_orientation", BindingFlags.NonPublic | BindingFlags.Instance);

            //Act
            robot.Place(placementPosition, (Orientation)orientationVal);

            var positionValue    = positionField.GetValue(robot);
            var orientationValue = orientationField.GetValue(robot);

            //Assert
            Assert.Equal(positionValue, placementPosition);
            Assert.Equal(orientationValue, (Orientation)orientationVal);
            Assert.True(robot.IsPlaced);
        }