From f4982606d87a4a110ac5da19d88064865193aa3d Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 21 May 2013 12:09:27 +0200 Subject: [PATCH 1/5] Refactor tests for full_name --- test/test_full_name.py | 45 +++++++++++++++++++++++++++++++++++++++++ test/test_regression.py | 23 --------------------- 2 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 test/test_full_name.py diff --git a/test/test_full_name.py b/test/test_full_name.py new file mode 100644 index 00000000..15a660c2 --- /dev/null +++ b/test/test_full_name.py @@ -0,0 +1,45 @@ +import textwrap + +import jedi +from jedi import api_classes +from .base import TestBase + + +class MixinTestFullName(object): + + def check(self, source, desired): + definitions = self.get_definitions(textwrap.dedent(source)) + self.assertEqual(definitions[0].full_name, desired) + + def test_os_path_join(self): + self.check('import os; os.path.join', 'os.path.join') + + def test_builtin(self): + self.check('type', 'type') + + def test_from_import(self): + self.check('from os import path', 'os.path') + + +class TestFullNameWithGotoDefinitions(MixinTestFullName, TestBase): + + get_definitions = TestBase.goto_definitions + + def test_tuple_mapping(self): + self.check(""" + import re + any_re = re.compile('.*') + any_re""", 're.RegexObject') + + +class TestFullNameWithCompletions(MixinTestFullName, TestBase): + get_definitions = TestBase.completions + + +def test_keyword_full_name_should_be_none(): + """issue #94""" + # Using `from jedi.keywords import Keyword` here does NOT work + # in Python 3. This is due to the import hack jedi using. + Keyword = api_classes.keywords.Keyword + d = api_classes.Definition(Keyword('(', (0, 0))) + assert d.full_name is None diff --git a/test/test_regression.py b/test/test_regression.py index bf80dc8b..5ccb1ff9 100755 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -16,7 +16,6 @@ from .base import TestBase, unittest, cwd_at import jedi from jedi._compatibility import utf8, unicode, is_py33 from jedi import api, parsing, common -api_classes = api.api_classes #jedi.set_debug_function(jedi.debug.print_to_stdout) @@ -410,28 +409,6 @@ class TestDocstring(TestBase): class TestFeature(TestBase): - def test_full_name(self): - """ feature request #61""" - assert self.completions('import os; os.path.join')[0].full_name \ - == 'os.path.join' - - def test_keyword_full_name_should_be_none(self): - """issue #94""" - # Using `from jedi.keywords import Keyword` here does NOT work - # in Python 3. This is due to the import hack jedi using. - Keyword = api_classes.keywords.Keyword - d = api_classes.Definition(Keyword('(', (0, 0))) - assert d.full_name is None - - def test_full_name_builtin(self): - self.assertEqual(self.completions('type')[0].full_name, 'type') - - def test_full_name_tuple_mapping(self): - s = """ - import re - any_re = re.compile('.*') - any_re""" - self.assertEqual(self.goto_definitions(s)[0].full_name, 're.RegexObject') def test_preload_modules(self): def check_loaded(*modules): From 339ebbbf4eae486df288ad1ff2b8b79e0623287e Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 21 May 2013 12:30:21 +0200 Subject: [PATCH 2/5] Fix TestFullNameWithCompletions.test_from_import --- jedi/api_classes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jedi/api_classes.py b/jedi/api_classes.py index 04be77e7..6d4a9bdd 100644 --- a/jedi/api_classes.py +++ b/jedi/api_classes.py @@ -144,6 +144,8 @@ class BaseDefinition(object): path = [] if not isinstance(self._definition, keywords.Keyword): par = self._definition + with common.ignored(AttributeError): + path.append(par.alias) while par is not None: with common.ignored(AttributeError): path.insert(0, par.name) From 5d6719ed8c1a5c78a430dea8617e70c9db19096a Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 21 May 2013 14:23:26 +0200 Subject: [PATCH 3/5] Add tests for defined_names + full_name --- test/test_full_name.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_full_name.py b/test/test_full_name.py index 15a660c2..3094f192 100644 --- a/test/test_full_name.py +++ b/test/test_full_name.py @@ -36,6 +36,28 @@ class TestFullNameWithCompletions(MixinTestFullName, TestBase): get_definitions = TestBase.completions +class TestFullDefinedName(TestBase): + + def check(self, source, desired): + definitions = jedi.defined_names(textwrap.dedent(source)) + full_names = [d.full_name for d in definitions] + self.assertEqual(full_names, desired) + + def test_local_names(self): + self.check(""" + def f(): pass + class C: pass + """, ['f', 'C']) + + def test_imports(self): + self.check(""" + import os + from os import path + from os.path import join + from os import path as opath + """, ['os', 'os.path', 'os.path.join', 'os.path']) + + def test_keyword_full_name_should_be_none(): """issue #94""" # Using `from jedi.keywords import Keyword` here does NOT work From 851238386f90200f7de1a1ee638f70c35d9e7590 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 21 May 2013 13:49:23 +0200 Subject: [PATCH 4/5] Fix tests for defined_names + full_name --- jedi/api_classes.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jedi/api_classes.py b/jedi/api_classes.py index 6d4a9bdd..d9ddbb29 100644 --- a/jedi/api_classes.py +++ b/jedi/api_classes.py @@ -142,11 +142,19 @@ class BaseDefinition(object): def path(self): """The module path.""" path = [] + + def insert_nonnone(x): + if x: + path.insert(0, x) + if not isinstance(self._definition, keywords.Keyword): par = self._definition - with common.ignored(AttributeError): - path.append(par.alias) while par is not None: + if isinstance(par, pr.Import): + insert_nonnone(par.namespace) + insert_nonnone(par.from_ns) + if par.relative_count == 0: + break with common.ignored(AttributeError): path.insert(0, par.name) par = par.parent From 91f3d524de8713b9d7007ef7a8b5bbc958ca3667 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 21 May 2013 16:57:40 +0200 Subject: [PATCH 5/5] Document test_full_name.py --- test/test_full_name.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/test_full_name.py b/test/test_full_name.py index 3094f192..f74c76d8 100644 --- a/test/test_full_name.py +++ b/test/test_full_name.py @@ -1,3 +1,19 @@ +""" +Tests for :attr:`.BaseDefinition.full_name`. + +There are three kinds of test: + +#. Test classes derived from :class:`MixinTestFullName`. + Child class defines :meth:`.get_definitions` to alter how + the api definition instance is created. + +#. :class:`TestFullDefinedName` is to test combination of + :attr:`.full_name` and :func:`.defined_names`. + +#. Misc single-function tests. + +""" + import textwrap import jedi @@ -7,6 +23,12 @@ from .base import TestBase class MixinTestFullName(object): + def get_definitions(self, source): + """ + Get definition objects of the variable at the end of `source`. + """ + raise NotImplementedError + def check(self, source, desired): definitions = self.get_definitions(textwrap.dedent(source)) self.assertEqual(definitions[0].full_name, desired) @@ -38,6 +60,10 @@ class TestFullNameWithCompletions(MixinTestFullName, TestBase): class TestFullDefinedName(TestBase): + """ + Test combination of :attr:`.full_name` and :func:`.defined_names`. + """ + def check(self, source, desired): definitions = jedi.defined_names(textwrap.dedent(source)) full_names = [d.full_name for d in definitions]