Show More
@@ -1,133 +1,140 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"] | |||
|
35 | ||||
34 | def setUp(self): |
|
36 | def setUp(self): | |
35 | self.BASETESTDIR = tempfile.mkdtemp() |
|
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 | with open(join(self.BASETESTDIR, fil), "w") as sfile: |
|
39 | with open(join(self.BASETESTDIR, fil), "w") as sfile: | |
38 | sfile.write("pass\n") |
|
40 | sfile.write("pass\n") | |
39 | self.oldpath = py3compat.getcwd() |
|
41 | self.oldpath = py3compat.getcwd() | |
40 | os.chdir(self.BASETESTDIR) |
|
42 | os.chdir(self.BASETESTDIR) | |
41 |
|
43 | |||
42 | def tearDown(self): |
|
44 | def tearDown(self): | |
43 | os.chdir(self.oldpath) |
|
45 | os.chdir(self.oldpath) | |
44 | shutil.rmtree(self.BASETESTDIR) |
|
46 | shutil.rmtree(self.BASETESTDIR) | |
45 |
|
47 | |||
46 | def test_1(self): |
|
48 | def test_1(self): | |
47 | """Test magic_run_completer, should match two alterntives |
|
49 | """Test magic_run_completer, should match two alterntives | |
48 | """ |
|
50 | """ | |
49 | event = MockEvent(u"%run a") |
|
51 | event = MockEvent(u"%run a") | |
50 | mockself = None |
|
52 | mockself = None | |
51 | match = set(magic_run_completer(mockself, event)) |
|
53 | match = set(magic_run_completer(mockself, event)) | |
52 | self.assertEqual(match, set([u"a.py", u"aao.py"])) |
|
54 | self.assertEqual(match, set([u"a.py", u"aao.py"])) | |
53 |
|
55 | |||
54 | def test_2(self): |
|
56 | def test_2(self): | |
55 | """Test magic_run_completer, should match one alterntive |
|
57 | """Test magic_run_completer, should match one alterntive | |
56 | """ |
|
58 | """ | |
57 | event = MockEvent(u"%run aa") |
|
59 | event = MockEvent(u"%run aa") | |
58 | mockself = None |
|
60 | mockself = None | |
59 | match = set(magic_run_completer(mockself, event)) |
|
61 | match = set(magic_run_completer(mockself, event)) | |
60 | self.assertEqual(match, set([u"aao.py"])) |
|
62 | self.assertEqual(match, set([u"aao.py"])) | |
61 |
|
63 | |||
62 | def test_3(self): |
|
64 | def test_3(self): | |
63 | """Test magic_run_completer with unterminated " """ |
|
65 | """Test magic_run_completer with unterminated " """ | |
64 | event = MockEvent(u'%run "a') |
|
66 | event = MockEvent(u'%run "a') | |
65 | mockself = None |
|
67 | mockself = None | |
66 | match = set(magic_run_completer(mockself, event)) |
|
68 | match = set(magic_run_completer(mockself, event)) | |
67 | self.assertEqual(match, set([u"a.py", u"aao.py"])) |
|
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 | def test_completion_in_dir(self): |
|
76 | def test_completion_in_dir(self): | |
70 | # Github issue #3459 |
|
77 | # Github issue #3459 | |
71 | event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a'))) |
|
78 | event = MockEvent(u'%run a.py {}'.format(join(self.BASETESTDIR, 'a'))) | |
72 | print(repr(event.line)) |
|
79 | print(repr(event.line)) | |
73 | match = set(magic_run_completer(None, event)) |
|
80 | match = set(magic_run_completer(None, event)) | |
74 | self.assertEqual(match, |
|
81 | self.assertEqual(match, | |
75 | {join(self.BASETESTDIR, f) for f in (u'a.py', u'aao.py', u'aao.txt')}) |
|
82 | {join(self.BASETESTDIR, f) for f in (u'a.py', u'aao.py', u'aao.txt')}) | |
76 |
|
83 | |||
77 | class Test_magic_run_completer_nonascii(unittest.TestCase): |
|
84 | class Test_magic_run_completer_nonascii(unittest.TestCase): | |
78 | @onlyif_unicode_paths |
|
85 | @onlyif_unicode_paths | |
79 | def setUp(self): |
|
86 | def setUp(self): | |
80 | self.BASETESTDIR = tempfile.mkdtemp() |
|
87 | self.BASETESTDIR = tempfile.mkdtemp() | |
81 | for fil in [u"aaø.py", u"a.py", u"b.py"]: |
|
88 | for fil in [u"aaø.py", u"a.py", u"b.py"]: | |
82 | with open(join(self.BASETESTDIR, fil), "w") as sfile: |
|
89 | with open(join(self.BASETESTDIR, fil), "w") as sfile: | |
83 | sfile.write("pass\n") |
|
90 | sfile.write("pass\n") | |
84 | self.oldpath = py3compat.getcwd() |
|
91 | self.oldpath = py3compat.getcwd() | |
85 | os.chdir(self.BASETESTDIR) |
|
92 | os.chdir(self.BASETESTDIR) | |
86 |
|
93 | |||
87 | def tearDown(self): |
|
94 | def tearDown(self): | |
88 | os.chdir(self.oldpath) |
|
95 | os.chdir(self.oldpath) | |
89 | shutil.rmtree(self.BASETESTDIR) |
|
96 | shutil.rmtree(self.BASETESTDIR) | |
90 |
|
97 | |||
91 | @onlyif_unicode_paths |
|
98 | @onlyif_unicode_paths | |
92 | def test_1(self): |
|
99 | def test_1(self): | |
93 | """Test magic_run_completer, should match two alterntives |
|
100 | """Test magic_run_completer, should match two alterntives | |
94 | """ |
|
101 | """ | |
95 | event = MockEvent(u"%run a") |
|
102 | event = MockEvent(u"%run a") | |
96 | mockself = None |
|
103 | mockself = None | |
97 | match = set(magic_run_completer(mockself, event)) |
|
104 | match = set(magic_run_completer(mockself, event)) | |
98 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) |
|
105 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) | |
99 |
|
106 | |||
100 | @onlyif_unicode_paths |
|
107 | @onlyif_unicode_paths | |
101 | def test_2(self): |
|
108 | def test_2(self): | |
102 | """Test magic_run_completer, should match one alterntive |
|
109 | """Test magic_run_completer, should match one alterntive | |
103 | """ |
|
110 | """ | |
104 | event = MockEvent(u"%run aa") |
|
111 | event = MockEvent(u"%run aa") | |
105 | mockself = None |
|
112 | mockself = None | |
106 | match = set(magic_run_completer(mockself, event)) |
|
113 | match = set(magic_run_completer(mockself, event)) | |
107 | self.assertEqual(match, set([u"aaø.py"])) |
|
114 | self.assertEqual(match, set([u"aaø.py"])) | |
108 |
|
115 | |||
109 | @onlyif_unicode_paths |
|
116 | @onlyif_unicode_paths | |
110 | def test_3(self): |
|
117 | def test_3(self): | |
111 | """Test magic_run_completer with unterminated " """ |
|
118 | """Test magic_run_completer with unterminated " """ | |
112 | event = MockEvent(u'%run "a') |
|
119 | event = MockEvent(u'%run "a') | |
113 | mockself = None |
|
120 | mockself = None | |
114 | match = set(magic_run_completer(mockself, event)) |
|
121 | match = set(magic_run_completer(mockself, event)) | |
115 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) |
|
122 | self.assertEqual(match, set([u"a.py", u"aaø.py"])) | |
116 |
|
123 | |||
117 | # module_completer: |
|
124 | # module_completer: | |
118 |
|
125 | |||
119 | def test_import_invalid_module(): |
|
126 | def test_import_invalid_module(): | |
120 | """Testing of issue https://github.com/ipython/ipython/issues/1107""" |
|
127 | """Testing of issue https://github.com/ipython/ipython/issues/1107""" | |
121 | invalid_module_names = set(['foo-bar', 'foo:bar', '10foo']) |
|
128 | invalid_module_names = set(['foo-bar', 'foo:bar', '10foo']) | |
122 | valid_module_names = set(['foobar']) |
|
129 | valid_module_names = set(['foobar']) | |
123 | with TemporaryDirectory() as tmpdir: |
|
130 | with TemporaryDirectory() as tmpdir: | |
124 | sys.path.insert( 0, tmpdir ) |
|
131 | sys.path.insert( 0, tmpdir ) | |
125 | for name in invalid_module_names | valid_module_names: |
|
132 | for name in invalid_module_names | valid_module_names: | |
126 | filename = os.path.join(tmpdir, name + '.py') |
|
133 | filename = os.path.join(tmpdir, name + '.py') | |
127 | open(filename, 'w').close() |
|
134 | open(filename, 'w').close() | |
128 |
|
135 | |||
129 | s = set( module_completion('import foo') ) |
|
136 | s = set( module_completion('import foo') ) | |
130 | intersection = s.intersection(invalid_module_names) |
|
137 | intersection = s.intersection(invalid_module_names) | |
131 | nt.assert_equal(intersection, set()) |
|
138 | nt.assert_equal(intersection, set()) | |
132 |
|
139 | |||
133 | assert valid_module_names.issubset(s), valid_module_names.intersection(s) |
|
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