def test_create_board(self): board = Board("player1") self.assertEqual(board.display_board(), [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
def test_udlr(self): for i in self.udlr.keys(): n = [[10, [30, 30], i]] n_pos = [30, 30] a = Board(100, n, self.ts1b) for jj in range(0, 10): if jj == 9: self.assertEqual(a.get_hit(n_pos, "a"), "SUNK") else: self.assertEqual(a.get_hit(n_pos, "a"), "HIT") n_pos[0] += self.udlr[i][0] n_pos[1] += self.udlr[i][1] self.assertEqual(a.get_hit(n_pos, "a"), "NOT HIT")
class BoardTests(unittest.TestCase): def setUp(self): self.board = Board(board_size=10) def test_board_length(self): self.assertEqual(len(self.board.grid), 10) self.assertEqual(len(self.board.grid[0]), 10) self.assertEqual(len(self.board.grid[-1]), 10) def test_board_labels(self): self.assertEqual(self.board.grid[0][0]['label'], 'A1') self.assertEqual(self.board.grid[-1][-1]['label'], 'J10') self.assertEqual(self.board.grid[0][0]['mark'], ' ') self.assertEqual(self.board.grid[-1][-1]['mark'], ' ') def test_add_ships(self): self.board.add_ships([Ship((1, 1), (1, 5), 'Carrier'), Ship((8, 8), (9, 8), 'Patrol boat')]) self.assertEqual(len(self.board.ships), 2)
def test_range(self): self.assertEqual( Board(100, [[5, [3, 0], "DOWN"]], self.ts1b).ships["a"], False) self.assertNotEqual( Board(100, [[5, [4, 0], "DOWN"]], self.ts1b).ships["a"], False) self.assertEqual( Board(100, [[10, [91, 0], "UP"]], self.ts1b).ships["a"], False) self.assertNotEqual( Board(100, [[10, [90, 0], "UP"]], self.ts1b).ships["a"], False) self.assertEqual( Board(100, [[10, [0, 91], "RIGHT"]], self.ts1b).ships["a"], False) self.assertNotEqual( Board(100, [[10, [0, 90], "RIGHT"]], self.ts1b).ships["a"], False) self.assertEqual( Board(100, [[10, [0, 8], "LEFT"]], self.ts1b).ships["a"], False) self.assertNotEqual( Board(100, [[10, [0, 9], "LEFT"]], self.ts1b).ships["a"], False)
def setUp(self): self.board = Board(board_size=10)
"""No descrption given. What do *you* think this does?""" from battleship import Board print("******** BEGINNING TESTCASE ********") board = Board(20) board.print() print("Testcase completed successfully.")
def test_ship_gets_removed(self): a = Board(100, deepcopy(self.ts2a), self.ts1b) for i in range(5, 1, -1): self.assertEqual(a.get_hit([i, 5], "a"), "HIT") self.assertEqual(a.get_hit([1, 5], "a"), "SUNK") self.assertEqual(len(a.ships["a"]), 0)
def test_hit_remove(self): a = Board(100, deepcopy(self.ts2a), self.ts1b) self.assertEqual(a.get_hit([5, 5], "a"), "HIT") self.assertEqual(a.get_hit([5, 5], "a"), "NOT HIT")