def test_scenario(self): players = [Player('A'), Player('B'), Player('C')] m = HolesMatch(3, players) self._first_hole(m) self._second_hole(m) with self.assertRaises(RuntimeError): m.get_winners() self._third_hole(m) with self.assertRaises(RuntimeError): m.hit() self.assertEqual(m.get_winners(), [players[0], players[2]])
def test_get_winners(self): players = [Player('A'), Player('B')] m = HolesMatch(2, players) m.hit() m.hit(True) m.hit(True) m.hit() self.assertEqual(m.get_winners(),[players[1]])
def test_get_winners(self): players = [Player('A'), Player('B'), Player('E'), Player('D')] m = HitsMatch(3, players) k = HolesMatch(3, players) with self.assertRaises(RuntimeError): m.get_winners() with self.assertRaises(RuntimeError): k.get_winners() m.hit(True) k.hit(True) with self.assertRaises(RuntimeError): m.get_winners() with self.assertRaises(RuntimeError): k.get_winners() for _ in range(11): m.hit(True) self.assertEqual(m.get_winners(), [players[0], players[1], players[2], players[3]]) for _ in range(11): k.hit(True) self.assertEqual(k.get_winners(), [players[0], players[1], players[2], players[3]])
def testGetWinners_Holes(self): players = [Player('A'), Player('B'), Player('C')] classHoles = HolesMatch(len(players), players) classHoles._finished = False with self.assertRaises(RuntimeError): classHoles.get_winners() # Test on 1 winner classHoles._results_table = [ [1, 0, 1], [1, 0, 1], [1, 0, 0], ] classHoles._finished = True self.assertEqual(classHoles.get_winners(), [players[0]]) # Test on 2 and more winners classHoles._results_table = [ [1, 0, 1], [1, 0, 1], [1, 1, 1], ] self.assertEqual(classHoles.get_winners(), [players[0], players[2]])
class TestHolesMatch(unittest.TestCase): def setUp(self): self.players_list = [Player('A'), Player('B'), Player('C')] # TODO: tuple instead of list self.players_names_tuple = tuple(player.name for player in self.players_list) self.holes_match = HolesMatch(3, self.players_list) def test_one_not_successful_hit_on_first_hole(self): """ Checks, that after unsuccessful hit nothing will be showed in the table """ self.holes_match.hit() # 1 self.assertEqual([ self.players_names_tuple, (None, None, None), (None, None, None), (None, None, None) ], self.holes_match.get_table()) def test_one_successful_hit_on_first_hole(self): """ Checks that first hit on the first hole will be made by a first player """ self.holes_match.hit(success=True) # 1 self.assertEqual([ self.players_names_tuple, (1, None, None), (None, None, None), (None, None, None) ], self.holes_match.get_table()) def test_auto_closed_first_hole_by_one_successful_hit(self): """ Checks that after one successful hit by one of the players hole will be closed and other players will get zero """ self.holes_match.hit() # 1 self.holes_match.hit(success=True) # 2 self.holes_match.hit() # 3 self.assertEqual([ self.players_names_tuple, (0, 1, 0), (None, None, None), (None, None, None) ], self.holes_match.get_table()) def test_hole_auto_closing_after_reaching_the_limit_of_hits(self): """ Checks that hole will close automatically after 30 unsuccessful hits """ for _ in range(10): for _ in range(3): self.holes_match.hit() # 2, 3, 1 self.assertEqual([ self.players_names_tuple, (0, 0, 0), (None, None, None), (None, None, None) ], self.holes_match.get_table()) def _close_first_hole_with_successful_first_hits(self): self.holes_match.hit(success=True) # 1 self.holes_match.hit(success=True) # 2 self.holes_match.hit(success=True) # 3 def test_one_successful_hit_on_second_hole(self): """ Checks that first hit on the second hole will be made by a second player """ self._close_first_hole_with_successful_first_hits() self.holes_match.hit(success=True) # 2 because it's a second hole self.assertEqual([ self.players_names_tuple, (1, 1, 1), (None, 1, None), (None, None, None) ], self.holes_match.get_table()) def test_auto_closed_second_hole_by_one_successful_hit(self): """ Checks that after one successful hit by one of the players hole will be closed and other players will get zero """ self._close_first_hole_with_successful_first_hits() self.holes_match.hit() # 2 self.holes_match.hit(success=True) # 3 self.holes_match.hit() # 1 self.assertEqual([ self.players_names_tuple, (1, 1, 1), (0, 0, 1), (None, None, None) ], self.holes_match.get_table()) def _close_second_hole_with_successful_first_hits(self): self.holes_match.hit(success=True) # 2 self.holes_match.hit(success=True) # 3 self.holes_match.hit(success=True) # 1 def test_one_successful_hit_on_third_hole(self): """ Checks that first hit on the third hole will be made by a third player """ self._close_first_hole_with_successful_first_hits() self._close_second_hole_with_successful_first_hits() self.holes_match.hit(success=True) # 3 because it's a third hole self.assertEqual( [self.players_names_tuple, (1, 1, 1), (1, 1, 1), (None, None, 1)], self.holes_match.get_table()) def _close_third_hole_with_successful_first_hits(self): self.holes_match.hit(success=True) # 3 self.holes_match.hit(success=True) # 1 self.holes_match.hit(success=True) # 2 def test_runtime_error_by_hit_on_finished_game(self): self._close_first_hole_with_successful_first_hits() self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() with self.assertRaises(RuntimeError): self.holes_match.hit() def test_runtime_error_by_get_winners_on_unfinished_game(self): with self.assertRaises(RuntimeError): self.holes_match.get_winners() def test_get_winners_1(self): self._close_first_hole_with_successful_first_hits() self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() self.assertEqual( [self.players_names_tuple, (1, 1, 1), (1, 1, 1), (1, 1, 1)], self.holes_match.get_table()) self.assertEqual(self.holes_match.get_winners(), self.players_list) def test_get_winners_2(self): self.holes_match.hit(success=True) self.holes_match.hit() self.holes_match.hit(success=True) self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() self.assertEqual( [self.players_names_tuple, (1, 0, 1), (1, 1, 1), (1, 1, 1)], self.holes_match.get_table()) self.assertEqual(self.holes_match.get_winners(), [self.players_list[0], self.players_list[2]]) def test_get_winners_3(self): self.holes_match.hit() self.holes_match.hit() self.holes_match.hit(success=True) self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() self.assertEqual( [self.players_names_tuple, (0, 0, 1), (1, 1, 1), (1, 1, 1)], self.holes_match.get_table()) self.assertEqual(self.holes_match.get_winners(), [self.players_list[2]])