def test_get_color_func(self): # arrange c = Card(CardUtils.get_possible_suits()[0], CardUtils.get_possible_colors()[0], CardUtils.get_possible_numbers()[0]) # act col = c.get_color() # assert self.assertTrue(col in CardUtils.get_possible_colors())
def __init__(self, suit, number): if number not in CardUtils.get_possible_numbers(): raise RuntimeError("the number passed to the card is not valid") if suit not in CardUtils.get_possible_suits(): raise RuntimeError("the suit passed to the card is not valid") self.suit = suit self.number = number
def test_card_for_number(self): flag = False try: Card(CardUtils.get_possible_suits()[0], CardUtils.get_possible_colors()[0], "asd") except RuntimeError: flag = True if flag: self.assertTrue(True) else: self.assertTrue(False)
def test_show_card(self): #arrange - make the card c = Card(CardUtils.get_possible_suits()[0], CardUtils.get_possible_numbers()[0]) with io.StringIO() as buffer: #redirect the stdout to the buffer - whatever goes to stdout will go to buffer with contextlib.redirect_stdout(buffer): #what is printed in this with will be redirected to the buffer; act c.show() #assert - getvalue from buffer - check it is the same as the string self.assertTrue(buffer.getvalue().strip() == c.to_string())
def test_card_creation(self): flag = True try: Card( CardUtils.get_possible_suits()[0], CardUtils.get_possible_numbers()[0] ) #here we want to have suit wrong (to make sure get exception) rest right except RuntimeError: flag = False #if we do get error test has failed (not passed as all should be ok) if flag: self.assertTrue(True) else: self.assertTrue(False)
def test_get_deck_gives_all_valid_cards_after_deck_init(self): d = Deck() d.init_deck() d.get_deck() for c in d.get_deck(): if isinstance(c, Card): if c.get_suit() not in CardUtils.get_possible_suits( ) or c.get_number() not in CardUtils.get_possible_numbers(): self.assertTrue(False) else: self.assertTrue( False ) #will fail test directly if there is something not a card self.assertTrue(True) #passes provided all are cards and all valid
def test_show_func_list_input(self): # arrange c = Card( random.choice(CardUtils.get_possible_suits()), random.choice(CardUtils.get_possible_colors()), random.choice(CardUtils.get_possible_numbers()) ) with io.StringIO() as buf: # act with contextlib.redirect_stdout(buf): c.show() # assert self.assertTrue(buf.getvalue().strip() == c.to_string())
def test_get_card_value(self): list_of_values = [] for n in CardUtils.get_possible_numbers(): # for each possibly number in one suit, add the value of card to list list_of_values.append(Card("D", n).get_card_value()) # checks the list of values is as expected for suit self.assertTrue( list_of_values == [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11])
def test_card_for_number(self): flag = False try: Card( CardUtils.get_possible_suits()[0], "uiu" ) #here we want to have num wrong (to make sure get exception) rest right except RuntimeError: flag = True #if there is something wrong i the card num the flag will become true if flag: self.assertTrue( True) #is always true - this is true if there is exception else: self.assertTrue(False)
def test_get_number_function(self): c = Card(CardUtils.get_possible_suits()[0], CardUtils.get_possible_numbers()[0]) number = c.get_number() self.assertTrue(number in CardUtils.get_possible_numbers())
def test_get_suit_function(self): c = Card(CardUtils.get_possible_suits()[0], CardUtils.get_possible_numbers()[0]) suit = c.get_suit() self.assertTrue(suit in CardUtils.get_possible_suits())
def init_deck(self): #go through the possible suits (4) and for each get the possible numbers then mark as red or blue for colour depending on the suit - colour not actually needed for i_suit in CardUtils.get_possible_suits(): for i_num in CardUtils.get_possible_numbers(): #each of the cards added to list of cards in deck self.deck.append(Card(i_suit, i_num))
def init_deck(self): for i_col in CardUtils.get_possible_colors(): for i_suit in CardUtils.get_possible_suits(): for i_num in CardUtils.get_possible_numbers(): self.deck.append(Card(i_suit, i_col, i_num))