From 9175c451f6300caa63f51cae768708d62bcd5830 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 20 Feb 2018 16:42:10 -0500 Subject: [PATCH] tests: Test handling of bad moves in connect four. This commit tests that we throw a BadMoveException if you try to move into a "full" column in connect four. This gets the controller up to 100% coverage. --- .../bots/connect_four/controller.py | 7 ++++++ .../bots/connect_four/test_connect_four.py | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/zulip_bots/zulip_bots/bots/connect_four/controller.py b/zulip_bots/zulip_bots/bots/connect_four/controller.py index 2ecabf5..a6f371c 100644 --- a/zulip_bots/zulip_bots/bots/connect_four/controller.py +++ b/zulip_bots/zulip_bots/bots/connect_four/controller.py @@ -25,6 +25,13 @@ class ConnectFourModel(object): def update_board(self, board): self.current_board = deepcopy(board) + def get_column(self, col): + # We use this in tests. + return [ + self.current_board[i][col] + for i in range(6) + ] + def validate_move(self, column_number): if column_number < 0 or column_number > 6: return False diff --git a/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py b/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py index e837e9d..af3ead9 100644 --- a/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py +++ b/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py @@ -3,6 +3,7 @@ from zulip_bots.test_lib import BotTestCase from contextlib import contextmanager from unittest.mock import MagicMock from zulip_bots.bots.connect_four.connect_four import * +from zulip_bots.game_handler import BadMoveException from typing import Dict, Any, List @@ -479,3 +480,24 @@ The first player to get 4 in a row wins!\n Good Luck!') confirmWinStates(vertical_win_boards) confirmWinStates(major_diagonal_win_boards) confirmWinStates(minor_diagonal_win_boards) + + def test_more_logic(self) -> None: + model = ConnectFourModel() + move = 'move 4' + col = 3 # zero-indexed + + self.assertEqual(model.get_column(col), [0, 0, 0, 0, 0, 0]) + model.make_move(move, player_number=0) + self.assertEqual(model.get_column(col), [0, 0, 0, 0, 0, 1]) + model.make_move(move, player_number=0) + self.assertEqual(model.get_column(col), [0, 0, 0, 0, 1, 1]) + model.make_move(move, player_number=1) + self.assertEqual(model.get_column(col), [0, 0, 0, -1, 1, 1]) + model.make_move(move, player_number=1) + self.assertEqual(model.get_column(col), [0, 0, -1, -1, 1, 1]) + model.make_move(move, player_number=1) + self.assertEqual(model.get_column(col), [0, -1, -1, -1, 1, 1]) + model.make_move(move, player_number=0) + self.assertEqual(model.get_column(col), [1, -1, -1, -1, 1, 1]) + with self.assertRaises(BadMoveException): + model.make_move(move, player_number=0)