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.

88 lines
3.2 KiB

4 years ago
  1. import os
  2. import unittest
  3. import shutil
  4. import sys
  5. import tempfile
  6. import mock
  7. import elpy.pydocutils
  8. class TestGetPydocCompletions(unittest.TestCase):
  9. def test_should_return_top_level_modules(self):
  10. modules = elpy.pydocutils.get_pydoc_completions("")
  11. self.assertIn('sys', modules)
  12. self.assertIn('json', modules)
  13. def test_should_return_submodules(self):
  14. modules = elpy.pydocutils.get_pydoc_completions("elpy")
  15. self.assertIn("elpy.rpc", modules)
  16. self.assertIn("elpy.server", modules)
  17. modules = elpy.pydocutils.get_pydoc_completions("os")
  18. self.assertIn("os.path", modules)
  19. def test_should_find_objects_in_module(self):
  20. self.assertIn("elpy.tests.test_pydocutils.TestGetPydocCompletions",
  21. elpy.pydocutils.get_pydoc_completions
  22. ("elpy.tests.test_pydocutils"))
  23. def test_should_find_attributes_of_objects(self):
  24. attribs = elpy.pydocutils.get_pydoc_completions(
  25. "elpy.tests.test_pydocutils.TestGetPydocCompletions")
  26. self.assertIn("elpy.tests.test_pydocutils.TestGetPydocCompletions."
  27. "test_should_find_attributes_of_objects",
  28. attribs)
  29. def test_should_return_none_for_inexisting_module(self):
  30. self.assertEqual([],
  31. elpy.pydocutils.get_pydoc_completions
  32. ("does_not_exist"))
  33. def test_should_work_for_unicode_strings(self):
  34. self.assertIsNotNone(elpy.pydocutils.get_pydoc_completions
  35. (u"sys"))
  36. def test_should_find_partial_completions(self):
  37. self.assertIn("multiprocessing",
  38. elpy.pydocutils.get_pydoc_completions
  39. ("multiprocess"))
  40. self.assertIn("multiprocessing.util",
  41. elpy.pydocutils.get_pydoc_completions
  42. ("multiprocessing.ut"))
  43. def test_should_ignore_trailing_dot(self):
  44. self.assertIn("elpy.pydocutils",
  45. elpy.pydocutils.get_pydoc_completions
  46. ("elpy."))
  47. class TestGetModules(unittest.TestCase):
  48. def test_should_return_top_level_modules(self):
  49. modules = elpy.pydocutils.get_modules()
  50. self.assertIn('sys', modules)
  51. self.assertIn('json', modules)
  52. def test_should_return_submodules(self):
  53. modules = elpy.pydocutils.get_modules("elpy")
  54. self.assertIn("rpc", modules)
  55. self.assertIn("server", modules)
  56. @mock.patch.object(elpy.pydocutils, 'safeimport')
  57. def test_should_catch_import_errors(self, safeimport):
  58. def raise_function(message):
  59. raise elpy.pydocutils.ErrorDuringImport(message,
  60. (None, None, None))
  61. safeimport.side_effect = raise_function
  62. self.assertEqual([], elpy.pydocutils.get_modules("foo.bar"))
  63. def test_should_not_fail_for_permission_denied(self):
  64. tmpdir = tempfile.mkdtemp(prefix="test-elpy-get-modules-")
  65. sys.path.append(tmpdir)
  66. os.chmod(tmpdir, 0o000)
  67. try:
  68. elpy.pydocutils.get_modules()
  69. finally:
  70. os.chmod(tmpdir, 0o755)
  71. shutil.rmtree(tmpdir)
  72. sys.path.remove(tmpdir)