Personal emacs config
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.

1029 lines
39 KiB

  1. # coding: utf-8
  2. """Support classes and functions for the elpy test code.
  3. Elpy uses a bit of a peculiar test setup to avoid redundancy. For the
  4. tests of the two backends, we provide generic test cases for generic
  5. tests and for specific callback tests.
  6. These mixins can be included in the actual test classes. We can't add
  7. these tests to a BackendTestCase subclass directly because the test
  8. discovery would find them there and try to run them, which would fail.
  9. """
  10. import os
  11. import shutil
  12. import sys
  13. import tempfile
  14. import unittest
  15. import re
  16. from elpy.tests import compat
  17. class BackendTestCase(unittest.TestCase):
  18. """Base class for backend tests.
  19. This class sets up a project root directory and provides an easy
  20. way to create files within the project root.
  21. """
  22. def setUp(self):
  23. """Create the project root and make sure it gets cleaned up."""
  24. super(BackendTestCase, self).setUp()
  25. self.project_root = tempfile.mkdtemp(prefix="elpy-test")
  26. self.addCleanup(shutil.rmtree, self.project_root, True)
  27. def project_file(self, relname, contents):
  28. """Create a file named relname within the project root.
  29. Write contents into that file.
  30. """
  31. full_name = os.path.join(self.project_root, relname)
  32. try:
  33. os.makedirs(os.path.dirname(full_name))
  34. except OSError:
  35. pass
  36. if compat.PYTHON3:
  37. fobj = open(full_name, "w", encoding="utf-8")
  38. else:
  39. fobj = open(full_name, "w")
  40. with fobj as f:
  41. f.write(contents)
  42. return full_name
  43. class GenericRPCTests(object):
  44. """Generic RPC test methods.
  45. This is a mixin to add tests that should be run for all RPC
  46. methods that follow the generic (filename, source, offset) calling
  47. conventions.
  48. """
  49. METHOD = None
  50. def rpc(self, filename, source, offset):
  51. method = getattr(self.backend, self.METHOD)
  52. return method(filename, source, offset)
  53. def test_should_not_fail_on_inexisting_file(self):
  54. filename = self.project_root + "/doesnotexist.py"
  55. self.rpc(filename, "", 0)
  56. def test_should_not_fail_on_empty_file(self):
  57. filename = self.project_file("test.py", "")
  58. self.rpc(filename, "", 0)
  59. def test_should_not_fail_if_file_is_none(self):
  60. self.rpc(None, "", 0)
  61. def test_should_not_fail_for_module_syntax_errors(self):
  62. source, offset = source_and_offset(
  63. "class Foo(object):\n"
  64. " def bar(self):\n"
  65. " foo(_|_"
  66. " bar("
  67. "\n"
  68. " def a(self):\n"
  69. " pass\n"
  70. "\n"
  71. " def b(self):\n"
  72. " pass\n"
  73. "\n"
  74. " def b(self):\n"
  75. " pass\n"
  76. "\n"
  77. " def b(self):\n"
  78. " pass\n"
  79. "\n"
  80. " def b(self):\n"
  81. " pass\n"
  82. "\n"
  83. " def b(self):\n"
  84. " pass\n"
  85. )
  86. filename = self.project_file("test.py", source)
  87. self.rpc(filename, source, offset)
  88. def test_should_not_fail_for_bad_indentation(self):
  89. source, offset = source_and_offset(
  90. "def foo():\n"
  91. " print(23)_|_\n"
  92. " print(17)\n")
  93. filename = self.project_file("test.py", source)
  94. self.rpc(filename, source, offset)
  95. # @unittest.skipIf((3, 3) <= sys.version_info < (3, 4),
  96. # "Bug in jedi for Python 3.3")
  97. def test_should_not_fail_for_relative_import(self):
  98. source, offset = source_and_offset(
  99. "from .. import foo_|_"
  100. )
  101. filename = self.project_file("test.py", source)
  102. self.rpc(filename, source, offset)
  103. def test_should_not_fail_on_keyword(self):
  104. source, offset = source_and_offset(
  105. "_|_try:\n"
  106. " pass\n"
  107. "except:\n"
  108. " pass\n")
  109. filename = self.project_file("test.py", source)
  110. self.rpc(filename, source, offset)
  111. def test_should_not_fail_with_bad_encoding(self):
  112. source, offset = source_and_offset(
  113. u'# coding: utf-8X_|_\n'
  114. )
  115. filename = self.project_file("test.py", source)
  116. self.rpc(filename, source, offset)
  117. def test_should_not_fail_with_form_feed_characters(self):
  118. # Bug in Jedi: jedi#424
  119. source, offset = source_and_offset(
  120. "\f\n"
  121. "class Test(object):_|_\n"
  122. " pass"
  123. )
  124. filename = self.project_file("test.py", source)
  125. self.rpc(filename, source, offset)
  126. def test_should_not_fail_for_dictionaries_in_weird_places(self):
  127. # Bug in Jedi: jedi#417
  128. source, offset = source_and_offset(
  129. "import json\n"
  130. "\n"
  131. "def foo():\n"
  132. " json.loads(_|_\n"
  133. "\n"
  134. " json.load.return_value = {'foo': [],\n"
  135. " 'bar': True}\n"
  136. "\n"
  137. " c = Foo()\n"
  138. )
  139. filename = self.project_file("test.py", source)
  140. self.rpc(filename, source, offset)
  141. def test_should_not_break_with_binary_characters_in_docstring(self):
  142. # Bug in Jedi: jedi#427
  143. template = '''\
  144. class Foo(object):
  145. def __init__(self):
  146. """
  147. COMMUNITY instance that this conversion belongs to.
  148. DISPERSY_VERSION is the dispersy conversion identifier (on the wire version; must be one byte).
  149. COMMUNIY_VERSION is the community conversion identifier (on the wire version; must be one byte).
  150. COMMUNIY_VERSION may not be '\\x00' or '\\xff'. '\\x00' is used by the DefaultConversion until
  151. a proper conversion instance can be made for the Community. '\\xff' is reserved for when
  152. more than one byte is needed as a version indicator.
  153. """
  154. pass
  155. x = Foo()
  156. x._|_
  157. '''
  158. source, offset = source_and_offset(template)
  159. filename = self.project_file("test.py", source)
  160. self.rpc(filename, source, offset)
  161. def test_should_not_fail_for_def_without_name(self):
  162. # Bug jedi#429
  163. source, offset = source_and_offset(
  164. "def_|_():\n"
  165. " if True:\n"
  166. " return True\n"
  167. " else:\n"
  168. " return False\n"
  169. )
  170. filename = self.project_file("project.py", source)
  171. self.rpc(filename, source, offset)
  172. def test_should_not_fail_on_lambda(self):
  173. # Bug #272 / jedi#431, jedi#572
  174. source, offset = source_and_offset(
  175. "map(lambda_|_"
  176. )
  177. filename = self.project_file("project.py", source)
  178. self.rpc(filename, source, offset)
  179. def test_should_not_fail_on_literals(self):
  180. # Bug #314, #344 / jedi#466
  181. source = u'lit = u"""\\\n# -*- coding: utf-8 -*-\n"""\n'
  182. offset = 0
  183. filename = self.project_file("project.py", source)
  184. self.rpc(filename, source, offset)
  185. def test_should_not_fail_with_args_as_args(self):
  186. # Bug #347 in rope_py3k
  187. source, offset = source_and_offset(
  188. "def my_function(*args):\n"
  189. " ret_|_"
  190. )
  191. filename = self.project_file("project.py", source)
  192. self.rpc(filename, source, offset)
  193. def test_should_not_fail_for_unicode_chars_in_string(self):
  194. # Bug #358 / jedi#482
  195. source = '''\
  196. # coding: utf-8
  197. logging.info(u"Saving «{}»...".format(title))
  198. requests.get(u"https://web.archive.org/save/{}".format(url))
  199. '''
  200. offset = 57
  201. filename = self.project_file("project.py", source)
  202. self.rpc(filename, source, offset)
  203. def test_should_not_fail_for_bad_escape_sequence(self):
  204. # Bug #360 / jedi#485
  205. source = r"v = '\x'"
  206. offset = 8
  207. filename = self.project_file("project.py", source)
  208. self.rpc(filename, source, offset)
  209. def test_should_not_fail_for_coding_declarations_in_strings(self):
  210. # Bug #314 / jedi#465 / python#22221
  211. source = u'lit = """\\\n# -*- coding: utf-8 -*-\n"""'
  212. offset = 8
  213. filename = self.project_file("project.py", source)
  214. self.rpc(filename, source, offset)
  215. def test_should_not_fail_if_root_vanishes(self):
  216. # Bug #353
  217. source, offset = source_and_offset(
  218. "import foo\n"
  219. "foo._|_"
  220. )
  221. filename = self.project_file("project.py", source)
  222. shutil.rmtree(self.project_root)
  223. self.rpc(filename, source, offset)
  224. # For some reason, this breaks a lot of other tests. Couldn't
  225. # figure out why.
  226. #
  227. # def test_should_not_fail_for_sys_path(self):
  228. # # Bug #365 / jedi#486
  229. # source, offset = source_and_offset(
  230. # "import sys\n"
  231. # "\n"
  232. # "sys.path.index(_|_\n"
  233. # )
  234. # filename = self.project_file("project.py", source)
  235. #
  236. # self.rpc(filename, source, offset)
  237. def test_should_not_fail_for_key_error(self):
  238. # Bug #561, #564, #570, #588, #593, #599 / jedi#572, jedi#579,
  239. # jedi#590
  240. source, offset = source_and_offset(
  241. "map(lambda_|_"
  242. )
  243. filename = self.project_file("project.py", source)
  244. self.rpc(filename, source, offset)
  245. def test_should_not_fail_for_badly_defined_global_variable(self):
  246. # Bug #519 / jedi#610
  247. source, offset = source_and_offset(
  248. """\
  249. def funct1():
  250. global global_dict_var
  251. global_dict_var = dict()
  252. def funct2():
  253. global global_dict_var
  254. q = global_dict_var.copy_|_()
  255. print(q)""")
  256. filename = self.project_file("project.py", source)
  257. self.rpc(filename, source, offset)
  258. def test_should_not_fail_with_mergednamesdict(self):
  259. # Bug #563 / jedi#589
  260. source, offset = source_and_offset(
  261. u'from email import message_|_'
  262. )
  263. filename = self.project_file("project.py", source)
  264. self.rpc(filename, source, offset)
  265. class RPCGetCompletionsTests(GenericRPCTests):
  266. METHOD = "rpc_get_completions"
  267. def test_should_complete_builtin(self):
  268. source, offset = source_and_offset("o_|_")
  269. expected = self.BUILTINS
  270. actual = [cand['name'] for cand in
  271. self.backend.rpc_get_completions("test.py",
  272. source, offset)]
  273. for candidate in expected:
  274. self.assertIn(candidate, actual)
  275. if sys.version_info >= (3, 5) or sys.version_info < (3, 0):
  276. JSON_COMPLETIONS = ["SONDecoder", "SONEncoder", "SONDecodeError"]
  277. else:
  278. JSON_COMPLETIONS = ["SONDecoder", "SONEncoder"]
  279. def test_should_complete_imports(self):
  280. source, offset = source_and_offset("import json\n"
  281. "json.J_|_")
  282. filename = self.project_file("test.py", source)
  283. completions = self.backend.rpc_get_completions(filename,
  284. source,
  285. offset)
  286. self.assertEqual(
  287. sorted([cand['suffix'] for cand in completions]),
  288. sorted(self.JSON_COMPLETIONS))
  289. def test_should_complete_top_level_modules_for_import(self):
  290. source, offset = source_and_offset("import multi_|_")
  291. filename = self.project_file("test.py", source)
  292. completions = self.backend.rpc_get_completions(filename,
  293. source,
  294. offset)
  295. if compat.PYTHON3:
  296. expected = ["processing"]
  297. else:
  298. expected = ["file", "processing"]
  299. self.assertEqual(sorted([cand['suffix'] for cand in completions]),
  300. sorted(expected))
  301. def test_should_complete_packages_for_import(self):
  302. source, offset = source_and_offset("import email.mi_|_")
  303. filename = self.project_file("test.py", source)
  304. completions = self.backend.rpc_get_completions(filename,
  305. source,
  306. offset)
  307. if sys.version_info < (3, 0):
  308. compl = [u'me', u'METext']
  309. else:
  310. compl = ['me']
  311. self.assertEqual([cand['suffix'] for cand in completions],
  312. compl)
  313. def test_should_not_complete_for_import(self):
  314. source, offset = source_and_offset("import foo.Conf_|_")
  315. filename = self.project_file("test.py", source)
  316. completions = self.backend.rpc_get_completions(filename,
  317. source,
  318. offset)
  319. self.assertEqual([cand['suffix'] for cand in completions],
  320. [])
  321. # @unittest.skipIf((3, 3) <= sys.version_info < (3, 4),
  322. # "Bug in jedi for Python 3.3")
  323. def test_should_not_fail_for_short_module(self):
  324. source, offset = source_and_offset("from .. import foo_|_")
  325. filename = self.project_file("test.py", source)
  326. completions = self.backend.rpc_get_completions(filename,
  327. source,
  328. offset)
  329. self.assertIsNotNone(completions)
  330. def test_should_complete_sys(self):
  331. source, offset = source_and_offset("import sys\nsys._|_")
  332. filename = self.project_file("test.py", source)
  333. completions = self.backend.rpc_get_completions(filename,
  334. source,
  335. offset)
  336. self.assertIn('path', [cand['suffix'] for cand in completions])
  337. def test_should_find_with_trailing_text(self):
  338. source, offset = source_and_offset(
  339. "import threading\nthreading.T_|_mumble mumble")
  340. expected = ["Thread", "ThreadError", "Timer"]
  341. actual = [cand['name'] for cand in
  342. self.backend.rpc_get_completions("test.py", source, offset)]
  343. for candidate in expected:
  344. self.assertIn(candidate, actual)
  345. def test_should_find_completion_different_package(self):
  346. # See issue #74
  347. self.project_file("project/__init__.py", "")
  348. source1 = ("class Add:\n"
  349. " def add(self, a, b):\n"
  350. " return a + b\n")
  351. self.project_file("project/add.py", source1)
  352. source2, offset = source_and_offset(
  353. "from project.add import Add\n"
  354. "class Calculator:\n"
  355. " def add(self, a, b):\n"
  356. " c = Add()\n"
  357. " c.ad_|_\n")
  358. file2 = self.project_file("project/calculator.py", source2)
  359. proposals = self.backend.rpc_get_completions(file2,
  360. source2,
  361. offset)
  362. self.assertEqual(["add"],
  363. [proposal["name"] for proposal in proposals])
  364. class RPCGetCompletionDocstringTests(object):
  365. def test_should_return_docstring(self):
  366. source, offset = source_and_offset("import json\n"
  367. "json.JSONEnc_|_")
  368. filename = self.project_file("test.py", source)
  369. completions = self.backend.rpc_get_completions(filename,
  370. source,
  371. offset)
  372. completions.sort(key=lambda p: p["name"])
  373. prop = completions[0]
  374. self.assertEqual(prop["name"], "JSONEncoder")
  375. docs = self.backend.rpc_get_completion_docstring("JSONEncoder")
  376. self.assertIn("Extensible JSON", docs)
  377. def test_should_return_none_if_unknown(self):
  378. docs = self.backend.rpc_get_completion_docstring("Foo")
  379. self.assertIsNone(docs)
  380. class RPCGetCompletionLocationTests(object):
  381. def test_should_return_location(self):
  382. source, offset = source_and_offset("donaudampfschiff = 1\n"
  383. "donau_|_")
  384. filename = self.project_file("test.py", source)
  385. completions = self.backend.rpc_get_completions(filename,
  386. source,
  387. offset)
  388. prop = completions[0]
  389. self.assertEqual(prop["name"], "donaudampfschiff")
  390. loc = self.backend.rpc_get_completion_location("donaudampfschiff")
  391. self.assertEqual((filename, 1), loc)
  392. def test_should_return_none_if_unknown(self):
  393. docs = self.backend.rpc_get_completion_location("Foo")
  394. self.assertIsNone(docs)
  395. class RPCGetDefinitionTests(GenericRPCTests):
  396. METHOD = "rpc_get_definition"
  397. def test_should_return_definition_location_same_file(self):
  398. source, offset = source_and_offset("import threading\n"
  399. "def test_function(a, b):\n"
  400. " return a + b\n"
  401. "\n"
  402. "test_func_|_tion(\n")
  403. filename = self.project_file("test.py", source)
  404. location = self.backend.rpc_get_definition(filename,
  405. source,
  406. offset)
  407. self.assertEqual(location[0], filename)
  408. # On def or on the function name
  409. self.assertIn(location[1], (17, 21))
  410. def test_should_return_location_in_same_file_if_not_saved(self):
  411. source, offset = source_and_offset(
  412. "import threading\n"
  413. "\n"
  414. "\n"
  415. "def other_function():\n"
  416. " test_f_|_unction(1, 2)\n"
  417. "\n"
  418. "\n"
  419. "def test_function(a, b):\n"
  420. " return a + b\n")
  421. filename = self.project_file("test.py", "")
  422. location = self.backend.rpc_get_definition(filename,
  423. source,
  424. offset)
  425. self.assertEqual(location[0], filename)
  426. # def or function name
  427. self.assertIn(location[1], (67, 71))
  428. def test_should_return_location_in_different_file(self):
  429. source1 = ("def test_function(a, b):\n"
  430. " return a + b\n")
  431. file1 = self.project_file("test1.py", source1)
  432. source2, offset = source_and_offset("from test1 import test_function\n"
  433. "test_funct_|_ion(1, 2)\n")
  434. file2 = self.project_file("test2.py", source2)
  435. definition = self.backend.rpc_get_definition(file2,
  436. source2,
  437. offset)
  438. self.assertEqual(definition[0], file1)
  439. # Either on the def or on the function name
  440. self.assertIn(definition[1], (0, 4))
  441. def test_should_return_none_if_location_not_found(self):
  442. source, offset = source_and_offset("test_f_|_unction()\n")
  443. filename = self.project_file("test.py", source)
  444. definition = self.backend.rpc_get_definition(filename,
  445. source,
  446. offset)
  447. self.assertIsNone(definition)
  448. def test_should_return_none_if_outside_of_symbol(self):
  449. source, offset = source_and_offset("test_function(_|_)\n")
  450. filename = self.project_file("test.py", source)
  451. definition = self.backend.rpc_get_definition(filename,
  452. source,
  453. offset)
  454. self.assertIsNone(definition)
  455. def test_should_return_definition_location_different_package(self):
  456. # See issue #74
  457. self.project_file("project/__init__.py", "")
  458. source1 = ("class Add:\n"
  459. " def add(self, a, b):\n"
  460. " return a + b\n")
  461. file1 = self.project_file("project/add.py", source1)
  462. source2, offset = source_and_offset(
  463. "from project.add import Add\n"
  464. "class Calculator:\n"
  465. " def add(self, a, b):\n"
  466. " return Add_|_().add(a, b)\n")
  467. file2 = self.project_file("project/calculator.py", source2)
  468. location = self.backend.rpc_get_definition(file2,
  469. source2,
  470. offset)
  471. self.assertEqual(location[0], file1)
  472. # class or class name
  473. self.assertIn(location[1], (0, 6))
  474. def test_should_find_variable_definition(self):
  475. source, offset = source_and_offset("SOME_VALUE = 1\n"
  476. "\n"
  477. "variable = _|_SOME_VALUE\n")
  478. filename = self.project_file("test.py", source)
  479. self.assertEqual(self.backend.rpc_get_definition(filename,
  480. source,
  481. offset),
  482. (filename, 0))
  483. class RPCGetAssignmentTests(GenericRPCTests):
  484. METHOD = "rpc_get_assignment"
  485. def test_should_return_assignment_location_same_file(self):
  486. source, offset = source_and_offset("import threading\n"
  487. "class TestClass(object):\n"
  488. " def __init__(self, a, b):\n"
  489. " self.a = a\n"
  490. " self.b = b\n"
  491. "\n"
  492. "testclass = TestClass(2, 4)"
  493. "\n"
  494. "testcl_|_ass(\n")
  495. filename = self.project_file("test.py", source)
  496. location = self.backend.rpc_get_assignment(filename,
  497. source,
  498. offset)
  499. self.assertEqual(location[0], filename)
  500. # On def or on the function name
  501. self.assertEqual(location[1], 111)
  502. def test_should_return_location_in_same_file_if_not_saved(self):
  503. source, offset = source_and_offset("import threading\n"
  504. "class TestClass(object):\n"
  505. " def __init__(self, a, b):\n"
  506. " self.a = a\n"
  507. " self.b = b\n"
  508. "\n"
  509. "testclass = TestClass(2, 4)"
  510. "\n"
  511. "testcl_|_ass(\n")
  512. filename = self.project_file("test.py", "")
  513. location = self.backend.rpc_get_assignment(filename,
  514. source,
  515. offset)
  516. self.assertEqual(location[0], filename)
  517. # def or function name
  518. self.assertEqual(location[1], 111)
  519. def test_should_return_location_in_different_file(self):
  520. source1 = ("class TestClass(object):\n"
  521. " def __init__(self, a, b):\n"
  522. " self.a = a\n"
  523. " self.b = b\n"
  524. "testclass = TestClass(3, 5)\n")
  525. file1 = self.project_file("test1.py", source1)
  526. source2, offset = source_and_offset("from test1 import testclass\n"
  527. "testcl_|_ass.a\n")
  528. file2 = self.project_file("test2.py", source2)
  529. # First jump goes to import statement
  530. assignment = self.backend.rpc_get_assignment(file2,
  531. source2,
  532. offset)
  533. # Second jump goes to test1 file
  534. self.assertEqual(assignment[0], file2)
  535. assignment = self.backend.rpc_get_assignment(file2,
  536. source2,
  537. assignment[1])
  538. self.assertEqual(assignment[0], file1)
  539. self.assertEqual(assignment[1], 93)
  540. def test_should_return_none_if_location_not_found(self):
  541. source, offset = source_and_offset("test_f_|_unction()\n")
  542. filename = self.project_file("test.py", source)
  543. assignment = self.backend.rpc_get_assignment(filename,
  544. source,
  545. offset)
  546. self.assertIsNone(assignment)
  547. def test_should_return_none_if_outside_of_symbol(self):
  548. source, offset = source_and_offset("testcl(_|_)ass\n")
  549. filename = self.project_file("test.py", source)
  550. assignment = self.backend.rpc_get_assignment(filename,
  551. source,
  552. offset)
  553. self.assertIsNone(assignment)
  554. def test_should_find_variable_assignment(self):
  555. source, offset = source_and_offset("SOME_VALUE = 1\n"
  556. "\n"
  557. "variable = _|_SOME_VALUE\n")
  558. filename = self.project_file("test.py", source)
  559. self.assertEqual(self.backend.rpc_get_assignment(filename,
  560. source,
  561. offset),
  562. (filename, 0))
  563. class RPCGetCalltipTests(GenericRPCTests):
  564. METHOD = "rpc_get_calltip"
  565. def test_should_get_calltip(self):
  566. expected = self.THREAD_CALLTIP
  567. source, offset = source_and_offset(
  568. "import threading\nthreading.Thread(_|_")
  569. filename = self.project_file("test.py", source)
  570. calltip = self.backend.rpc_get_calltip(filename,
  571. source,
  572. offset)
  573. self.assertEqual(calltip, expected)
  574. calltip = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  575. source,
  576. offset)
  577. calltip.pop('kind')
  578. self.assertEqual(calltip, expected)
  579. def test_should_get_calltip_even_after_parens(self):
  580. source, offset = source_and_offset(
  581. "import threading\nthreading.Thread(foo()_|_")
  582. filename = self.project_file("test.py", source)
  583. actual = self.backend.rpc_get_calltip(filename,
  584. source,
  585. offset)
  586. self.assertEqual(self.THREAD_CALLTIP, actual)
  587. actual = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  588. source,
  589. offset)
  590. actual.pop('kind')
  591. self.assertEqual(self.THREAD_CALLTIP, actual)
  592. def test_should_get_calltip_at_closing_paren(self):
  593. source, offset = source_and_offset(
  594. "import threading\nthreading.Thread(_|_)")
  595. filename = self.project_file("test.py", source)
  596. actual = self.backend.rpc_get_calltip(filename,
  597. source,
  598. offset)
  599. self.assertEqual(self.THREAD_CALLTIP, actual)
  600. actual = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  601. source,
  602. offset)
  603. self.assertEqual(actual['kind'], "oneline_doc")
  604. def test_should_not_missing_attribute_get_definition(self):
  605. # Bug #627 / jedi#573
  606. source, offset = source_and_offset(
  607. "import threading\nthreading.Thread(_|_)")
  608. filename = self.project_file("test.py", source)
  609. self.backend.rpc_get_calltip(filename, source, offset)
  610. def test_should_return_none_for_bad_identifier(self):
  611. source, offset = source_and_offset(
  612. "froblgoo(_|_")
  613. filename = self.project_file("test.py", source)
  614. calltip = self.backend.rpc_get_calltip(filename,
  615. source,
  616. offset)
  617. self.assertIsNone(calltip)
  618. calltip = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  619. source,
  620. offset)
  621. self.assertIsNone(calltip)
  622. def test_should_remove_self_argument(self):
  623. source, offset = source_and_offset(
  624. "d = dict()\n"
  625. "d.keys(_|_")
  626. filename = self.project_file("test.py", source)
  627. actual = self.backend.rpc_get_calltip(filename,
  628. source,
  629. offset)
  630. self.assertEqual(self.KEYS_CALLTIP, actual)
  631. actual = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  632. source,
  633. offset)
  634. actual.pop('kind')
  635. self.assertEqual(self.KEYS_CALLTIP, actual)
  636. def test_should_remove_package_prefix(self):
  637. source, offset = source_and_offset(
  638. "import decimal\n"
  639. "d = decimal.Decimal('1.5')\n"
  640. "d.radix(_|_")
  641. filename = self.project_file("test.py", source)
  642. actual = self.backend.rpc_get_calltip(filename,
  643. source,
  644. offset)
  645. self.assertEqual(self.RADIX_CALLTIP, actual)
  646. actual = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  647. source,
  648. offset)
  649. actual.pop('kind')
  650. self.assertEqual(self.RADIX_CALLTIP, actual)
  651. def test_should_return_none_outside_of_all(self):
  652. filename = self.project_file("test.py", "")
  653. source, offset = source_and_offset("import thr_|_eading\n")
  654. calltip = self.backend.rpc_get_calltip(filename,
  655. source, offset)
  656. self.assertIsNone(calltip)
  657. calltip = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  658. source,
  659. offset)
  660. self.assertIsNotNone(calltip)
  661. def test_should_find_calltip_different_package(self):
  662. # See issue #74
  663. self.project_file("project/__init__.py", "")
  664. source1 = ("class Add:\n"
  665. " def add(self, a, b):\n"
  666. " return a + b\n")
  667. self.project_file("project/add.py", source1)
  668. source2, offset = source_and_offset(
  669. "from project.add import Add\n"
  670. "class Calculator:\n"
  671. " def add(self, a, b):\n"
  672. " c = Add()\n"
  673. " c.add(_|_\n")
  674. file2 = self.project_file("project/calculator.py", source2)
  675. actual = self.backend.rpc_get_calltip(file2,
  676. source2,
  677. offset)
  678. self.assertEqual(self.ADD_CALLTIP, actual)
  679. actual = self.backend.rpc_get_calltip_or_oneline_docstring(file2,
  680. source2,
  681. offset)
  682. actual.pop('kind')
  683. self.assertEqual(self.ADD_CALLTIP, actual)
  684. class RPCGetDocstringTests(GenericRPCTests):
  685. METHOD = "rpc_get_docstring"
  686. def check_docstring(self, docstring):
  687. def first_line(s):
  688. return s[:s.index("\n")]
  689. match = re.match(self.JSON_LOADS_REGEX,
  690. first_line(docstring))
  691. self.assertIsNotNone(match)
  692. def test_should_get_docstring(self):
  693. source, offset = source_and_offset(
  694. "import json\njson.loads_|_(")
  695. filename = self.project_file("test.py", source)
  696. docstring = self.backend.rpc_get_docstring(filename,
  697. source,
  698. offset)
  699. self.check_docstring(docstring)
  700. def test_should_return_none_for_bad_identifier(self):
  701. source, offset = source_and_offset(
  702. "froblgoo_|_(\n")
  703. filename = self.project_file("test.py", source)
  704. docstring = self.backend.rpc_get_docstring(filename,
  705. source,
  706. offset)
  707. self.assertIsNone(docstring)
  708. class RPCGetOnelineDocstringTests(GenericRPCTests):
  709. METHOD = "rpc_get_oneline_docstring"
  710. def check_docstring(self, docstring):
  711. self.assertEqual(docstring['doc'],
  712. self.JSON_LOADS_DOCSTRING)
  713. def check_module_docstring(self, docstring):
  714. self.assertEqual(docstring['doc'],
  715. self.JSON_DOCSTRING)
  716. def test_should_get_oneline_docstring(self):
  717. source, offset = source_and_offset(
  718. "import json\njson.loads_|_(")
  719. filename = self.project_file("test.py", source)
  720. docstring = self.backend.rpc_get_oneline_docstring(filename,
  721. source,
  722. offset)
  723. self.check_docstring(docstring)
  724. docstring = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  725. source,
  726. offset)
  727. docstring.pop('kind')
  728. self.check_docstring(docstring)
  729. def test_should_get_oneline_docstring_for_modules(self):
  730. source, offset = source_and_offset(
  731. "import json_|_\njson.loads(")
  732. filename = self.project_file("test.py", source)
  733. docstring = self.backend.rpc_get_oneline_docstring(filename,
  734. source,
  735. offset)
  736. self.check_module_docstring(docstring)
  737. docstring = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  738. source,
  739. offset)
  740. docstring.pop('kind')
  741. self.check_module_docstring(docstring)
  742. def test_should_return_none_for_bad_identifier(self):
  743. source, offset = source_and_offset(
  744. "froblgoo_|_(\n")
  745. filename = self.project_file("test.py", source)
  746. docstring = self.backend.rpc_get_oneline_docstring(filename,
  747. source,
  748. offset)
  749. self.assertIsNone(docstring)
  750. docstring = self.backend.rpc_get_calltip_or_oneline_docstring(filename,
  751. source,
  752. offset)
  753. self.assertIsNone(docstring)
  754. class RPCGetNamesTests(GenericRPCTests):
  755. METHOD = "rpc_get_names"
  756. def test_shouldreturn_names_in_same_file(self):
  757. filename = self.project_file("test.py", "")
  758. source, offset = source_and_offset(
  759. "def foo(x, y):\n"
  760. " return x + y\n"
  761. "c = _|_foo(5, 2)\n")
  762. names = self.backend.rpc_get_names(filename,
  763. source,
  764. offset)
  765. self.assertEqual(names,
  766. [{'name': 'foo',
  767. 'filename': filename,
  768. 'offset': 4},
  769. {'name': 'x',
  770. 'filename': filename,
  771. 'offset': 8},
  772. {'name': 'y',
  773. 'filename': filename,
  774. 'offset': 11},
  775. {'name': 'x',
  776. 'filename': filename,
  777. 'offset': 26},
  778. {'name': 'y',
  779. 'filename': filename,
  780. 'offset': 30},
  781. {'name': 'c',
  782. 'filename': filename,
  783. 'offset': 32},
  784. {'name': 'foo',
  785. 'filename': filename,
  786. 'offset': 36}])
  787. def test_should_not_fail_without_symbol(self):
  788. filename = self.project_file("test.py", "")
  789. names = self.backend.rpc_get_names(filename,
  790. "",
  791. 0)
  792. self.assertEqual(names, [])
  793. class RPCGetUsagesTests(GenericRPCTests):
  794. METHOD = "rpc_get_usages"
  795. def test_should_return_uses_in_same_file(self):
  796. filename = self.project_file("test.py", "")
  797. source, offset = source_and_offset(
  798. "def foo(x):\n"
  799. " return _|_x + x\n")
  800. usages = self.backend.rpc_get_usages(filename,
  801. source,
  802. offset)
  803. self.assertEqual(usages,
  804. [{'name': 'x',
  805. 'offset': 8,
  806. 'filename': filename},
  807. {'name': 'x',
  808. 'filename': filename,
  809. 'offset': 23},
  810. {'name': u'x',
  811. 'filename': filename,
  812. 'offset': 27}])
  813. def test_should_return_uses_in_other_file(self):
  814. file1 = self.project_file("file1.py", "")
  815. file2 = self.project_file("file2.py", "\n\n\n\n\nx = 5")
  816. source, offset = source_and_offset(
  817. "import file2\n"
  818. "file2._|_x\n")
  819. usages = self.backend.rpc_get_usages(file1,
  820. source,
  821. offset)
  822. self.assertEqual(usages,
  823. [{'name': 'x',
  824. 'filename': file1,
  825. 'offset': 19},
  826. {'name': 'x',
  827. 'filename': file2,
  828. 'offset': 5}])
  829. def test_should_not_fail_without_symbol(self):
  830. filename = self.project_file("file.py", "")
  831. usages = self.backend.rpc_get_usages(filename,
  832. "",
  833. 0)
  834. self.assertEqual(usages, [])
  835. def source_and_offset(source):
  836. """Return a source and offset from a source description.
  837. >>> source_and_offset("hello, _|_world")
  838. ("hello, world", 7)
  839. >>> source_and_offset("_|_hello, world")
  840. ("hello, world", 0)
  841. >>> source_and_offset("hello, world_|_")
  842. ("hello, world", 12)
  843. """
  844. offset = source.index("_|_")
  845. return source[:offset] + source[offset + 3:], offset