mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
another improvement to test/base.py
This commit is contained in:
@@ -18,10 +18,7 @@ sample_int = 1 # This is used in completion/imports.py
|
|||||||
|
|
||||||
|
|
||||||
class TestBase(unittest.TestCase):
|
class TestBase(unittest.TestCase):
|
||||||
def get_script(self, src, pos=None, path=None):
|
def get_script(self, src, pos=(None, None), path=None):
|
||||||
if pos is None:
|
|
||||||
lines = src.splitlines()
|
|
||||||
pos = len(lines), len(lines[-1])
|
|
||||||
return jedi.Script(src, pos[0], pos[1], path)
|
return jedi.Script(src, pos[0], pos[1], path)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
@@ -29,7 +26,6 @@ class TestBase(unittest.TestCase):
|
|||||||
if not hasattr(jedi.Script, name):
|
if not hasattr(jedi.Script, name):
|
||||||
raise AttributeError("Don't use getattr on this without Jedi methods")
|
raise AttributeError("Don't use getattr on this without Jedi methods")
|
||||||
def action(*args, **kwargs):
|
def action(*args, **kwargs):
|
||||||
print args, kwargs
|
|
||||||
script = self.get_script(*args, **kwargs)
|
script = self.get_script(*args, **kwargs)
|
||||||
return getattr(script, name)()
|
return getattr(script, name)()
|
||||||
return action
|
return action
|
||||||
|
|||||||
@@ -4,33 +4,28 @@ Tests for :attr:`.BaseDefinition.full_name`.
|
|||||||
There are three kinds of test:
|
There are three kinds of test:
|
||||||
|
|
||||||
#. Test classes derived from :class:`MixinTestFullName`.
|
#. Test classes derived from :class:`MixinTestFullName`.
|
||||||
Child class defines :meth:`.get_definitions` to alter how
|
Child class defines :attr:`.operation` to alter how
|
||||||
the api definition instance is created.
|
the api definition instance is created.
|
||||||
|
|
||||||
#. :class:`TestFullDefinedName` is to test combination of
|
#. :class:`TestFullDefinedName` is to test combination of
|
||||||
:attr:`.full_name` and :func:`.defined_names`.
|
``obj.full_name`` and ``jedi.defined_names``.
|
||||||
|
|
||||||
#. Misc single-function tests.
|
#. Misc single-function tests.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
from jedi import api_classes
|
from jedi import api_classes
|
||||||
from .base import TestBase
|
from .base import unittest
|
||||||
|
|
||||||
|
|
||||||
class MixinTestFullName(object):
|
class MixinTestFullName(object):
|
||||||
|
operation = None
|
||||||
def get_definitions(self, source):
|
|
||||||
"""
|
|
||||||
Get definition objects of the variable at the end of `source`.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def check(self, source, desired):
|
def check(self, source, desired):
|
||||||
definitions = self.get_definitions(textwrap.dedent(source))
|
script = jedi.Script(textwrap.dedent(source))
|
||||||
|
definitions = getattr(script, type(self).operation)()
|
||||||
self.assertEqual(definitions[0].full_name, desired)
|
self.assertEqual(definitions[0].full_name, desired)
|
||||||
|
|
||||||
def test_os_path_join(self):
|
def test_os_path_join(self):
|
||||||
@@ -43,9 +38,8 @@ class MixinTestFullName(object):
|
|||||||
self.check('from os import path', 'os.path')
|
self.check('from os import path', 'os.path')
|
||||||
|
|
||||||
|
|
||||||
class TestFullNameWithGotoDefinitions(MixinTestFullName, TestBase):
|
class TestFullNameWithGotoDefinitions(MixinTestFullName, unittest.TestCase):
|
||||||
|
operation = 'goto_definitions'
|
||||||
get_definitions = TestBase.goto_definitions
|
|
||||||
|
|
||||||
def test_tuple_mapping(self):
|
def test_tuple_mapping(self):
|
||||||
self.check("""
|
self.check("""
|
||||||
@@ -54,14 +48,13 @@ class TestFullNameWithGotoDefinitions(MixinTestFullName, TestBase):
|
|||||||
any_re""", 're.RegexObject')
|
any_re""", 're.RegexObject')
|
||||||
|
|
||||||
|
|
||||||
class TestFullNameWithCompletions(MixinTestFullName, TestBase):
|
class TestFullNameWithCompletions(MixinTestFullName, unittest.TestCase):
|
||||||
get_definitions = TestBase.completions
|
operation = 'completions'
|
||||||
|
|
||||||
|
|
||||||
class TestFullDefinedName(TestBase):
|
class TestFullDefinedName(unittest.TestCase):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Test combination of :attr:`.full_name` and :func:`.defined_names`.
|
Test combination of ``obj.full_name`` and ``jedi.defined_names``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def check(self, source, desired):
|
def check(self, source, desired):
|
||||||
|
|||||||
Reference in New Issue
Block a user