Example #1
0
    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 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_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]])
Example #4
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 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]])
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]])