def test_generate_bad(self): for (width, height) in (([], 1), (1, []), ([], [])): with self.assertRaises(TypeError): Field((width, height), 2) for bombs in (-10, 0, 9, 10): with self.assertRaises(ValueError): Field.generate((3, 3), bombs)
def test_eq_operator(self): f1 = Field.generate((10, 10), 5) f2 = Field.generate((10, 10), 6) f3 = Field.generate((6, 6), 5) fx = [] self.assertNotEqual(f1, f2) self.assertNotEqual(f2, f3) self.assertNotEqual(f3, fx) self.assertEqual(f1, f1)
def test_add_score(self): scoreboard = Scoreboard(self.fn) params = ((8, 8), 10) self.assertListEqual([], scoreboard.get_scores(*params)) scoreboard.add_score(Field.generate(*params), "u1", 1500) scoreboard.add_score(Field.generate(*params), "u2", 400) scoreboard.add_score(Field.generate(*params), "u3", 5000) self.assertListEqual([("u2", 400.0), ("u1", 1500.0), ("u3", 5000.0)], scoreboard.get_scores(*params)) _ = Scoreboard(self.fn) self.assertEqual(3, len(scoreboard.get_scores(*params)))
def test_add_score(self): scoreboard = Scoreboard(self.fn) params = ((8, 8), 10) self.assertListEqual([], scoreboard.get_scores(*params)) scoreboard.add_score(Field.generate(*params), "u1", 1500) scoreboard.add_score(Field.generate(*params), "u2", 400) scoreboard.add_score(Field.generate(*params), "u3", 5000) self.assertListEqual([("u2", 400.0), ("u1", 1500.0), ("u3", 5000.0)], scoreboard.get_scores(*params)) _ = Scoreboard(self.fn) self.assertEqual(3, len(scoreboard.get_scores(*params)))
def test_handler_remove(self): state = GameState(Field.generate((2, 2), 2)) callargs = [] handler = lambda *args: callargs.append(args) state.add_cell_handler(handler) state.set_flag((0, 0)) self.assertEqual(1, len(callargs)) state.remove_cell_handler(handler) state.set_flag((1, 1)) self.assertEqual(1, len(callargs))
def test_eq_operator(self): field = Field((3, 3), {(0, 1), (1, 0), (1, 1)}) state0 = GameState(field) state1 = GameState(field) self.assertEqual(state0, state1) state0.set_flag((1, 1)) self.assertNotEqual(state0, state1) state0.unset_flag((1, 1)) self.assertEqual(state0, state1) state2 = GameState(field) state2.open_cell((0, 0)) self.assertNotEqual(state1, state2) state3 = GameState(field) state3.open_cell((2, 2)) self.assertNotEqual(state1, state3) self.assertNotEqual(state0, []) self.assertNotEqual(GameState(Field.generate((2, 2), 1)), GameState(Field.generate((2, 2), 2)))
def test_handler_flag(self): state = GameState(Field.generate((5, 5), 8)) callargs = [] state.add_cell_handler(lambda *args: callargs.append(args)) state.set_flag((2, 4)) self.assertEqual(1, len(callargs)) self.assertEqual(((2, 4), CellState.FLAG), callargs[-1]) state.set_flag((2, 4)) self.assertEqual(1, len(callargs)) state.unset_flag((2, 4)) self.assertEqual(2, len(callargs)) self.assertEqual(((2, 4), CellState.UNKNOWN), callargs[-1])
def test_clone(self): calls = 0 def handler(*args): nonlocal calls calls += 1 state = GameState(Field.generate((10, 15), 21)) state.add_cell_handler(handler) state2 = copy.copy(state) self.assertEqual(state, state2) state2.set_flag((0, 0)) self.assertNotEqual(state, state2) self.assertEqual(1, calls) state2.remove_cell_handler(handler) state2.set_flag((1, 0)) self.assertEqual(1, calls) state.set_flag((5, 0)) self.assertEqual(2, calls)
def test_generate(self): field = Field.generate(('5', '7'), '10') self.assertEqual((5, 7), field.size()) self.assertEqual(10, field.bombs())
def test_handler_bad(self): state = GameState(Field.generate((2, 2), 1)) with self.assertRaises(TypeError): state.add_cell_handler([])
def test_save_and_load(self): field = Field.generate((10, 10), 15) self.assertEqual(field, Field.fromstr(str(field)))