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.

986 lines
36 KiB

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