PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vim_runtime/sources_non_forked/jedi-vim/pythonx/jedi/test/test_utils.py

https://gitlab.com/lokiexinferis/vim-configs
Python | 119 lines | 84 code | 19 blank | 16 comment | 13 complexity | d2b9e6d102852df75eadcfb27b1fe180 MD5 | raw file
  1. try:
  2. import readline
  3. except ImportError:
  4. readline = False
  5. from jedi import utils
  6. from .helpers import unittest, cwd_at
  7. @unittest.skipIf(not readline, "readline not found")
  8. class TestSetupReadline(unittest.TestCase):
  9. class NameSpace(object):
  10. pass
  11. def __init__(self, *args, **kwargs):
  12. super(type(self), self).__init__(*args, **kwargs)
  13. self.namespace = self.NameSpace()
  14. utils.setup_readline(self.namespace)
  15. def complete(self, text):
  16. completer = readline.get_completer()
  17. i = 0
  18. completions = []
  19. while True:
  20. completion = completer(text, i)
  21. if completion is None:
  22. break
  23. completions.append(completion)
  24. i += 1
  25. return completions
  26. def test_simple(self):
  27. assert self.complete('list') == ['list']
  28. assert self.complete('importerror') == ['ImportError']
  29. s = "print(BaseE"
  30. assert self.complete(s) == [s + 'xception']
  31. def test_nested(self):
  32. assert self.complete('list.Insert') == ['list.insert']
  33. assert self.complete('list().Insert') == ['list().insert']
  34. def test_magic_methods(self):
  35. assert self.complete('list.__getitem__') == ['list.__getitem__']
  36. assert self.complete('list().__getitem__') == ['list().__getitem__']
  37. def test_modules(self):
  38. import sys
  39. import os
  40. self.namespace.sys = sys
  41. self.namespace.os = os
  42. try:
  43. assert self.complete('os.path.join') == ['os.path.join']
  44. string = 'os.path.join("a").upper'
  45. assert self.complete(string) == [string]
  46. c = {'os.' + d for d in dir(os) if d.startswith('ch')}
  47. assert set(self.complete('os.ch')) == set(c)
  48. finally:
  49. del self.namespace.sys
  50. del self.namespace.os
  51. def test_calls(self):
  52. s = 'str(bytes'
  53. assert self.complete(s) == [s, 'str(BytesWarning']
  54. def test_import(self):
  55. s = 'from os.path import a'
  56. assert set(self.complete(s)) == {s + 'ltsep', s + 'bspath'}
  57. assert self.complete('import keyword') == ['import keyword']
  58. import os
  59. s = 'from os import '
  60. goal = {s + el for el in dir(os)}
  61. # There are minor differences, e.g. the dir doesn't include deleted
  62. # items as well as items that are not only available on linux.
  63. difference = set(self.complete(s)).symmetric_difference(goal)
  64. difference = {
  65. x for x in difference
  66. if all(not x.startswith('from os import ' + s)
  67. for s in ['_', 'O_', 'EX_', 'MFD_', 'SF_', 'ST_'])
  68. }
  69. # There are quite a few differences, because both Windows and Linux
  70. # (posix and nt) librariesare included.
  71. assert len(difference) < 20
  72. @cwd_at('test')
  73. def test_local_import(self):
  74. s = 'import test_utils'
  75. assert self.complete(s) == [s]
  76. def test_preexisting_values(self):
  77. self.namespace.a = range(10)
  78. assert set(self.complete('a.')) == {'a.' + n for n in dir(range(1))}
  79. del self.namespace.a
  80. def test_colorama(self):
  81. """
  82. Only test it if colorama library is available.
  83. This module is being tested because it uses ``setattr`` at some point,
  84. which Jedi doesn't understand, but it should still work in the REPL.
  85. """
  86. try:
  87. # if colorama is installed
  88. import colorama
  89. except ImportError:
  90. pass
  91. else:
  92. self.namespace.colorama = colorama
  93. assert self.complete('colorama')
  94. assert self.complete('colorama.Fore.BLACK') == ['colorama.Fore.BLACK']
  95. del self.namespace.colorama
  96. def test_version_info():
  97. assert utils.version_info()[:2] > (0, 7)