diff --git a/test/refactor.py b/test/refactor.py index e8db9e1b..595886ae 100644 --- a/test/refactor.py +++ b/test/refactor.py @@ -45,6 +45,7 @@ class RefactoringCase(object): def _collect_file_tests(code, path, lines_to_execute): r = r'^# -{5,} ?([^\n]*)\n((?:(?!\n# \+{5,}).)*\n)' \ r'# \+{5,}\n((?:(?!\n# -{5,}).)*\n)' + match = None for match in re.finditer(r, code, re.DOTALL | re.MULTILINE): name = match.group(1).strip() first = match.group(2) @@ -53,17 +54,24 @@ def _collect_file_tests(code, path, lines_to_execute): # get the line with the position of the operation p = re.match(r'((?:(?!#\?).)*)#\? (\d*) ?([^\n]*)', first, re.DOTALL) if p is None: - print("Please add a test start.") + raise Exception("Please add a test start.") continue until = p.group(1) index = int(p.group(2)) - kwargs = eval(p.group(3)) + if p.group(3): + kwargs = eval(p.group(3)) + else: + kwargs = {} line_nr = until.count('\n') + 2 if lines_to_execute and line_nr - 1 not in lines_to_execute: continue yield RefactoringCase(name, first, line_nr, index, path, kwargs, second) + if match is None: + raise Exception("Didn't match any test") + if match.end() != len(code): + raise Exception("Didn't match until the end of the file in %s" % path) def collect_dir_tests(base_dir, test_files): diff --git a/test/refactor/extract.py b/test/refactor/extract.py index 312aced8..1317a354 100644 --- a/test/refactor/extract.py +++ b/test/refactor/extract.py @@ -1,31 +1,31 @@ -# --- simple +# -------------------------------------------------- simple-1 def test(): - #? 35 a + #? 35 {'new_name': 'a'} return test(100, (30 + b, c) + 1) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ def test(): a = (30 + b, c) + 1 return test(100, a) -# --- simple #2 +# -------------------------------------------------- simple-2 def test(): - #? 25 a + #? 25 {'new_name': 'a'} return test(100, (30 + b, c) + 1) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ def test(): a = 30 + b return test(100, (a, c) + 1) -# --- multiline +# -------------------------------------------------- multiline-1 def test(): - #? 30 x + #? 30 {'new_name': 'a'} return test(1, (30 + b, c) + 1) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ def test(): x = ((30 + b, c) + 1) @@ -33,12 +33,12 @@ def test(): ) -# --- multiline #2 +# -------------------------------------------------- multiline-2 def test(): - #? 25 x + #? 25 {'new_name': 'a'} return test(1, (30 + b, c) + 1) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ def test(): x = 30 + b return test(1, (x, c) diff --git a/test/refactor/inline.py b/test/refactor/inline.py index c373be25..2679b998 100644 --- a/test/refactor/inline.py +++ b/test/refactor/inline.py @@ -1,18 +1,18 @@ -# --- simple +# -------------------------------------------------- simple def test(): #? 4 a = (30 + b, c) + 1 return test(100, a) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ def test(): return test(100, (30 + b, c) + 1) -# --- simple +# -------------------------------------------------- simple if 1: #? 4 a = 1, 2 return test(100, a) -# +++ +# ++++++++++++++++++++++++++++++++++++++++++++++++++ if 1: return test(100, (1, 2))