def test_test_function_twice(first_ok, second_ok, second_solves_first, first_solves_second): sct = 'test_function("round", index=1)\ntest_function("round", index=2)' first_ok['DC_SCT'] = sct output = helper.run(first_ok) assert not output['correct'] helper.no_line_info(output) second_ok['DC_SCT'] = sct output = helper.run(second_ok) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 7) second_solves_first['DC_SCT'] = sct output = helper.run(second_solves_first) assert not output['correct'] helper.no_line_info(output) first_solves_second['DC_SCT'] = sct output = helper.run(first_solves_second) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 7) first_ok['DC_SCT'] = 'test_function("round", index=2)' output = helper.run(first_ok) assert not output['correct'] helper.no_line_info(output)
def test_check_function_twice(first_ok, second_ok, second_solves_first, first_solves_second): patt = 'Ex().check_function("round", index={}).check_args("number").has_equal_value()' sct = patt.format(0) + "\n" + patt.format(1) first_ok['DC_SCT'] = sct output = helper.run(first_ok) assert not output['correct'] helper.with_line_info(output, 2, 2, 7, 7) second_ok['DC_SCT'] = sct output = helper.run(second_ok) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 7) second_solves_first['DC_SCT'] = sct output = helper.run(second_solves_first) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 7) first_solves_second['DC_SCT'] = sct output = helper.run(first_solves_second) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 7) first_ok['DC_SCT'] = patt.format(1) output = helper.run(first_ok) assert not output['correct'] helper.with_line_info(output, 2, 2, 7, 7) ## THIS IS INTUITIVE!
def test_test_with_3(sct, passes, patt, lines): res = helper.run({ "DC_PEC": ''' from urllib.request import urlretrieve; urlretrieve('http://s3.amazonaws.com/assets.datacamp.com/production/course_998/datasets/moby_opens.txt', 'moby_dick.txt') from urllib.request import urlretrieve; urlretrieve('https://s3.amazonaws.com/assets.datacamp.com/production/course_998/datasets/moby_opens.txt', 'not_moby_dick.txt') ''', "DC_CODE": ''' # Read & print the first 3 lines with open('moby_dick.txt') as file: print(file.readline()) print(file.readline()) print('test') # The rows that you wish to print I = [0,1,3,5,6,7,8,9] # Print out these rows with open('moby_dick.txt') as not_file, open('moby_dick.txt') as file: for i, row in enumerate(not_file): if i in I: print(row) ''', "DC_SOLUTION": ''' # Read & print the first 3 lines with open('moby_dick.txt') as file, open('moby_dick.txt'): print(file.readline()) print(file.readline()) print(file.readline()) # The rows that you wish to print I = [0,1,3,5,6,7,8,9] # Print out these rows with open('moby_dick.txt') as file, open('not_moby_dick.txt') as not_file: for i, row in enumerate(file): if i in I: print(row) ''', "DC_SCT": sct }) assert res['correct'] == passes if patt: assert patt in res['message'] if lines: helper.with_line_info(res, *lines)
def test_check_if_else_basic(stu, patt, lines): output = helper.run({ 'DC_PEC': 'offset = 8', 'DC_SOLUTION': 'if offset > 8: x = 5\nelse: x = round(2.123)', 'DC_CODE': stu, 'DC_SCT': ''' Ex().check_if_else().multi( check_test().multi([ set_env(offset = i).has_equal_value() for i in range(7,10) ]), check_body().has_code(r'x\s*=\s*5'), check_orelse().check_function('round').check_args(0).has_equal_value() ) ''' }) assert not output['correct'] assert message(output, patt) if lines: helper.with_line_info(output, *lines)
def test_test_list_comp_messaging(stu, passes, patt, lines): pec = "x = {'a': 2, 'b':3, 'c':4, 'd':'test'}" sol = "[key + str(val) for key,val in x.items() if isinstance(key, str) if isinstance(val, int)]" sct = ''' test_list_comp(index=1, not_called_msg=None, comp_iter=lambda: test_expression_result(), iter_vars_names=True, incorrect_iter_vars_msg=None, body=lambda: test_expression_result(context_vals = ['a', 2]), ifs=[lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False]), lambda: test_function_v2('isinstance', params = ['obj'], do_eval = [False])], insufficient_ifs_msg=None) ''' res = helper.run({ "DC_PEC": pec, "DC_CODE": stu, "DC_SOLUTION": sol, "DC_SCT": sct }) assert res['correct'] == passes assert patt in res['message'] if lines: helper.with_line_info(res, *lines)
def test_with_highlighting(data): data['DC_SCT'] = 'Ex().check_function("round").check_args("number").has_equal_value()' output = helper.run(data) assert not output['correct'] helper.with_line_info(output, 1, 1, 7, 11)
def test_parsing(name, ls, le, cs, ce): stu_code = ''' if True: a = 1 if False: b = 2 else: c = 3 for i in range(2): d = 4 x = 2 while x > 0: e = 5 x -= 1 try: f = 6 except: pass try: g = 7 except: pass finally: h = 8 # 2 assignments i = 9 if True: i = 9 ''' sol_code = ''' if True: a = 10 if False: b = 20 else: c = 30 for i in range(2): d = 40 x = 2 while x > 0: e = 50 x -= 1 try: f = 60 except: pass try: g = 70 except: pass finally: h = 80 # 2 assignments i = 90 if True: i = 90 ''' res = helper.run({ 'DC_CODE': stu_code, 'DC_SOLUTION': sol_code, 'DC_SCT': 'Ex().check_object("%s").has_equal_value()' % name }) assert not res['correct'] if name == 'i': helper.no_line_info(res) else: helper.with_line_info(res, ls, le, cs, ce)