PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/elpa/elpy-20170211.449/elpy/tests/test_jedibackend.py

https://gitlab.com/csagedy/prelude
Python | 286 lines | 228 code | 51 blank | 7 comment | 15 complexity | b94ab268a85fee89725ebf8c467694bd MD5 | raw file
  1. """Tests for the elpy.jedibackend module."""
  2. import sys
  3. import unittest
  4. import jedi
  5. import mock
  6. from elpy import jedibackend
  7. from elpy import rpc
  8. from elpy.tests import compat
  9. from elpy.tests.support import BackendTestCase
  10. from elpy.tests.support import RPCGetCompletionsTests
  11. from elpy.tests.support import RPCGetCompletionDocstringTests
  12. from elpy.tests.support import RPCGetCompletionLocationTests
  13. from elpy.tests.support import RPCGetDocstringTests
  14. from elpy.tests.support import RPCGetDefinitionTests
  15. from elpy.tests.support import RPCGetCalltipTests
  16. from elpy.tests.support import RPCGetUsagesTests
  17. class JediBackendTestCase(BackendTestCase):
  18. def setUp(self):
  19. super(JediBackendTestCase, self).setUp()
  20. self.backend = jedibackend.JediBackend(self.project_root)
  21. class TestInit(JediBackendTestCase):
  22. def test_should_have_jedi_as_name(self):
  23. self.assertEqual(self.backend.name, "jedi")
  24. class TestRPCGetCompletions(RPCGetCompletionsTests,
  25. JediBackendTestCase):
  26. BUILTINS = ['object', 'oct', 'open', 'ord', 'OSError', 'OverflowError']
  27. class TestRPCGetCompletionDocstring(RPCGetCompletionDocstringTests,
  28. JediBackendTestCase):
  29. pass
  30. class TestRPCGetCompletionLocation(RPCGetCompletionLocationTests,
  31. JediBackendTestCase):
  32. pass
  33. class TestRPCGetDocstring(RPCGetDocstringTests,
  34. JediBackendTestCase):
  35. JSON_LOADS_DOCSTRING = (
  36. 'loads(s, encoding=None, cls=None, '
  37. 'object_hook=None, parse_float=None,'
  38. )
  39. def check_docstring(self, docstring):
  40. lines = docstring.splitlines()
  41. self.assertEqual(lines[0], 'Documentation for json.loads:')
  42. self.assertEqual(lines[2], self.JSON_LOADS_DOCSTRING)
  43. class TestRPCGetDefinition(RPCGetDefinitionTests,
  44. JediBackendTestCase):
  45. @mock.patch("jedi.Script")
  46. def test_should_not_fail_if_module_path_is_none(self, Script):
  47. """Do not fail if loc.module_path is None.
  48. This can happen under some circumstances I am unsure about.
  49. See #537 for the issue that reported this.
  50. """
  51. locations = [
  52. mock.Mock(module_path=None)
  53. ]
  54. script = Script.return_value
  55. script.goto_definitions.return_value = locations
  56. script.goto_assignments.return_value = locations
  57. location = self.rpc("", "", 0)
  58. self.assertIsNone(location)
  59. class TestRPCGetCalltip(RPCGetCalltipTests,
  60. JediBackendTestCase):
  61. KEYS_CALLTIP = {'index': 0,
  62. 'params': ['param '],
  63. 'name': u'keys'}
  64. if sys.version_info >= (3, 5):
  65. RADIX_CALLTIP = {'index': 0,
  66. 'params': ['param 10'],
  67. 'name': u'radix'}
  68. else:
  69. RADIX_CALLTIP = {'index': None,
  70. 'params': [],
  71. 'name': u'radix'}
  72. ADD_CALLTIP = {'index': 0,
  73. 'params': [u'param a', u'param b'],
  74. 'name': u'add'}
  75. if compat.PYTHON3:
  76. THREAD_CALLTIP = {"name": "Thread",
  77. "params": ["group=None",
  78. "target=None",
  79. "name=None",
  80. "args=()",
  81. "kwargs=None",
  82. "daemon=None"],
  83. "index": 0}
  84. else:
  85. THREAD_CALLTIP = {"name": "Thread",
  86. "params": ["param group=None",
  87. "param target=None",
  88. "param name=None",
  89. "param args=()",
  90. "param kwargs=None",
  91. "param verbose=None"],
  92. "index": 0}
  93. def test_should_not_fail_with_get_subscope_by_name(self):
  94. # Bug #677 / jedi#628
  95. source = (
  96. u"my_lambda = lambda x: x+1\n"
  97. u"my_lambda(1)"
  98. )
  99. filename = self.project_file("project.py", source)
  100. offset = 37
  101. sigs = self.backend.rpc_get_calltip(filename, source, offset)
  102. sigs["index"]
  103. class TestRPCGetUsages(RPCGetUsagesTests,
  104. JediBackendTestCase):
  105. def test_should_not_fail_for_missing_module(self):
  106. # This causes use.module_path to be None
  107. source = "import sys\n\nsys.path.\n" # insert()"
  108. offset = 21
  109. filename = self.project_file("project.py", source)
  110. self.rpc(filename, source, offset)
  111. class TestPosToLinecol(unittest.TestCase):
  112. def test_should_handle_beginning_of_string(self):
  113. self.assertEqual(jedibackend.pos_to_linecol("foo", 0),
  114. (1, 0))
  115. def test_should_handle_end_of_line(self):
  116. self.assertEqual(jedibackend.pos_to_linecol("foo\nbar\nbaz\nqux", 9),
  117. (3, 1))
  118. def test_should_handle_end_of_string(self):
  119. self.assertEqual(jedibackend.pos_to_linecol("foo\nbar\nbaz\nqux", 14),
  120. (4, 2))
  121. class TestLinecolToPos(unittest.TestCase):
  122. def test_should_handle_beginning_of_string(self):
  123. self.assertEqual(jedibackend.linecol_to_pos("foo", 1, 0),
  124. 0)
  125. def test_should_handle_end_of_string(self):
  126. self.assertEqual(jedibackend.linecol_to_pos("foo\nbar\nbaz\nqux",
  127. 3, 1),
  128. 9)
  129. def test_should_return_offset(self):
  130. self.assertEqual(jedibackend.linecol_to_pos("foo\nbar\nbaz\nqux",
  131. 4, 2),
  132. 14)
  133. def test_should_fail_for_line_past_text(self):
  134. self.assertRaises(ValueError,
  135. jedibackend.linecol_to_pos, "foo\n", 3, 1)
  136. def test_should_fail_for_column_past_text(self):
  137. self.assertRaises(ValueError,
  138. jedibackend.linecol_to_pos, "foo\n", 1, 10)
  139. class TestRunWithDebug(unittest.TestCase):
  140. @mock.patch('jedi.Script')
  141. def test_should_call_method(self, Script):
  142. Script.return_value.test_method.return_value = "test-result"
  143. result = jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  144. Script.assert_called_with(1, 2, arg=3)
  145. self.assertEqual(result, 'test-result')
  146. @mock.patch('jedi.Script')
  147. def test_should_re_raise(self, Script):
  148. Script.side_effect = RuntimeError
  149. with self.assertRaises(RuntimeError):
  150. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3,
  151. re_raise=(RuntimeError,))
  152. @mock.patch('jedi.Script')
  153. @mock.patch('jedi.set_debug_function')
  154. def test_should_keep_debug_info(self, set_debug_function, Script):
  155. Script.side_effect = RuntimeError
  156. try:
  157. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  158. except rpc.Fault as e:
  159. self.assertGreaterEqual(e.code, 400)
  160. self.assertIsNotNone(e.data)
  161. self.assertIn("traceback", e.data)
  162. jedi_debug_info = e.data["jedi_debug_info"]
  163. self.assertIsNotNone(jedi_debug_info)
  164. self.assertEqual(jedi_debug_info["script_args"],
  165. "1, 2, arg=3")
  166. self.assertEqual(jedi_debug_info["source"], None)
  167. self.assertEqual(jedi_debug_info["method"], "test_method")
  168. self.assertEqual(jedi_debug_info["debug_info"], [])
  169. else:
  170. self.fail("Fault not thrown")
  171. @mock.patch('jedi.Script')
  172. @mock.patch('jedi.set_debug_function')
  173. def test_should_keep_error_text(self, set_debug_function, Script):
  174. Script.side_effect = RuntimeError
  175. try:
  176. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  177. except rpc.Fault as e:
  178. self.assertEqual(str(e), str(RuntimeError()))
  179. self.assertEqual(e.message, str(RuntimeError()))
  180. else:
  181. self.fail("Fault not thrown")
  182. @mock.patch('jedi.Script')
  183. @mock.patch('jedi.set_debug_function')
  184. def test_should_handle_source_special(self, set_debug_function, Script):
  185. Script.side_effect = RuntimeError
  186. try:
  187. jedibackend.run_with_debug(jedi, 'test_method', source="foo")
  188. except rpc.Fault as e:
  189. self.assertEqual(e.data["jedi_debug_info"]["script_args"],
  190. "source=source")
  191. self.assertEqual(e.data["jedi_debug_info"]["source"], "foo")
  192. else:
  193. self.fail("Fault not thrown")
  194. @mock.patch('jedi.Script')
  195. @mock.patch('jedi.set_debug_function')
  196. def test_should_set_debug_info(self, set_debug_function, Script):
  197. the_debug_function = [None]
  198. def my_set_debug_function(debug_function, **kwargs):
  199. the_debug_function[0] = debug_function
  200. def my_script(*args, **kwargs):
  201. the_debug_function[0](jedi.debug.NOTICE, "Notice")
  202. the_debug_function[0](jedi.debug.WARNING, "Warning")
  203. the_debug_function[0]("other", "Other")
  204. raise RuntimeError
  205. set_debug_function.side_effect = my_set_debug_function
  206. Script.return_value.test_method = my_script
  207. try:
  208. jedibackend.run_with_debug(jedi, 'test_method', source="foo")
  209. except rpc.Fault as e:
  210. self.assertEqual(e.data["jedi_debug_info"]["debug_info"],
  211. ["[N] Notice",
  212. "[W] Warning",
  213. "[?] Other"])
  214. else:
  215. self.fail("Fault not thrown")
  216. @mock.patch('jedi.set_debug_function')
  217. @mock.patch('jedi.Script')
  218. def test_should_not_fail_with_bad_data(self, Script, set_debug_function):
  219. import jedi.debug
  220. def set_debug(function, speed=True):
  221. if function is not None:
  222. function(jedi.debug.NOTICE, u"\xab")
  223. set_debug_function.side_effect = set_debug
  224. Script.return_value.test_method.side_effect = Exception
  225. with self.assertRaises(rpc.Fault):
  226. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)