Beispiel #1
0
        public void MovementPlayerMustMatchInitializedOnes()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();
            Player     firstPlayer;

            // When
            firstPlayer = game.StartGame(grid, playerA, playerB);

            // Then
            Assert.IsTrue(firstPlayer.Equals(playerA) || firstPlayer.Equals(playerB), "First player must be Player A or Player B");
        }
Beispiel #2
0
        public void GridIsFullMustRaiseAGameOverException()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.IsFull()).Returns(true);
            igridMock.Setup(m => m.Check()).Returns(CellPlayer.NONE);

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            Assert.Throws <GameOverException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise GameOverException");
        }
Beispiel #3
0
        public void InexistingPlayerMustThrowException()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            Player     other   = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);

            // Then
            NotValidValueException exc = Assert.Throws <NotValidValueException>(() => game.PlayerMove(other, new Coordinate(0, 0)), "Move must return an exception is player is not in initialization");

            Assert.AreEqual(ErrorCode.PLAYER_NOT_EXISTS, exc.ErrorCode, "Exception ErrorCode must be PLAYER_NOT_EXISTS");
        }
Beispiel #4
0
        public void StartGameAndPlayerMoveMustReturnNextPlayer()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();
            Player     firstPlayer;
            Player     nextPlayer;

            // When
            firstPlayer = game.StartGame(grid, playerA, playerB);
            nextPlayer  = game.PlayerMove(firstPlayer, new Coordinate(0, 0));

            // Then
            Assert.IsTrue(firstPlayer.Equals(playerA) || firstPlayer.Equals(playerB), "First player must be Player A or Player B");
            Assert.IsFalse(firstPlayer.Equals(nextPlayer), "First player must be different from next player");
        }
Beispiel #5
0
        public void PlayerMoveMustReturnAnExceptionIfGridCheckReturnsNotValidPlayerValue()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.Check()).Returns(CellPlayer.TEST_OTHER_PLAYER);

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            NotValidStateException exc = Assert.Throws <NotValidStateException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise NotValidStateException");

            Assert.AreEqual(exc.ErrorCode, ErrorCode.OUT_OF_RANGE, "Exception must have OUT_OF_RANGE error code");
        }
Beispiel #6
0
        public void PlayerMoveMustSpreadExceptionWhenGridSetReturnsNotValidStateExceptionWithUnknownErrorCode()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.Set(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CellPlayer>())).Throws(new NotValidStateException("", ErrorCode.UNKNOWN));

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            NotValidStateException exc = Assert.Throws <NotValidStateException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise NotValidStateException");

            Assert.AreEqual(exc.ErrorCode, ErrorCode.UNKNOWN, "Exception must have UNKNOWN error code");
        }
Beispiel #7
0
        public void SameMovementMustThrowPlayerMovementExceptionWithMustRetry()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            Player     other   = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);
            game.PlayerMove(playerA, new Coordinate(0, 0));

            // Then
            PlayerMovementException pme = Assert.Throws <PlayerMovementException>(() => game.PlayerMove(playerB, new Coordinate(0, 0)), "If the movement has been already made, must raise an exception");

            Assert.AreEqual(pme.ErrorCode, ErrorCode.MOVEMENT_ERROR_MUST_RETRY, "Exception must have MOVEMENT_ERROR_MUST_RETRY");
        }
Beispiel #8
0
        public void TicTacToeHappyPath()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);
            game.PlayerMove(playerA, new Coordinate(0, 0));
            game.PlayerMove(playerA, new Coordinate(0, 1));

            // Then
            TicTacToeGameOverException rest = Assert.Throws <TicTacToeGameOverException>(() => game.PlayerMove(playerA, new Coordinate(0, 2)), "Winning the game throws TicTacToeGameOverException");

            Assert.AreEqual(rest.Player, playerA, "Winning player must be Player A");
            Assert.AreEqual(rest.Winner, true, "Winner flag must be set to true");
        }
Beispiel #9
0
        public void PlayerAMustBeDifferentFromPlayerB()
        {
            // Given
            Player     player = new Player();
            ITicTacToe game   = new TicTacToeImpl();

            // When/Then
            NotValidValueException exc = Assert.Throws <NotValidValueException>(() => game.StartGame(grid, player, player), "Initializing with same player must return exception");

            Assert.AreEqual(ErrorCode.PLAYERS_ARE_SAME, exc.ErrorCode, "Exception ErrorCode must be PLAYERS_ARE_SAME");
        }