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