示例#1
0
文件: tests.py 项目: darioush/setgame
def test_play():
    game = SetGame()
    sets = game.play()
    assert all(Card.is_set(set) for set in sets)
    assert len(game.deck) == 0
    with pytest.raises(NoSetFound):
        game.board.find_set()
示例#2
0
文件: tests.py 项目: zcr1/set
 def setUp(self):
     self.game = SetGame()
示例#3
0
文件: tests.py 项目: zcr1/set
class TestSetGame(unittest.TestCase):

    def setUp(self):
        self.game = SetGame()

    def test_initialize_deck(self):
        """Test that we generate 81 unique cards"""
        self.game.init_deck(self.game.deck)
        self.assertTrue(len(self.game.deck) == 81)

        seen_cards = set()
        for card in self.game.deck:
            self.assertFalse(card in seen_cards)
            seen_cards.add(card)

    def test_initialize_board(self):
        deck = []
        board = set()
        self.game.init_deck(deck)
        self.game.init_board(deck, board)

        self.assertEqual(len(deck), 81 - 12)
        self.assertEqual(len(board), 12)

    def test_is_set_invalid(self):
        """The following sets of cards are invalid"""
        cards = [
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.green, shape=Shape.diamond, shading=Shading.striped, number=3)
        ]
        self.assertFalse(self.game.is_set(*cards))

        cards = [
            Card(color=Color.red, shape=Shape.squiggle, shading=Shading.solid, number=2),
            Card(color=Color.green, shape=Shape.squiggle, shading=Shading.solid, number=1),
            Card(color=Color.purple, shape=Shape.oval, shading=Shading.solid, number=3)
        ]
        self.assertFalse(self.game.is_set(*cards))

        cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=1),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=3),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=3)
        ]
        self.assertFalse(self.game.is_set(*cards))

        cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.empty, number=1),
            Card(color=Color.red, shape=Shape.squiggle, shading=Shading.striped, number=3),
            Card(color=Color.purple, shape=Shape.diamond, shading=Shading.empty, number=3)
        ]
        self.assertFalse(self.game.is_set(*cards))

    def test_is_set_valid(self):
        """The following sets of cards are valid"""
        cards = [
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3)
        ]
        self.assertTrue(self.game.is_set(*cards))

        cards = [
            Card(color=Color.red, shape=Shape.squiggle, shading=Shading.solid, number=2),
            Card(color=Color.green, shape=Shape.squiggle, shading=Shading.solid, number=1),
            Card(color=Color.purple, shape=Shape.squiggle, shading=Shading.solid, number=3)
        ]
        self.assertTrue(self.game.is_set(*cards))

        cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=1),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=2),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=3)
        ]
        self.assertTrue(self.game.is_set(*cards))

        cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.empty, number=1),
            Card(color=Color.red, shape=Shape.squiggle, shading=Shading.striped, number=3),
            Card(color=Color.purple, shape=Shape.diamond, shading=Shading.solid, number=2)
        ]
        self.assertTrue(self.game.is_set(*cards))

    def test_get_set_invalid(self):
        """No valid sets in boards"""
        board = set([
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3)
        ])
        card_set = self.game.get_set(board)
        self.assertEqual(len(card_set), 0)

        board = set([
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3)
        ])
        card_set = self.game.get_set(board)
        self.assertEqual(len(card_set), 0)

        board = set([
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=2),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=2)
        ])
        card_set = self.game.get_set(board)
        self.assertEqual(len(card_set), 0)

    def test_get_set_valid(self):
        valid_cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=1),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=2),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=3)
        ]
        board = set([
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=3),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=2),
            Card(color=Color.red, shape=Shape.diamond, shading=Shading.striped, number=2)
        ])
        board.update(valid_cards)

        card_set = self.game.get_set(board)
        self.assertTrue(card_set is not None)
        # compare as sets so order of cards is irrelevant
        self.assertEqual(set(valid_cards), set(card_set))

    def test_get_cards(self):
        cards = [
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=1),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=2),
            Card(color=Color.green, shape=Shape.oval, shading=Shading.solid, number=3)
        ]
        deck = list(cards)
        new_cards = self.game.get_cards(deck)

        self.assertEqual(len(new_cards), 3)
        self.assertEqual(len(self.game.deck), 0)
        self.assertEqual(set(cards), set(new_cards))