def test_equality_detection(self): list_of_cards = [Card(10, 'O'), Card(3, 'C')] reversed_list_of_cards = list_of_cards.copy() reversed_list_of_cards.reverse() cards_a = Cards.from_list_of_cards(list_of_cards) cards_b = Cards.from_list_of_cards(reversed_list_of_cards) assert cards_a == cards_b
def test_dropping_last_card_when_cards_exist(self): list_of_cards = [Card(2, "E"), Card(1, "B"), Card(12, "O")] cards = Cards.from_list_of_cards(list_of_cards) number_of_cards_before_dropping_card = len(cards) dropped_card = cards.drop_cards() number_of_cards_after_dropping_card = len(cards) assert dropped_card == list_of_cards[ -1] and number_of_cards_before_dropping_card - number_of_cards_after_dropping_card == 1
def test_dropping_a_card_that_exists_in_cards(self): card_to_drop = Card(1, "E") list_of_cards = [Card(3, "E"), card_to_drop, Card(8, "O")] cards = Cards.from_list_of_cards(list_of_cards) number_of_cards_before_dropping_card = len(cards) dropped_card = cards.drop_cards(card_to_drop) number_of_cards_after_dropping_card = len(cards) assert dropped_card == card_to_drop and number_of_cards_before_dropping_card - number_of_cards_after_dropping_card == 1
def test_building_cards_from_list(self): all_card_items_belong_to_cards = True list_of_cards = [Card(10, "B"), Card(5, "E"), Card(12, "C")] cards = Cards.from_list_of_cards(list_of_cards) for index, card in enumerate(cards): if card != list_of_cards[index]: all_card_items_belong_to_cards = False assert isinstance(cards, Cards) and all_card_items_belong_to_cards
def test_sorting(self): correctly_sorted = True list_of_cards = [Card(11, "B"), Card(4, "E"), Card(7, "C")] cards = Cards.from_list_of_cards(list_of_cards) cards.sort() for index, card in enumerate(cards): if (index == 0 and card != list_of_cards[1]) or ( index == 1 and card != list_of_cards[2]) or ( index == 2 and card != list_of_cards[0]): correctly_sorted = False break assert correctly_sorted
def test_dropping_a_card_that_does_not_exists_in_cards(self): card_to_drop = Card(11, "E") list_of_cards = [Card(3, "E"), Card(3, "B"), Card(8, "O")] cards = Cards.from_list_of_cards(list_of_cards) number_of_cards_before_dropping_card = len(cards) try: cards.drop_cards(card_to_drop) has_raised_card_not_found_exception = False except game_exceptions.CardNotFound: has_raised_card_not_found_exception = True except Exception: has_raised_card_not_found_exception = False number_of_cards_after_dropping_card = len(cards) assert has_raised_card_not_found_exception and number_of_cards_before_dropping_card == number_of_cards_after_dropping_card
def test_valid_card(self): try: Card(number=12, kind="B") has_rised_exception = False except Exception: has_rised_exception = True assert not has_rised_exception
def cards_with_ladder(self, cards): kind_groups = cards.group_by_kind() cards_with_ladder = [] for kind in kind_groups.keys(): consecutive_cards = [] last_card = Card() kind_groups[kind].sort() for index, card in enumerate(kind_groups[kind]): if not last_card.number: last_card = card consecutive_cards.append(last_card) elif card.number - last_card.number == 1: last_card = card consecutive_cards.append(card) if index == len(kind_groups[kind]) - 1 and len( consecutive_cards ) >= self.MIN_EQUAL_CARD_NUMBER_FOR_GAME: cards_with_ladder.append( Cards.from_list_of_cards(consecutive_cards)) else: if len(consecutive_cards ) >= self.MIN_EQUAL_CARD_NUMBER_FOR_GAME: cards_with_ladder.append( Cards.from_list_of_cards(consecutive_cards)) last_card = card consecutive_cards = [last_card] return cards_with_ladder
def test_receiving_valid_card(self): cards = Cards() number_of_cards_before_receiving_card = len(cards) valid_card = Card(4, "O") cards.receive_card(valid_card) number_of_cards_after_receiving_card = len(cards) assert cards[ 0] == valid_card and number_of_cards_after_receiving_card - number_of_cards_before_receiving_card == 1
def test_invalid_card_kind(self): try: Card(number=1, kind="H") has_rised_invalid_kind_exception = False except game_exceptions.InvalidCardKind: has_rised_invalid_kind_exception = True except Exception: has_rised_invalid_kind_exception = False assert has_rised_invalid_kind_exception
def test_invalid_card_number(self): try: Card(number=13, kind="B") has_rised_invalid_number_exception = False except game_exceptions.InvalidCardNumber: has_rised_invalid_number_exception = True except Exception: has_rised_invalid_number_exception = False assert has_rised_invalid_number_exception
def test_group_by_kind(self): ten_of_basto = Card(10, 'B') nine_of_gold = Card(9, 'O') six_of_cup = Card(6, 'C') four_of_basto = Card(4, 'B') twelve_of_gold = Card(12, 'O') eleven_of_gold = Card(11, 'O') cards = Cards.from_list_of_cards([ ten_of_basto, nine_of_gold, six_of_cup, four_of_basto, twelve_of_gold, eleven_of_gold ]) kind_groups = cards.group_by_kind() for kind in ['B', 'O', 'C']: if kind == "B": cards_were_correctly_grouped = ten_of_basto in kind_groups[kind] and four_of_basto in kind_groups[kind] \ and len(kind_groups[kind]) == 2 elif kind == "O": cards_were_correctly_grouped = twelve_of_gold in kind_groups[kind] and eleven_of_gold in kind_groups[kind] \ and nine_of_gold in kind_groups[kind] and len(kind_groups[kind]) == 3 elif kind == "C": cards_were_correctly_grouped = six_of_cup in kind_groups[ kind] and len(kind_groups[kind]) == 1 else: cards_were_correctly_grouped = False assert cards_were_correctly_grouped
def test_detection_of_normal_cut(self): cards = Cards.from_list_of_cards([ Card(4, 'E'), Card(6, 'B'), Card(3, 'E'), Card(5, 'E'), Card(6, 'C'), Card(6, 'E'), Card(8, 'O') ]) grouped_cards = self._get_cards_group(cards) assert grouped_cards.find_type_of_cut() == "normal_cut"
def test_value_computation_when_zero_rest(self): cards = Cards.from_list_of_cards([ Card(4, 'E'), Card(6, 'B'), Card(3, 'E'), Card(5, 'E'), Card(6, 'C'), Card(6, 'E'), Card(6, 'O') ]) grouped_cards = self._get_cards_group(cards) assert grouped_cards.value() == -10
def test_detection_of_conga_cut(self): cards = Cards.from_list_of_cards([ Card(4, 'E'), Card(2, 'E'), Card(3, 'E'), Card(5, 'E'), Card(7, 'E'), Card(6, 'E'), Card(8, 'E') ]) grouped_cards = self._get_cards_group(cards) assert grouped_cards.find_type_of_cut() == 'conga_cut'
def test_detection_of_one_ladder_and_one_same_number_game_with_rest(self): cards = Cards.from_list_of_cards([ Card(1, 'O'), Card(6, 'B'), Card(3, 'O'), Card(2, 'O'), Card(6, 'C'), Card(6, 'E'), Card(10, 'O') ]) cards_grouper = CardsGrouper() grouped_cards = cards_grouper.group_by_games_found(cards) assert len(grouped_cards.games) == 2 and grouped_cards.rest
def test_detection_of_one_same_number_and_no_other_game(self): cards = Cards.from_list_of_cards([ Card(4, 'E'), Card(7, 'B'), Card(3, 'E'), Card(6, 'E'), Card(6, 'C'), Card(6, 'B'), Card(6, 'O') ]) cards_grouper = CardsGrouper() grouped_cards = cards_grouper.group_by_games_found(cards) assert len(grouped_cards.games) == 1 and grouped_cards.rest
def test_string_representation(self): card = Card(number=10, kind="O") string_representation = card.to_string() assert string_representation == "10 de oro"
def _initialize_hand(self, style=None): if not hasattr(self, 'games_detector'): self.games_detector = GamesDetector() hand = Cards() if style == "one_same_number_game_one_ladder": hand.receive_card(Card(1, "O")) hand.receive_card(Card(1, "B")) hand.receive_card(Card(1, "C")) hand.receive_card(Card(3, "O")) hand.receive_card(Card(4, "O")) hand.receive_card(Card(5, "O")) hand.receive_card(Card(9, "O")) elif style == "two_same_number_game_no_ladder": hand.receive_card(Card(1, "O")) hand.receive_card(Card(1, "B")) hand.receive_card(Card(1, "C")) hand.receive_card(Card(3, "E")) hand.receive_card(Card(3, "O")) hand.receive_card(Card(3, "C")) hand.receive_card(Card(9, "O")) elif style == "no_same_number_game_one_ladder": hand.receive_card(Card(1, "O")) hand.receive_card(Card(2, "B")) hand.receive_card(Card(4, "C")) hand.receive_card(Card(5, "E")) hand.receive_card(Card(6, "E")) hand.receive_card(Card(7, "E")) hand.receive_card(Card(8, "E")) elif style == "no_game": hand.receive_card(Card(1, "O")) hand.receive_card(Card(2, "B")) hand.receive_card(Card(4, "C")) hand.receive_card(Card(5, "E")) hand.receive_card(Card(6, "E")) hand.receive_card(Card(10, "E")) hand.receive_card(Card(11, "C")) return hand
def test_inequality_detection(self): list_of_cards_a = [Card(10, 'O'), Card(3, 'C')] list_of_cards_b = [Card(10, 'O'), Card(4, 'C')] cards_a = Cards.from_list_of_cards(list_of_cards_a) cards_b = Cards.from_list_of_cards(list_of_cards_b) assert cards_a != cards_b
def test_inequality_detection(self): card_a = Card(number=6, kind="E") card_b = Card(number=6, kind="C") assert card_a != card_b
def test_equality_detection(self): card_a = Card(number=7, kind="C") card_b = Card(number=7, kind="C") assert card_a == card_b
def test_dictionary_conversion(self): card = Card(number=8, kind="E") dict_representation = card.to_dict() assert type(dict_representation) == dict