def check_the_win(self, reply: list, attach: Any) -> CheckResult: design = '=' * 70 if not reply: raise WrongAnswerException( "The reply is empty. Please, output the required data.") reply_parsed = reply.split(design) the_last = [ i.strip() for i in reply_parsed[-1].strip().split('\n') if i ] try: comp_pieces = int([i.strip() for i in the_last[1].split(':') if i][-1]) except Exception: raise WrongAnswerException( "Make sure you output pieces in the required format.") last_output = reply_parsed[-1].replace(' ', '') # check for the win if '1:[' not in last_output: if ':[' in last_output or comp_pieces == 0: return CheckResult.wrong("The result is wrong") if "the game is over. you won" not in the_last[-1].lower(): return CheckResult.wrong("The status is not right") # check for the computer win elif int(the_last[1][-1]) == 0: if ':[' not in last_output or comp_pieces > 0: return CheckResult.wrong("The result is wrong") if "the game is over. the computer won" not in the_last[-1].lower( ): return CheckResult.wrong("The status is not right") else: if "the game is over. it's a draw" not in the_last[-1].lower(): return CheckResult.wrong("The status is not right") return CheckResult.correct()
def get_the_ends(self, output): """Get the ends of the domino snake""" try: domino_snake = self.parse_the_output(output)[3] self.left_end = ast.literal_eval(domino_snake[:6]) self.right_end = ast.literal_eval(domino_snake[-6:]) except (SyntaxError, ValueError, IndexError): raise WrongAnswerException("Make sure your output is formatted according to the examples") except IndexError: raise WrongAnswerException("Some elements are missing from the snake")
def test8_input3(self, out): out_put_list = out.strip().split('\n') if len(out_put_list) == 0: raise WrongAnswerException("The output can't be empty") else: first_line = out_put_list[0].strip().split(':') if not first_line[1].strip() == NEW_SECOND_ANSWER: raise WrongAnswerException( "update function doesn't update answer properly") return CheckResult.correct()
def test3_input2(self, out): out_list = out.strip().split('\n') if len(out_list) < 2: raise WrongAnswerException("The output is not printed correctly") else: if not out_list[0] == 'Rome is not an option': raise WrongAnswerException( 'Rome is not an option\nshould be printed') out_list.pop(0) if self.check_sub_menu('\n'.join(out_list)): return CheckResult.correct()
def test2_input2(self, out): out_list = out.strip().split('\n') if len(out_list) < 2: raise WrongAnswerException("The output is not printed correctly") else: if not out_list[0] == '5 is not an option': raise WrongAnswerException( '5 is not an option\nshould be printed in output') out_list.pop(0) if self.check_main_menu('\n'.join(out_list)): return 'we'
def test8_input2(self, out): out_put_list = out.strip().split('\n') if len(out_put_list) == 0: raise WrongAnswerException("The output can't be empty") else: first_line = out_put_list[0].strip().split(':') if not first_line[1].strip() == NEW_SECOND_QUESTION: raise WrongAnswerException( "update function doesn't update question properly") return 'y'
def test7_input3(self, out): out_put_list = out.strip().split('\n') update_menu_list = UPDATE_MENU.strip().split('\n') if len(out_put_list) > 3 or len(out_put_list) < 2: raise WrongAnswerException( f'The update_menu has "2" lines and it should be like this: \n{UPDATE_MENU}' ) for index, content in enumerate(update_menu_list): if not content == out_put_list[index]: raise WrongAnswerException( f'The line no.{index + 1} should be like this: {content}') return 'd'
def test10_input11(self, out): out_put = out.strip().split('\n') if len(out_put) == 0: raise WrongAnswerException("The output can't be empty") else: first_line = out_put[0].strip() if not first_line == 'There is no flashcard to practice!': raise WrongAnswerException( 'After three successive correct answers the question should be deleted from ' 'database\nand the first line in the output should be like this:' ' \nThere is no flashcard to practice!') return CheckResult.correct()
def check_sub_menu(self, out): sub_menu_list = SUB_MENU.strip().split('\n') out_list = out.strip().split('\n') if len(out_list) > 3 or len(out_list) < 2: raise WrongAnswerException( f'The sub_menu has "2" lines and it must be like this:\n{SUB_MENU}' ) for index, content in enumerate(sub_menu_list): if not content == out_list[index]: raise WrongAnswerException( f'The line no.{index + 1} must be like this:\n{sub_menu_list[index]}' ) return True
def check_main_menu(self, out): main_menu_list = MAIN_MENU.strip().split('\n') out_list = out.strip().split('\n') if len(out_list) > 4 or len(out_list) < 3: raise WrongAnswerException( f'The main menu has "3" lines and it must be like this:\n{MAIN_MENU}' ) for index, action in enumerate(main_menu_list): if not action == out_list[index]: raise WrongAnswerException( f'the line no.{index + 1} of main menu must be like this:\n {action}' ) return True
def get_the_ends(self, output): """Get the ends of the domino snake""" try: domino_snake = self.parse_the_output(output)[3] self.left_end = ast.literal_eval(domino_snake[:6]) self.right_end = ast.literal_eval(domino_snake[-6:]) except IndexError: raise WrongAnswerException("Some elements are missing") except (ValueError, SyntaxError): raise WrongAnswerException( "An error occurred while processing your output.\n" "Please make sure that your program's output is formatted exactly as described." )
def get_credentials(output: str): number = re.findall(r'400000\d{10}', output, re.MULTILINE) if not number: raise WrongAnswerException( 'You are printing the card number incorrectly. ' 'The card number should look like in the example: 400000DDDDDDDDDD, where D is a digit.' ) PIN = re.findall(r'^\d{4}$', output, re.MULTILINE) if not PIN: raise WrongAnswerException( 'You are printing the card PIN incorrectly. ' 'The PIN should look like in the example: DDDD, where D is a digit.' ) return number[0], PIN[0]
def eject_next_input(self) -> List[str]: if len(self.input_text_funcs) == 0: return [] input_function = self.input_text_funcs[0] if input_function.trigger_count > 0: input_function.trigger_count -= 1 curr_output = StdoutHandler.get_partial_output() next_func = input_function.input_function new_input: str try: obj = next_func(curr_output) if isinstance(obj, str): new_input = obj elif isinstance(obj, CheckResult): if obj.result: raise TestPassedException() else: raise WrongAnswerException(obj.feedback) else: raise FatalErrorException( 'Dynamic input should return ' + f'str or CheckResult objects only. Found: {type(obj)}') except BaseException as ex: TestRun.curr_test_run.error_in_test = ex return [] if input_function.trigger_count == 0: self.input_text_funcs.pop(0) new_input = clear_text(new_input) return new_input.strip().split('\n')
def test1_input11(self, out): question = out.strip().split('\n') if len(question) == 0: raise WrongAnswerException("The output can't be empty") else: question = '\n'.join(question) if self.check_practice_question(question.strip(), SECOND_QUESTION): return 'n'
def get_stock_size(self, output): """Get the stock size""" output_parsed = self.parse_the_output(output) try: stock_size = int([i.strip() for i in output_parsed[1].split(':')][-1]) except ValueError: raise WrongAnswerException("Make sure your output is formatted according to the examples") return stock_size
def get_the_computer_pieces(self, output): """Get the amount of computer pieces""" output_parsed = self.parse_the_output(output) try: len_comp_pieces = int([i.strip() for i in output_parsed[2].split(':')][-1]) except ValueError: raise WrongAnswerException("Make sure your output is formatted according to the examples") return len_comp_pieces
def get_credentials(output: str): number = re.findall(r'400000\d{10}', output, re.MULTILINE) if not number: raise WrongAnswerException( 'You are printing the card number incorrectly. ' 'The card number should look like in the example: 400000DDDDDDDDDD,' ' where D is a digit.\nMake sure the card number is 16-digit length and ' 'you don\'t print any extra spaces at the end of the line!') PIN = re.findall(r'^\d{4}$', output, re.MULTILINE) if not PIN: raise WrongAnswerException( 'You are printing the card PIN incorrectly. ' 'The PIN should look like in the example: DDDD, where D is a digit.\n' 'Make sure the PIN is 4-digit length and you don\'t print any extra spaces at the' ' end of the line!') return number[0], PIN[0]
def get_the_stock(self, output): """Get the player's stock""" out_parsed = self.parse_the_output(output) try_stock = [i for i in out_parsed if ':[' in i] try: the_stock = [ast.literal_eval(i[-6:]) for i in try_stock] except (ValueError, SyntaxError): raise WrongAnswerException("An error occurred while processing your output.\n" "Please make sure that your program's output is formatted exactly as described.") return the_stock
def test7_input7(self, out): output_list = out.strip().split('\n') if len(output_list) == 0: raise WrongAnswerException("The output can't be empty") else: first_line = output_list[0].split(':') if len(first_line) < 2: raise WrongAnswerException("The ':' is missing in output") else: second_line = output_list[1] if not first_line[0].strip() == "current answer": raise WrongAnswerException( 'current answer:\nis missing or misspelling') if not first_line[1].strip() == SECOND_ANSWER: raise WrongAnswerException( ' after current answer: answer should be printed') if not second_line == 'please write a new answer:': raise WrongAnswerException( 'please write a new answer: \nis missing or misspelling' ) return NEW_SECOND_ANSWER
def check_practice_answer(self, out, answer): out_list = out.strip().split('\n') q_s_list = Q_S.strip().split('\n') if len(out_list) == 0: raise WrongAnswerException("output can't be empty") else: first_part = out_list.pop(0).strip().split(':') if len(first_part) == 0: raise WrongAnswerException("output can't be empty") else: if not 'Answer' == first_part[0]: raise WrongAnswerException( 'Answer:\n is missing or misspelling!') if not answer == first_part[1].strip(): raise WrongAnswerException( 'The answer is not printed correctly') if q_s_list[0] in out_list and q_s_list[ 1] in out_list and q_s_list[2] in out_list: return True raise WrongAnswerException( f'{Q_S}\nshould be printed after answer')
def set_the_currents(self, output): """Too random, need to consider computer options""" error = WrongAnswerException( f"Make sure you calculate the number of stock and computer pieces correctly" ) self.get_the_ends(output) if self.current_status == 'player': stock_dif = self.get_stock_size(output) - self.current_stock_size comp_dif = self.get_the_computer_pieces( output) - self.current_computer_pieces if self.first_move: if stock_dif != 0 or comp_dif != 0: raise error else: return # if the computer took a piece from the stock if stock_dif == -1 and comp_dif == 1: self.current_computer_pieces += 1 self.current_stock_size -= 1 # if the computer made a move elif stock_dif == 0 and comp_dif == -1: self.current_computer_pieces -= 1 elif self.get_stock_size(output) == 0: return else: raise error elif self.current_status == 'computer': stock_dif = self.get_stock_size(output) - self.current_stock_size player_dif = len( self.get_the_stock(output)) - self.current_player_pieces if self.first_move: if stock_dif != 0 or player_dif != 0: raise error else: return # if player took piece from the if stock_dif == -1 and player_dif == 1: self.current_player_pieces += 1 self.current_stock_size -= 1 # if the player made a move elif stock_dif == 0 and player_dif == -1: self.current_player_pieces -= 1 elif self.get_stock_size(output) == 0: return else: raise error
def check_practice_question(self, out, question): out_list = out.strip().split('\n') if len(out_list) < 2: raise WrongAnswerException("output can't be empty") else: out_first_line = out_list[0].split(':') if len(out_first_line) == 0: raise WrongAnswerException("output can't be empty") else: if not 'Question' == out_first_line[0]: raise WrongAnswerException( f'The line \"{out_first_line[0]}\" is wrong!\nPlease check extra spaces, misspelling or ":"' ) if len(out_list) < 4: raise WrongAnswerException( "Incorrect number of lines is found for the menu after the question \n" f"\"{out_first_line[0]}\"\n" f"Please, format your output according to the example." ) question_menu = f'{out_list[1]}\n{out_list[2]}\n{out_list[3]}' if not question == out_first_line[1].strip(): raise WrongAnswerException('wrong question is printed') if not Q_S.strip() == question_menu.strip(): raise WrongAnswerException( f'{question_menu} is wrong\n{Q_S} is correct') return True
def check_the_move_ver_2(self, output, to_fail=None): """Check the result when the computer made a move""" if not self.check_the_design(output): raise WrongAnswerException("The design is not right") if not self.check_stock_size(output): raise WrongAnswerException("The stock size is not right") if not self.check_computer_pieces(output): raise WrongAnswerException("The amount of computer pieces is not right") if not self.check_player_unique(output): raise WrongAnswerException("The player pieces are not unique") if not self.check_the_status(output): raise WrongAnswerException("The result is not right") if not self.check_the_snake(output): raise WrongAnswerException("Your snake is too long") if 'computer is' in output.lower(): self.current_status = 'player' return '' else: self.current_status = 'computer' if to_fail is not None: return to_fail else: self.current_stock_size -= 1 return '0'
def check_the_move(self, output, to_fail=False, mistake=None): """Check the result when the computer made a move""" if not self.check_the_design(output): raise WrongAnswerException("The design is not right") if not self.check_stock_size(output): raise WrongAnswerException("The stock size is not right") if not self.check_computer_pieces(output): raise WrongAnswerException( "The amount of computer pieces is not right") if not self.check_player_unique(output): raise WrongAnswerException("The player pieces are not unique") if not self.check_the_status(output): raise WrongAnswerException("The result is not right") if not self.check_the_piece(output): raise WrongAnswerException("The piece added is illegal") if 'computer is' in output.lower(): self.current_status = 'player' return '' else: self.current_status = 'computer' if to_fail: return self.choose_false(output) if mistake is not None: return mistake else: return self.choose_the_piece(output)
def test9_input3(self, out): out_put_list = out.strip().split('\n') if len(out_put_list) == 0: raise WrongAnswerException("The output can't be empty") else: first_line = out_put_list[0].strip().split(':') if len(first_line) < 2: raise WrongAnswerException("The ':' is missing in output") else: out_put_list.pop(0) check_learn_menu = '\n'.join(out_put_list) if not first_line[0].strip() == 'Answer': raise WrongAnswerException( 'Answer is missing or misspelling') if not first_line[1].strip() == NEW_SECOND_ANSWER: raise WrongAnswerException( 'before check_learn_menu the answer should be printed') if not check_learn_menu.strip() == CHECK_LEARN_MENU.strip(): raise WrongAnswerException( f'your check_learn_menu:\n{check_learn_menu} \ncorrect check_learn_ menu\n' f'{CHECK_LEARN_MENU} ') return CheckResult.correct()
def choose_the_piece(self, output): """Choose the piece for the player to pick""" self.get_the_ends(output) try: end1 = self.left_end[0] end2 = self.right_end[1] except Exception: raise WrongAnswerException("Your output has wrong format! Make sure you print the pieces like in example!") player_stock = self.get_the_stock(output) for i, j in enumerate(player_stock): if end2 in j: return str(i + 1) elif end1 in j: return str(-(i + 1)) else: return '0'
def check_the_piece(self, output): """Check if the piece added is acceptable""" domino_snake = self.parse_the_output(output)[3] try: new1 = ast.literal_eval(domino_snake[:6]) new2 = ast.literal_eval(domino_snake[-6:]) except (ValueError, SyntaxError): raise WrongAnswerException("An error occurred while processing your output.\n" "Please make sure that your program's output is formatted exactly as described.") new_to_check = [] if new1 != self.left_end: new_to_check = new1 elif new2 != self.right_end: new_to_check = new2 if new_to_check: if new_to_check[1] != self.left_end[0]: return False elif new_to_check[0] != self.right_end[1]: return False return True
def test5_input6(self, out): output = out.strip().split('\n') if not 'Answer:' in output: raise WrongAnswerException("the answer can't be empty!") return CheckResult.correct()
def check(self, reply: str, attach): all_output = reply.strip().split('\n') if not 'Bye!' == all_output[-1]: raise WrongAnswerException('Bye! is missing or misspelling!') return CheckResult.correct()
def test4_input5(self, out): output = out.strip().split('\n') if not 'Question:' in output: raise WrongAnswerException("the question can't be empty!") return CheckResult.correct()