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