Ejemplo n.º 1
0
 def check_vacancies(self) -> CheckResult:
     try:
         page = self.read_page(f'http://localhost:{self.port}/vacancies')
         for person, vacancy in zip(INITIAL_USERS, INITIAL_VACANCIES):
             description = f'{person[1]}: {vacancy[1]}'
             if description not in page:
                 return CheckResult.false(
                     f'Vacancies page should contain vacancies in form <username>: <description>'
                 )
         return CheckResult.true()
     except urllib.error.URLError:
         return CheckResult.false(
             'Cannot connect to the vacancies page.'
         )
Ejemplo n.º 2
0
    def check(self, reply, attach):
        if '200 OK' not in reply:
            return CheckResult.wrong(
                "There isn't internet connection identificator.")

        print(reply.count('['))
        print('Translation' in reply)

        if reply.count('[') >= 2 and 'Translation' in reply:
            return CheckResult.correct()

        return CheckResult.wrong(
            "Try to print lists of translations in both "
            "stages or not to delete first word 'Translation' from it")
Ejemplo n.º 3
0
 def check_sorting_choice(self):
     main = TestedProgram()
     output = main.start(root_dir_path).lower()
     output = main.execute("csv").lower()
     if 'sort' in output and 'desc' in output and 'asc' in output and 'option' in output:
         output = main.execute("3").lower()
         if 'wrong' in output:
             return CheckResult.correct()
         return CheckResult.wrong(
             "You should read and check the user's choice of sorting option"
         )
     return CheckResult.wrong(
         "You should print out sorting options and add input prompt message"
     )
Ejemplo n.º 4
0
 def get_ticket(self, service: str, content: str,
                helper_msg: str) -> CheckResult:
     try:
         page = self.read_page(
             f'http://localhost:{self.port}/get_ticket/{service}')
         if content in page:
             return CheckResult.true()
         else:
             return CheckResult.false(
                 f'Expected to have {content} on /get_ticket/{service} page after\n'
                 f'{helper_msg}')
     except URLError:
         return CheckResult.false(
             f'Cannot connect to the /get_ticket/{service} page.')
Ejemplo n.º 5
0
 def check_resumes(self) -> CheckResult:
     try:
         page = self.read_page(f'http://localhost:{self.port}/resumes')
         for person, resume in zip(INITIAL_USERS[len(INITIAL_VACANCIES):], INITIAL_RESUMES):
             description = f'{person[1]}: {resume[1]}'
             if description not in page:
                 return CheckResult.false(
                     f'Resumes page should contain resumes in form <username>: <description>'
                 )
         return CheckResult.true()
     except urllib.error.URLError:
         return CheckResult.false(
             'Cannot connect to the resumes page.'
         )
Ejemplo n.º 6
0
    def check(self, reply, attach):
        numbers = re.findall(r'[-+]?(\d*\.\d+|\d+)', reply)
        if len(numbers) == 0:
            return CheckResult.wrong(
                'No numbers in the answer',
            )

        if isinstance(attach, tuple):
            for i in numbers:
                if abs(attach[0] - float(i)) < 2:
                    return CheckResult.correct()
            output = 'Numbers in your answer: ' + ' '.join(numbers)
            output += 'But correct principal is {0}'.format(attach)
            return CheckResult.wrong(output)

        if isinstance(attach, list):
            # to exclude answers like 'it takes 2.01 years'
            # but 'it takes 2.0 years' let it be OK.
            epsilon = 0.00001
            numbers = [
                int(float(x)) for x in numbers
                if abs(int(float(x)) - float(x)) < epsilon
            ]
            if attach[1] == 0:
                if 'year' in reply and attach[0] in numbers:
                    return CheckResult.correct()

                output = 'Correct result: {0} years, but you output "{1}"'
                return CheckResult.wrong(
                    output.format(attach[0], reply),
                )
            else:
                if attach[0] in numbers and 'year' in reply:
                    if attach[1] in numbers and 'month' in reply:
                        return CheckResult.correct()

                output = (
                    'Correct result: {0} years {1} months, '
                    'but you output "{2}"'
                )
                return CheckResult.wrong(
                    output.format(attach[0], attach[1], reply),
                )

        if str(attach) not in reply:
            output = (
                'Correct annuity payment is {0} but you output numbers: {1}'
            )
            figures = ' '.join(numbers)
            return CheckResult.wrong(
                output.format(attach, figures),
            )

        return CheckResult.correct()
Ejemplo n.º 7
0
    def check(self, reply, attach):
        self.stop_server()

        if not self.connected:
            return CheckResult.wrong("You didn't connect to the server")

        real_password = attach[0]
        printed_password = reply.split('\n')[0]
        if reply.split('\n')[0] != real_password:
            return CheckResult.wrong(
                'The password you printed is not correct\n'
                'You printed: \"' + printed_password + '\"\n'
                'Correct password: \"' + real_password + '\"')

        return CheckResult.correct()
Ejemplo n.º 8
0
 def generate(self) -> List[TestCase]:
     return [
         TestCase(stdin=["1", (2, lambda x: CheckResult.correct())],
                  attach="1\n2\n2\n"),
         TestCase(stdin=[(2, lambda x: "3"), lambda x: "3",
                         lambda x: CheckResult.wrong("WA TEST 2")],
                  attach="3\n3\n3\n"),
         TestCase(stdin=[(-1, lambda x: "4"),
                         lambda x: CheckResult.wrong("WA TEST 3")],
                  attach="4\n4\n4\n"),
         TestCase(stdin=[(2, lambda x: "5"),
                         lambda x: CheckResult.wrong("WA TEST 4")
                         if x == "5\n" else 5],
                  attach="4\n4\n4\n"),
     ]
Ejemplo n.º 9
0
 def test_first_ask(self, reply):
     lines_split = reply.strip().split('\n')
     if "print the definition of" not in reply.lower():
         return CheckResult.wrong(
             "Your program did not ask the user to print the definition of the card.\n"
         )
     if "cat" not in reply.lower() and "dog" not in reply.lower():
         return CheckResult.wrong(
             "Your program did not ask for the definition of any of existing cards:\n"
             "{0}".format(
                 lines_split[-1] if len(lines_split) > 0 else reply))
     if "cat" in reply.lower():
         return "woof"
     else:
         return "meow"
Ejemplo n.º 10
0
    def check_dead_end(self, reply: str, attach: Any) -> CheckResult:
        for line in reply.lower().split("\n")[-4:-1]:
            if "possible" in line:
                break
        else:
            return CheckResult.wrong(
                "You need check if there are no more possible moves")

        for line in reply.lower().split("\n")[-4:-1]:
            if attach in line:
                break
        else:
            return CheckResult.wrong(
                "Number of moves taken is incorrect or not displayed")
        return CheckResult.correct()
Ejemplo n.º 11
0
    def check_main_page_login_link(self):
        login_link = '/login/'
        try:
            page = self.read_page(f'http://localhost:{self.port}/tube/')
        except urllib.error.URLError:
            return CheckResult.false('Cannot connect to the main page.')

        links_from_page = re.findall(self.COMMON_LINK_PATTERN, page, re.S)
        links_from_page = self.__stripped_list(links_from_page)

        if login_link not in links_from_page:
            return CheckResult.false(
                f'Main page should contain <a> element with href {login_link}')

        return CheckResult.true()
Ejemplo n.º 12
0
    def check_duplicate_enum(self):
        main = TestedProgram()
        output = main.start(root_dir_path).lower()
        output = main.execute("").lower()
        output = main.execute("1").lower()
        output = main.execute("yes").lower().split('\n')

        n = 1
        for val in output:
            if '.' in val:
                if val[0] != str(n):
                    return CheckResult.wrong(
                        f"Wrong file numbering. File: {val} ")
                n += 1
        return CheckResult.correct()
Ejemplo n.º 13
0
    def check_group_first_line(self):
        main = TestedProgram()
        output = main.start(root_dir_path).lower()
        output = main.execute("").lower()
        output = main.execute("2").lower().split('\n')
        output = [val for val in output if val]

        if not output:
            return CheckResult.wrong(
                "After choosing ascending sorting order your output is empty!")

        if 'byte' in output[0]:
            return CheckResult.correct()
        return CheckResult.wrong(
            f"The first line of group of files should contain files size")
Ejemplo n.º 14
0
 def check_valid_res(self, reply, attach=None) -> CheckResult:
     qod = requests.get(
         "http://api.quotable.io/quotes/-CzNrWMGIg8V").json()["content"]
     if qod in reply:
         return CheckResult.correct()
     elif isinstance(reply, str):
         return CheckResult.wrong(
             "Couldn't find the exact quote in the result.")
     elif isinstance(reply, (list, dict)):
         return CheckResult.wrong(
             "Make sure you extracted the quote from the json body correctly."
         )
     else:
         return CheckResult.wrong(
             "The result doesn't look like a quote... at all.")
Ejemplo n.º 15
0
    def check_duplicate_first_line(self):
        main = TestedProgram()
        output = main.start(root_dir_path).lower()
        output = main.execute("").lower()
        output = main.execute("2").lower()
        output = main.execute("yes").lower().split('\n')
        output = [val for val in output if val]

        if len(output) < 1 or 'byte' not in output[0]:
            return CheckResult.wrong(
                f"The first line of group of files should contain files size")
        if len(output) < 2 or 'hash' not in output[1]:
            return CheckResult.wrong(
                f"The second line of group of files should contain hash value")
        return CheckResult.correct()
Ejemplo n.º 16
0
    def check(self, reply, attach):
        punct = {".", "?", "!"}
        corpus = preprocess()
        trigrams = {" ".join(corpus[i:i + 3]) for i in range(len(corpus) - 2)}
        sentences = [
            sentence for sentence in reply.split('\n') if len(sentence)
        ]

        if len(sentences) != 10:
            return CheckResult.wrong(
                "You should output exactly 10 sentences! Every sentence should be in a new line."
            )

        for sentence in sentences:
            sent = sentence.split()
            if len(sent) < 5:
                return CheckResult.wrong(
                    "A pseudo-sentence should not be shorter than 5 tokens.")
            if len(set(sent)) == 1:
                return CheckResult.wrong(
                    "Invalid output. All words of a sentence are identical.")
            if not sent[0][0].isupper():
                return CheckResult.wrong(
                    "Every pseudo-sentence should start with a capitalized word."
                )
            if sent[0][-1] in punct:
                return CheckResult.wrong(
                    "The first token of a pseudo-sentence should not end with sentence-ending punctuation."
                )
            if sent[-1][-1] not in punct:
                return CheckResult.wrong(
                    "Every pseudo-sentence should end with a sentence-ending punctuation mark."
                )
            for i, token in enumerate(sent):
                if token not in corpus:
                    return CheckResult.wrong(
                        "Sentences should contain only words from the corpus!")
                if token[-1] in punct and 4 < i + 1 < len(sent):
                    return CheckResult.wrong(
                        "If a sentence is longer than 5 tokens, it should end at the first sentence ending punctuation."
                    )
            for i in range(len(sent) - 2):
                trigram = " ".join(sent[i:i + 3])
                if trigram not in trigrams:
                    return CheckResult.wrong(
                        "Pseudo-sentences should entirely consist of trigrams from the corpus."
                    )
        return CheckResult.correct()
Ejemplo n.º 17
0
    def check_main_header(self) -> CheckResult:
        self.__setup()
        try:
            page = self.read_page(f'http://localhost:{self.port}/news/')
        except urllib.error.URLError:
            return CheckResult.false('Cannot connect to the main page.')

        h2_headers = re.findall(self.H2_PATTERN, page, re.S)
        h2_headers = self.__stripped_list(h2_headers)
        main_header = 'Hyper news'

        if main_header not in h2_headers:
            return CheckResult.false(
                'Main page should contain <h2> element with text "Hyper news"')

        return CheckResult.true()
Ejemplo n.º 18
0
 def _check_solution(self, test: TestCase, output: str):
     if isinstance(TestRun.curr_test_run.error_in_test, TestPassedException):
         return CheckResult.correct()
     if test.check_function is not None:
         return test.check_function(output, test.attach)
     else:
         return self.check(output, test.attach)
Ejemplo n.º 19
0
 def test_reset(self, reply):
     if "has been reset" not in reply.lower():
         return CheckResult.wrong(
             "The line \"Card statistics has been reset.\" was expected in the output "
             "after the user entered \"reset stats\", but it was not found there."
         )
     return "hardest card"
Ejemplo n.º 20
0
 def test_zero_mistakes(self, reply):
     if 'no cards with errors' not in reply:
         return CheckResult.wrong("The user ask to output the hardest card. Since no questions were asked yet, "
                                  "the output \"There are no cards with errors.\" was expected. \n"
                                  "However, it was not found. \n"
                                  "Make sure your program correctly counts the number of mistakes that were made.")
     return "add"
Ejemplo n.º 21
0
 def get_client_menu_page(self) -> CheckResult:
     try:
         page = self.read_page(f'http://localhost:{self.port}/menu')
         links = re.findall(self.ELEMENT_PATTERN, page)
         for link in (
                 '/get_ticket/change_oil',
                 '/get_ticket/inflate_tires',
                 '/get_ticket/diagnostic',
         ):
             if link not in links:
                 return CheckResult.false(
                     f'Menu page should contain <a> element with href {link}'
                 )
         return CheckResult.true()
     except URLError:
         return CheckResult.false('Cannot connect to the /menu page.')
Ejemplo n.º 22
0
 def test_ask_2(self, reply):
     if "dog" not in reply.lower() and "horse" not in reply.lower():
         return CheckResult.wrong("Your program did not ask for the definition of any of the existing cards.")
     if "dog" in reply.lower():
         return "woof"
     else:
         return "neigh"
Ejemplo n.º 23
0
    def test_import(self, reply):
        if "2 cards have been loaded" not in reply.lower():
            return CheckResult.wrong("Your program should have imported cards from a file and output a message:\n"
                                     "\"{n} cards have been loaded.\" "
                                     "Check if the message is printed in the correct format "
                                     "and the number of cards is calculated correctly.")
        try:
            os.remove(filepath)
        except PermissionError:
            return CheckResult.wrong("Impossible to remove the file with the exported cards. "
                                     "Perhaps you haven't closed this file?")
        except FileNotFoundError:
            return CheckResult.wrong("A file from which the cards should have been imported is not found. "
                                     "Make sure you did not delete the file after importing the cards.")

        return "ask\n1"
Ejemplo n.º 24
0
    def test_output_existing_def(self, reply):
        if "already exists" not in reply.lower():
            return CheckResult.wrong("Your program did not output an error message "
                                     "when the user tried to input a duplicate definition. "
                                     "Instead, your program printed this line:\n{}".format(reply))

        return "woof\nask"
Ejemplo n.º 25
0
    def test_randomness(self, reply):
        reply = reply.lower()
        quiz_start_index = reply.find("print the definition of")
        reply = reply[quiz_start_index:]

        if "the hardest card is" not in reply or "errors" not in reply or "answering it" not in reply:
            return CheckResult.wrong("The line naming the hardest card and stating the number of mistakes "
                                     "made for this card was expected in the output of your program.\n"
                                     "However, it was not found. Make sure you use the correct formatting of your output.")

        quiz_end_index = reply.find("the hardest card is")
        quiz = reply[:quiz_end_index]

        questions_found = quiz.count("print the definition of")
        if questions_found != questions_number:
            return CheckResult.wrong("The program did not ask the user "
                                     "for the definition of a card for the required number of times.\n"
                                     "Make sure that your program reads correctly the input specifying the number of times to ask. \n"
                                     "Also make sure that it is able to ask definitions more than once:\n"
                                     "for example, even if there are only 3 cards added, your program still "
                                     "should be able to ask the user for 100 times.")
        japan_asked_n = quiz.count("japan")
        canada_asked_n = quiz.count("canada")
        poland_asked_n = quiz.count("poland")
        minimum_asked = questions_number // 6

        if japan_asked_n < minimum_asked or canada_asked_n < minimum_asked or poland_asked_n < minimum_asked:
            return CheckResult.wrong("It looks like during the quiz your program does not choose the cards randomly.\n "
                                     "Make sure your program uses the random module to choose which definition "
                                     "it'll ask the user to give.\n"
                                     "If you're sure that you choose the cards randomly, try to rerun the tests.")

        asked_n = {'japan': japan_asked_n, 'canada': canada_asked_n, 'poland': poland_asked_n}
        sorted_keys = sorted(asked_n.keys(), key=lambda x:asked_n[x])
        n_wrong = asked_n[sorted_keys[-1]]
        max_wrong = [key for key in asked_n.keys() if asked_n[key] == n_wrong]

        hardest_card_line = reply[reply.find("the hardest card is"):reply.find("answering it")]
        if str(n_wrong) not in hardest_card_line:
            return CheckResult.wrong("Seems like your program incorrectly calculated "
                                     "the number of errors for the hardest card.")

        if not any(card_name in hardest_card_line for card_name in max_wrong):
            return CheckResult.wrong("Seems like your program incorrectly identified the card with "
                                     "the maximum number of errors.")

        return "export\n{0}\nimport\n{0}\nhardest card".format(filepath_2)
Ejemplo n.º 26
0
    def check(self, reply, attach):

        # Incorrect URL
        if attach is None:
            if 'error' in reply.lower():
                return CheckResult.correct()
            else:
                return CheckResult.wrong(
                    'There was no "error" word, but should be.')

        # Correct URL
        if isinstance(attach, tuple):

            if len(attach) == 4:
                _, *attach = attach
                if 'error' not in reply.lower():
                    return CheckResult.wrong(
                        'There was no "error" word, but should be.')

            right_word, wrong_word, correct_file_name = attach

            path_for_tabs = 'tb_tabs'

            if not os.path.isdir(path_for_tabs):
                return CheckResult.wrong(
                    "Can't find a directory \"" + path_for_tabs + "\" "
                    "in which you should save your web pages.")

            if not self._check_files(path_for_tabs, right_word):
                return CheckResult.wrong(
                    "Seems like you did\'n save the web page "
                    "\"" + right_word + "\" into the "
                    "directory \"" + path_for_tabs + "\". "
                    "This file with page should be named \"" +
                    correct_file_name + "\"")

            try:
                shutil.rmtree(path_for_tabs)
            except PermissionError:
                return CheckResult.wrong(
                    "Impossible to remove the directory for tabs. Perhaps you haven't closed some file?"
                )

            if wrong_word in reply:
                return CheckResult.wrong(
                    'It seems like you printed wrong variable')

            if right_word in reply:
                return CheckResult.correct()

            return CheckResult.wrong(
                'You printed neither bloomberg_com nor nytimes_com')
Ejemplo n.º 27
0
 def check_the_win(self, reply: list, attach: Any) -> CheckResult:
     design = '=' * 70
     reply_parsed = reply.split(design)
     try:
         the_last = [i for i in reply_parsed[-1].strip().split('\n') if i]
         comp_pieces = int([i.strip() for i in the_last[1].split(':') if i][-1])
         check_the_pieces = ':[' in reply_parsed[-1] or comp_pieces == 0
     except Exception:
         return CheckResult.wrong('Your output is wrong! Make sure you print data like in examples!')
     your_true_win = not check_the_pieces
     you_won_status = "the game is over. you won" in the_last[-1].lower()
     if your_true_win or you_won_status:
         if not (your_true_win and you_won_status):
             return CheckResult.wrong("Either the result or the status is wrong")
     elif "the game is over. the computer won" not in the_last[-1].lower():
         return CheckResult.wrong("The status is not right")
     return CheckResult.correct()
Ejemplo n.º 28
0
 def func5(self, output):
     if self.current_status == 'computer':
         if "invalid input. please try again." not in output.lower():
             return CheckResult.wrong("The player should be informed about tne incorrect move")
         return '1'
     else:
         self.set_the_currents(output)
         return self.check_the_move_ver_2(output)
Ejemplo n.º 29
0
    def check_sys_export(reply, attach):
        if "cards have been saved" not in reply.lower():
            return CheckResult.wrong("The user has provided the --export_to command-line argument. \n"
                                     "So, after the user inputs the command \"exit\", \n"
                                     "your program should save the cards to the file specified in the command-line argument\n."
                                     "After that, a message about the number of cards that have been saved should be printed by your program.\n"
                                     "However, this message was not found.")
        if "3 cards have been saved" not in reply.lower():
            return CheckResult.wrong("Seems like your program incorrectly printed the number of cards "
                                     "exported to file after \"exit\" command.")
        if not os.path.exists(attach):
            return CheckResult.wrong("The user has provided the --export_to command-line argument. \n"
                                     "However, the file where the cards should have been exported after \"exit\" was not found. "
                                     "Make sure you named the file with exported cards "
                                     "as was required in --export_to command-line argument.")

        return CheckResult.correct()
Ejemplo n.º 30
0
 def test_hardest_after_reset(self, reply):
     if "no cards with errors" not in reply.lower():
         return CheckResult.wrong("After the reset of stats, "
                                  "the line \"There are no cards with errors.\" is expected\n"
                                  "when the program is asked about the hardest card.\n"
                                  "However, your program does not seem to respond to this command correctly.")
     self.is_completed = True
     return "exit"