def testGetTable(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(len(players), players) # Current hole is not finished and some players did not ended playing, but scored classHits._results_table = [ [1, 0, 1], [1, 2, None], [None, None, None], ] classHits._current_hole = 1 for player in classHits._players_array: player.hits_no_more = 0 classHits._players_array[0].hits_no_more = 1 self.assertEqual(classHits.get_table(), [ ('A', 'B', 'C'), (1, 0, 1), (1, None, None), (None, None, None), ]) # Current hole is finished classHits._results_table = [ [1, 0, 1], [1, 2, 0], [None, None, None], ] classHits._current_hole = 2 for player in classHits._players_array: player.hits_no_more = 0 self.assertEqual(classHits.get_table(), [ ('A', 'B', 'C'), (1, 0, 1), (1, 2, 0), (None, None, None), ]) # Game finished classHits._results_table = [ [1, 0, 1], [1, 2, 0], [1, 1, 1], ] classHits._current_hole = 3 self.assertEqual(classHits.get_table(), [ ('A', 'B', 'C'), (1, 0, 1), (1, 2, 0), (1, 1, 1), ])
def append_test(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(3, players) (self.assertEqual(m._table[0][i]._name, players[i]._name) for i in range(0, 2)) listt = ['a', 'b', 'c'] m.append_to_table(listt) self.assertEqual(m.get_table(), [ ('A', 'B', 'C'), ('A', 'B', 'C'), ])
def test_get_table(self): players = [ Player('A'), Player('B'), Player('E'), ] m = HitsMatch(2, players) k = HolesMatch(2, players) self.assertEqual(m.get_table(), [('A', 'B', 'E'), (None, None, None), (None, None, None)]) self.assertEqual(k.get_table(), [('A', 'B', 'E'), (None, None, None), (None, None, None)]) m.hit(True) m.hit(True) self.assertEqual(m.get_table(), [('A', 'B', 'E'), (1, 1, None), (None, None, None)]) k.hit(True) # 1 k.hit() # 2 k.hit() # 3 k.hit(True) # 2 k.hit() # 3 self.assertEqual(k.get_table(), [('A', 'B', 'E'), (1, 0, 0), (None, 1, 0)])
class TestHitsMatch(unittest.TestCase): def setUp(self): self.players_list = [Player('A'), Player('B'), Player('C')] self.players_names_tuple = tuple(player.name for player in self.players_list) self.hits_match = HitsMatch(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.hits_match.hit() # 1 self.assertEqual([ self.players_names_tuple, (None, None, None), (None, None, None), (None, None, None) ], self.hits_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.hits_match.hit(success=True) # 1 self.assertEqual([ self.players_names_tuple, (1, None, None), (None, None, None), (None, None, None) ], self.hits_match.get_table()) def test_first_hole_auto_closing_after_reaching_the_limit_of_hits_1(self): """ Checks reaching the limit of hits """ self.hits_match.hit() # 1 self.hits_match.hit(success=True) # 2 self.hits_match.hit() # 3 self.hits_match.hit(success=True) # 1 [self.hits_match.hit() for _ in range(8)] # 3 self.assertEqual([ self.players_names_tuple, (2, 1, 10), (None, None, None), (None, None, None) ], self.hits_match.get_table()) def test_first_hole_auto_closing_after_reaching_the_limit_of_hits_2(self): """ Checks that hole will close automatically after 30 unsuccessful hits """ for _ in range(10): for _ in range(3): self.hits_match.hit() # 2, 3, 1 self.assertEqual([ self.players_names_tuple, (10, 10, 10), (None, None, None), (None, None, None) ], self.hits_match.get_table()) def _close_first_hole_with_successful_first_hits(self): self.hits_match.hit(success=True) # 1 self.hits_match.hit(success=True) # 2 self.hits_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.hits_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.hits_match.get_table()) def test_second_hole_auto_closing_after_reaching_the_limit_of_hits(self): """ Checks reaching the limit of hits """ self._close_first_hole_with_successful_first_hits() self.hits_match.hit() # 2 self.hits_match.hit(success=True) # 3 self.hits_match.hit() # 1 self.hits_match.hit(success=True) # 2 [self.hits_match.hit() for _ in range(8)] # 1 self.assertEqual([ self.players_names_tuple, (1, 1, 1), (10, 2, 1), (None, None, None) ], self.hits_match.get_table()) def _close_second_hole_with_successful_first_hits(self): self.hits_match.hit(success=True) # 2 self.hits_match.hit(success=True) # 3 self.hits_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.hits_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.hits_match.get_table()) def _close_third_hole_with_successful_first_hits(self): self.hits_match.hit(success=True) # 3 self.hits_match.hit(success=True) # 1 self.hits_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.hits_match.hit() def test_runtime_error_by_get_winners_on_unfinished_game(self): with self.assertRaises(RuntimeError): self.hits_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.hits_match.get_table()) self.assertEqual(self.hits_match.get_winners(), self.players_list) def test_get_winners_2(self): self.hits_match.hit(success=True) # 1 self.hits_match.hit() # 2 self.hits_match.hit(success=True) # 3 self.hits_match.hit(success=True) # 2 self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() self.assertEqual( [self.players_names_tuple, (1, 2, 1), (1, 1, 1), (1, 1, 1)], self.hits_match.get_table()) self.assertEqual(self.hits_match.get_winners(), [self.players_list[0], self.players_list[2]]) def test_get_winners_3(self): self.hits_match.hit() # 1 self.hits_match.hit() # 2 self.hits_match.hit(success=True) # 3 self.hits_match.hit(success=True) # 1 self.hits_match.hit(success=True) # 2 self._close_second_hole_with_successful_first_hits() self._close_third_hole_with_successful_first_hits() self.assertEqual( [self.players_names_tuple, (2, 2, 1), (1, 1, 1), (1, 1, 1)], self.hits_match.get_table()) self.assertEqual(self.hits_match.get_winners(), [self.players_list[2]])