def run_related_name_test(script, correct, line_nr): """ Runs tests for gotos. Tests look like this: >>> abc = 1 >>> #< abc@1,0 abc@3,0 >>> abc Return if the test was a fail or not, with 1 for fail and 0 for success. """ result = script.related_names() correct = correct.strip() compare = sorted((r.module_name, r.start_pos[0], r.start_pos[1]) for r in result) wanted = [] if not correct: positions = [] else: positions = literal_eval(correct) for pos_tup in positions: if type(pos_tup[0]) == str: # this means that there is a module specified wanted.append(pos_tup) else: wanted.append(('renaming', line_nr + pos_tup[0], pos_tup[1])) wanted = sorted(wanted) if compare != wanted: print('Solution @%s not right, received %s, wanted %s'\ % (line_nr - 1, compare, wanted)) return 1 return 0
def run_completion_test(script, correct, line_nr): """ Runs tests for completions. Return if the test was a fail or not, with 1 for fail and 0 for success. """ completions = script.complete() #import cProfile; cProfile.run('script.complete()') comp_str = set([c.word for c in completions]) if comp_str != set(literal_eval(correct)): print('Solution @%s not right, received %s, wanted %s'\ % (line_nr - 1, comp_str, correct)) return 1 return 0
def detect_encoding(): """ For the implementation of encoding definitions in Python, look at: http://www.python.org/dev/peps/pep-0263/ http://docs.python.org/2/reference/lexical_analysis.html#encoding-\ declarations """ byte_mark = '\xef\xbb\xbf' if is_py25 else literal_eval(r"b'\xef\xbb\xbf'") if source.startswith(byte_mark): # UTF-8 byte-order mark return 'utf-8' first_two_lines = re.match(r'(?:[^\n]*\n){0,2}', str(source)).group(0) possible_encoding = re.search(r"coding[=:]\s*([-\w.]+)", first_two_lines) if possible_encoding: return possible_encoding.group(1) else: # the default if nothing else has been set -> PEP 263 return encoding if encoding is not None else 'iso-8859-1'
def run_related_name_test(script, correct, line_nr): """ Runs tests for gotos. Tests look like this: >>> abc = 1 >>> #< abc@1,0 abc@3,0 >>> abc Return if the test was a fail or not, with 1 for fail and 0 for success. """ result = script.related_names() correct = correct.strip() comp_str = sorted(r.start_pos for r in result) correct = sorted(literal_eval(correct)) if sorted(comp_str) != sorted(correct): print('Solution @%s not right, received %s, wanted %s'\ % (line_nr - 1, comp_str, correct)) return 1 return 0