예제 #1
0
def theTests(path_to_code_to_check=""):
    """Run the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    # stack the tests here

    testResults.append(
        test(test_flake8("week{}/exercise1.py".format(WEEK_NUMBER)),
             "Exercise 1: pass the linter"))

    message = '{"message": "Python and requests are working!"}'
    testResults.append(
        test(exercise1.success_is_relative() == message,
             "Exercise 1: read a file using a relative path"))

    testDict = {'lastName': u'hoogmoed',
                'password': u'jokers',
                'postcodePlusID': 4311240}
    testResults.append(
        test(exercise1.get_some_details() == testDict,
             "Exercise 1: get some data out of a JSON file"))

    lengths = [3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 18, 16, 14, 12, 10, 8, 6, 4]
    testResults.append(
        test([len(w) for w in exercise1.wordy_pyramid()] == lengths,
             "Exercise 1: request some simple data from the internet"))

    weather_results = {'latitude': u'-33.924206',
                       'state': u'NSW',
                       'longitude': u'151.187912',
                       'local_tz_offset': u'+1100'}
    testResults.append(
        test(process_wunderground(exercise1.wunderground()) ==
             process_wunderground(weather_results),
             "Exercise 1: get some data from the weather underground."))

    if os.path.isfile(LOCAL + "/lasers.pew"):
        testResults.append(
            test(open(LOCAL + "/lasers.pew").read() == "6",
                 "Exercise 1: count the lasers."))
    else:
        testResults.append(False)
        print("can't find lasers.pew, did you make it?"
              " Does it have exactly that file name?")

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        nyan_cat()
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {"of_total": len(testResults), "mark": sum(testResults)}
예제 #2
0
def theTests(path_to_code_to_check=""):
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    # stack the test here #
    testResults.append(
        test(test_flake8("week{}/exercise1.py".format(WEEK_NUMBER)),
             "Exercise 1: pass the linter"))

    testResults.append(test(ex1runs(), "Exercise 1: EXPLAIN TEST HERE"))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {"of_total": sum(testResults), "mark": len(testResults)}
예제 #3
0
파일: tests.py 프로젝트: cathyvu/cathyvu
def theTests(path_to_code_to_check="."):
    """Run all the tests."""
    print("\nWelcome to the exam!")
    print("May the odds be ever in your favour.\nEspecially today!")

    ex1path = "{}/week{}/exercise1.py".format(path_to_code_to_check,
                                              WEEK_NUMBER)

    if ex_runs(path_to_code_to_check, exNumber=1, weekNumber=WEEK_NUMBER):
        exam = imp.load_source("exercise1", ex1path)

        testResults.append(test(test_flake8(ex1path), "pass the linter"))

        exam_test("Hello the Queen", ["the Queen"], exam.greet)
        exam_test("Hello Pr♂nc♀♂", ["Pr♂nc♀♂"], exam.greet)

        exam_test(4, [[3, 3, 3, 3, 1]], exam.three_counter)
        exam_test(0, [[0, 1, 2, 5, -9]], exam.three_counter)

        fizza = [
            1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz',
            13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz', 'Fizz', 22, 23,
            'Fizz', 'Buzz', 26, 'Fizz', 28, 29, 'FizzBuzz', 31, 32, 'Fizz', 34,
            'Buzz', 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44,
            'FizzBuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz',
            'Buzz', 56, 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz',
            'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74, 'FizzBuzz', 76,
            77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86, 'Fizz',
            88, 89, 'FizzBuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98,
            'Fizz', 'Buzz'
        ]
        exam_test(fizza, [], exam.fizz_buzz)

        exam_test("|a| |s|e|r|i|a|l| |k|i|l|l|e|r|", ["a serial killer"],
                  exam.put_behind_bars)
        exam_test("|a| |b|a|r|t|e|n|d|e|r|", ["a bartender"],
                  exam.put_behind_bars)

        exam_test(['red fox'], ["x"], exam.pet_filter)
        exam_test([], ["q"], exam.pet_filter)
        exam_test(['pig', 'sheep', 'guinea pig', 'pigeon', 'alpaca', 'guppy'],
                  ["p"], exam.pet_filter)

        exam_test("e", [], exam.best_letter_for_pets)

        exam_test(175, [], exam.make_filler_text_dictionary,
                  lambda x: len(str(x)))

        exam_test(True, [50], exam.random_filler_text,
                  lambda x: len(x.split(" ")) == 50 and len(x) > 3 * 50)

        exam_test(True, [1000], exam.random_filler_text,
                  lambda x: len(x.split(" ")) == 1000 and len(x) > 3 * 1000)

        exam_test(True, [100], exam.fast_filler,
                  lambda x: len(x.split(" ")) == 100 and len(x) > 3 * 100)
        exam_test(
            True, [10], exam.fast_filler,
            lambda x: x[0] in string.uppercase and x[1] in string.lowercase,
            "Test if fast_filler is capitalised")
        exam_test(True, [10], exam.fast_filler, lambda x: x[-1] == ".",
                  "Test if fast_filler finishes with a .")

        #
        # try:
        #     with Timeout(1):
        #         for _ in range(10):
        #             exam.fast_filler(1000)
        #         testResults.append(
        #             test(True,
        #                  "subsiquent fast_filler"))
        # except Timeout as t:
        #     testResults.append(
        #         test(False,
        #              "subsiquent fast_filler probably wasn't fast enough"))
        # except Exception as e:
        #     testResults.append(
        #         test(False,
        #              "subsiquent fast_filler failed" + str(e)))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Cowabunga! You've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {
        "of_total": len(testResults),
        "mark": sum(testResults),
        "results": testResults
    }
예제 #4
0
def theTests(path_to_code_to_check="."):
    """Run the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER)
    print(path)

    exercise1 = imp.load_source("exercise1", path)

    testResults = []

    # stack the tests below here

    print("Linter test:", path)
    testResults.append(test(test_flake8(path), "Exercise 1: pass the linter"))
    print("tr ", testResults)
    message = '{"message": "Python and requests are working!"}'
    testResults.append(
        test(exercise1.success_is_relative() == message,
             "Exercise 1: read a file using a relative path"))

    testDict = {
        'lastName': u'hoogmoed',
        'password': u'jokers',
        'postcodePlusID': 4311240
    }
    testResults.append(
        test(exercise1.get_some_details() == testDict,
             "Exercise 1: get some data out of a JSON file"))

    lengths = [3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 18, 16, 14, 12, 10, 8, 6, 4]
    try:
        testResults.append(
            test([len(w) for w in exercise1.wordy_pyramid()] == lengths,
                 "Exercise 1: request some simple data from the internet"))
    except Exception as e:
        testResults.append(0)
        print("Exercise 1: request some simple data from the internet", e)

    weather_results = {
        'latitude': u'-33.924206',
        'state': u'NSW',
        'longitude': u'151.187912',
        'local_tz_offset': u'+{}00'.format(int(tzOffset()))
    }
    try:
        ex_name = "Exercise 1: get some data from the weather underground."
        theirs = process_wunderground(exercise1.wunderground())
        mine = process_wunderground(weather_results)
        print("you gave:", theirs)
        print("expected:", mine)
        testResults.append(test(theirs == mine, ex_name))
    except Exception as e:
        testResults.append(0)
        print(ex_name, e)

    testResults.append(
        test(find_lasers(path_to_code_to_check),
             "Exercise 1: count the lasers."))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        nyan_cat()
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)
        treat()

    return {
        "of_total": len(testResults),
        "mark": sum(testResults),
        "results": testResults
    }
예제 #5
0
파일: tests.py 프로젝트: icefz/code1161
        testResults.append(test(False, message))


def theTests(path_to_code_to_check="."):
    """Run all the tests."""
    print("\nWelcome to the exam!")
    print("May the odds be ever in your favour.\nEspecially today!")

    ex1path = "{}/week{}/exercise1.py".format(path_to_code_to_check,
                                              WEEK_NUMBER)

<<<<<<< HEAD
    if ex_runs(path_to_code_to_check, exNumber=1, weekNumber=WEEK_NUMBER):
        exam = imp.load_source("exercise1", ex1path)

        testResults.append(test(test_flake8(ex1path), "pass the linter"))
=======
    if ex_runs(ex1path, exNumber=1, weekNumber=WEEK_NUMBER):
        exam = imp.load_source("exercise1", ex1path)

        # testResults.append(test(test_flake8(ex1path), "pass the linter"))
>>>>>>> f76a1f2d8baff68dc359e69da49380b6073630ee

        exam_test("Hello the Queen", ["the Queen"], exam.greet)
        exam_test("Hello Pr♂nc♀♂", ["Pr♂nc♀♂"], exam.greet)

        exam_test(4, [[3, 3, 3, 3, 1]], exam.three_counter)
        exam_test(0, [[0, 1, 2, 5, -9]], exam.three_counter)

        fizza = [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11,
                 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz',
예제 #6
0
def theTests(path_to_code_to_check="."):
    """Run all the tests."""
    print("\nWelcome to week {}!".format(path_to_code_to_check, WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    # Give each person 10 seconds to complete all tests.
    ex1path = "{}/week{}/exercise1.py".format(path_to_code_to_check,
                                              WEEK_NUMBER)
    testResults.append(
        test(test_flake8(ex1path), "Exercise 1: pass the linter"))

    if ex_runs(path_to_code_to_check, exNumber=1, weekNumber=WEEK_NUMBER):
        exercise1 = imp.load_source("exercise1", ex1path)

        testResults.append(
            test(
                exercise1.loop_ranger(3, 8, 1) == [3, 4, 5, 6, 7],
                "Exercise 1: Loop ranger (3, 8, 1)"))
        testResults.append(
            test(
                exercise1.loop_ranger(100, 104, 2) == [100, 102],
                "Exercise 1: Loop ranger (100, 104, 2)"))

        testResults.append(
            test(
                exercise1.lone_ranger(3, 8, 1) == [3, 4, 5, 6, 7],
                "Exercise 1: Lone ranger (3, 8, 1)"))
        testResults.append(
            test(
                exercise1.lone_ranger(100, 104, 2) == [100, 102],
                "Exercise 1: Lone ranger (100, 104, 2)"))

        testResults.append(
            test(
                exercise1.two_step_ranger(100, 104) == [100, 102],
                "Exercise 1: Two step ranger (100, 104)"))
        testResults.append(
            test(
                exercise1.two_step_ranger(0, 10) == [0, 2, 4, 6, 8],
                "Exercise 1: Two step ranger (100, 104)"))

        testResults.append(
            test(
                exercise1.gene_krupa_range(0, 10, 2,
                                           1) == [0, 2, 3, 5, 6, 8, 9],
                "Exercise 1: gene_krupa_range(0, 10, 2, 1)"))
        testResults.append(
            test(
                exercise1.gene_krupa_range(0, 100, 30,
                                           7) == [0, 30, 37, 67, 74],
                "Exercise 1: gene_krupa_range(0, 100, 30, 7)"))

        testResults.append(
            test(test_stubborn_asker(path_to_code_to_check, 50, 60),
                 "Exercise 1: Stubborn asker"))

        testResults.append(
            test(test_stubborn_asker(path_to_code_to_check, 10, 20),
                 "Exercise 1: Stubborn asker"))

        testResults.append(
            test(test_not_number_rejector(path_to_code_to_check),
                 "Exercise 1: not_number_rejector"))

        testResults.append(
            test(test_super_asker(path_to_code_to_check, 50, 60),
                 "Exercise 1: test_super_asker"))

    local_path = "{}/week{}/exercise2.py".format(path_to_code_to_check,
                                                 WEEK_NUMBER)
    testResults.append(
        test(test_flake8(local_path), "Exercise 2: pass the linter"))

    testResults.append(
        test(test_example_guessingGame(path_to_code_to_check),
             "Exercise 2: example guessing game"))

    if ex_runs(path_to_code_to_check, exNumber=3, weekNumber=WEEK_NUMBER):
        imp.load_source(
            "exercise3",
            os.path.join(path_to_code_to_check, "week" + str(WEEK_NUMBER)))

        to_lint = "{}/week{}/exercise3.py".format(path_to_code_to_check,
                                                  WEEK_NUMBER)
        testResults.append(
            test(test_flake8(to_lint), "Exercise 3: pass the linter"))

        lowerBound = 10
        upperBound = 15
        guesses = range(lowerBound, upperBound + 1)
        mockInputs = [lowerBound] + [upperBound] + guesses
        testResults.append(
            test(test_advanced_guessingGame(path_to_code_to_check, mockInputs),
                 "Exercise 3: guessing game, U&L"))

        mockInputs = ["ten"] + [lowerBound] + [upperBound] + \
                     ["cats"] + guesses
        testResults.append(
            test(test_advanced_guessingGame(path_to_code_to_check, mockInputs),
                 "Exercise 3: guessing game, polite failures"))

        lowerBound = 15
        upperBound = 10
        secondGuess = 25
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(
                test_advanced_guessingGame(path_to_code_to_check, mockInputs),
                "Exercise 3: guessing game, lowerBound "
                "bigger than upperBound"))

        lowerBound = 10
        upperBound = 11
        secondGuess = 15
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(
                test_advanced_guessingGame(path_to_code_to_check, mockInputs),
                "Exercise 3: guessing game, no " +
                "range to guess in (delta 1)"))

        lowerBound = 10
        upperBound = 10
        secondGuess = 15
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(
                test_advanced_guessingGame(path_to_code_to_check, mockInputs),
                "Exercise 3: guessing game, no " +
                "range to guess in (equal)"))

    if ex_runs(path_to_code_to_check, exNumber=4, weekNumber=WEEK_NUMBER):
        path = "{}/week{}/exercise4.py".format(path_to_code_to_check,
                                               WEEK_NUMBER)
        imp.load_source("exercise4", path)

        testResults.append(
            test(test_flake8(path), "Exercise 4: pass the linter"))

        try_these = [(1, 100, 5), (1, 100, 6), (1, 100, 95), (1, 51, 5),
                     (1, 50, 5)]
        for i in range(10):
            try_these.append((0, 100, random.randint(1, 99)))

        for tv in try_these:
            print(tv)
            try:
                testResults.append(  # *tv unpacks this tuple ------v
                    test(
                        test_binary_search(path_to_code_to_check,
                                           *tv), "Exercise 4: binary_search" +
                        "({}, {}, {})".format(*tv)))
            except Exception:
                print("********\n\nfailed:", tv)
                print("tv failure", Exception)
                # raise ValueError("********\n\nfailed: {}".format(tv))
                testResults.append(0)

        # if the binary search is working, show a graph of guess numbers
        if test(test_binary_search(path_to_code_to_check, 1, 10, 5), ""):
            # If you aren't Ben, then show the histogram
            if os.uname()[1] != "um":  # um is ben's computer
                vis_binary_search_performance()

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Cowabunga! You've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {
        "of_total": len(testResults),
        "mark": sum(testResults),
        "results": testResults
    }
예제 #7
0
def theTests(path_to_code_to_check="."):
    """Run the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    # Tests from here:
    path = "{}/week{}/exercise0.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(test(test_flake8(path), "Exercise 0: pass the linter"))

    if ex_runs(path_to_code_to_check, exNumber=0, weekNumber=WEEK_NUMBER):
        exercise0 = imp.load_source("exercise0", path)

        testResults.append(
            test(
                exercise0.add_5(55) == 60, "Exercise 0: add_5 - 55 + 5 = 60?"))
        testResults.append(
            test(exercise0.add_5(-5) == 0, "Exercise 0: add_5 - -5 + 5 = 0?"))
        testResults.append(
            test(
                exercise0.add_5(0.1) == 5.1,
                "Exercise 0: add_5 - 0.1 + 5 = 5.1?"))

        testResults.append(
            test(
                exercise0.adder(5, 5) == 10,
                "Exercise 0: adder - 5 + 5 = 10?"))
        testResults.append(
            test(
                exercise0.adder(-5, -5) == 10,
                "Exercise 0: adder - -5 + -5 = 10?"))
        testResults.append(
            test(
                exercise0.adder(0.1, 0.9) == 1,
                "Exercise 0: adder - 0.1 + 0.9 = 1?"))

        testResults.append(
            test(
                exercise0.shout("you've") == "YOU'VE",
                "Exercise 0: shout - you've => YOU'VE?"))
        testResults.append(
            test(
                exercise0.shout("got") == "GOT",
                "Exercise 0: shout - got => GOT?"))
        testResults.append(
            test(
                exercise0.shout("to") == "TO",
                "Exercise 0: shout - to => TO?"))

        testResults.append(
            test(
                exercise0.really_shout("fight") == "FIGHT!",
                "Exercise 0: really_shout - fight => FIGHT!?"))
        testResults.append(
            test(
                exercise0.shout("for") == "FOR",
                "Exercise 0: shout - for => FOR?"))
        testResults.append(
            test(
                exercise0.shout("your") == "YOUR",
                "Exercise 0: shout - your => YOUR?"))
        testResults.append(
            test(
                exercise0.really_shout("right") == "RIGHT!",
                "Exercise 0: really_shout - right => RIGHT!?"))
        testResults.append(
            test(
                exercise0.shout("to") == "TO",
                "Exercise 0: shout - to => TO?"))
        testResults.append(
            test(
                exercise0.really_shout("PARTY") == "PARTY!",
                "Exercise 0: really_shout - PARTY => PARTY!?"))

    path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(test(test_flake8(path), "Exercise 1: pass the linter"))

    path = "{}/week{}/exercise2.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(test(test_flake8(path), "Exercise 2: pass the linter"))

    testResults.append(
        test(ex2runs(path_to_code_to_check), "Exercise 2: debug the file"))

    path = "{}/week{}/exercise3.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(test(test_flake8(path), "Exercise 3: pass the linter"))

    if ex3runs(path_to_code_to_check):
        exercise3 = imp.load_source("exercise3", path)
        # is odd
        testResults.append(
            test(
                exercise3.is_odd(2) is False,
                "Exercise 3: is_odd - is 2 odd?"))

        testResults.append(
            test(exercise3.is_odd(5), "Exercise 3: is_odd - is 5 odd?"))

        # fix it
        testResults.append(
            test(
                exercise3.fix_it(True, True) == "No Problem",
                "Exercise 3: fix_it - it moves, and it should"))

        testResults.append(
            test(
                exercise3.fix_it(False, True) == "WD-40",
                "Exercise 3: fix_it - it doesn't move, and it should"))

        testResults.append(
            test(
                exercise3.fix_it(True, False) == "Duct Tape",
                "Exercise 3: fix_it - it moves, and it shouldn't"))

        testResults.append(
            test(
                exercise3.fix_it(False, False) == "No Problem",
                "Exercise 3: fix_it - it doesn't move, and it shouldn't"))

        # loops
        tenStars = ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*']
        testResults.append(
            test(exercise3.loops_1a() == tenStars,
                 "Exercise 3: loops_1a - 1d for loop"))

        bang_star = ["!", "*", "!", "*", "!", "*", "!", "*", "!", "*"]
        testResults.append(
            test(
                'map' in exercise3.star_map.func_code.co_names
                and exercise3.star_map() == bang_star,
                "Exercise 3: loops_1b - 1d map"))

        testResults.append(
            test(
                exercise3.loops_1c(3, ":)") == [':)', ':)', ':)'],
                "Exercise 3: loops_1c - 1d with arguments"))

        ten_by_ten_stars = [
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
        ]
        testResults.append(
            test(exercise3.loops_2() == ten_by_ten_stars,
                 "Exercise 3: loops_2 - 10×10 stars"))

        ten_matching_numbers = [
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
            ['3', '3', '3', '3', '3', '3', '3', '3', '3', '3'],
            ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4'],
            ['5', '5', '5', '5', '5', '5', '5', '5', '5', '5'],
            ['6', '6', '6', '6', '6', '6', '6', '6', '6', '6'],
            ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
            ['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'],
            ['9', '9', '9', '9', '9', '9', '9', '9', '9', '9']
        ]
        testResults.append(
            test(exercise3.loops_3() == ten_matching_numbers,
                 "Exercise 3: loops_3 - 10 matching lists"))

        ten_rising_lists = [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
                            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']]
        testResults.append(
            test(exercise3.loops_4() == ten_rising_lists,
                 "Exercise 3: loops_4 - ten rising lists"))

        coords = [['(i0, j0)', '(i0, j1)', '(i0, j2)', '(i0, j3)', '(i0, j4)'],
                  ['(i1, j0)', '(i1, j1)', '(i1, j2)', '(i1, j3)', '(i1, j4)'],
                  ['(i2, j0)', '(i2, j1)', '(i2, j2)', '(i2, j3)', '(i2, j4)'],
                  ['(i3, j0)', '(i3, j1)', '(i3, j2)', '(i3, j3)', '(i3, j4)'],
                  ['(i4, j0)', '(i4, j1)', '(i4, j2)', '(i4, j3)', '(i4, j4)'],
                  ['(i5, j0)', '(i5, j1)', '(i5, j2)', '(i5, j3)', '(i5, j4)'],
                  ['(i6, j0)', '(i6, j1)', '(i6, j2)', '(i6, j3)', '(i6, j4)'],
                  ['(i7, j0)', '(i7, j1)', '(i7, j2)', '(i7, j3)', '(i7, j4)'],
                  ['(i8, j0)', '(i8, j1)', '(i8, j2)', '(i8, j3)', '(i8, j4)'],
                  ['(i9, j0)', '(i9, j1)', '(i9, j2)', '(i9, j3)', '(i9, j4)']]
        testResults.append(
            test(exercise3.loops_5() == coords,
                 "Exercise 3: loops_5 - write the coords"))

        wedge = [['0'], ['0', '1'], ['0', '1', '2'], ['0', '1', '2', '3'],
                 ['0', '1', '2', '3', '4'], ['0', '1', '2', '3', '4', '5'],
                 ['0', '1', '2', '3', '4', '5', '6'],
                 ['0', '1', '2', '3', '4', '5', '6', '7'],
                 ['0', '1', '2', '3', '4', '5', '6', '7', '8'],
                 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']]
        testResults.append(
            test(exercise3.loops_6() == wedge,
                 "Exercise 3: loops_6 - make a wedge"))

        pyramid = [[' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' '],
                   [' ', ' ', ' ', '*', '*', '*', ' ', ' ', ' '],
                   [' ', ' ', '*', '*', '*', '*', '*', ' ', ' '],
                   [' ', '*', '*', '*', '*', '*', '*', '*', ' '],
                   ['*', '*', '*', '*', '*', '*', '*', '*', '*']]
        testResults.append(
            test(exercise3.loops_7() == pyramid,
                 "Exercise 3: loops_7 - pyramid of stars"))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {
        "of_total": len(testResults),
        "mark": sum(testResults),
        "results": testResults
    }
예제 #8
0
def theTests(path_to_code_to_check="."):
    """Run the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []
    path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(
        test(test_flake8(path),
             "Exercise 1: pass the linter"))

    path = "{}/week{}/exercise2.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(
        test(test_flake8(path),
             "Exercise 2: pass the linter"))

    testResults.append(
        test(ex2runs(path_to_code_to_check),
             "Exercise 2: debug the file"))

    path = "{}/week{}/exercise3.py".format(path_to_code_to_check, WEEK_NUMBER)
    testResults.append(
        test(test_flake8(path),
             "Exercise 3: pass the linter"))

    if ex3runs(path_to_code_to_check):
        exercise3 = imp.load_source("exercise3", path)
        # is odd
        testResults.append(
            test(exercise3.is_odd(2) is False,
                 "Exercise 3: is_odd - is 2 odd?"))

        testResults.append(
            test(exercise3.is_odd(5),
                 "Exercise 3: is_odd - is 5 odd?"))

        # fix it
        testResults.append(
            test(exercise3.fix_it(True, True) == "No Problem",
                 "Exercise 3: fix_it - it moves, and it should"))

        testResults.append(
            test(exercise3.fix_it(False, True) == "WD-40",
                 "Exercise 3: fix_it - it doesn't move, and it should"))

        testResults.append(
            test(exercise3.fix_it(True, False) == "Duct Tape",
                 "Exercise 3: fix_it - it moves, and it shouldn't"))

        testResults.append(
            test(exercise3.fix_it(False, False) == "No Problem",
                 "Exercise 3: fix_it - it doesn't move, and it shouldn't"))

        # loops
        tenStars = ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*']
        testResults.append(
            test(exercise3.loops_1a() == tenStars,
                 "Exercise 3: loops_1a - 1d for loop"))

        bang_star = ["!", "*", "!", "*", "!", "*", "!", "*", "!", "*"]
        testResults.append(
            test('map' in exercise3.star_map.func_code.co_names and
                 exercise3.star_map() == bang_star,
                 "Exercise 3: loops_1b - 1d map"))

        testResults.append(
            test(exercise3.loops_1c(3, ":)") == [':)', ':)', ':)'],
                 "Exercise 3: loops_1c - 1d with arguments"))

        ten_by_ten_stars = [
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
            ]
        testResults.append(
            test(exercise3.loops_2() == ten_by_ten_stars,
                 "Exercise 3: loops_2 - 10×10 stars"))

        ten_matching_numbers = [
            ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
            ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
            ['2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
            ['3', '3', '3', '3', '3', '3', '3', '3', '3', '3'],
            ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4'],
            ['5', '5', '5', '5', '5', '5', '5', '5', '5', '5'],
            ['6', '6', '6', '6', '6', '6', '6', '6', '6', '6'],
            ['7', '7', '7', '7', '7', '7', '7', '7', '7', '7'],
            ['8', '8', '8', '8', '8', '8', '8', '8', '8', '8'],
            ['9', '9', '9', '9', '9', '9', '9', '9', '9', '9']
        ]
        testResults.append(
            test(exercise3.loops_3() == ten_matching_numbers,
                 "Exercise 3: loops_3 - 10 matching lists"))

        ten_rising_lists = [
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        ]
        testResults.append(
            test(exercise3.loops_4() == ten_rising_lists,
                 "Exercise 3: loops_4 - ten rising lists"))

        coords = [
            ['(i0, j0)', '(i0, j1)', '(i0, j2)', '(i0, j3)', '(i0, j4)'],
            ['(i1, j0)', '(i1, j1)', '(i1, j2)', '(i1, j3)', '(i1, j4)'],
            ['(i2, j0)', '(i2, j1)', '(i2, j2)', '(i2, j3)', '(i2, j4)'],
            ['(i3, j0)', '(i3, j1)', '(i3, j2)', '(i3, j3)', '(i3, j4)'],
            ['(i4, j0)', '(i4, j1)', '(i4, j2)', '(i4, j3)', '(i4, j4)'],
            ['(i5, j0)', '(i5, j1)', '(i5, j2)', '(i5, j3)', '(i5, j4)'],
            ['(i6, j0)', '(i6, j1)', '(i6, j2)', '(i6, j3)', '(i6, j4)'],
            ['(i7, j0)', '(i7, j1)', '(i7, j2)', '(i7, j3)', '(i7, j4)'],
            ['(i8, j0)', '(i8, j1)', '(i8, j2)', '(i8, j3)', '(i8, j4)'],
            ['(i9, j0)', '(i9, j1)', '(i9, j2)', '(i9, j3)', '(i9, j4)']
        ]
        testResults.append(
            test(exercise3.loops_5() == coords,
                 "Exercise 3: loops_5 - write the coords"))

        wedge = [
            ['0'],
            ['0', '1'],
            ['0', '1', '2'],
            ['0', '1', '2', '3'],
            ['0', '1', '2', '3', '4'],
            ['0', '1', '2', '3', '4', '5'],
            ['0', '1', '2', '3', '4', '5', '6'],
            ['0', '1', '2', '3', '4', '5', '6', '7'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8'],
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        ]
        testResults.append(
            test(exercise3.loops_6() == wedge,
                 "Exercise 3: loops_6 - make a wedge"))

        pyramid = [
            [' ', ' ', ' ', ' ', '*', ' ', ' ', ' ', ' '],
            [' ', ' ', ' ', '*', '*', '*', ' ', ' ', ' '],
            [' ', ' ', '*', '*', '*', '*', '*', ' ', ' '],
            [' ', '*', '*', '*', '*', '*', '*', '*', ' '],
            ['*', '*', '*', '*', '*', '*', '*', '*', '*']
        ]
        testResults.append(
            test(exercise3.loops_7() == pyramid,
                 "Exercise 3: loops_7 - pyramid of stars"))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {"of_total": len(testResults),
            "mark": sum(testResults),
            "results": testResults}
예제 #9
0
def theTests(path_to_code_to_check="."):
    """Run the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    # stack the tests below here
    path = "{}/week{}/exercise1.py".format(path_to_code_to_check, WEEK_NUMBER)
    print(path)

    e1 = imp.load_source("exercise1", path)

    # Linter test
    print("Linter test:", path)
    testResults.append(test(test_flake8(path), "Exercise 1: pass the linter"))

    # pydocstyle test
    print("Docstyle test:", path)
    testResults.append(
        test(test_pydocstyle(path), "Exercise 1: pass the pydocstyle test"))

    # countdown test
    book_of_counts = [{
        "message": "let's get ready to rumble",
        "start": 8,
        "stop": 1,
        "completion_message": "*rumbling sound*"
    }, {
        "message": "prepare to die in this many ways:",
        "start": 0,
        "stop": 15,
        "completion_message": "or not, I guess"
    }]
    for countdown in book_of_counts:
        try:
            the_countdown = e1.countdown(**countdown)
            if (len(the_countdown) > 0):
                [print(x) for x in the_countdown]
            else:
                print("Nothing to see here yet")
            expected = abs(countdown["start"] - countdown["stop"]) + 1
            testResults.append(
                test(
                    len(the_countdown) == expected, "Exercise 1: countdown!!"))
        except Exception as e:
            print("countdown test failed", e)
            testResults.append(FAIL)

    triangles = [{
        'area': 6.0,
        'aspect': 'tall',
        'base': 3,
        'height': 4,
        'hypotenuse': 5.0,
        'perimeter': 12.0,
        'units': 'mm'
    }, {
        'area': 15,
        'aspect': 'wide',
        'base': 10,
        'height': 3,
        'hypotenuse': 10.44030650891055,
        'perimeter': 23.440306508910552,
        'units': 'mm'
    }, {
        'area': 60.0,
        'aspect': 'tall',
        'base': 8,
        'height': 15,
        'hypotenuse': 17.0,
        'perimeter': 40.0,
        'units': 'mm'
    }, {
        'area': 12.5,
        'aspect': 'equal',
        'base': 5,
        'height': 5,
        'hypotenuse': 7.0710678118654755,
        'perimeter': 17.071067811865476,
        'units': 'mm'
    }, {
        'area': 180.0,
        'aspect': 'tall',
        'base': 9,
        'height': 40,
        'hypotenuse': 41.0,
        'perimeter': 90.0,
        'units': 'mm'
    }]

    pattern = "Exercise {}: {}: {}×{}⇨{}"
    for t in triangles:
        hyp = e1.calculate_hypotenuse(t["base"], t["height"])
        testResults.append(
            test(
                hyp == t["hypotenuse"],
                pattern.format(1, "calculate_hypotenuse", t["base"],
                               t["height"], t["hypotenuse"])))

        area = e1.calculate_area(t["base"], t["height"])
        testResults.append(
            test(
                area == t["area"],
                pattern.format(1, "calculate_area", t["base"], t["height"],
                               t["area"])))

        aspect = e1.calculate_aspect(t["base"], t["height"])
        testResults.append(
            test(
                aspect == t["aspect"],
                pattern.format(1, "calculate_aspect", t["base"], t["height"],
                               t["aspect"])))

        perimeter = e1.calculate_perimeter(t["base"], t["height"])
        testResults.append(
            test(
                perimeter == t["perimeter"],
                pattern.format(1, "calculate_perimeter", t["base"],
                               t["height"], t["perimeter"])))

        facts = e1.get_triangle_facts(t["base"], t["height"])
        testResults.append(
            test(
                facts == t,
                pattern.format(1, "get_triangle_facts", t["base"], t["height"],
                               t)))

        facts = e1.get_triangle_facts(t["base"], t["height"])
        diagram = e1.tell_me_about_this_right_triangle(facts)
        testResults.append(
            test(test_diagrams(diagram=diagram, expected=facts["aspect"]),
                 "exercise 1: draw a diagram\n" + diagram))

    ff = e1.triangle_master(base=5,
                            height=5,
                            return_diagram=False,
                            return_dictionary=False)
    tf = e1.triangle_master(base=5,
                            height=5,
                            return_diagram=True,
                            return_dictionary=False)
    ft = e1.triangle_master(base=5,
                            height=5,
                            return_diagram=False,
                            return_dictionary=True)
    tt = e1.triangle_master(base=5,
                            height=5,
                            return_diagram=True,
                            return_dictionary=True)
    testResults.append(
        test(ff is None,
             "exercise 1: triangle_master diagram: F, dictionary: F"))
    testResults.append(
        test(
            type(tf) is str,
            "exercise 1: triangle_master diagram: T, dictionary: F"))
    testResults.append(
        test(
            type(ft) is dict and "units" in ft["facts"]
            and type(ft["facts"]) is dict,
            "exercise 1: triangle_master diagram: F, dictionary: T"))
    testResults.append(
        test(
            type(tt) is dict and type(tt["diagram"]) is str,
            "exercise 1: triangle_master diagram: T, dictionary: T"))

    pattern = "Exercise 1: get_triangle_facts uses {}"
    for function_name in [
            "calculate_hypotenuse", "calculate_area", "calculate_perimeter",
            "calculate_aspect"
    ]:
        testResults.append(
            test(function_name in e1.get_triangle_facts.func_code.co_names,
                 pattern.format(function_name)))

    for length in zip([5, 8, 4, 0, "a"], [5, 8, 4, None, None]):
        word = e1.get_a_word_of_length_n(length[0])
        print(word, length)
        testResults.append(
            test(
                test_word_length(word=word,
                                 requested_length=length[0],
                                 expected_length=length[1]),
                "exercise 1: get_a_word_of_length_n {}".format(word)))

    some_lengths = [[4, 5, 6], [4, 18, 4]]
    for lengths in some_lengths:
        words = e1.list_of_words_with_lengths(lengths)
        checks = [len(x[0]) == x[1] for x in zip(words, lengths)]
        print(words, lengths, checks)
        testResults.append(
            test(all(checks),
                 "exercise 1: list_of_words_with_lengths {}".format(word)))

    testResults.append(
        test(
            "list_of_words_with_lengths"
            in e1.wordy_pyramid.func_code.co_names,
            "exercise 1: wordy_pyramid has been refactored"))

    lengths = [3, 5, 7, 9, 11, 13, 15, 17, 19, 20, 18, 16, 14, 12, 10, 8, 6, 4]
    works = None
    try:
        words = e1.wordy_pyramid()
        expected = [len(w) for w in words]
        works = expected == lengths
        print("expected     ", expected, "\ngiven lengths", lengths)
        [print(w + " " + str(len(w))) for w in words]
    except Exception as e:
        works = False
        print("Exercise 1: wordy_pyramid is broken", e)
    testResults.append(test(works, "Exercise 1: wordy_pyramid still works"))

    # EXERCISE 2 tests
    path = "{}/week{}/exercise2.py".format(path_to_code_to_check, WEEK_NUMBER)
    print(path)

    e2 = imp.load_source("exercise2", path)

    # Linter test
    print("Linter test:", path)
    testResults.append(test(test_flake8(path), "Exercise 2: pass the linter"))

    # pydocstyle test
    print("Docstyle test:", path)
    testResults.append(
        test(test_pydocstyle(path), "Exercise 2: pass the pydocstyle test"))

    source = ["baaab", "b", "roof", "hell"]
    result = [
        "bbaoaaobaobaobbbaaobaobbbaaobaobbbabbaoaaob", "bbaoaaob",
        "roabbaoabbaf", "hell"
    ]
    for source, result in zip(source, result):
        testResults.append(
            test(
                e2.abba(source, 2) == result,
                "exercise 2: abba {}⇨{}".format(source, result)))

    testResults.append(
        test(
            e2.draw_square(2) == "2100000100000100000100000100000",
            "exercise 2: Koch _^_"))
    testResults.append(
        test(
            e2.draw_pointy(2) == "210000100001000010000",
            "exercise 2: Koch _|-|_"))

    # CLEANUP AND FINISH

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        nyan_cat()
        message = "Rad, you've got all the tests passing!"
        completion_message(message, len(message) + 2)
        # treat()

    return {
        "of_total": len(testResults),
        "mark": sum(testResults),
        "results": testResults
    }
예제 #10
0
def theTests(path_to_code_to_check=""):
    """Run all the tests."""
    print("\nWelcome to week {}!".format(WEEK_NUMBER))
    print("May the odds be ever in your favour.\n")

    testResults = []

    testResults.append(
        test(test_flake8("week{}/exercise1.py".format(WEEK_NUMBER)),
             "Exercise 1: pass the linter"))

    if ex1runs():
        import exercise1
        testResults.append(
            test(
                exercise1.loop_ranger(3, 8, 1) == [3, 4, 5, 6, 7],
                "Exercise 1: Loop ranger (3, 8, 1)"))
        testResults.append(
            test(
                exercise1.loop_ranger(100, 104, 2) == [100, 102],
                "Exercise 1: Loop ranger (100, 104, 2)"))

        testResults.append(
            test(
                exercise1.lone_ranger(3, 8, 1) == [3, 4, 5, 6, 7],
                "Exercise 1: Lone ranger (3, 8, 1)"))
        testResults.append(
            test(
                exercise1.lone_ranger(100, 104, 2) == [100, 102],
                "Exercise 1: Lone ranger (100, 104, 2)"))

        testResults.append(
            test(
                exercise1.two_step_ranger(100, 104) == [100, 102],
                "Exercise 1: Two step ranger (100, 104)"))
        testResults.append(
            test(
                exercise1.two_step_ranger(0, 10) == [0, 2, 4, 6, 8],
                "Exercise 1: Two step ranger (100, 104)"))

        testResults.append(
            test(
                exercise1.gene_krupa_range(0, 10, 2,
                                           1) == [0, 2, 3, 5, 6, 8, 9],
                "Exercise 1: gene_krupa_range (0, 10, 2, 1)"))
        testResults.append(
            test(
                exercise1.gene_krupa_range(0, 100, 30,
                                           7) == [0, 30, 37, 67, 74],
                "Exercise 1: gene_krupa_range (0, 10, 2, 1)"))

        testResults.append(
            test(test_stubborn_asker(50, 60), "Exercise 1: Stubborn asker"))
        testResults.append(
            test(test_stubborn_asker(10, 20), "Exercise 1: Stubborn asker"))

        testResults.append(
            test(test_not_number_rejector(),
                 "Exercise 1: not_number_rejector"))

        testResults.append(
            test(test_super_asker(50, 60), "Exercise 1: test_super_asker"))

    testResults.append(
        test(test_flake8("week{}/exercise2.py".format(WEEK_NUMBER)),
             "Exercise 2: pass the linter"))

    testResults.append(
        test(test_example_guessingGame(), "Exercise 2: example guessing game"))

    if ex3runs():
        import exercise3

        testResults.append(
            test(test_flake8("week{}/exercise3.py".format(WEEK_NUMBER)),
                 "Exercise 3: pass the linter"))

        lowerBound = 10
        upperBound = 15
        guesses = range(lowerBound, upperBound + 1)
        mockInputs = [lowerBound] + [upperBound] + guesses
        testResults.append(
            test(test_advanced_guessingGame(mockInputs),
                 "Exercise 3: guessing game, U&L"))

        mockInputs = ["ten"] + [lowerBound] + [upperBound] + ["cats"] + guesses
        testResults.append(
            test(test_advanced_guessingGame(mockInputs),
                 "Exercise 3: guessing game, polite failures"))

        lowerBound = 15
        upperBound = 10
        secondGuess = 25
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(
                test_advanced_guessingGame(mockInputs),
                "Exercise 3: guessing game, lowerBound "
                "bigger than upperBound"))

        lowerBound = 10
        upperBound = 11
        secondGuess = 15
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(test_advanced_guessingGame(mockInputs),
                 "Exercise 3: guessing game, no range to guess in (delta 1)"))

        lowerBound = 10
        upperBound = 10
        secondGuess = 15
        guesses = range(lowerBound, secondGuess + 1)
        mockInputs = [lowerBound] + [upperBound] + [secondGuess] + guesses
        testResults.append(
            test(test_advanced_guessingGame(mockInputs),
                 "Exercise 3: guessing game, no range to guess in (equal)"))

    if ex4runs():
        import exercise4

        testResults.append(
            test(test_flake8("week{}/exercise4.py".format(WEEK_NUMBER)),
                 "Exercise 4: pass the linter"))

        try_these = [(1, 100, 5), (1, 100, 6), (1, 100, 95), (1, 51, 5),
                     (1, 50, 5)]
        for i in range(10):
            try_these.append((0, 100, random.randint(1, 99)))

        for tv in try_these:
            try:
                testResults.append(
                    test(
                        test_binary_search(*tv),  # *tv unpacks the tuple
                        "Exercise 4: binary_search({}, {}, {})".format(*tv)))
            except Exception:
                print("********\n\nfailed:", tv)
                print(Exception)
                raise ValueError("********\n\nfailed: {}".format(tv))

        # if the binary search is working, show a graph of guess numbers
        if test(test_binary_search(1, 10, 5), ""):
            vis_binary_search_performance()

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Cowabunga! You've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {"of_total": len(testResults), "mark": sum(testResults)}
예제 #11
0
def theTests(path_to_code_to_check="."):
    """Run all the tests."""
    print("\nWelcome to the exam!")
    print("May the odds be ever in your favour.\nEspecially today!")

    testResults = []

    ex1path = "{}/week{}/exercise1.py".format(path_to_code_to_check,
                                              WEEK_NUMBER)

    if ex_runs(path_to_code_to_check, exNumber=1, weekNumber=WEEK_NUMBER):
        exam = imp.load_source("exercise1", ex1path)

        testResults.append(
            test(test_flake8(ex1path),
                 "Pass the linter"))

        testResults.append(
            test(exam.greet("the Queen") == "Hello the Queen",
                 "greet the Queen"))
        testResults.append(
            test(exam.greet("Pr♂nc♀♂") == "Hello Pr♂nc♀♂",
                 "greet Pr♂nc♀♂"))

        testResults.append(
            test(exam.three_counter([3, 3, 3, 3, 1]) == 4,
                 "three_counter [3, 3, 3, 3, 1]"))
        testResults.append(
            test(exam.three_counter([0, 1, 2, 5, -9]) == 0,
                 "three_counter [0, 1, 2, 5, -9]"))

        fizza = [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11,
                 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz',
                 'Fizz', 22, 23, 'Fizz', 'Buzz', 26, 'Fizz', 28, 29,
                 'FizzBuzz', 31, 32, 'Fizz', 34, 'Buzz', 'Fizz', 37, 38,
                 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'FizzBuzz', 46, 47,
                 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz', 'Buzz', 56,
                 'Fizz', 58, 59, 'FizzBuzz', 61, 62, 'Fizz', 64, 'Buzz',
                 'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74,
                 'FizzBuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83,
                 'Fizz', 'Buzz', 86, 'Fizz', 88, 89, 'FizzBuzz', 91, 92,
                 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz', 'Buzz']
        testResults.append(
            test(exam.fizz_buzz() == fizza,
                 "fizz_buzz"))

        a = exam.put_behind_bars("a serial killer")
        testResults.append(
            test(a == "|a| |s|e|r|i|a|l| |k|i|l|l|e|r|",
                 "put_behind_bars"))
        a = exam.put_behind_bars("a bartender")
        testResults.append(
            test(a == "|a| |b|a|r|t|e|n|d|e|r|",
                 "put_behind_bars"))

        testResults.append(
            test(exam.pet_filter("x") == ['red fox'],
                 "pet_filter"))
        testResults.append(
            test(exam.pet_filter("q") == [],
                 "pet_filter"))
        testResults.append(
            test(exam.pet_filter("p") == ['pig', 'sheep', 'guinea pig', 'pigeon', 'alpaca', 'guppy'],
                 "pet_filter"))

        testResults.append(
            test(exam.best_letter_for_pets() == "e",
                 "best_letter_for_pets"))

        testResults.append(
            test(len(str(exam.make_filler_text_dictionary())) == 175,
                 "make_filler_text_dictionary"))

        filler1 = exam.random_filler_text(50)
        testResults.append(
            test(len(filler1.split(" ")) == 50 and len(filler1) > 3*50,
                 "random_filler_text"))

        filler2 = exam.fast_filler(1000)
        testResults.append(
            test(len(filler2.split(" ")) == 1000 and len(filler2) > 3*1000,
                 "first fast_filler"))

        testResults.append(
            test(exam.fast_filler(10)[0] in string.uppercase and
                 exam.fast_filler(10)[1] in string.lowercase,
                 "first fast_filler has a starting capital"))

        testResults.append(
            test(exam.fast_filler(10)[-1] == ".",
                 "first fast_filler ends with a ."))
        try:
            with Timeout(1):
                for _ in range(10):
                    exam.fast_filler(1000)
                testResults.append(
                    test(True,
                         "subsiquent fast_filler"))
        except Exception as e:
            print(e)
            testResults.append(
                test(False,
                     "subsiquent fast_filler wasn't fast enough"))

    print("{0}/{1} (passed/attempted)".format(sum(testResults),
                                              len(testResults)))

    if sum(testResults) == len(testResults):
        print(nyan_cat())
        message = "Cowabunga! You've got all the tests passing!"
        completion_message(message, len(message) + 2)

    return {"of_total": len(testResults),
            "mark": sum(testResults),
            "results": testResults}