class CardInit(unittest.TestCase): card1 = card.Card(SPADES, J, 'Jimothy') card2 = card.Card(SPADES, J, '') card3 = card.Card(SPADES, J) def test_new_card(self): '''Creating a card with holder "Not Held" raises an error''' with self.assertRaises(ValueError): card.Card(SPADES, J, NOT_HELD) def test_new_card_2(self): '''New card should have correct suit''' self.assertEqual(self.card1.suit, SPADES) def test_new_card_3(self): '''New card should have correct symbol''' self.assertEqual(self.card1.symbol, J) def test_new_card_4(self): '''New card should have correct holder''' self.assertEqual(self.card1.holder, 'Jimothy') def test_new_card_5(self): '''New card should set blank string holder to "Not Held"''' self.assertEqual(self.card2.holder, NOT_HELD) def test_new_card_6(self): '''New card without a holder should set holder to "Not Held"''' self.assertEqual(self.card3.holder, NOT_HELD)
def test_evaluate_cards_10(self): '''A beats Q, both trump''' card1 = card.Card(CLUBS, A) card2 = card.Card(CLUBS, Q) self.current_game.trump = CLUBS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card1)
def test_evaluate_cards_9(self): '''J1 beats J2, both trump, J1 right bower and J2 left bower''' card1 = card.Card(HEARTS, J) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card1)
def test_evaluate_cards_8(self): '''J beats A, both trump, J left bower''' card1 = card.Card(HEARTS, A) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2)
def test_evaluate_cards_7(self): '''J beats A, A follows suit & J does not, J left bower''' card1 = card.Card(DIAMONDS, A) card2 = card.Card(DIAMONDS, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card2)
def test_evaluate_cards_5(self): '''9 beats 10, 10 follows suit & 9 doesn't, 9 trump''' card1 = card.Card(HEARTS, NINE) card2 = card.Card(DIAMONDS, TEN) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card1)
def test_evaluate_cards_4(self): '''9 beats 10, 9 follows suit & 10 doesn't, neither trump''' card1 = card.Card(DIAMONDS, NINE) card2 = card.Card(SPADES, TEN) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, DIAMONDS) self.assertEqual(result, card1)
def test_evaluate_cards_3(self): '''Should raise error when comparing two non-suit led, non-trump cards''' card1 = card.Card(SPADES, TEN) card2 = card.Card(SPADES, J) self.current_game.trump = HEARTS with self.assertRaises(ValueError): self.current_game.evaluateCards(card1, card2, DIAMONDS)
def test_evaluate_cards_2(self): '''J beats 10, same suit as led, neither trump''' card1 = card.Card(SPADES, TEN) card2 = card.Card(SPADES, J) self.current_game.trump = HEARTS result = self.current_game.evaluateCards(card1, card2, SPADES) self.assertEqual(result, card2)
def test_evaluate_cards_12(self): '''Should raise error if comparing same cards, not trump''' card1 = card.Card(CLUBS, A) card2 = card.Card(CLUBS, A) self.current_game.trump = DIAMONDS with self.assertRaises(ValueError): self.current_game.evaluateCards(card1, card2, DIAMONDS)
def test_evaluate_hand_2(self): ''' Trump Hearts, 9 of Hearts beats A of Spades, A of Clubs, A of Diamonds''' card1 = card.Card(SPADES, A) card2 = card.Card(HEARTS, NINE) card3 = card.Card(CLUBS, A) card4 = card.Card(DIAMONDS, A) self.current_game.trump = HEARTS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card2)
def test_evaluate_hand_4(self): ''' Trump Clubs, J of Spades beats A of Diamonds, K of Hearts, A of Clubs, when A of Diamonds played first''' card1 = card.Card(DIAMONDS, A) card2 = card.Card(CLUBS, A) card3 = card.Card(HEARTS, K) card4 = card.Card(SPADES, J) self.current_game.trump = CLUBS self.current_game.played = [card1, card2, card3, card4] result = self.current_game.evaluateHand() self.assertEqual(result, card4)
class DecideTrump(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) card1 = card.Card(CLUBS, NINE, player1) card2 = card.Card(CLUBS, A, player1) card3 = card.Card(SPADES, Q, player1) card4 = card.Card(DIAMONDS, TEN, player1) card5 = card.Card(HEARTS, K, player1) card6 = card.Card(DIAMONDS, J) card7 = card.Card(CLUBS, Q, player2) card8 = card.Card(HEARTS, NINE, player2) def test_decide_trump_face_up_1(self): '''Returns false if player does not have any of suit in hannd''' self.player1.hand = [ self.card3, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceUp( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertEqual(result, False) def test_decide_trump_face_up_2(self): '''In current stubbbed implementation, should always return True if valid''' self.player1.hand = [ self.card2, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceUp( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertEqual(result, True) def test_decide_trump_face_down_1(self): '''Returns any suit except the suit which was face up''' self.player1.hand = [ self.card2, self.card4, self.card5, self.card6, self.card8 ] result = self.player1.decideTrumpFaceDown( self.card1, [self.player1, self.player2, self.player3, self.player4]) self.assertIn(result, [SPADES, DIAMONDS, HEARTS])
def test_is_left_bower_5(self): '''J of Hearts is not a left bower when trump is Hearts''' result = card.Card(HEARTS, J).isLeftBower(HEARTS) self.assertEqual(result, False)
def test_is_left_bower_6(self): '''Q cannot be a left bower''' result = card.Card(HEARTS, Q).isLeftBower(HEARTS) self.assertEqual(result, False)
def test_is_left_bower_2(self): '''J of Clubs is a left bower when trump is Spades''' result = card.Card(CLUBS, J).isLeftBower(SPADES) self.assertEqual(result, True)
def test_is_left_bower_4(self): '''J of Hearts is a left bower when trump is Diamonds''' result = card.Card(HEARTS, J).isLeftBower(DIAMONDS) self.assertEqual(result, True)
class PlayCard(unittest.TestCase): player1 = player.Player('') player2 = player.Player('') player3 = player.Player('') player4 = player.Player('') team1 = team.Team(player1, player2) team2 = team.Team(player3, player4) current_game = game.Game(team1, team2) card1 = card.Card(CLUBS, NINE, player1) card2 = card.Card(CLUBS, A, player1) card3 = card.Card(SPADES, Q, player1) card4 = card.Card(DIAMONDS, TEN, player1) card5 = card.Card(HEARTS, K, player1) card6 = card.Card(DIAMONDS, J) card7 = card.Card(CLUBS, Q, player2) card8 = card.Card(HEARTS, NINE, player2) def test_play_card_1(self): '''Any card in hand should be valid if first to play, function should return any card from hand''' self.current_game.trump = SPADES # self.current_game.played = [] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] self.player1.hand = copy.copy(available_cards) result = self.player1.play(self.current_game.trump, None) self.assertIn( result, available_cards ) # as card is removed from hand, need to use copy of hand def test_play_card_2(self): '''Card which is played should be appropriately removed from the hand''' self.current_game.trump = SPADES # self.current_game.played = [] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] self.player1.hand = copy.copy( [self.card1, self.card2, self.card3, self.card4, self.card5]) result = self.player1.play(self.current_game.trump, None) self.assertNotIn( result, self.player1.hand ) # as card is removed from hand, test properly removed def test_play_card_3(self): '''Should only play one of the clubs cards when clubs are led''' self.current_game.trump = SPADES # self.current_game.played = [card7] -> for reference, not required for function invocation self.player1.hand = [ self.card1, self.card2, self.card3, self.card4, self.card5 ] result = self.player1.play(self.current_game.trump, CLUBS) self.assertIn(result, [self.card1, self.card2]) def test_play_card_4(self): '''Should only play a heart or the Jack of diamonds when hearts are led and trump''' self.current_game.trump = HEARTS # self.current_game.played = [card8] -> for reference, not required for function invocation self.player1.hand = [ self.card2, self.card3, self.card4, self.card5, self.card6 ] result = self.player1.play(self.current_game.trump, HEARTS) self.assertIn(result, [self.card5, self.card6]) def test_play_card_5(self): '''Should play any card when cannot follow suit''' self.current_game.trump = HEARTS # self.current_game.played = [card8] -> for reference, not required for function invocation available_cards = [ self.card1, self.card2, self.card4, self.card5, self.card6 ] self.player1.hand = copy.copy(available_cards) result = self.player1.play(self.current_game.trump, SPADES) self.assertIn(result, available_cards) def test_select_card_1(self): '''selectCard method should raise exception if list of cards empty''' with self.assertRaises(ValueError): self.player1.selectCard([]) def test_can_play_1(self): '''canPlay method should return True for J of Diamonds, trump clubs, suitLed Diamonds''' result = self.player1.canPlay(self.card6, CLUBS, DIAMONDS) self.assertEqual(result, True) def test_can_play_2(self): '''canPlay method should return False for J of Diamonds, trump clubs, suitLed Hearts''' result = self.player1.canPlay(self.card6, CLUBS, HEARTS) self.assertEqual(result, False) def test_can_play_3(self): '''canPlay method should return True for J of Diamonds, trump Hearts, suitLed Hearts''' result = self.player1.canPlay(self.card6, HEARTS, HEARTS) self.assertEqual(result, True) def test_can_play_4(self): '''canPlay method should return False for J of Diamonds, trump Hearts, suitLed Diamonds''' result = self.player1.canPlay(self.card6, HEARTS, DIAMONDS) self.assertEqual(result, False)
def test_is_trump_5(self): '''K of Clubs is not trump when trump is Hearts''' card1 = card.Card(CLUBS, J) result = card1.isTrump(HEARTS) self.assertEqual(result, False)
def test_is_trump_4(self): '''J of Clubs is trump when trump is Clubs''' card1 = card.Card(CLUBS, J) result = card1.isTrump(CLUBS) self.assertEqual(result, True)
def test_is_trump_2(self): '''K of Clubs is not trump when trump is Spades''' card1 = card.Card(CLUBS, K) result = card1.isTrump(SPADES) self.assertEqual(result, False)
def test_new_card(self): '''Creating a card with holder "Not Held" raises an error''' with self.assertRaises(ValueError): card.Card(SPADES, J, NOT_HELD)