Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
14 KiB

5 years ago
  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 RPCGetOnelineDocstringTests
  15. from elpy.tests.support import RPCGetDefinitionTests
  16. from elpy.tests.support import RPCGetAssignmentTests
  17. from elpy.tests.support import RPCGetCalltipTests
  18. from elpy.tests.support import RPCGetUsagesTests
  19. from elpy.tests.support import RPCGetNamesTests
  20. class JediBackendTestCase(BackendTestCase):
  21. def setUp(self):
  22. super(JediBackendTestCase, self).setUp()
  23. self.backend = jedibackend.JediBackend(self.project_root)
  24. class TestInit(JediBackendTestCase):
  25. def test_should_have_jedi_as_name(self):
  26. self.assertEqual(self.backend.name, "jedi")
  27. class TestRPCGetCompletions(RPCGetCompletionsTests,
  28. JediBackendTestCase):
  29. BUILTINS = ['object', 'oct', 'open', 'ord', 'OSError', 'OverflowError']
  30. class TestRPCGetCompletionDocstring(RPCGetCompletionDocstringTests,
  31. JediBackendTestCase):
  32. pass
  33. class TestRPCGetCompletionLocation(RPCGetCompletionLocationTests,
  34. JediBackendTestCase):
  35. pass
  36. class TestRPCGetDocstring(RPCGetDocstringTests,
  37. JediBackendTestCase):
  38. def __init__(self, *args, **kwargs):
  39. super(TestRPCGetDocstring, self).__init__(*args, **kwargs)
  40. if sys.version_info >= (3, 6):
  41. self.JSON_LOADS_DOCSTRING = (
  42. 'loads(s, *, encoding=None, cls=None, object_hook=None, '
  43. 'parse_float=None, parse_int=None, parse_constant=None, '
  44. 'object_pairs_hook=None, **kw)'
  45. )
  46. else:
  47. self.JSON_LOADS_DOCSTRING = (
  48. 'loads(s, encoding=None, cls=None, object_hook=None, '
  49. 'parse_float=None, parse_int=None, parse_constant=None, '
  50. 'object_pairs_hook=None, **kw)'
  51. )
  52. def check_docstring(self, docstring):
  53. lines = docstring.splitlines()
  54. self.assertEqual(lines[0], 'Documentation for json.loads:')
  55. self.assertEqual(lines[2], self.JSON_LOADS_DOCSTRING)
  56. @mock.patch("elpy.jedibackend.run_with_debug")
  57. def test_should_not_return_empty_docstring(self, run_with_debug):
  58. location = mock.MagicMock()
  59. location.full_name = "testthing"
  60. location.docstring.return_value = ""
  61. run_with_debug.return_value = [location]
  62. filename = self.project_file("test.py", "print")
  63. docstring = self.backend.rpc_get_docstring(filename, "print", 0)
  64. self.assertIsNone(docstring)
  65. class TestRPCGetOnelineDocstring(RPCGetOnelineDocstringTests,
  66. JediBackendTestCase):
  67. def __init__(self, *args, **kwargs):
  68. super(TestRPCGetOnelineDocstring, self).__init__(*args, **kwargs)
  69. if sys.version_info >= (3, 6):
  70. self.JSON_LOADS_DOCSTRING = (
  71. 'Deserialize ``s`` (a ``str``, ``bytes`` or'
  72. ' ``bytearray`` instance containing a JSON'
  73. ' document) to a Python object.'
  74. )
  75. self.JSON_DOCSTRING = (
  76. "JSON (JavaScript Object Notation) <http://json.org>"
  77. " is a subset of JavaScript syntax (ECMA-262"
  78. " 3rd edition) used as a lightweight data interchange format.")
  79. elif sys.version_info >= (3, 0):
  80. self.JSON_LOADS_DOCSTRING = (
  81. 'Deserialize ``s`` (a ``str`` instance '
  82. 'containing a JSON document) to a Python object.'
  83. )
  84. self.JSON_DOCSTRING = (
  85. "JSON (JavaScript Object Notation) <http://json.org>"
  86. " is a subset of JavaScript syntax (ECMA-262"
  87. " 3rd edition) used as a lightweight data interchange format.")
  88. else:
  89. self.JSON_LOADS_DOCSTRING = (
  90. 'Deserialize ``s`` (a ``str`` or ``unicode`` '
  91. 'instance containing a JSON document) to a Python object.'
  92. )
  93. self.JSON_DOCSTRING = (
  94. "JSON (JavaScript Object Notation) <http://json.org>"
  95. " is a subset of JavaScript syntax (ECMA-262"
  96. " 3rd edition) used as a lightweight data interchange format.")
  97. @mock.patch("elpy.jedibackend.run_with_debug")
  98. def test_should_not_return_empty_docstring(self, run_with_debug):
  99. location = mock.MagicMock()
  100. location.full_name = "testthing"
  101. location.docstring.return_value = ""
  102. run_with_debug.return_value = [location]
  103. filename = self.project_file("test.py", "print")
  104. docstring = self.backend.rpc_get_oneline_docstring(filename, "print", 0)
  105. self.assertIsNone(docstring)
  106. class TestRPCGetDefinition(RPCGetDefinitionTests,
  107. JediBackendTestCase):
  108. @mock.patch("jedi.Script")
  109. def test_should_not_fail_if_module_path_is_none(self, Script):
  110. """Do not fail if loc.module_path is None.
  111. This can happen under some circumstances I am unsure about.
  112. See #537 for the issue that reported this.
  113. """
  114. locations = [
  115. mock.Mock(module_path=None)
  116. ]
  117. script = Script.return_value
  118. script.goto_definitions.return_value = locations
  119. script.goto_assignments.return_value = locations
  120. location = self.rpc("", "", 0)
  121. self.assertIsNone(location)
  122. class TestRPCGetAssignment(RPCGetAssignmentTests,
  123. JediBackendTestCase):
  124. @mock.patch("jedi.Script")
  125. def test_should_not_fail_if_module_path_is_none(self, Script):
  126. """Do not fail if loc.module_path is None.
  127. """
  128. locations = [
  129. mock.Mock(module_path=None)
  130. ]
  131. script = Script.return_value
  132. script.goto_assignments.return_value = locations
  133. script.goto_assignments.return_value = locations
  134. location = self.rpc("", "", 0)
  135. self.assertIsNone(location)
  136. class TestRPCGetCalltip(RPCGetCalltipTests,
  137. JediBackendTestCase):
  138. KEYS_CALLTIP = {'index': None,
  139. 'params': [],
  140. 'name': u'keys'}
  141. RADIX_CALLTIP = {'index': None,
  142. 'params': [],
  143. 'name': u'radix'}
  144. ADD_CALLTIP = {'index': 0,
  145. 'params': [u'a', u'b'],
  146. 'name': u'add'}
  147. if compat.PYTHON3:
  148. THREAD_CALLTIP = {'name': 'Thread',
  149. 'index': 0,
  150. 'params': ['group: None=...',
  151. 'target: Optional[Callable[..., Any]]=...',
  152. 'name: Optional[str]=...',
  153. 'args: Iterable=...',
  154. 'kwargs: Mapping[str, Any]=...',
  155. 'daemon: Optional[bool]=...']}
  156. else:
  157. THREAD_CALLTIP = {'index': 0,
  158. 'name': u'Thread',
  159. 'params': [u'group: None=...',
  160. u'target: Optional[Callable[..., Any]]=...',
  161. u'name: Optional[str]=...',
  162. u'args: Iterable=...',
  163. u'kwargs: Mapping[str, Any]=...']}
  164. def test_should_not_fail_with_get_subscope_by_name(self):
  165. # Bug #677 / jedi#628
  166. source = (
  167. u"my_lambda = lambda x: x+1\n"
  168. u"my_lambda(1)"
  169. )
  170. filename = self.project_file("project.py", source)
  171. offset = 37
  172. sigs = self.backend.rpc_get_calltip(filename, source, offset)
  173. sigs["index"]
  174. class TestRPCGetUsages(RPCGetUsagesTests,
  175. JediBackendTestCase):
  176. def test_should_not_fail_for_missing_module(self):
  177. # This causes use.module_path to be None
  178. source = "import sys\n\nsys.path.\n" # insert()"
  179. offset = 21
  180. filename = self.project_file("project.py", source)
  181. self.rpc(filename, source, offset)
  182. class TestRPCGetNames(RPCGetNamesTests,
  183. JediBackendTestCase):
  184. pass
  185. class TestPosToLinecol(unittest.TestCase):
  186. def test_should_handle_beginning_of_string(self):
  187. self.assertEqual(jedibackend.pos_to_linecol("foo", 0),
  188. (1, 0))
  189. def test_should_handle_end_of_line(self):
  190. self.assertEqual(jedibackend.pos_to_linecol("foo\nbar\nbaz\nqux", 9),
  191. (3, 1))
  192. def test_should_handle_end_of_string(self):
  193. self.assertEqual(jedibackend.pos_to_linecol("foo\nbar\nbaz\nqux", 14),
  194. (4, 2))
  195. class TestLinecolToPos(unittest.TestCase):
  196. def test_should_handle_beginning_of_string(self):
  197. self.assertEqual(jedibackend.linecol_to_pos("foo", 1, 0),
  198. 0)
  199. def test_should_handle_end_of_string(self):
  200. self.assertEqual(jedibackend.linecol_to_pos("foo\nbar\nbaz\nqux",
  201. 3, 1),
  202. 9)
  203. def test_should_return_offset(self):
  204. self.assertEqual(jedibackend.linecol_to_pos("foo\nbar\nbaz\nqux",
  205. 4, 2),
  206. 14)
  207. def test_should_fail_for_line_past_text(self):
  208. self.assertRaises(ValueError,
  209. jedibackend.linecol_to_pos, "foo\n", 3, 1)
  210. def test_should_fail_for_column_past_text(self):
  211. self.assertRaises(ValueError,
  212. jedibackend.linecol_to_pos, "foo\n", 1, 10)
  213. class TestRunWithDebug(unittest.TestCase):
  214. @mock.patch('jedi.Script')
  215. def test_should_call_method(self, Script):
  216. Script.return_value.test_method.return_value = "test-result"
  217. result = jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  218. Script.assert_called_with(1, 2, arg=3)
  219. self.assertEqual(result, 'test-result')
  220. @mock.patch('jedi.Script')
  221. def test_should_re_raise(self, Script):
  222. Script.side_effect = RuntimeError
  223. with self.assertRaises(RuntimeError):
  224. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3,
  225. re_raise=(RuntimeError,))
  226. @mock.patch('jedi.Script')
  227. @mock.patch('jedi.set_debug_function')
  228. def test_should_keep_debug_info(self, set_debug_function, Script):
  229. Script.side_effect = RuntimeError
  230. try:
  231. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  232. except rpc.Fault as e:
  233. self.assertGreaterEqual(e.code, 400)
  234. self.assertIsNotNone(e.data)
  235. self.assertIn("traceback", e.data)
  236. jedi_debug_info = e.data["jedi_debug_info"]
  237. self.assertIsNotNone(jedi_debug_info)
  238. self.assertEqual(jedi_debug_info["script_args"],
  239. "1, 2, arg=3")
  240. self.assertEqual(jedi_debug_info["source"], None)
  241. self.assertEqual(jedi_debug_info["method"], "test_method")
  242. self.assertEqual(jedi_debug_info["debug_info"], [])
  243. else:
  244. self.fail("Fault not thrown")
  245. @mock.patch('jedi.Script')
  246. @mock.patch('jedi.set_debug_function')
  247. def test_should_keep_error_text(self, set_debug_function, Script):
  248. Script.side_effect = RuntimeError
  249. try:
  250. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)
  251. except rpc.Fault as e:
  252. self.assertEqual(str(e), str(RuntimeError()))
  253. self.assertEqual(e.message, str(RuntimeError()))
  254. else:
  255. self.fail("Fault not thrown")
  256. @mock.patch('jedi.Script')
  257. @mock.patch('jedi.set_debug_function')
  258. def test_should_handle_source_special(self, set_debug_function, Script):
  259. Script.side_effect = RuntimeError
  260. try:
  261. jedibackend.run_with_debug(jedi, 'test_method', source="foo")
  262. except rpc.Fault as e:
  263. self.assertEqual(e.data["jedi_debug_info"]["script_args"],
  264. "source=source")
  265. self.assertEqual(e.data["jedi_debug_info"]["source"], "foo")
  266. else:
  267. self.fail("Fault not thrown")
  268. @mock.patch('jedi.Script')
  269. @mock.patch('jedi.set_debug_function')
  270. def test_should_set_debug_info(self, set_debug_function, Script):
  271. the_debug_function = [None]
  272. def my_set_debug_function(debug_function, **kwargs):
  273. the_debug_function[0] = debug_function
  274. def my_script(*args, **kwargs):
  275. the_debug_function[0](jedi.debug.NOTICE, "Notice")
  276. the_debug_function[0](jedi.debug.WARNING, "Warning")
  277. the_debug_function[0]("other", "Other")
  278. raise RuntimeError
  279. set_debug_function.side_effect = my_set_debug_function
  280. Script.return_value.test_method = my_script
  281. try:
  282. jedibackend.run_with_debug(jedi, 'test_method', source="foo")
  283. except rpc.Fault as e:
  284. self.assertEqual(e.data["jedi_debug_info"]["debug_info"],
  285. ["[N] Notice",
  286. "[W] Warning",
  287. "[?] Other"])
  288. else:
  289. self.fail("Fault not thrown")
  290. @mock.patch('jedi.set_debug_function')
  291. @mock.patch('jedi.Script')
  292. def test_should_not_fail_with_bad_data(self, Script, set_debug_function):
  293. import jedi.debug
  294. def set_debug(function, speed=True):
  295. if function is not None:
  296. function(jedi.debug.NOTICE, u"\xab")
  297. set_debug_function.side_effect = set_debug
  298. Script.return_value.test_method.side_effect = Exception
  299. with self.assertRaises(rpc.Fault):
  300. jedibackend.run_with_debug(jedi, 'test_method', 1, 2, arg=3)