Example #1
0
def ensure_cisc108_tests(test_count, report=None):
    student = compatibility.get_student_data()
    if 'assert_equal' not in student.data:
        gently("You have not imported assert_equal from the cisc108 module.")
        return False
    assert_equal = student.data['assert_equal']
    if not hasattr(assert_equal, 'student_tests'):
        gently(
            "The assert_equal function has been modified. Do not let it be overwritten!",
            label="Assertion Function Corrupted")
        return False
    student_tests = assert_equal.student_tests
    if student_tests.tests == 0:
        gently("You are not unit testing the result.",
               label="No Student Unit Tests")
        return False
    elif student_tests.tests < test_count:
        gently("You have not written enough unit tests.",
               label="Not Enough Student Unit Tests")
        return False
    elif student_tests.failures > 0:
        gently("Your unit tests are not passing.",
               label="Student Unit Tests Failing")
        return False
    return True
Example #2
0
def unit_test(name, *tests):
    """
    Show a table
    :param name:
    :param tests:
    :return:
    """
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = (
                "<table class='blockpy-feedback-unit table table-condensed table-bordered table-hover'>"
                "<tr class='active'><th></th><th>Arguments</th><th>Returned</th><th>Expected</th></tr>"
            )
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(
                    ["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = ("<td><code>{}</code></td>" * 3)
                test_out = the_function(*inp)
                message = message.format(inputs, repr(test_out), repr(out))
                if (isinstance(out, float)
                        and isinstance(test_out, (float, int))
                        and abs(out - test_out) < DELTA):
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                elif out != test_out:
                    # gently(message)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = "I ran your function <code>{}</code> on some new arguments, " \
                         "and it failed {}/{} tests.".format(name, len(tests) - success_count, len(tests)) + result
                gently(result + "</table>")
                return None
        else:
            gently(
                "You defined {}, but did not define it as a function.".format(
                    name))
            return None
    else:
        gently("The function <code>{}</code> was not defined.".format(name))
        return None
Example #3
0
def unit_test(name, *tests):
    """
    Show a table
    :param name:
    :param tests:
    :return:
    """
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = ("<table class='blockpy-feedback-unit table table-condensed table-bordered table-hover'>"
                      "<tr class='active'><th></th><th>Arguments</th><th>Returned</th><th>Expected</th></tr>"
                      )
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = ("<td><code>{}</code></td>" * 3)
                test_out = the_function(*inp)
                message = message.format(inputs, repr(test_out), repr(out))
                if (isinstance(out, float) and
                        isinstance(test_out, (float, int)) and
                        abs(out - test_out) < DELTA):
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                elif out != test_out:
                    # gently(message)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = "I ran your function <code>{}</code> on some new arguments, " \
                         "and it failed {}/{} tests.".format(name, len(tests) - success_count, len(tests)) + result
                gently(result + "</table>")
                return None
        else:
            gently("You defined {}, but did not define it as a function.".format(name))
            return None
    else:
        gently("The function <code>{}</code> was not defined.".format(name))
        return None
Example #4
0
def ensure_cisc108_tests(test_count, report=None):
    student = compatibility.get_student_data()
    if 'assert_equal' not in student.data:
        gently("You have not imported assert_equal from the cisc108 module.")
        return False
    assert_equal = student.data['assert_equal']
    if not hasattr(assert_equal, 'student_tests'):
        gently("The assert_equal function has been modified. Do not let it be overwritten!",
               label="Assertion Function Corrupted")
        return False
    student_tests = assert_equal.student_tests
    if student_tests.tests == 0:
        gently("You are not unit testing the result.", label="No Student Unit Tests")
        return False
    elif student_tests.tests < test_count:
        gently("You have not written enough unit tests.", label="Not Enough Student Unit Tests")
        return False
    elif student_tests.failures > 0:
        gently("Your unit tests are not passing.", label="Student Unit Tests Failing")
        return False
    return True
Example #5
0
def output_test(name, *tests):
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = (
                "<table class='blockpy-feedback-unit table table-condensed table-bordered table-hover'>"
                "<tr class='active'><th></th><th>Arguments</th><th>Expected</th><th>Actual</th></tr>"
            )
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(
                    ["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = "<td><code>{}</code></td>" + (
                    "<td><pre>{}</pre></td>" * 2)
                test_out = compatibility.capture_output(the_function, *inp)
                if isinstance(out, str):
                    if len(test_out) < 1:
                        message = message.format(inputs, repr(out),
                                                 "<i>No output</i>", tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif len(test_out) > 1:
                        message = message.format(inputs, repr(out),
                                                 "<i>Too many outputs</i>",
                                                 tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif out not in test_out:
                        message = message.format(inputs, repr(out),
                                                 repr(test_out[0]), tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    else:
                        message = message.format(inputs, repr(out),
                                                 repr(test_out[0]), tip)
                        message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                        success_count += 1
                elif out != test_out:
                    if len(test_out) < 1:
                        message = message.format(inputs, repr(out),
                                                 "<i>No output</i>", tip)
                    else:
                        message = message.format(inputs, repr(out),
                                                 repr(test_out[0]), tip)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = message.format(inputs, repr(out),
                                             repr(test_out[0]), tip)
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = (
                    "I ran your function <code>{}</code> on some new arguments, and it gave the wrong output "
                    "{}/{} times.".format(name,
                                          len(tests) - success_count,
                                          len(tests)) + result)
                gently(result + "</table>")
                return None
        else:
            gently("You defined {}, but did not define it as a function."
                   "<br><br><i>(not_func_def)<i>".format(name))
            return None
    else:
        gently(
            "The function <code>{}</code> was not defined.<br><br><i>(no_func_def)<i>"
            .format(name))
        return None
Example #6
0
def output_test(name, *tests):
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = ("<table class='blockpy-feedback-unit table table-condensed table-bordered table-hover'>"
                      "<tr class='active'><th></th><th>Arguments</th><th>Expected</th><th>Actual</th></tr>"
                      )
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = "<td><code>{}</code></td>" + ("<td><pre>{}</pre></td>" * 2)
                test_out = compatibility.capture_output(the_function, *inp)
                if isinstance(out, str):
                    if len(test_out) < 1:
                        message = message.format(inputs, repr(out), "<i>No output</i>", tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif len(test_out) > 1:
                        message = message.format(inputs, repr(out), "<i>Too many outputs</i>", tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif out not in test_out:
                        message = message.format(inputs, repr(out), repr(test_out[0]), tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    else:
                        message = message.format(inputs, repr(out), repr(test_out[0]), tip)
                        message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                        success_count += 1
                elif out != test_out:
                    if len(test_out) < 1:
                        message = message.format(inputs, repr(out), "<i>No output</i>", tip)
                    else:
                        message = message.format(inputs, repr(out), repr(test_out[0]), tip)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = message.format(inputs, repr(out), repr(test_out[0]), tip)
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = ("I ran your function <code>{}</code> on some new arguments, and it gave the wrong output "
                          "{}/{} times.".format(name, len(tests) - success_count, len(tests)) + result)
                gently(result + "</table>")
                return None
        else:
            gently("You defined {}, but did not define it as a function."
                   "<br><br><i>(not_func_def)<i>".format(name))
            return None
    else:
        gently("The function <code>{}</code> was not defined.<br><br><i>(no_func_def)<i>".format(name))
        return None
Example #7
0
def unit_test(name, *tests):
    """
    Show a table
    :param name:
    :param tests:
    :return:
    """
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = TEST_TABLE_UNITS
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(
                    ["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = ("<td><code>{}</code></td>" * 3)
                ran = True
                try:
                    test_out = the_function(*inp)
                except Exception as e:
                    message = message.format(inputs, str(e), repr(out))
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    success = False
                    ran = False
                if not ran:
                    result += message
                    continue
                message = message.format(inputs, repr(test_out), repr(out))
                if (isinstance(out, float)
                        and isinstance(test_out, (float, int))
                        and abs(out - test_out) < DELTA):
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                elif out != test_out:
                    # gently(message)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='table-info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = "I ran your function <code>{}</code> on some new arguments, " \
                         "and it failed {}/{} tests.".format(name, len(tests) - success_count, len(tests)) + result
                gently_r(result + "</table>", "tests_failed")
                return None
        else:
            gently(
                "You defined {}, but did not define it as a function.".format(
                    name))
            return None
    else:
        gently("The function <code>{}</code> was not defined.".format(name))
        return None
Example #8
0
def output_test(name, *tests):
    student = compatibility.get_student_data()
    if name in student.data:
        the_function = student.data[name]
        if callable(the_function):
            result = TEST_TABLE_OUTPUT
            success = True
            success_count = 0
            for test in tests:
                inp = test[:-1]
                inputs = ', '.join(
                    ["<code>{}</code>".format(repr(i)) for i in inp])
                out = test[-1]
                tip = ""
                if isinstance(out, tuple):
                    tip = out[1]
                    out = out[0]
                message = "<td><code>{}</code></td>" + (
                    "<td><pre>{}</pre></td>" * 2)
                test_out = compatibility.capture_output(the_function, *inp)
                if isinstance(out, str):
                    if len(test_out) < 1:
                        message = message.format(inputs, repr(out),
                                                 "<i>No output</i>", tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='table-info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif len(test_out) > 1:
                        message = message.format(inputs, "\n".join(out),
                                                 "<i>Too many outputs</i>",
                                                 tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    elif out not in test_out:
                        message = message.format(inputs, "\n".join(out),
                                                 "\n".join(test_out), tip)
                        message = "<tr class=''>" + RED_X + message + "</tr>"
                        if tip:
                            message += "<tr class='table-info'><td colspan=4>" + tip + "</td></tr>"
                        success = False
                    else:
                        message = message.format(inputs, "\n".join(out),
                                                 "\n".join(test_out), tip)
                        message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                        success_count += 1
                elif out != test_out:
                    if len(test_out) < 1:
                        message = message.format(inputs, "\n".join(out),
                                                 "<i>No output</i>", tip)
                    else:
                        message = message.format(inputs, "\n".join(out),
                                                 "\n".join(test_out), tip)
                    message = "<tr class=''>" + RED_X + message + "</tr>"
                    if tip:
                        message += "<tr class='table-info'><td colspan=4>" + tip + "</td></tr>"
                    success = False
                else:
                    message = message.format(inputs, "\n".join(out),
                                             "\n".join(test_out), tip)
                    message = "<tr class=''>" + GREEN_CHECK + message + "</tr>"
                    success_count += 1
                result += message
            if success:
                return the_function
            else:
                result = (
                    "I ran your function <code>{}</code> on some new arguments, and it gave the wrong output "
                    "{}/{} times.".format(name,
                                          len(tests) - success_count,
                                          len(tests)) + result)
                gently_r(result + "</table>", "wrong_output")
                return None
        else:
            gently_r(
                "You defined {}, but did not define it as a function.".format(
                    name), "not_func_def")
            return None
    else:
        gently_r("The function <code>{}</code> was not defined.".format(name),
                 "no_func_def")
        return None