def testNextHole(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(10, players) classHits._current_hole = 1 for player in classHits._players_array: player.hits_no_more = 1 # Switch to last hole classHits._switch_to_next_hole() self.assertEqual(classHits._current_hole, 2) for player in classHits._players_array: self.assertEqual(player.hits_no_more, 0) self.assertEqual(classHits._finished, False) self.assertEqual(classHits._current_player, 2) # Switch to the holenum that is > than number of players, e.g. next player = 0 classHits._switch_to_next_hole() self.assertEqual(classHits._current_hole, 3) self.assertEqual(classHits._finished, False) self.assertEqual(classHits._current_player, 0) # Switch to the end of game classHits._current_hole = 9 classHits._switch_to_next_hole() self.assertEqual(classHits._current_hole, 10) self.assertEqual(classHits._finished, True)
def test_scenario(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(4, players) self._first_hole(m) self._second_hole(m) self._third_hole(m) self.assertFalse(m.finished) self._fourth_hole(m) self.assertEqual(m.get_winners(), [players[0]])
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 testIsFinished(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(len(players), players) for player in classHits._players_array: player.hits_no_more = 0 # NOT Everybody finished self.assertEqual(classHits._everybody_finished_hole(), False) # NOT Everybody finished, only one classHits._players_array[0].hits_no_more = 1 self.assertEqual(classHits._everybody_finished_hole(), False) for player in classHits._players_array: player.hits_no_more = 1 # Everybody finished self.assertEqual(classHits._everybody_finished_hole(), True)
def testInit(self): players = [Player('A'), Player('B'), Player('C')] # Wrong INIT with self.assertRaises(TypeError): classObj = HitsMatch() with self.assertRaises(TypeError): classObj = HolesMatch() with self.assertRaises(TypeError): classObj = HitsMatch(players) classHits = HitsMatch(len(players), players) self.assertEqual(classHits._results_table, [ [None, None, None], [None, None, None], [None, None, None], ]) classHoles = HolesMatch(0, []) self.assertEqual(classHoles.finished, 1)
def test_hit(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(3, players) m.hit(True) self.assertEqual(m.finished, False) with self.assertRaises(RuntimeError): m.get_winners()
def testGetWinners_Hits(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(len(players), players) classHits._finished = False with self.assertRaises(RuntimeError): classHits.get_winners() # Test on 1 winner classHits._results_table = [ [1, 4, 2], [1, 4, 1], [1, 2, 5], ] classHits._finished = True self.assertEqual(classHits.get_winners(), [players[0]]) # Test on 2 and more winners classHits._results_table = [ [1, 4, 1], [1, 4, 1], [1, 2, 1], ] self.assertEqual(classHits.get_winners(), [players[0], players[2]])
def test_scenario(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(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_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)])
def test_change_hole(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(3, players) m.change_hole() self.assertEqual(m.hit_list,deque([{'number': 1,'hit': 0},{'number': 2,'hit': 0},{'number': 0,'hit': 0}]))
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 setUp(self): self._players = [Player('A'), Player('B'), Player('C')] self._m1 = HitsMatch(3, self._players) self._m2 = HolesMatch(3, self._players)
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 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 testHitInHitsMatch(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(len(players), players) # Test usual bad hit for 0 player classHits._results_table = [ [1, 8, 2], [1, 8, 0], [None, None, None], ] classHits._current_player = 0 classHits._current_hole = 2 classHits.hit() self.assertEqual(classHits._results_table, [ [1, 8, 2], [1, 8, 0], [1, None, None], ]) for player in classHits._players_array: self.assertEqual(player.hits_no_more, 0) # Test usual good hit for 1 player classHits.hit(True) self.assertEqual(classHits._results_table, [ [1, 8, 2], [1, 8, 0], [1, 1, None], ]) self.assertEqual(classHits._players_array[0].hits_no_more, 0) self.assertEqual(classHits._players_array[1].hits_no_more, 1) self.assertEqual(classHits._players_array[2].hits_no_more, 0) # Test no hit for 9 points bad hit for 1 player => 10 points classHits._players_array[1].hits_no_more = 0 classHits._current_player = 1 classHits._current_hole = 0 classHits.hit() self.assertEqual(classHits._results_table, [ [1, 10, 2], [1, 8, 0], [1, 1, None], ]) self.assertEqual(classHits._players_array[1].hits_no_more, 1) # Test no hit for 8 points GOOD hit for 1 player => 9 points classHits._players_array[1].hits_no_more = 0 classHits._current_player = 1 classHits._current_hole = 1 classHits.hit(True) self.assertEqual(classHits._results_table, [ [1, 10, 2], [1, 9, 0], [1, 1, None], ]) self.assertEqual(classHits._players_array[1].hits_no_more, 1)
def testSetNextUser(self): players = [Player('A'), Player('B'), Player('C')] classHits = HitsMatch(len(players), players) # Should set next player 2(last), but not hole classHits._current_player = 1 classHits._current_hole = 1 classHits._set_next_player_and_hole_if_needed() self.assertEqual(classHits._current_hole, 1) self.assertEqual(classHits._current_player, 2) # Should set next player 0(first)!!!, but not hole classHits._set_next_player_and_hole_if_needed() self.assertEqual(classHits._current_hole, 1) self.assertEqual(classHits._current_player, 0) # Should set next player 2(= number of hole), AND next hole 2 for player in classHits._players_array: player.hits_no_more = 1 classHits._set_next_player_and_hole_if_needed() self.assertEqual(classHits._current_hole, 2) self.assertEqual(classHits._current_player, 2) # Should set finished state for player in classHits._players_array: player.hits_no_more = 1 classHits._set_next_player_and_hole_if_needed() self.assertEqual(classHits.finished, True)
def test_table(self): players = [Player('A'), Player('B'), Player('C')] m = HitsMatch(3, players) (self.assertEqual(m._table[0][0]._name, players[0]._name) for i in range(0, 2))
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]])