Make sure that tests for refactoring are redirected

This commit is contained in:
Dave Halter
2020-02-09 14:05:16 +01:00
parent 6e63799a7d
commit 6166e7961e
3 changed files with 21 additions and 20 deletions

View File

@@ -76,9 +76,11 @@ def pytest_generate_tests(metafunc):
if 'refactor_case' in metafunc.fixturenames: if 'refactor_case' in metafunc.fixturenames:
base_dir = metafunc.config.option.refactor_case_dir base_dir = metafunc.config.option.refactor_case_dir
cases = list(refactor.collect_dir_tests(base_dir, test_files))
metafunc.parametrize( metafunc.parametrize(
'refactor_case', 'refactor_case', cases,
refactor.collect_dir_tests(base_dir, test_files)) ids=[c.refactor_type + '-' + c.name for c in cases]
)
if 'static_analysis_case' in metafunc.fixturenames: if 'static_analysis_case' in metafunc.fixturenames:
base_dir = os.path.join(os.path.dirname(__file__), 'static_analysis') base_dir = os.path.join(os.path.dirname(__file__), 'static_analysis')

View File

@@ -11,15 +11,14 @@ import re
from functools import reduce from functools import reduce
import jedi import jedi
from jedi import refactoring
class RefactoringCase(object): class RefactoringCase(object):
def __init__(self, name, source, line_nr, index, path, def __init__(self, name, code, line_nr, index, path,
new_name, start_line_test, desired): new_name, start_line_test, desired):
self.name = name self.name = name
self.source = source self.code = code
self.line_nr = line_nr self.line_nr = line_nr
self.index = index self.index = index
self.path = path self.path = path
@@ -27,10 +26,14 @@ class RefactoringCase(object):
self.start_line_test = start_line_test self.start_line_test = start_line_test
self.desired = desired self.desired = desired
def refactor(self): @property
script = jedi.Script(self.source, self.line_nr, self.index, self.path) def refactor_type(self):
f_name = os.path.basename(self.path) f_name = os.path.basename(self.path)
refactor_func = getattr(refactoring, f_name.replace('.py', '')) return f_name.replace('.py', '')
def refactor(self):
script = jedi.Script(self.code, self.line_nr, self.index, self.path)
refactor_func = getattr(script, self.refactor_type)
args = (self.new_name,) if self.new_name else () args = (self.new_name,) if self.new_name else ()
return refactor_func(script, *args) return refactor_func(script, *args)
@@ -61,14 +64,14 @@ class RefactoringCase(object):
self.name, self.line_nr - 1) self.name, self.line_nr - 1)
def collect_file_tests(source, path, lines_to_execute): def collect_file_tests(code, path, lines_to_execute):
r = r'^# --- ?([^\n]*)\n((?:(?!\n# \+\+\+).)*)' \ r = r'^# --- ?([^\n]*)\n((?:(?!\n# \+\+\+).)*)' \
r'\n# \+\+\+((?:(?!\n# ---).)*)' r'\n# \+\+\+((?:(?!\n# ---).)*)'
for match in re.finditer(r, source, re.DOTALL | re.MULTILINE): for match in re.finditer(r, code, re.DOTALL | re.MULTILINE):
name = match.group(1).strip() name = match.group(1).strip()
first = match.group(2).strip() first = match.group(2).strip()
second = match.group(3).strip() second = match.group(3).strip()
start_line_test = source[:match.start()].count('\n') + 1 start_line_test = code[:match.start()].count('\n') + 1
# get the line with the position of the operation # get the line with the position of the operation
p = re.match(r'((?:(?!#\?).)*)#\? (\d*) ?([^\n]*)', first, re.DOTALL) p = re.match(r'((?:(?!#\?).)*)#\? (\d*) ?([^\n]*)', first, re.DOTALL)
@@ -83,7 +86,7 @@ def collect_file_tests(source, path, lines_to_execute):
if lines_to_execute and line_nr - 1 not in lines_to_execute: if lines_to_execute and line_nr - 1 not in lines_to_execute:
continue continue
yield RefactoringCase(name, source, line_nr, index, path, yield RefactoringCase(name, code, line_nr, index, path,
new_name, start_line_test, second) new_name, start_line_test, second)
@@ -94,6 +97,6 @@ def collect_dir_tests(base_dir, test_files):
if f_name.endswith(".py") and (not test_files or files_to_execute): if f_name.endswith(".py") and (not test_files or files_to_execute):
path = os.path.join(base_dir, f_name) path = os.path.join(base_dir, f_name)
with open(path) as f: with open(path) as f:
source = f.read() code = f.read()
for case in collect_file_tests(source, path, lines_to_execute): for case in collect_file_tests(code, path, lines_to_execute):
yield case yield case

View File

@@ -58,9 +58,5 @@ def test_refactor(refactor_case):
:type refactor_case: :class:`.refactor.RefactoringCase` :type refactor_case: :class:`.refactor.RefactoringCase`
""" """
if 0: refactor_case.run()
# TODO Refactoring is not relevant at the moment, it will be changed assert_case_equal(refactor_case, refactor_case.result, refactor_case.desired)
# significantly in the future, but maybe we can use these tests:
refactor_case.run()
assert_case_equal(refactor_case,
refactor_case.result, refactor_case.desired)