testing structure for static analysis.

This commit is contained in:
Dave Halter
2014-05-11 12:09:42 +02:00
parent c59a8dce28
commit 40a54961cd
2 changed files with 46 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import pytest
from . import helpers from . import helpers
from . import run from . import run
from . import refactor from . import refactor
import jedi
def pytest_addoption(parser): def pytest_addoption(parser):
@@ -59,12 +60,51 @@ def pytest_generate_tests(metafunc):
os.path.join(base_dir, 'thirdparty'), test_files, True)) os.path.join(base_dir, 'thirdparty'), test_files, True))
ids = ["%s:%s" % (c.module_name, c.line_nr_test) for c in cases] ids = ["%s:%s" % (c.module_name, c.line_nr_test) for c in cases]
metafunc.parametrize('case', cases, ids=ids) metafunc.parametrize('case', cases, ids=ids)
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
metafunc.parametrize( metafunc.parametrize(
'refactor_case', 'refactor_case',
refactor.collect_dir_tests(base_dir, test_files)) refactor.collect_dir_tests(base_dir, test_files))
if 'static_analysis_case' in metafunc.fixturenames:
base_dir = os.path.dirname(__file__) + 'static_analysis'
metafunc.parametrize(
'static_analysis_case',
refactor.collect_dir_tests(base_dir, test_files))
def collect_static_analysis_tests(base_dir, test_files):
for f_name in os.listdir(base_dir):
files_to_execute = [a for a in test_files.items() if a[0] in f_name]
if f_name.endswith(".py") and (not test_files or files_to_execute):
path = os.path.join(base_dir, f_name)
yield StaticAnalysisCase(path)
class StaticAnalysisCase(object):
"""
Static Analysis cases lie in the static_analysis folder.
The tests also start with `#!`, like the goto_definition tests.
"""
def __init__(self, path):
self._path = path
with open(path) as f:
self._source = f.read()
def collect_comparison(self):
cases = []
for line_nr, line in enumerate(self._source.splitlines(), 1):
if line.startswith('#! '):
rest = line[3:]
cases.append((line_nr, rest))
return cases
def run(self, compare_cb):
analysis = jedi.Script(self._source).analysis()
analysis = [(r.line, r.name) for r in analysis]
assert compare_cb(self, self.collect_comparison(), analysis)
@pytest.fixture() @pytest.fixture()
def isolated_jedi_cache(monkeypatch, tmpdir): def isolated_jedi_cache(monkeypatch, tmpdir):

View File

@@ -28,6 +28,12 @@ def test_integration(case, monkeypatch, pytestconfig):
case.run(assert_case_equal) case.run(assert_case_equal)
def test_static_analysis(static_analysis_case, monkeypatch, pytestconfig):
if static_analysis_case.skip is not None:
pytest.skip(static_analysis_case.skip)
static_analysis_case.run(assert_case_equal)
def test_refactor(refactor_case): def test_refactor(refactor_case):
""" """
Run refactoring test case. Run refactoring test case.