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