Show More
@@ -1,133 +1,140 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for completerlib. |
|
3 | 3 | |
|
4 | 4 | """ |
|
5 | 5 | from __future__ import absolute_import |
|
6 | 6 | |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Imports |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | import os |
|
12 | 12 | import shutil |
|
13 | 13 | import sys |
|
14 | 14 | import tempfile |
|
15 | 15 | import unittest |
|
16 | 16 | from os.path import join |
|
17 | 17 | |
|
18 | 18 | import nose.tools as nt |
|
19 | 19 | |
|
20 | 20 | from IPython.core.completerlib import magic_run_completer, module_completion |
|
21 | 21 | from IPython.utils import py3compat |
|
22 | 22 | from IPython.utils.tempdir import TemporaryDirectory |
|
23 | 23 | from IPython.testing.decorators import onlyif_unicode_paths |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | class MockEvent(object): |
|
27 | 27 | def __init__(self, line): |
|
28 | 28 | self.line = line |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | # Test functions begin |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | class Test_magic_run_completer(unittest.TestCase): |
|
34 | files = [u"aao.py", u"a.py", u"b.py", u"aao.txt"] | |
|
35 | ||
|
34 | 36 | def setUp(self): |
|
35 | 37 | self.BASETESTDIR = tempfile.mkdtemp() |
|
36 | for fil in [u"aao.py", u"a.py", u"b.py", u"aao.txt"]: | |
|
38 | for fil in self.files: | |
|
37 | 39 | with open(join(self.BASETESTDIR, fil), "w") as sfile: |
|
38 | 40 | sfile.write("pass\n") |
|
39 | 41 | self.oldpath = py3compat.getcwd() |
|
40 | 42 | os.chdir(self.BASETESTDIR) |
|
41 | 43 | |
|
42 | 44 | def tearDown(self): |
|
43 | 45 | os.chdir(self.oldpath) |
|
44 | 46 | shutil.rmtree(self.BASETESTDIR) |
|
45 | 47 | |
|
46 | 48 | def test_1(self): |
|
47 | 49 | """Test magic_run_completer, should match two alterntives |
|
48 | 50 | """ |
|
49 | 51 | event = MockEvent(u"%run a") |
|
50 | 52 | mockself = None |
|
51 | 53 | match = set(magic_run_completer(mockself, event)) |
|
52 | 54 | self.assertEqual(match, set([u"a.py", u"aao.py"])) |
|
53 | 55 | |
|
54 | 56 | def test_2(self): |
|
55 | 57 | """Test magic_run_completer, should match one alterntive |
|
56 | 58 | """ |
|
57 | 59 | event = MockEvent(u"%run aa") |
|
58 | 60 | mockself = None |
|
59 | 61 | match = set(magic_run_completer(mockself, event)) |
|
60 | 62 | self.assertEqual(match, set([u"aao.py"])) |
|
61 | 63 | |
|
62 | 64 | def test_3(self): |
|
63 | 65 | """Test magic_run_completer with unterminated " """ |
|
64 | 66 | event = MockEvent(u'%run "a') |
|
65 | 67 | mockself = None |
|
66 | 68 | match = set(magic_run_completer(mockself, event)) |
|
67 | 69 | self.assertEqual(match, set([u"a.py", u"aao.py"])) |
|
68 | 70 | |
|
71 | def test_completion_more_args(self): | |
|
72 | event = MockEvent(u'%run a.py ') | |
|
73 | match = set(magic_run_completer(None, event)) | |
|
74 | self.assertEqual(match, set(self.files)) | |
|
75 | ||
|
69 | 76 | def test_completion_in_dir(self): |
|
70 | 77 | # Github issue #3459 |
|
71 | 78 | event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a'))) |
|
72 | 79 | print(repr(event.line)) |
|
73 | 80 | match = set(magic_run_completer(None, event)) |
|
74 | 81 | self.assertEqual(match, |
|
75 | 82 | {join(self.BASETESTDIR, f) for f in (u'a.py', u'aao.py', u'aao.txt')}) |
|
76 | 83 | |
|
77 | 84 | class Test_magic_run_completer_nonascii(unittest.TestCase): |
|
78 | 85 | @onlyif_unicode_paths |
|
79 | 86 | def setUp(self): |
|
80 | 87 | self.BASETESTDIR = tempfile.mkdtemp() |
|
81 | 88 | for fil in [u"aaø.py", u"a.py", u"b.py"]: |
|
82 | 89 | with open(join(self.BASETESTDIR, fil), "w") as sfile: |
|
83 | 90 | sfile.write("pass\n") |
|
84 | 91 | self.oldpath = py3compat.getcwd() |
|
85 | 92 | os.chdir(self.BASETESTDIR) |
|
86 | 93 | |
|
87 | 94 | def tearDown(self): |
|
88 | 95 | os.chdir(self.oldpath) |
|
89 | 96 | shutil.rmtree(self.BASETESTDIR) |
|
90 | 97 | |
|
91 | 98 | @onlyif_unicode_paths |
|
92 | 99 | def test_1(self): |
|
93 | 100 | """Test magic_run_completer, should match two alterntives |
|
94 | 101 | """ |
|
95 | 102 | event = MockEvent(u"%run a") |
|
96 | 103 | mockself = None |
|
97 | 104 | match = set(magic_run_completer(mockself, event)) |
|
98 | 105 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) |
|
99 | 106 | |
|
100 | 107 | @onlyif_unicode_paths |
|
101 | 108 | def test_2(self): |
|
102 | 109 | """Test magic_run_completer, should match one alterntive |
|
103 | 110 | """ |
|
104 | 111 | event = MockEvent(u"%run aa") |
|
105 | 112 | mockself = None |
|
106 | 113 | match = set(magic_run_completer(mockself, event)) |
|
107 | 114 | self.assertEqual(match, set([u"aaø.py"])) |
|
108 | 115 | |
|
109 | 116 | @onlyif_unicode_paths |
|
110 | 117 | def test_3(self): |
|
111 | 118 | """Test magic_run_completer with unterminated " """ |
|
112 | 119 | event = MockEvent(u'%run "a') |
|
113 | 120 | mockself = None |
|
114 | 121 | match = set(magic_run_completer(mockself, event)) |
|
115 | 122 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) |
|
116 | 123 | |
|
117 | 124 | # module_completer: |
|
118 | 125 | |
|
119 | 126 | def test_import_invalid_module(): |
|
120 | 127 | """Testing of issue https://github.com/ipython/ipython/issues/1107""" |
|
121 | 128 | invalid_module_names = set(['foo-bar', 'foo:bar', '10foo']) |
|
122 | 129 | valid_module_names = set(['foobar']) |
|
123 | 130 | with TemporaryDirectory() as tmpdir: |
|
124 | 131 | sys.path.insert( 0, tmpdir ) |
|
125 | 132 | for name in invalid_module_names | valid_module_names: |
|
126 | 133 | filename = os.path.join(tmpdir, name + '.py') |
|
127 | 134 | open(filename, 'w').close() |
|
128 | 135 | |
|
129 | 136 | s = set( module_completion('import foo') ) |
|
130 | 137 | intersection = s.intersection(invalid_module_names) |
|
131 | 138 | nt.assert_equal(intersection, set()) |
|
132 | 139 | |
|
133 | 140 | assert valid_module_names.issubset(s), valid_module_names.intersection(s) |
General Comments 0
You need to be logged in to leave comments.
Login now