def test_deck_json_dumps(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) string = json.dumps(deck.reprJSON(), cls=ComplexEncoder) expected = """{"discardDeck": {"discardDeck": null, "cards": [{"id": 1, "value": "3", "color": "Red"}, {"id": 2, "value": "4", "color": "Blue"}]}, "cards": [{"id": 3, "value": "5", "color": "Yellow"}, {"id": 4, "value": "6", "color": "Green"}]}""" assert string == expected
def test_deck_json_dump_load(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) string = json.dumps(deck.reprJSON(), cls=ComplexEncoder) deck2 = Deck() deck2.loadJSON(string) assert deck.cards == deck2.cards
def test_deck_setup(): discardDeck = Deck(None, None) cards = [] colors = ["Red", "Green", "Yellow", "Blue"] for color in colors: cards.append(Card(len(cards), str(0), color)) for num in range(1, 10): cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), "+2", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "+4", "wild")) deck = Deck(discardDeck, cards) return deck
def test_deck_draw(test_deck_setup): drawncard = test_deck_setup.draw(1) assert str(drawncard[0]) == str(Card(1, "+4", "wild"))
def test_deck_not_list(): sample_card = Card(1, "4", "Red") with pytest.raises(TypeError): deck = Deck(None, sample_card)
def test_card_str(): card = Card(1, "4", "Red") assert card.__str__() == "Red 4"
def test_deck(): sample_discard = Deck() sample_card = Card(1, "4", "Red") deck = Deck(sample_discard, [sample_card]) assert deck.cards[0] == sample_card assert deck.discardDeck == sample_discard
def test_deck_no_discard(): sample_card = Card(1, "4", "Red") deck = Deck(None, [sample_card]) assert deck.cards[0] == sample_card
def generate_uno_deck(): # Generate cards for UNO cards = [] colors = ["Red", "Green", "Yellow", "Blue"] for color in colors: cards.append(Card(len(cards), str(0), color)) for num in range(1, 10): # generate 2 cards each from [1 - 9] cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), "draw", color)) cards.append(Card(len(cards), "draw", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) cards.append(Card(len(cards), "wild_draw", "wild")) for card in cards: surface = f"{card.color.upper()}_{card.value.upper()}" animation.game.track_card(animation.assets.CARDS[surface], card.id) # Populate main deck and create discard discard = Deck() deck = Deck(discard=discard, cards=cards) deck.shuffle() sfx_card_shuffle.play() return deck
def test_deck_reprJSON(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert deck.reprJSON() == dict(discardDeck=deck.discardDeck, cards=deck.cards)
def test_deck_getDiscard_empty(): discardDeck = Deck() cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert deck.getDiscard() == None
def test_deck_getDiscard(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) assert str(deck.getDiscard()) == "Blue 4"
def test_card_str(): card = Card(1, "4", "Red") assert card.__str__() == "Red 4" def test_card_repr(): card = Card(1, "4", "Red") assert card.__repr__() == "Red 4" @pytest.mark.parametrize( "card1, card2, output", [ (Card(1, "4", "Red"), Card(2, "4", "Blue"), True), # Test value match (Card(1, "5", "Red"), Card(2, "4", "Red"), True), # Test color match (Card(1, "2", "Red"), Card(2, "4", "Blue"), False), # Test no match (Card(1, "4", "Red"), Card(2, "4", "Red"), True) ] # Test both match ) def test_card_match(card1, card2, output): assert card1.match(card2) == output def test_card_reprJSON(): output = {"id": 1, "value": "4", "color": "Red"} card = Card(1, "4", "Red") assert card.reprJSON() == output
def test_deck_draw_excess_reshuffle(): discardDeck = Deck(None, [Card(1, "3", "Red"), Card(2, "4", "Blue")]) cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(discardDeck, cards) drawn_cards = deck.draw(3) assert len(drawn_cards) == 3
def test_card_reprJSON(): output = {"id": 1, "value": "4", "color": "Red"} card = Card(1, "4", "Red") assert card.reprJSON() == output
def test_deck_draw_excess_no_discard(): cards = [Card(3, "5", "Yellow"), Card(4, "6", "Green")] deck = Deck(None, cards) drawn_cards = deck.draw(3) assert len(drawn_cards) == 2
if __name__ == "__main__": # pragma: no cover from cardgame.cards import Card, Deck, Hand, ComplexEncoder from cardgame.player import Player import json # Generate cards for UNO cards = [] colors = ["Red", "Green", "Yellow", "Blue"] for color in colors: cards.append(Card(len(cards), str(0), color)) for num in range(1,10): # generate 2 cards each from [1 - 9] cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), str(num), color)) cards.append(Card(len(cards), "+2", color)) cards.append(Card(len(cards), "+2", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "skip", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "reverse", color)) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "wild", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) cards.append(Card(len(cards), "+4", "wild")) # Populate main deck and create discard discard = Deck() deck = Deck(discard=discard, cards=cards)