def python_2_051a(p_filename, p_filename_data):
    """
    Does the tests for 2.051a
    :param p_filename: filename (string)
    :param p_filename_data: contents of the filename (string)
    :return: A dictionary of test info
    """
    from app.python_labs.find_items import find_list_items
    from app.python_labs.io_test import io_test

    prizes = find_list_items(p_filename_data, 'prizes')
    test_2 = io_test(p_filename, prizes[1], 2)
    test_3 = io_test(p_filename, prizes[2], 3)

    p_pass_tests = {
        "name":
        "2 test cases for 2.051a work (8 points) <br>",
        "pass":
        True,
        "pass_message":
        "<h5 style=\"color:green;\">Pass!</h5>  2 test cases work",
        "fail_message":
        "<h5 style=\"color:red;\">Fail.</h5>  Check your 2 test cases.<br>"
        "If you run the program, and I type '1', it should print out 0th element of prizes "
        "list."
        " <br> "
        "Same for prize2, prize3, and prize4. <br>"
        "User should input '1', '2', '3', or '4', not 'door1', 'prize1' or anything like "
        "that",
        "points":
        0,
    }

    debug_string = ''

    if test_2['pass']:
        p_pass_tests['points'] += 4
        debug_string += ' 2 prize passed '
    else:
        p_pass_tests['fail_message'] += test_2['fail_message'] + "<br> This was result after entering " \
                                                                 "'2' at keyboard.<br>"
    if test_3['pass']:
        p_pass_tests['points'] += 4
        debug_string += ' 3 prize passed'
    else:
        p_pass_tests['fail_message'] += test_3['fail_message'] + "<br> This was result after entering " \
                                                                 "'3' at keyboard.<br>"
    if p_pass_tests['points'] != 8:
        p_pass_tests['pass'] = False
        p_pass_tests['debug'] = debug_string
    return p_pass_tests
def python_2_051b_2(p_filename, points):
    """
    Does the tests for 2.051b test 12
    :param p_filename: filename (string)
    :param points: Points this is worth(int)
    :return: A dictionary of test info
    """
    from app.python_labs.io_test import io_test

    test_io = io_test(p_filename, r'\[\s*1\s*,\s*1\s*,\s*1\s*,\s*1\s*\]', 1)

    p_tests = {
        "name":
        "Test case #2 for 2.051b works (" + str(points) + " points) <br>"
        " Human input of C R L S 'blahblah', should get back [ 1, 1, 1, 1] on screen",
        "pass":
        True,
        "pass_message":
        "<h5 style=\"color:green;\">Pass!</h5>  Test case #2 for 2.051a works!",
        "fail_message":
        "<h5 style=\"color:red;\">Fail.</h5>  Check your test cases.<br>"
        "Be sure your program works with capital letters input from "
        "keyboard (i.e. C R L S not c r l s).<br>"
        "Be sure you are asking the user for 5 votes.<br>",
        "points":
        0,
    }

    if test_io['pass']:
        p_tests['points'] += points
    else:
        p_tests['pass'] = False
        p_tests['fail_message'] += test_io['fail_message']
    return p_tests
def win_most(p_filename):
    import re
    from app.python_labs.io_test import io_test

    print("yes")
    p_test = {"name": "Given a win % of .75, Wimbledon win% should be .75 (15 points) <br>",
              "pass": True,
              "pass_message": "<h5 style=\"color:green;\">Pass!</h5>  Given a win% of .75 Serena wins around 75% of "
                              "Wimbledon",
              "fail_message": "<h5 style=\"color:red;\">Fail.</h5> "
                              "Given a win% of .75, Serena does NOT win Wimbledon correct % of times.<br>"
                              "Check looks for a string that looks like this: 'Serena won Wimbledon 75.0% of the time'"
                              "<br>Spacing and percent symbols matter! 75.0 % or 75.0 will fail.<br>",
              "points": 0,
              }
    test1 = io_test(p_filename, r'NO_MATCH', 2)
    match = re.search(r'([.0-9]+)%', test1['fail_message'], re.X | re.M | re.S)
    percent = "no matches were run"
    if match:
        print(str(match.group(0)) + " " + str(match.group(1)))
        percent = float(match.group(1))
        if percent < 12.5 or percent > 14.5:
            p_test['pass'] = False
        else:
            p_test['points'] += 15
    else:
        p_test['pass'] = False
    if p_test['pass'] is False:
        p_test['fail_message'] += 'Test found that Serena wins this percentage of matches: ' + str(percent) +\
                                  '<br>  Expect her to win between 12.5 and 14.5% of matches.<br>'
    return p_test
def win_all(p_filename):
    import re
    from app.python_labs.io_test import io_test

    print("yes")
    p_test = {"name": "Given a win % of 1.0, Wimbledon win% should be 1.0 (15 points) <br>",
              "pass": True,
              "pass_message": "<h5 style=\"color:green;\">Pass!</h5>  Given a win% of 1.0, Serena always wins "
                              "Wimbledon",
              "fail_message": "<h5 style=\"color:red;\">Fail.</h5> "
                              "Given a win% of 1.0, Serena does NOT always wins Wimbledon.<br>"
                              "Check looks for a string that looks like this: 'Serena won Wimbledon 100.0% of the time'"
                              "<br>Spacing and percent symbols matter! 100.0 % or 100.0 will fail.<br>",
              "points": 0,
              }
    test1 = io_test(p_filename, r'NO_MATCH', 1)
    match = re.search(r'([.0-9]+)%', test1['fail_message'], re.X | re.M | re.S)
    percent = "no matches were run"
    if match:
        print(str(match.group(0)) + " " + str(match.group(1)))
        percent = float(match.group(1))
        if percent > 101.0 or percent < 99.0:
            p_test['pass'] = False
        else:
            p_test['points'] += 15
    else:
        p_test['pass'] = False
    if p_test['pass'] is False:
        p_test['fail_message'] += 'Test found that Serena wins this percentage of matches: ' + str(percent) +\
                                  '<br>  Expect her to win between 101 and 99% of matches.<br>' \
                                  'Sometimes this error is because game is slightly broken.  Test it in isolation, ' \
                                  'it should give a win every single time if winning_percentage is 1.0.<br> '
    return p_test
def python_2_040(p_filename, p_filename_data):

    import re
    from app.python_labs.io_test import io_test
    # TODO there are some pep8 regex errors
    match_obj_prize1 = re.search(
        r'prize1 \s* = \s* (\'|") ([a-zA-Z0-9!-\.\$\+\s]+) (\'|")',
        p_filename_data, re.X | re.M | re.S)
    match_obj_prize2 = re.search(
        r'prize2 \s* = \s* (\'|") ([a-zA-Z0-9!-\.\$\+\s]+) (\'|")',
        p_filename_data, re.X | re.M | re.S)
    match_obj_prize3 = re.search(
        r'prize3 \s* = \s* (\'|") ([a-zA-Z0-9!-\.\$\+\s]+) (\'|")',
        p_filename_data, re.X | re.M | re.S)
    match_obj_prize4 = re.search(
        r'prize4 \s* = \s* (\'|") ([a-zA-Z0-9!-\.\$\+\s]+) (\'|")',
        p_filename_data, re.X | re.M | re.S)

    prize1 = ' NOT FOUND '
    prize2 = ' NOT FOUND '
    prize3 = ' NOT FOUND '
    prize4 = ' NOT FOUND '
    debug_string = '<br> These are the prizes we found:<br>'
    if match_obj_prize1:
        prize1 = match_obj_prize1.group(2)
        debug_string += prize1 + "<br>"
    if match_obj_prize2:
        prize2 = match_obj_prize2.group(2)
        debug_string += prize2 + "<br>"
    if match_obj_prize3:
        prize3 = match_obj_prize3.group(2)
        debug_string += prize3 + "<br>"
    if match_obj_prize4:
        prize4 = match_obj_prize4.group(2)
        debug_string += prize4 + "<br>"

    if not match_obj_prize1:
        raise Exception("Did not find a prize after prize1 variable")
    if not match_obj_prize2:
        raise Exception("Did not find a prize after prize2 variable")
    if not match_obj_prize3:
        raise Exception("Did not find a prize after prize3 variable")
    if not match_obj_prize4:
        raise Exception("Did not find a prize after prize4 variable")

    p_pass_tests = {
        "name":
        "4 test cases for 2.040 work (12 points) <br>",
        "pass":
        True,
        "pass_message":
        "<h5 style=\"color:green;\">Pass!</h5>  All 4 test cases work",
        "fail_message":
        "<h5 style=\"color:red;\">Fail.</h5>  Check your 4 test cases.<br>"
        "If you run the program, and I type '1', it should print out prize1 somehow."
        " <br> "
        "Same for prize2, prize3, and prize4. <br>"
        "User should input '1', '2', '3', or '4', not 'door1', 'prize1' or anything like "
        "that",
        "points":
        0,
    }
    test_1 = io_test(p_filename, prize1, 1)
    test_2 = io_test(p_filename, prize2, 2)
    test_3 = io_test(p_filename, prize3, 3)
    test_4 = io_test(p_filename, prize4, 4)

    if test_1['pass']:
        p_pass_tests['points'] += 3
        debug_string += ' <br>1  prize passed '
    if test_2['pass']:
        p_pass_tests['points'] += 3
        debug_string += ' 2 prize passed '
    if test_3['pass']:
        p_pass_tests['points'] += 3
        debug_string += ' 3 prize passed'
    if test_4['pass']:
        p_pass_tests['points'] += 3
        debug_string += ' 4 prize pass'
    if p_pass_tests['points'] != 12:
        p_pass_tests['pass'] = False
        p_pass_tests['debug'] = debug_string
    return p_pass_tests
Example #6
0
def python_2_032b(p_filename, p_filename_data, *, debug_statement=''):
    """
    Function runs the test for python 2.032a lab
    :param p_filename: name of python code being graded (string)
    :param p_filename_data: contents of python code, (string)
    :param debug_statement: debug string for this particular test (2.032a for example says something
                            about order  of inputs) (string)
    :return: dictionary of test results
    """
    from app.python_labs.find_items import find_string
    from app.python_labs.io_test import io_test
    test_and_or = find_string(p_filename_data, r'(and|or) \s+ .+ \s+ (and|or)',
                              1)
    ands_or = test_and_or['pass']

    p_pass_tests = {
        "name":
        "8 test cases for 2.032b work (8 points) <br>",
        "pass":
        True,
        "pass_message":
        "Pass!  All 8 test cases work",
        "fail_message":
        "Fail.  Check your 8 test cases.<br>"
        "Please review the table that is after the 2.032b program run in your assignment."
        " <br> "
        "As part of this assignment, you should have populated that table.<br>"
        "You should test your code with the data from this table.<br>"
        "You need to figure out which ones, we do not tell you.<br>",
        "score":
        0,
        "pass_and_or":
        ands_or,
        "debug":
        ''
    }

    if not ands_or:
        p_pass_tests['fail_message'] = '<h5 style=\"color:red;\">Fail.</h5>  ' \
                                       'Program needs to contain either ands or ors and what you have is ' \
                                       'not correct. <br>' \
                                       '(We aren\'t telling you which of ands or ors you need).<br>' \
                                       'Your code should look like this: <br>' \
                                       'print(2 == 3 and 2 == 4 or 2 == 3) <br>' \
                                       'That is, 3 tests with' \
                                       '"and" or "or" in between, with possible parentheses.<br>' \
                                       'Please edit your python code and try again.<br>'
        p_pass_tests['pass'] = False
        return p_pass_tests

    tests = [
        io_test(p_filename, 'True', 1),
        io_test(p_filename, 'False', 2),
        io_test(p_filename, 'True', 3),
        io_test(p_filename, 'False', 4),
        io_test(p_filename, 'True', 5),
        io_test(p_filename, 'False', 6),
        io_test(p_filename, 'False', 7),
        io_test(p_filename, 'False', 8)
    ]
    pass_count = [x['pass'] for x in tests].count(True)

    p_pass_tests['score'] = pass_count

    p_pass_tests['pass_message'] = "You have " + str(pass_count) + "/8 tests pass.<br>" + \
                                   "These passed tests translate to a score of " + str(pass_count * 1.5) + \
                                   "/12.<br>Hints in case you did not pass all tests:<br>"
    p_pass_tests['pass_message'] += debug_statement

    p_pass_tests['points'] = pass_count * 1.5
    if pass_count == 8:
        p_pass_tests[
            'pass_message'] = "<h5 style=\"color:green;\">Pass!</h5>" + p_pass_tests[
                'pass_message']

    return p_pass_tests