Ejemplo n.º 1
0
    def test_fromdict(self):
        """Given a dictionary as created by asdict(), fromdict()
        should deserialize the Player object.
        """
        hands = (cards.Hand((
            cards.Card(11, 3),
            cards.Card(2, 1),
        )), )
        exp = players.Player(hands, 'spam', 200)
        exp.will_buyin = MethodType(players.will_buyin_always, exp)
        exp.will_double_down = MethodType(players.will_double_down_always, exp)
        exp.will_hit = MethodType(players.will_hit_dealer, exp)
        exp.will_insure = MethodType(players.will_insure_always, exp)
        exp.will_split = MethodType(players.will_split_always, exp)

        value = {
            'class': 'Player',
            'chips': 200,
            'hands': hands,
            'insured': 0,
            'name': 'spam',
            'will_buyin': 'will_buyin_always',
            'will_double_down': 'will_double_down_always',
            'will_hit': 'will_hit_dealer',
            'will_insure': 'will_insure_always',
            'will_split': 'will_split_always',
        }
        act = players.Player.fromdict(value)

        self.assertEqual(exp, act)
Ejemplo n.º 2
0
    def test_has_decision_methods(self):
        """The players created by make_player() should have the
        decision methods for Players, such as will_hit and
        will_insure.
        """
        # As long as no exception is raised, this test passes.
        phand = cards.Hand([
            cards.Card(11, 3),
            cards.Card(1, 2),
        ])
        dhand = cards.Hand([
            cards.Card(11, 3),
            cards.Card(1, 2),
        ])
        dealer = players.Dealer((dhand, ), 'Dealer')
        g = game.Engine(None, dealer, None, None, 2)
        player = players.make_player()

        methods = [
            player.will_hit,
            player.will_split,
            player.will_double_down,
        ]
        for method in methods:
            _ = method(phand, g)

        methods = [
            player.will_buyin,
            player.will_insure,
        ]
        for method in methods:
            _ = method(g)
Ejemplo n.º 3
0
    def test_fromdict_methods(self):
        """Given a dictionary as created by asdict(), fromdict()
        should deserialize the Player object.
        """
        exp = MethodType

        hands = (cards.Hand((
            cards.Card(11, 3),
            cards.Card(2, 1),
        )), )
        value = {
            'class': 'Player',
            'chips': 200,
            'hands': hands,
            'insured': 0,
            'name': 'spam',
            'will_buyin': 'will_buyin_always',
            'will_double_down': 'will_double_down_always',
            'will_hit': 'will_hit_dealer',
            'will_insure': 'will_insure_always',
            'will_split': 'will_split_always',
        }
        methods = [key for key in value if key.startswith('will_')]
        player = players.Player.fromdict(value)
        for method in methods:
            act = getattr(player, method)

            self.assertIsInstance(act, exp)
Ejemplo n.º 4
0
    def test_serialize(self):
        """When called, serialize() should return the object
        serialized as a JSON string.
        """
        hands = (cards.Hand((
            cards.Card(11, 3),
            cards.Card(2, 1),
        )), )
        exp = json.dumps({
            'class': 'Player',
            'chips': 200,
            'hands': (hands[0].serialize(), ),
            'insured': 0,
            'name': 'spam',
            'will_buyin': 'will_buyin_always',
            'will_double_down': 'will_double_down_always',
            'will_hit': 'will_hit_dealer',
            'will_insure': 'will_insure_always',
            'will_split': 'will_split_always',
        })

        player = players.Player(hands, 'spam', 200)
        player.will_buyin = MethodType(players.will_buyin_always, player)
        player.will_double_down = MethodType(players.will_double_down_always,
                                             player)
        player.will_hit = MethodType(players.will_hit_dealer, player)
        player.will_insure = MethodType(players.will_insure_always, player)
        player.will_split = MethodType(players.will_split_always, player)
        act = player.serialize()

        self.assertEqual(exp, act)
Ejemplo n.º 5
0
    def test_serialize(self):
        """When called, serialize() should return the Pile object
        serialized to a JSON string.
        """
        exp = json.dumps({
            'class':
            'Pile',
            '_iter_index':
            0,
            'cards': [
                '["Card", 11, "clubs", true]',
                '["Card", 12, "clubs", true]',
                '["Card", 13, "clubs", true]',
            ]
        })

        cardlist = [
            cards.Card(11, 0, True),
            cards.Card(12, 0, True),
            cards.Card(13, 0, True),
        ]
        pile = cards.Pile(cardlist)
        act = pile.serialize()

        self.assertEqual(exp, act)
Ejemplo n.º 6
0
    def test_hand_updates(self, mock_update):
        """The methods tested should send _update_hand a player,
        hand, and event text for display to the user.
        """
        player = players.Player(name='spam')
        hand = cards.Hand([
            cards.Card(11, 3),
            cards.Card(1, 0),
        ])
        events = [
            'Dealt hand.',
            'Flip.',
            'Hit.',
            'Stand.',
        ]
        exp = [call(player, hand, event) for event in events]

        ui = cli.LogUI()
        ui.deal(player, hand)
        ui.flip(player, hand)
        ui.hit(player, hand)
        ui.stand(player, hand)
        act = mock_update.mock_calls

        self.assertListEqual(exp, act)
Ejemplo n.º 7
0
    def test__asdict(self):
        """When called, asdict() should serialize the object to a
        dictionary.
        """
        hands = (cards.Hand((
            cards.Card(11, 3),
            cards.Card(2, 1),
        )), )
        exp = {
            'class': 'Player',
            'chips': 200,
            'hands': hands,
            'insured': 0,
            'name': 'spam',
            'will_buyin': 'will_buyin_always',
            'will_double_down': 'will_double_down_always',
            'will_hit': 'will_hit_dealer',
            'will_insure': 'will_insure_always',
            'will_split': 'will_split_always',
        }

        player = players.Player(hands, 'spam', 200)
        player.will_buyin = MethodType(players.will_buyin_always, player)
        player.will_double_down = MethodType(players.will_double_down_always,
                                             player)
        player.will_hit = MethodType(players.will_hit_dealer, player)
        player.will_insure = MethodType(players.will_insure_always, player)
        player.will_split = MethodType(players.will_split_always, player)
        act = player._asdict()

        self.assertEqual(exp, act)
Ejemplo n.º 8
0
    def test_serialize(self):
        """When called, serialize() should return the Deck object
        serialized as a JSON string.
        """
        exp = json.dumps({
            'class':
            'Deck',
            '_iter_index':
            0,
            'cards': [
                '["Card", 11, "clubs", true]',
                '["Card", 12, "clubs", true]',
                '["Card", 13, "clubs", true]',
            ],
            'size':
            1
        })

        cardlist = [
            cards.Card(11, 0, True),
            cards.Card(12, 0, True),
            cards.Card(13, 0, True),
        ]
        deck = cards.Deck(cardlist)
        act = deck.serialize()

        self.assertEqual(exp, act)
Ejemplo n.º 9
0
    def test_serialize(self):
        """When called, serialize() should return the object
        serialized as a JSON string.
        """
        exp = json.dumps({
            'class':
            'Hand',
            '_iter_index':
            0,
            'cards': [
                '["Card", 11, "clubs", true]',
                '["Card", 12, "clubs", true]',
                '["Card", 13, "clubs", true]',
            ],
            'doubled_down':
            False,
        })

        cardlist = [
            cards.Card(11, 0, True),
            cards.Card(12, 0, True),
            cards.Card(13, 0, True),
        ]
        hand = cards.Hand(cardlist)
        act = hand.serialize()

        self.assertEqual(exp, act)
Ejemplo n.º 10
0
    def test__update_bet_split(self, mock_main):
        """When is_split is True, _update_bet should update the split
        row of the data table for the player.
        """
        hands = [
            cards.Hand([
                cards.Card(11, 0),
            ]),
            cards.Hand([
                cards.Card(11, 3),
            ]),
        ]
        player = players.Player(hands, name='spam', chips=100)
        player2 = players.Player(name='eggs', chips=100)
        new_data = [
            [player, 100, 20, 'J♣', 'Splits hand.'],
            ['  \u2514\u2500', '', 20, 'J♠', 'Loses.'],
            [player2, 100, 20, '3♣ 4♣', 'Takes hand.'],
        ]
        exp = call().send(('update', new_data))

        data = [
            [player, 100, 20, 'J♣', 'Splits hand.'],
            ['  \u2514\u2500', '', '', 'J♠', ''],
            [player2, 100, 20, '3♣ 4♣', 'Takes hand.'],
        ]
        ui = cli.TableUI()
        ui.ctlr.data = data
        ui.start()
        ui._update_bet(player, 20, 'Loses.', split=True)
        act = mock_main.mock_calls[-1]
        ui.end()

        self.assertEqual(exp, act)
Ejemplo n.º 11
0
    def test_hand_updates(self, mock_update_hand, _):
        """The tested methods should call the _update_hand() method
        with the player, hand, and event text.
        """
        player = players.Player(name='spam', chips=100)
        hand = cards.Hand([
            cards.Card(11, 0),
            cards.Card(10, 3),
        ])
        handstr = str(hand)
        exp = [
            call(player, hand, 'Takes hand.'),
            call(player, hand, 'Flips card.'),
            call(player, hand, 'Hits.'),
            call(player, hand, 'Stands.'),
        ]

        data = [
            [player, 80, 20, '', ''],
        ]
        ui = cli.TableUI()
        ui.ctlr.data = data
        ui.start()
        ui.deal(player, hand)
        ui.flip(player, hand)
        ui.hit(player, hand)
        ui.stand(player, hand)
        act = mock_update_hand.mock_calls[-4:]
        ui.end()

        self.assertEqual(exp, act)
Ejemplo n.º 12
0
    def test__update_hand_split(self, mock_main):
        """If sent a split hand, _update_hand() should update the
        split row of the table.
        """
        hands = [
            cards.Hand([
                cards.Card(11, 0),
            ]),
            cards.Hand([
                cards.Card(11, 3),
            ]),
        ]
        player = players.Player(hands, name='spam', chips=100)
        player2 = players.Player(name='eggs', chips=100)
        new_data = [
            [player, 100, 20, 'J♣', 'Splits hand.'],
            ['  \u2514\u2500', '', 20, 'J♠ 5♣', 'Hits.'],
            [player2, 100, 20, '3♣ 4♣', 'Takes hand.'],
        ]
        exp = call().send(('update', new_data))

        data = [
            [player, 100, 20, 'J♣', 'Splits hand.'],
            ['  \u2514\u2500', '', 20, 'J♠', 'Splits hand.'],
            [player2, 100, 20, '3♣ 4♣', 'Takes hand.'],
        ]
        ui = cli.TableUI()
        ui.ctlr.data = data
        ui.start()
        hands[1].append(cards.Card(5, 0))
        ui._update_hand(player, hands[1], 'Hits.')
        act = mock_main.mock_calls[-1]
        ui.end()

        self.assertEqual(exp, act)
Ejemplo n.º 13
0
    def test_cleanup(self, mock_main):
        """When called, cleanup() should clear the bet, hand, and
        event field of every row in the data table, then send it to
        the UI.
        """
        hands = [
            cards.Hand([
                cards.Card(11, 0),
            ]),
            cards.Hand([
                cards.Card(11, 3),
            ]),
        ]
        player = players.Player(hands, name='spam', chips=100)
        player2 = players.Player(hands, name='eggs', chips=100)
        new_data = [
            [player, 100, '', '', ''],
            [player2, 100, '', '', ''],
        ]
        exp = call().send(('update', new_data))

        data = [
            [player, 100, 20, 'J♣', 'Splits hand.'],
            ['  \u2514\u2500', '', 20, 'J♠', 'Splits hand.'],
            [player2, 100, 20, '3♣ 4♣', 'Takes hand.'],
        ]
        ui = cli.TableUI()
        ui.ctlr.data = data
        ui.start()
        ui.cleanup()
        act = mock_main.mock_calls[-1]

        self.assertEqual(exp, act)
Ejemplo n.º 14
0
    def test_fromdict_invalid_method(self):
        """Given a dictionary as created by asdict(), fromdict()
        should deserialize the Player object.
        """
        exp = ValueError

        hands = (cards.Hand((
            cards.Card(11, 3),
            cards.Card(2, 1),
        )), )
        dict_ = {
            'class': 'Player',
            'chips': 200,
            'hands': hands,
            'insured': 0,
            'name': 'spam',
            'will_buyin': 'will_buyin_always',
            'will_double_down': 'will_double_down_always',
            'will_hit': 'will_hit_dealer',
            'will_insure': 'will_insure_always',
            'will_split': 'will_split_always',
        }
        methkeys = [key for key in dict_ if key.startswith('will_')]
        test = 'spam'
        for key in methkeys:
            test_dict = copy(dict_)
            test_dict[key] = test

            with self.assertRaises(exp):
                act = players.Player.fromdict(test_dict)
Ejemplo n.º 15
0
 def test___contains__(self):
     """Pile should implement the in operator."""
     c1 = cards.Card(1, 0)
     c2 = cards.Card(2, 0)
     c3 = cards.Card(3, 0)
     d = cards.Pile([c1, c2])
     self.assertTrue(c1 in d)
     self.assertFalse(c3 in d)
Ejemplo n.º 16
0
 def test__astuple_deserialize(self):
     """The result of astuple() should be able to be used to create
     a new instance of cards.Card with the same attributes.
     """
     exp = cards.Card(11, 3, True)
     serialized = exp._astuple()
     act = cards.Card(*serialized[1:])
     self.assertEqual(exp, act)
Ejemplo n.º 17
0
    def test_can_split_true(self):
        """can_split() should return true if the hand can be split."""
        cardlist = [
            cards.Card(10, 0),
            cards.Card(10, 2),
        ]
        h = cards.Hand(cardlist)
        actual = h.can_split()

        self.assertTrue(actual)
Ejemplo n.º 18
0
 def test_is_will_hit(self):
     """A will_hit function should accept a Player, a Hand, and a
     Game objects.
     """
     player = players.Player()
     hand = cards.Hand([
         cards.Card(11, 0),
         cards.Card(11, 3),
     ])
     g = game.Engine()
     _ = willhit.will_hit_dealer(player, hand, g)
Ejemplo n.º 19
0
    def test_can_split_false(self):
        """can_split() should return false if the hand cannot be
        split.
        """
        cardlist = [
            cards.Card(10, 0),
            cards.Card(11, 2),
        ]
        h = cards.Hand(cardlist)
        actual = h.can_split()

        self.assertFalse(actual)
Ejemplo n.º 20
0
    def test_is_blackjack_false_two_cards(self):
        """is_blackjack() should return false if the hand doesn't
        equal 21.
        """
        cardlist = [
            cards.Card(11, 3),
            cards.Card(4, 2),
        ]
        h = cards.Hand(cardlist)
        actual = h.is_blackjack()

        self.assertFalse(actual)
Ejemplo n.º 21
0
    def test_is_blackjack_true(self):
        """is_blackjack() should return true if the hand is a natural
        blackjack.
        """
        cardlist = [
            cards.Card(11, 3),
            cards.Card(1, 2),
        ]
        h = cards.Hand(cardlist)
        actual = h.is_blackjack()

        self.assertTrue(actual)
Ejemplo n.º 22
0
    def test_split_invalid(self):
        """If the hand cannot be split, split() should raise a
        ValueError exception.
        """
        expected = ValueError

        h = cards.Hand([
            cards.Card(11, 0),
            cards.Card(2, 3),
        ])

        with self.assertRaises(ValueError):
            _ = h.split()
Ejemplo n.º 23
0
    def test_valid(self):
        """Given a valid value, validate_cardtuple should normalize
        and validate it then return the normalized value.
        """
        exp = (
            cards.Card(11, 3),
            cards.Card(1, 1),
        )

        value = list(exp)
        act = cards.validate_cardtuple(None, value)

        self.assertEqual(exp, act)
Ejemplo n.º 24
0
    def test_is_blackjack_false_three_cards(self):
        """is_blackjack() should return false if the hand has more
        than two cards.
        """
        cardlist = [
            cards.Card(11, 3),
            cards.Card(4, 2),
            cards.Card(7, 3),
        ]
        h = cards.Hand(cardlist)
        actual = h.is_blackjack()

        self.assertFalse(actual)
Ejemplo n.º 25
0
    def test___ne__nonequality_test(self):
        """Card objects should compare for non equality based on their
        rank and suit.
        """
        c1 = cards.Card(2, 'hearts')
        c2 = cards.Card(2, 'hearts')
        c3 = cards.Card(11, 'clubs')
        c4 = cards.Card(2, 'spades')
        c5 = cards.Card(11, 'hearts')

        self.assertFalse(c1 != c2)
        self.assertTrue(c1 != c3)
        self.assertTrue(c1 != c4)
        self.assertTrue(c1 != c5)
Ejemplo n.º 26
0
    def test_cards(self):
        """An instance of Pile should be able to hold cards in its
        cards attribute.
        """
        expected = (
            cards.Card(1, 3),
            cards.Card(2, 3),
            cards.Card(3, 3),
        )

        d = cards.Pile(expected)
        actual = d.cards

        self.assertEqual(expected, actual)
Ejemplo n.º 27
0
    def test___iter__(self):
        """__iter__() should return a copy of of the Pile object for
        iteration.
        """
        card_list = [
            cards.Card(1, 0),
            cards.Card(2, 0),
        ]
        expected = cards.Pile(card_list)

        actual = expected.__iter__()

        self.assertEqual(expected, actual)
        self.assertFalse(expected is actual)
Ejemplo n.º 28
0
    def test_is_bust_false(self):
        """When called, is_bust() should return true if their are
        possible scores under 21.
        """
        exp = False

        hand = cards.Hand((
            cards.Card(1, 0),
            cards.Card(1, 0),
            cards.Card(1, 0),
        ))
        act = hand.is_bust()

        self.assertEqual(exp, act)
Ejemplo n.º 29
0
    def test_is_bust(self):
        """When called, is_bust() should return true if the score of
        the hand is over 21.
        """
        exp = True

        hand = cards.Hand((
            cards.Card(11, 0),
            cards.Card(11, 0),
            cards.Card(11, 0),
        ))
        act = hand.is_bust()

        self.assertEqual(exp, act)
Ejemplo n.º 30
0
    def test_stand_on_bust(self):
        """If the hand is bust, will_hit_dealer() should return
        False.
        """
        expected = willhit.STAND

        h = cards.Hand([
            cards.Card(11, 0),
            cards.Card(4, 2),
            cards.Card(11, 3),
        ])
        actual = willhit.will_hit_dealer(None, h)

        self.assertEqual(expected, actual)