示例#1
0
    def test_o_wins(self):
        board = TicTacToeBoard()
        board["C3"] = "O"
        board["A3"] = "X"
        board["B2"] = "O"
        board["B1"] = "X"
        board["A1"] = "O"

        self.assertEqual("O wins!", board.game_status())
示例#2
0
    def test_draw(self):
        board = TicTacToeBoard()
        board["A1"] = 'O'
        board["B1"] = 'X'
        board["A3"] = 'O'
        board["A2"] = 'X'
        board["C2"] = 'O'
        board["C3"] = 'X'
        board["B3"] = 'O'
        board["B2"] = 'X'
        board["C1"] = 'O'

        self.assertEqual('Draw!', board.game_status())
示例#3
0
    def test_print_empty_board(self):
        board = TicTacToeBoard()
        empty_board = """
  -------------
3 |   |   |   |
  -------------
2 |   |   |   |
  -------------
1 |   |   |   |
  -------------
    A   B   C
"""
        self.assertEqual(empty_board.strip(), str(board).strip())
示例#4
0
    def test_print_full_board(self):
        board = TicTacToeBoard()
        board_str = """
  -------------
3 | X | X | X |
  -------------
2 | O | O | X |
  -------------
1 | O | X | O |
  -------------
    A   B   C
"""
        board["A3"] = "X"
        board["A2"] = "O"
        board["B3"] = "X"
        board["B2"] = "O"
        board["C3"] = "X"
        board["C1"] = "O"
        board["C2"] = "X"
        board["A1"] = "O"
        board["B1"] = "X"
        self.assertEqual(board_str.strip(), str(board).strip())
示例#5
0
 def test_move_when_cell_number_before_letter_raises(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidKey):
         board["1B"] = "X"
示例#6
0
 def test_game_in_progress_when_board_instantiated(self):
     board = TicTacToeBoard()
     self.assertEqual("Game in progress.", board.game_status())
示例#7
0
 def test_move_twice_with_same_value(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.NotYourTurn):
         board["A1"] = 'X'
         board["B1"] = 'X'
示例#8
0
 def test_move_twice_on_same_cell(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidMove):
         board["A1"] = 'X'
         board["A1"] = 'O'
示例#9
0
 def test_can_move_when_cell_valid(self):
     board = TicTacToeBoard()
     board["B1"] = "X"
     board["C1"] = "O"
     self.assertEqual(board["B1"], "X")
     self.assertEqual(board["C1"], "O")
示例#10
0
 def test_move_when_invalid_value(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidValue):
         board["B3"] = "d"
示例#11
0
 def test_move_when_cell_has_invalid_number(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidKey):
         board["B8"] = "X"
示例#12
0
 def test_move_when_cell_is_invalid_letter(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidKey):
         board["Z"] = "X"
示例#13
0
 def test_move_when_cell_is_just_a_number(self):
     board = TicTacToeBoard()
     with self.assertRaises(solution.InvalidKey):
         board["1"] = "X"
示例#14
0
    def test_game_in_progress(self):
        board = TicTacToeBoard()
        board["A1"] = 'X'
        board["A3"] = 'O'

        self.assertEqual('Game in progress.', board.game_status())
示例#15
0
 def setUp(self):
     self.board = TicTacToeBoard()
示例#16
0
class TestTicTacToe(unittest.TestCase):
    def setUp(self):
        self.board = TicTacToeBoard()

        # # column tests
    def test_left_column_with_x_win(self):
        self.board["A1"] = "X"
        self.board["B2"] = "O"
        self.board["A2"] = "X"
        self.board["B3"] = "O"
        self.board["A3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

    def test_middle_column_with_x_win(self):
        self.board["B1"] = "X"
        self.board["A1"] = "O"
        self.board["B2"] = "X"
        self.board["A2"] = "O"
        self.board["B3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

    def test_right_column_with_x_win(self):
        self.board["C1"] = "X"
        self.board["A1"] = "O"
        self.board["C2"] = "X"
        self.board["B2"] = "O"
        self.board["C3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

        # # row tests
    def test_low_row_with_x_win(self):
        self.board["A1"] = "X"
        self.board["A3"] = "O"
        self.board["B1"] = "X"
        self.board["B2"] = "O"
        self.board["C1"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

    def test_middle_row_with_x_win(self):
        self.board["A2"] = "X"
        self.board["A1"] = "O"
        self.board["B2"] = "X"
        self.board["B3"] = "O"
        self.board["C2"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

    def test_top_row_with_x_win(self):
        self.board["A3"] = "X"
        self.board["A2"] = "O"
        self.board["B3"] = "X"
        self.board["B2"] = "O"
        self.board["C3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

        # # diagonal tests
    def test_main_diagonal_with_x_win(self):
        self.board["A1"] = "X"
        self.board["A2"] = "O"
        self.board["B2"] = "X"
        self.board["B3"] = "O"
        self.board["C3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

    def test_main_diagonal_with_x_win(self):
        self.board["C1"] = "X"
        self.board["A2"] = "O"
        self.board["B2"] = "X"
        self.board["B3"] = "O"
        self.board["A3"] = "X"

        self.assertEqual("X wins!", self.board.game_status())

        # # column tests
    def test_left_column_with_o_win(self):
        self.board["A1"] = "O"
        self.board["B1"] = "X"
        self.board["A2"] = "O"
        self.board["B2"] = "X"
        self.board["A3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_middle_column_with_o_win(self):
        self.board["B1"] = "O"
        self.board["A1"] = "X"
        self.board["B2"] = "O"
        self.board["A2"] = "X"
        self.board["B3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_right_column_with_o_win(self):
        self.board["C1"] = "O"
        self.board["A1"] = "X"
        self.board["C2"] = "O"
        self.board["A3"] = "X"
        self.board["C3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

        # # row tests
    def test_low_row_with_o_win(self):
        self.board["A1"] = "O"
        self.board["B2"] = "X"
        self.board["B1"] = "O"
        self.board["C2"] = "X"
        self.board["C1"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_middle_row_with_o_win(self):
        self.board["A2"] = "O"
        self.board["A1"] = "X"
        self.board["B2"] = "O"
        self.board["C3"] = "X"
        self.board["C2"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_top_row_with_o_win(self):
        self.board["A3"] = "O"
        self.board["A1"] = "X"
        self.board["B3"] = "O"
        self.board["B2"] = "X"
        self.board["C3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

        # # diagonal tests
    def test_main_diagonal_with_o_win(self):
        self.board["A1"] = "O"
        self.board["A2"] = "X"
        self.board["B2"] = "O"
        self.board["B3"] = "X"
        self.board["C3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_main_diagonal_with_o_win(self):
        self.board["C1"] = "O"
        self.board["A2"] = "X"
        self.board["B2"] = "O"
        self.board["B3"] = "X"
        self.board["A3"] = "O"

        self.assertEqual("O wins!", self.board.game_status())

    def test_draw(self):
        self.board["B1"] = "O"
        self.board["A1"] = "X"
        self.board["A2"] = "O"
        self.board["B2"] = "X"
        self.board["C3"] = "O"
        self.board["C2"] = "X"
        self.board["A3"] = "O"
        self.board["B3"] = "X"
        self.board["C1"] = "O"

        self.assertEqual("Draw!", self.board.game_status())

    def test_game_in_progress(self):
        self.board["A1"] = "X"
        self.board["A2"] = "O"

        self.assertEqual("Game in progress.", self.board.game_status())

    # test __str__() method
    def test_print_empty_board(self):

        board_string = "\n  -------------\n\
3 |   |   |   |\n\
  -------------\n\
2 |   |   |   |\n\
  -------------\n\
1 |   |   |   |\n\
  -------------\n\
    A   B   C  \n"

        self.assertEqual(board_string, self.board.__str__())

    def test_print_full_board(self):

        self.board["B1"] = "O"
        self.board["A1"] = "X"
        self.board["A2"] = "O"
        self.board["B2"] = "X"
        self.board["C3"] = "O"
        self.board["C2"] = "X"
        self.board["A3"] = "O"
        self.board["B3"] = "X"
        self.board["C1"] = "O"

        board_string = "\n  -------------\n\
3 | O | X | O |\n\
  -------------\n\
2 | O | X | X |\n\
  -------------\n\
1 | X | O | O |\n\
  -------------\n\
    A   B   C  \n"

        self.assertEqual(board_string, self.board.__str__())