/bundle/youcompleteme/third_party/jedi/test/test_utils.py

https://bitbucket.org/MindTwister/vimconfig
Python | 100 lines | 67 code | 19 blank | 14 comment | 6 complexity | aaea0956d7aabfd7fa6296679b4d3b07 MD5 | raw file
  1. import readline
  2. from jedi import utils
  3. from .helpers import TestCase, cwd_at
  4. class TestSetupReadline(TestCase):
  5. class NameSpace():
  6. pass
  7. def __init__(self, *args, **kwargs):
  8. super(type(self), self).__init__(*args, **kwargs)
  9. self.namespace = self.NameSpace()
  10. utils.setup_readline(self.namespace)
  11. def completions(self, text):
  12. completer = readline.get_completer()
  13. i = 0
  14. completions = []
  15. while True:
  16. completion = completer(text, i)
  17. if completion is None:
  18. break
  19. completions.append(completion)
  20. i += 1
  21. return completions
  22. def test_simple(self):
  23. assert self.completions('list') == ['list']
  24. assert self.completions('importerror') == ['ImportError']
  25. s = "print BaseE"
  26. assert self.completions(s) == [s + 'xception']
  27. def test_nested(self):
  28. assert self.completions('list.Insert') == ['list.insert']
  29. assert self.completions('list().Insert') == ['list().insert']
  30. def test_magic_methods(self):
  31. assert self.completions('list.__getitem__') == ['list.__getitem__']
  32. assert self.completions('list().__getitem__') == ['list().__getitem__']
  33. def test_modules(self):
  34. import sys
  35. import os
  36. self.namespace.sys = sys
  37. self.namespace.os = os
  38. assert self.completions('os.path.join') == ['os.path.join']
  39. assert self.completions('os.path.join().upper') == ['os.path.join().upper']
  40. c = set(['os.' + d for d in dir(os) if d.startswith('ch')])
  41. assert set(self.completions('os.ch')) == set(c)
  42. del self.namespace.sys
  43. del self.namespace.os
  44. def test_calls(self):
  45. s = 'str(bytes'
  46. assert self.completions(s) == [s, 'str(BytesWarning']
  47. def test_import(self):
  48. s = 'from os.path import a'
  49. assert set(self.completions(s)) == set([s + 'ltsep', s + 'bspath'])
  50. assert self.completions('import keyword') == ['import keyword']
  51. import os
  52. s = 'from os import '
  53. goal = set([s + el for el in dir(os)])
  54. # There are minor differences, e.g. the dir doesn't include deleted
  55. # items as well as items that are not only available on linux.
  56. assert len(set(self.completions(s)).symmetric_difference(goal)) < 20
  57. @cwd_at('test')
  58. def test_local_import(self):
  59. s = 'import test_utils'
  60. assert self.completions(s) == [s]
  61. def test_preexisting_values(self):
  62. self.namespace.a = range(10)
  63. assert set(self.completions('a.')) == set(['a.' + n for n in dir(range(1))])
  64. del self.namespace.a
  65. def test_colorama(self):
  66. """
  67. Only test it if colorama library is available.
  68. This module is being tested because it uses ``setattr`` at some point,
  69. which Jedi doesn't understand, but it should still work in the REPL.
  70. """
  71. try:
  72. # if colorama is installed
  73. import colorama
  74. except ImportError:
  75. pass
  76. else:
  77. self.namespace.colorama = colorama
  78. assert self.completions('colorama')
  79. assert self.completions('colorama.Fore.BLACK') == ['colorama.Fore.BLACK']
  80. del self.namespace.colorama