tictactoe: Add tests for TicTacToeModel functions.

Encountered several issues with test cases initially,
where test cases were failing due to the structure of the tests
within the file. This was circumvented by creating a separate
instance of the class TicTacToeModel for each test case
in order to focus on unit testing. There might be an issue with
the _get_game_handlers() function at the end of the file
which is used to obtain the model and message information,
it seems to be getting the entire class TicTacToeModel instead of
creating an instance of the class. Overall, this commit focuses
on creating a precedent for writing test cases involving TicTacToeModel,
and implements basic cases which can be extended to larger
edge cases in the future. Testing was done locally by running
./tools/test-bots until we were able to get the response 'ok' for
all the test cases written. We also used coverage in order to test the
coverage of the cases. While we were not able to increase
the coverage as much as we would have liked, we were able to
identify several issues and fix them, hopefully making it easier
for future contributors to add additional test cases to TicTacToeModel.
Overall, we would like special attention given to the way
that TicTacToeModel() was initailized in these tests and whether or not
this is a scalable precedent for future tests.

Fixes: #122
This commit is contained in:
Kishan Patel 2018-04-15 22:25:55 -04:00 committed by showell
parent e6972bcbb9
commit ea370031ee

View file

@ -10,6 +10,91 @@ class TestTicTacToeBot(BotTestCase, DefaultTests):
# FIXME: Add tests for computer moves
# FIXME: Add test lib for game_handler
# Tests for TicTacToeModel functions
# Things that might need to be checked: how model is being used in these functions,
# When running the tests, many of the failures involved current_board. This
# may need to be initialized prior to the constructor initialization in order to
# avoid these errors.
def test_get_value(self) -> None:
board = [[0, 1, 0],
[0, 0, 0],
[0, 0, 2]]
position = (0, 1)
response = 1
self._test_get_value(board, position, response)
def _test_get_value(self, board: List[List[int]], position: Tuple[int, int], expected_response: int) -> None:
model, message_handler = self._get_game_handlers()
tictactoeboard = model(board)
response = tictactoeboard.get_value(board, position)
self.assertEqual(response, expected_response)
def test_determine_game_over_with_win(self) -> None:
board = [[1, 1, 1],
[0, 2, 0],
[2, 0, 2]]
players = ['Human', 'Computer']
response = 'current turn'
self._test_determine_game_over_with_win(board, players, response)
def _test_determine_game_over_with_win(self, board: List[List[int]], players: List[str], expected_response: str) -> None:
model, message_handler = self._get_game_handlers()
tictactoegame = model(board)
response = tictactoegame.determine_game_over(players)
self.assertEqual(response, expected_response)
def test_determine_game_over_with_draw(self) -> None:
board = [[1, 2, 1],
[1, 2, 1],
[2, 1, 2]]
players = ['Human', 'Computer']
response = 'draw'
self._test_determine_game_over_with_draw(board, players, response)
def _test_determine_game_over_with_draw(self, board: List[List[int]], players: List[str], expected_response: str) -> None:
model, message_handler = self._get_game_handlers()
tictactoeboard = model(board)
response = tictactoeboard.determine_game_over(players)
self.assertEqual(response, expected_response)
def test_board_is_full(self) -> None:
board = [[1, 0, 1],
[1, 2, 1],
[2, 1, 2]]
response = False
self._test_board_is_full(board, response)
def _test_board_is_full(self, board: List[List[int]], expected_response: bool) -> None:
model, message_handler = self._get_game_handlers()
tictactoeboard = model(board)
response = tictactoeboard.board_is_full(board)
self.assertEqual(response, expected_response)
def test_contains_winning_move(self) -> None:
board = [[1, 1, 1],
[0, 2, 0],
[2, 0, 2]]
response = True
self._test_contains_winning_move(board, response)
def _test_contains_winning_move(self, board: List[List[int]], expected_response: bool) -> None:
model, message_handler = self._get_game_handlers()
tictactoeboard = model(board)
response = tictactoeboard.contains_winning_move(board)
self.assertEqual(response, expected_response)
def test_player_color(self) -> None:
turn = 0
response = ':cross_mark_button:'
self._test_player_color(turn, response)
def _test_player_color(self, turn: int, expected_response: str) -> None:
model, message_handler = self._get_game_handlers()
response = message_handler.get_player_color(0)
self.assertEqual(response, expected_response)
def test_static_responses(self) -> None:
model, message_handler = self._get_game_handlers()
self.assertNotEqual(message_handler.get_player_color(0), None)
@ -39,11 +124,6 @@ class TestTicTacToeBot(BotTestCase, DefaultTests):
response = message_handler.parse_board(board)
self.assertEqual(response, expected_response)
def _test_determine_game_over(self, board: List[List[int]], players: List[str], expected_response: str) -> None:
model, message_handler = self._get_game_handlers()
response = model.determine_game_over(players)
self.assertEqual(response, expected_response)
def add_user_to_cache(self, name: str, bot: Any=None) -> Any:
if bot is None:
bot, bot_handler = self._get_handlers()