def test_start_round(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() self.assertNotEqual(uno_round.current_player_index, None) for i in range(2): self.assertEqual(len(uno_round.player_list[1].cards), 7) self.assertTrue(uno_round.deck.active_card) self.assertEqual(len(uno_round.actions_list), 9) for item in uno_round.actions_list: self.assertTrue( item in allowed_actions_for_card(Card(color=Color.RED, number=1)) ) events = self._get_all_events() logger.debug(events) self.assertEqual(len(events), 19)
def test_get_card(self): d = Deck(cards=[ Card(color=Color.RED, number=2), Card(color=Color.BLUE, number=2), ]) card = d.get_card() self.assertEqual(card, Card(color=Color.RED, number=2))
def __init__(self, deck=None): self.player_list = list() self.uid = str(uuid.uuid4()) self.player_by_uid = dict() self.human_players = list() if deck is None: self.deck = Deck() else: self.deck = deck
def test_count_by_color(self): d = Deck() self.assertEqual(len([i for i in d._cards if i.color == Color.RED]), 25) self.assertEqual(len([i for i in d._cards if i.color == Color.YELLOW]), 25) self.assertEqual(len([i for i in d._cards if i.color == Color.BLUE]), 25) self.assertEqual(len([i for i in d._cards if i.color == Color.GREEN]), 25) self.assertEqual(len([i for i in d._cards if i.color == Color.BLACK]), 8)
def test_get_card_shuffle(self): d = Deck(cards=[], used_cards=[ Card(color=Color.YELLOW, number=2), Card(color=Color.GREEN, number=2), Card(color=Color.BLUE, number=2), Card(color=Color.RED, number=2), ]) active_card = d.active_card card = d.get_card() self.assertEqual(len(d._cards), 2) self.assertEqual(len(d._used_cards), 1) self.assertEqual(d.active_card, Card(color=Color.YELLOW, number=2))
def test_put_correct_card(self): d = Deck(cards=[ Card(color=Color.RED, number=2), Card(color=Color.BLUE, number=2), Card(color=Color.BLUE, action=CardAction.SKIP), Card(color=Color.BLACK, action=CardAction.SET_COLOR, required_color=Color.YELLOW), Card(color=Color.YELLOW, number=5), ]) while bool(d._cards): card = d.get_card() d.put_card(card)
def test_count_by_action(self): d = Deck() self.assertEqual( len([i for i in d._cards if i.action == CardAction.TAKE_TWO]), 8) self.assertEqual( len([i for i in d._cards if i.action == CardAction.REVERSE]), 8) self.assertEqual( len([i for i in d._cards if i.action == CardAction.SKIP]), 8) self.assertEqual( len([i for i in d._cards if i.action == CardAction.SET_COLOR]), 4) self.assertEqual( len([ i for i in d._cards if i.action == CardAction.TAKE_FOUR_AND_SET_COLOR ]), 4)
def test_skip_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request(Command.TAKE)) old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request(Command.SKIP)) # current player changed self.assertNotEqual(uno_round.current_player_index, old_current_player_index) # card not removed from player self.assertEqual(player_cards_len, len(player.cards)) # card not taken from deck self.assertEqual(deck_cards_len, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 8) for item in uno_round.actions_list: self.assertTrue(item in allowed_actions_for_card( Card(color=Color.RED, number=1))) # player put card events events = self._get_all_events() self.assertEqual(len(events), 3) # your turn event self.assertTrue( self._find_event(NotificationType.PLAYER_SKIPPED, events)) self.assertTrue(self._find_event(NotificationType.YOUR_TURN, events))
def test_take_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() events = self._get_all_events() old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( player, uno_service.UnoService.execute_command.request(Command.TAKE)) # current player equal self.assertEqual(uno_round.current_player_index, old_current_player_index) # card added to player cards self.assertEqual(player_cards_len + 1, len(player.cards)) # card removed from deck self.assertEqual(deck_cards_len - 1, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 3) for item in uno_round.actions_list: self.assertTrue( item in [PutTakenCardAction, SkipAction, ListCardsAction]) events = self._get_all_events() self.assertEqual(len(events), 2) self.assertTrue( self._find_event(NotificationType.YOU_TOOK_CARD, events)) self.assertTrue( self._find_event(NotificationType.PLAYER_TOOK_CARD, events))
def test_take_two_and_skip_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() events = self._get_all_events() uno_round.current_player.cards.append( Card(color=Color.RED, action=CardAction.TAKE_TWO)) uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.PUT, Card(color=Color.RED, action=CardAction.TAKE_TWO))) with self.assertRaises(ActionNotAllowedError): uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.PUT, Card(color=Color.RED, number=1))) old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.TAKE_TWO_AND_SKIP)) # current player changed self.assertNotEqual(uno_round.current_player_index, old_current_player_index) # cards added to player self.assertEqual(player_cards_len + 2, len(player.cards)) # card removed from deck self.assertEqual(deck_cards_len - 2, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 8) for item in uno_round.actions_list: self.assertTrue(item in allowed_actions_for_card( Card(color=Color.RED, number=1))) # player put card events events = self._get_all_events() self.assertEqual(len(events), 4) # your turn event self.assertTrue(self._find_event(NotificationType.YOUR_TURN, events)) self.assertTrue( self._find_event(NotificationType.YOU_TOOK_CARD, events)) self.assertTrue( self._find_event(NotificationType.PLAYER_TOOK_CARDS, events))
def test_cards_attr(self): d = Deck(cards=[Card(color=Color.RED, number=2)]) self.assertEqual(len(d._cards), 1)
def test_count_by_class(self): d = Deck() self.assertEqual(len([i for i in d._cards if i.action is None]), 108 - 32) self.assertEqual(len([i for i in d._cards if i.action is not None]), 32)
def test_count_total(self): d = Deck() self.assertEqual(len(d._cards), 108)