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.

378 lines
13 KiB

5 years ago
  1. # coding: utf-8
  2. """Tests for the elpy.server module"""
  3. import os
  4. import tempfile
  5. import unittest
  6. import mock
  7. from elpy import rpc
  8. from elpy import server
  9. from elpy.tests import compat
  10. from elpy.tests.support import BackendTestCase
  11. import elpy.refactor
  12. class ServerTestCase(unittest.TestCase):
  13. def setUp(self):
  14. self.srv = server.ElpyRPCServer()
  15. class BackendCallTestCase(ServerTestCase):
  16. def assert_calls_backend(self, method):
  17. with mock.patch("elpy.server.get_source") as get_source:
  18. with mock.patch.object(self.srv, "backend") as backend:
  19. get_source.return_value = "transformed source"
  20. getattr(self.srv, method)("filename", "source", "offset")
  21. get_source.assert_called_with("source")
  22. getattr(backend, method).assert_called_with(
  23. "filename", "transformed source", "offset"
  24. )
  25. class TestInit(ServerTestCase):
  26. def test_should_not_select_a_backend_by_default(self):
  27. self.assertIsNone(self.srv.backend)
  28. class TestRPCEcho(ServerTestCase):
  29. def test_should_return_arguments(self):
  30. self.assertEqual(("hello", "world"),
  31. self.srv.rpc_echo("hello", "world"))
  32. class TestRPCInit(ServerTestCase):
  33. @mock.patch("elpy.jedibackend.JediBackend")
  34. def test_should_set_project_root(self, JediBackend):
  35. self.srv.rpc_init({"project_root": "/project/root"})
  36. self.assertEqual("/project/root", self.srv.project_root)
  37. @mock.patch("elpy.jedibackend.JediBackend")
  38. def test_should_initialize_jedi(self, JediBackend):
  39. self.srv.rpc_init({"project_root": "/project/root"})
  40. JediBackend.assert_called_with("/project/root")
  41. @mock.patch("elpy.jedibackend.JediBackend")
  42. def test_should_use_jedi_if_available(self, JediBackend):
  43. JediBackend.return_value.name = "jedi"
  44. self.srv.rpc_init({"project_root": "/project/root"})
  45. self.assertEqual("jedi", self.srv.backend.name)
  46. @mock.patch("elpy.jedibackend.JediBackend")
  47. def test_should_use_none_if_nothing_available(
  48. self, JediBackend):
  49. JediBackend.return_value.name = "jedi"
  50. old_jedi = server.jedibackend
  51. server.jedibackend = None
  52. try:
  53. self.srv.rpc_init({"project_root": "/project/root"})
  54. finally:
  55. server.jedibackend = old_jedi
  56. self.assertIsNone(self.srv.backend)
  57. class TestRPCGetCalltip(BackendCallTestCase):
  58. def test_should_call_backend(self):
  59. self.assert_calls_backend("rpc_get_calltip")
  60. def test_should_handle_no_backend(self):
  61. self.srv.backend = None
  62. self.assertIsNone(self.srv.rpc_get_calltip("filname", "source",
  63. "offset"))
  64. class TestRPCGetCompletions(BackendCallTestCase):
  65. def test_should_call_backend(self):
  66. self.assert_calls_backend("rpc_get_completions")
  67. def test_should_handle_no_backend(self):
  68. self.srv.backend = None
  69. self.assertEqual([],
  70. self.srv.rpc_get_completions("filname", "source",
  71. "offset"))
  72. def test_should_sort_results(self):
  73. with mock.patch.object(self.srv, 'backend') as backend:
  74. backend.rpc_get_completions.return_value = [
  75. {'name': '_e'},
  76. {'name': '__d'},
  77. {'name': 'c'},
  78. {'name': 'B'},
  79. {'name': 'a'},
  80. ]
  81. expected = list(reversed(backend.rpc_get_completions.return_value))
  82. actual = self.srv.rpc_get_completions("filename", "source",
  83. "offset")
  84. self.assertEqual(expected, actual)
  85. def test_should_uniquify_results(self):
  86. with mock.patch.object(self.srv, 'backend') as backend:
  87. backend.rpc_get_completions.return_value = [
  88. {'name': 'a'},
  89. {'name': 'a'},
  90. ]
  91. expected = [{'name': 'a'}]
  92. actual = self.srv.rpc_get_completions("filename", "source",
  93. "offset")
  94. self.assertEqual(expected, actual)
  95. class TestRPCGetCompletionDocs(ServerTestCase):
  96. def test_should_call_backend(self):
  97. with mock.patch.object(self.srv, "backend") as backend:
  98. self.srv.rpc_get_completion_docstring("completion")
  99. (backend.rpc_get_completion_docstring
  100. .assert_called_with("completion"))
  101. def test_should_handle_no_backend(self):
  102. self.srv.backend = None
  103. self.assertIsNone(self.srv.rpc_get_completion_docstring("foo"))
  104. class TestRPCGetCompletionLocation(ServerTestCase):
  105. def test_should_call_backend(self):
  106. with mock.patch.object(self.srv, "backend") as backend:
  107. self.srv.rpc_get_completion_location("completion")
  108. (backend.rpc_get_completion_location
  109. .assert_called_with("completion"))
  110. def test_should_handle_no_backend(self):
  111. self.srv.backend = None
  112. self.assertIsNone(self.srv.rpc_get_completion_location("foo"))
  113. class TestRPCGetDefinition(BackendCallTestCase):
  114. def test_should_call_backend(self):
  115. self.assert_calls_backend("rpc_get_definition")
  116. def test_should_handle_no_backend(self):
  117. self.srv.backend = None
  118. self.assertIsNone(self.srv.rpc_get_definition("filname", "source",
  119. "offset"))
  120. class TestRPCGetAssignment(BackendCallTestCase):
  121. def test_should_call_backend(self):
  122. self.assert_calls_backend("rpc_get_assignment")
  123. def test_should_handle_no_backend(self):
  124. self.srv.backend = None
  125. self.assertIsNone(self.srv.rpc_get_assignment("filname", "source",
  126. "offset"))
  127. class TestRPCGetDocstring(BackendCallTestCase):
  128. def test_should_call_backend(self):
  129. self.assert_calls_backend("rpc_get_docstring")
  130. def test_should_handle_no_backend(self):
  131. self.srv.backend = None
  132. self.assertIsNone(self.srv.rpc_get_docstring("filname", "source",
  133. "offset"))
  134. class TestRPCGetOnelineDocstring(BackendCallTestCase):
  135. def test_should_call_backend(self):
  136. self.assert_calls_backend("rpc_get_oneline_docstring")
  137. def test_should_handle_no_backend(self):
  138. self.srv.backend = None
  139. self.assertIsNone(self.srv.rpc_get_oneline_docstring("filname",
  140. "source",
  141. "offset"))
  142. class TestRPCGetPydocCompletions(ServerTestCase):
  143. @mock.patch.object(server, 'get_pydoc_completions')
  144. def test_should_call_pydoc_completions(self, get_pydoc_completions):
  145. srv = server.ElpyRPCServer()
  146. srv.rpc_get_pydoc_completions()
  147. get_pydoc_completions.assert_called_with(None)
  148. srv.rpc_get_pydoc_completions("foo")
  149. get_pydoc_completions.assert_called_with("foo")
  150. class TestGetPydocDocumentation(ServerTestCase):
  151. @mock.patch("pydoc.render_doc")
  152. def test_should_find_documentation(self, render_doc):
  153. render_doc.return_value = "expected"
  154. actual = self.srv.rpc_get_pydoc_documentation("open")
  155. render_doc.assert_called_with("open",
  156. "Elpy Pydoc Documentation for %s",
  157. False)
  158. self.assertEqual("expected", actual)
  159. def test_should_return_none_for_unknown_module(self):
  160. actual = self.srv.rpc_get_pydoc_documentation("frob.open")
  161. self.assertIsNone(actual)
  162. def test_should_return_valid_unicode(self):
  163. import json
  164. docstring = self.srv.rpc_get_pydoc_documentation("tarfile")
  165. json.dumps(docstring)
  166. class TestRPCGetRefactorOptions(BackendTestCase):
  167. @mock.patch.object(compat.builtins, '__import__')
  168. def test_should_fail_if_rope_is_not_available(self, import_):
  169. import_.side_effect = ImportError
  170. filename = self.project_file("foo.py", "")
  171. srv = server.ElpyRPCServer()
  172. self.assertRaises(ImportError, srv.rpc_get_refactor_options,
  173. filename, 0)
  174. @mock.patch.object(elpy.refactor, 'Refactor')
  175. def test_should_initialize_and_call_refactor_object(self, Refactor):
  176. filename = self.project_file("foo.py", "import foo")
  177. srv = server.ElpyRPCServer()
  178. srv.project_root = self.project_root
  179. srv.rpc_get_refactor_options(filename, 5)
  180. Refactor.assert_called_with(self.project_root, filename)
  181. Refactor.return_value.get_refactor_options.assert_called_with(5, None)
  182. class TestRPCRefactor(BackendTestCase):
  183. @mock.patch.object(compat.builtins, '__import__')
  184. def test_should_fail_if_rope_is_not_available(self, import_):
  185. import_.side_effect = ImportError
  186. filename = self.project_file("foo.py", "")
  187. srv = server.ElpyRPCServer()
  188. self.assertRaises(ImportError, srv.rpc_refactor,
  189. filename, 'foo', ())
  190. @mock.patch.object(elpy.refactor, 'Refactor')
  191. def test_should_initialize_and_call_refactor_object_with_args(
  192. self, Refactor):
  193. filename = self.project_file("foo.py", "import foo")
  194. srv = server.ElpyRPCServer()
  195. srv.project_root = self.project_root
  196. srv.rpc_refactor(filename, 'foo', (1, 2, 3))
  197. Refactor.assert_called_with(self.project_root, filename)
  198. Refactor.return_value.get_changes.assert_called_with('foo', 1, 2, 3)
  199. @mock.patch.object(elpy.refactor, 'Refactor')
  200. def test_should_initialize_and_call_refactor_object_without_args(
  201. self, Refactor):
  202. filename = self.project_file("foo.py", "import foo")
  203. srv = server.ElpyRPCServer()
  204. srv.project_root = self.project_root
  205. srv.rpc_refactor(filename, 'foo', None)
  206. Refactor.assert_called_with(self.project_root, filename)
  207. Refactor.return_value.get_changes.assert_called_with('foo')
  208. class TestRPCGetUsages(BackendCallTestCase):
  209. def test_should_call_backend(self):
  210. self.assert_calls_backend("rpc_get_usages")
  211. def test_should_handle_no_backend(self):
  212. self.srv.backend = None
  213. with self.assertRaises(rpc.Fault):
  214. self.assertIsNone(self.srv.rpc_get_usages("filname", "source",
  215. "offset"))
  216. class TestRPCGetNames(BackendCallTestCase):
  217. def test_should_call_backend(self):
  218. self.assert_calls_backend("rpc_get_names")
  219. def test_should_handle_no_backend(self):
  220. self.srv.backend = None
  221. with self.assertRaises(rpc.Fault):
  222. self.assertIsNone(self.srv.rpc_get_names("filname", "source", 0))
  223. class TestGetSource(unittest.TestCase):
  224. def test_should_return_string_by_default(self):
  225. self.assertEqual(server.get_source("foo"),
  226. "foo")
  227. def test_should_return_file_contents(self):
  228. fd, filename = tempfile.mkstemp(prefix="elpy-test-")
  229. self.addCleanup(os.remove, filename)
  230. with open(filename, "w") as f:
  231. f.write("file contents")
  232. fileobj = {'filename': filename}
  233. self.assertEqual(server.get_source(fileobj),
  234. "file contents")
  235. def test_should_clean_up_tempfile(self):
  236. fd, filename = tempfile.mkstemp(prefix="elpy-test-")
  237. with open(filename, "w") as f:
  238. f.write("file contents")
  239. fileobj = {'filename': filename,
  240. 'delete_after_use': True}
  241. self.assertEqual(server.get_source(fileobj),
  242. "file contents")
  243. self.assertFalse(os.path.exists(filename))
  244. def test_should_support_utf8(self):
  245. fd, filename = tempfile.mkstemp(prefix="elpy-test-")
  246. self.addCleanup(os.remove, filename)
  247. with open(filename, "wb") as f:
  248. f.write(u"möp".encode("utf-8"))
  249. source = server.get_source({'filename': filename})
  250. self.assertEqual(source, u"möp")
  251. class TestPysymbolKey(BackendTestCase):
  252. def keyLess(self, a, b):
  253. self.assertLess(b, a)
  254. self.assertLess(server._pysymbol_key(a),
  255. server._pysymbol_key(b))
  256. def test_should_be_case_insensitive(self):
  257. self.keyLess("bar", "Foo")
  258. def test_should_sort_private_symbols_after_public_symbols(self):
  259. self.keyLess("foo", "_bar")
  260. def test_should_sort_private_symbols_after_dunder_symbols(self):
  261. self.assertLess(server._pysymbol_key("__foo__"),
  262. server._pysymbol_key("_bar"))
  263. def test_should_sort_dunder_symbols_after_public_symbols(self):
  264. self.keyLess("bar", "__foo")
  265. class Autopep8TestCase(ServerTestCase):
  266. def test_rpc_fix_code_should_return_formatted_string(self):
  267. code_block = 'x= 123\n'
  268. new_block = self.srv.rpc_fix_code(code_block, os.getcwd())
  269. self.assertEqual(new_block, 'x = 123\n')