##// END OF EJS Templates
Ross Jones -
Show More
@@ -1,67 +1,79 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 from nose import SkipTest
19 from nose import SkipTest
20
20
21 from IPython.core.completerlib import magic_run_completer
21 from IPython.core.completerlib import magic_run_completer, module_completion
22 from IPython.utils import py3compat
22 from IPython.utils import py3compat
23 from IPython.utils.tempdir import TemporaryDirectory
23
24
24
25
25 class MockEvent(object):
26 class MockEvent(object):
26 def __init__(self, line):
27 def __init__(self, line):
27 self.line = line
28 self.line = line
28
29
29 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
30 # Test functions begin
31 # Test functions begin
31 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
32 class Test_magic_run_completer(unittest.TestCase):
33 class Test_magic_run_completer(unittest.TestCase):
33 def setUp(self):
34 def setUp(self):
34 self.BASETESTDIR = tempfile.mkdtemp()
35 self.BASETESTDIR = tempfile.mkdtemp()
35 for fil in [u"aaø.py", u"a.py", u"b.py"]:
36 for fil in [u"aaø.py", u"a.py", u"b.py"]:
36 with open(join(self.BASETESTDIR, fil), "w") as sfile:
37 with open(join(self.BASETESTDIR, fil), "w") as sfile:
37 sfile.write("pass\n")
38 sfile.write("pass\n")
38 self.oldpath = os.getcwdu()
39 self.oldpath = os.getcwdu()
39 os.chdir(self.BASETESTDIR)
40 os.chdir(self.BASETESTDIR)
40
41
41 def tearDown(self):
42 def tearDown(self):
42 os.chdir(self.oldpath)
43 os.chdir(self.oldpath)
43 shutil.rmtree(self.BASETESTDIR)
44 shutil.rmtree(self.BASETESTDIR)
44
45
45 def test_1(self):
46 def test_1(self):
46 """Test magic_run_completer, should match two alterntives
47 """Test magic_run_completer, should match two alterntives
47 """
48 """
48 event = MockEvent(u"%run a")
49 event = MockEvent(u"%run a")
49 mockself = None
50 mockself = None
50 match = set(magic_run_completer(mockself, event))
51 match = set(magic_run_completer(mockself, event))
51 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
52 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
52
53
53 def test_2(self):
54 def test_2(self):
54 """Test magic_run_completer, should match one alterntive
55 """Test magic_run_completer, should match one alterntive
55 """
56 """
56 event = MockEvent(u"%run aa")
57 event = MockEvent(u"%run aa")
57 mockself = None
58 mockself = None
58 match = set(magic_run_completer(mockself, event))
59 match = set(magic_run_completer(mockself, event))
59 self.assertEqual(match, set([u"aaø.py"]))
60 self.assertEqual(match, set([u"aaø.py"]))
60
61
61 def test_3(self):
62 def test_3(self):
62 """Test magic_run_completer with unterminated " """
63 """Test magic_run_completer with unterminated " """
63 event = MockEvent(u'%run "a')
64 event = MockEvent(u'%run "a')
64 mockself = None
65 mockself = None
65 match = set(magic_run_completer(mockself, event))
66 match = set(magic_run_completer(mockself, event))
66 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
67 self.assertEqual(match, set([u"a.py", u"aaø.py"]))
67
68
69 def test_import_invalid_module(self):
70 """Testing of issue https://github.com/ipython/ipython/issues/1107"""
71 invalid_module_names = set(['foo-bar', 'foo:bar', '10foo'])
72 with TemporaryDirectory() as tmpdir:
73 sys.path.insert( 0, tmpdir )
74 for name in invalid_module_names:
75 filename = os.path.join(tmpdir, name + '.py')
76 open(filename, 'w').close()
77
78 s = set( module_completion('import foo') )
79 self.assertFalse( s.intersection(invalid_module_names) )
General Comments 0
You need to be logged in to leave comments. Login now