コード例 #1
0
class TestGameCommentator:
    def setup_method(self, method):
        team1 = Team(0, Player("Alex"), Player("Thibaud"))
        team2 = Team(1, Player("Marie"), Player("Veltin"))
        deck = Deck()
        distributor = Distributor()
        referee = Referee()
        self.game = Round(0, team1, team2, deck, distributor, referee)
        self.output = StringIO()
        self.commentator = RoundCommentator(1, stream=self.output)

    def test_commentator_should_introduce_game(self):
        self.commentator.comment_start_of_round(self.game)
        assert self.output.getvalue().strip() == """Game 0"""

    def test_commentator_should_announce_cards(self):
        regex = re.compile(r"([\w]+ plays [\w]+ of [\w]+(, )*){4}.")
        self.game.distribute_cards_and_choose_trump()
        trick = self.game.play_one_turn()
        self.commentator.comment_turn(self.game, trick)
        comment = self.output.getvalue().strip()
        assert regex.match(comment) is not None

    def test_commentator_should_comment_end_of_turn(self):
        regex = re.compile(r"[\w]+ wins.")
        self.game.distribute_cards_and_choose_trump()
        self.game.play_one_turn()
        self.commentator.comment_end_of_turn(self.game)
        comment = self.output.getvalue().strip()
        assert regex.match(comment) is not None

    def test_commentator_should_comment_end_of_game(self):
        regex = re.compile(r"(Team \w+: \d{1,3} points.\n){2}Team \w+ wins!")
        self.game.distribute_cards_and_choose_trump()
        self.game.play()
        self.game.count_points()
        self.commentator.comment_end_of_round(self.game)
        comment = self.output.getvalue().strip()
        assert regex.match(comment) is not None

    def test_commentator_should_comment_distribution(self):
        regex = re.compile(r"Trump suit will be (Clubs|Hearts|Diamonds|Spades).")
        self.game.distribute_cards_and_choose_trump()
        self.commentator.comment_distribution(self.game)
        comment = self.output.getvalue().strip()
        assert regex.match(comment) is not None
コード例 #2
0
class TestRound:
    def setup_method(self, method):
        team1 = Team(0, Player("Alex"), Player("Thibaud"))
        team2 = Team(1, Player("Marie"), Player("Veltin"))
        deck = Deck()
        distributor = Distributor()
        referee = Referee()
        self.round = Round(0, team1, team2, deck, distributor,
                           referee, seed=36)

    def test_players_should_have_no_hand_at_beginning(self):
        for player in self.round.players:
            assert len(player.hand) == 0

    def test_players_should_have_8_cards_after_distribution(self):
        self.round.distribute_cards_and_choose_trump()
        for player in self.round.players:
            assert len(player.hand) == 8

    def test_all_cards_should_be_ranked_after_distribution(self):
        self.round.distribute_cards_and_choose_trump()
        for player in self.round.players:
            for card in player.hand:
                assert isinstance(card, RankedCard)

    def test_first_distribution_should_reveal_card(self):
        revealed_card = self.round.perform_first_distribution_and_reveal_card()
        assert isinstance(revealed_card, Card)

    def test_players_should_have_5_cards_in_hand_after_first_distribution(self):
        _ = self.round.perform_first_distribution_and_reveal_card()
        for player in self.round.players:
            assert len(player.hand) == 5

    def test_deck_should_be_empty_after_distribution(self):
        self.round.distribute_cards_and_choose_trump()
        assert len(self.round.deck) == 0

    def test_players_should_have_8_minus_n_cards_in_hand_after_n_turns(self):
        self.round.distribute_cards_and_choose_trump()
        for i in range(8):
            self.round.play_one_turn()
            for player in self.round.players:
                assert len(player.hand) == 8 - (i + 1)

    def test_teams_should_have_32_cards_total_after_game_has_been_played(self):
        self.round.distribute_cards_and_choose_trump()
        print(self.round.get_team_by_id(0).player1.hand[0].owner)
        self.round.play()
        assert len(self.round.get_team_by_id(0).won_cards) + len(self.round.get_team_by_id(1).won_cards) == 32

    def test_total_points_at_the_end_should_be_162(self):
        self.round.distribute_cards_and_choose_trump()
        self.round.play()
        self.round.count_points()
        assert sum([team.current_game_points for team in self.round.teams]) == 162

    def test_play_should_not_fail(self):
        self.round.distribute_cards_and_choose_trump()
        assert self.round.play() == 0