##// END OF EJS Templates
use TemporaryDirectory ctx manager in test_render_unicode_cwd
MinRK -
Show More
@@ -1,77 +1,74 b''
1 # -*- coding: utf-8
1 # -*- coding: utf-8
2 """Tests for prompt generation."""
2 """Tests for prompt generation."""
3
3
4 import tempfile
5 import unittest
4 import unittest
6
5
7 import os
6 import os
8 import nose.tools as nt
7 import nose.tools as nt
9
8
10 from IPython.testing import tools as tt, decorators as dec
9 from IPython.testing import tools as tt, decorators as dec
11 from IPython.core.prompts import PromptManager
10 from IPython.core.prompts import PromptManager
12 from IPython.testing.globalipapp import get_ipython
11 from IPython.testing.globalipapp import get_ipython
12 from IPython.utils.tempdir import TemporaryDirectory
13
13
14 ip = get_ipython()
14 ip = get_ipython()
15
15
16
16
17 class PromptTests(unittest.TestCase):
17 class PromptTests(unittest.TestCase):
18 def setUp(self):
18 def setUp(self):
19 self.pm = PromptManager(shell=ip, config=ip.config)
19 self.pm = PromptManager(shell=ip, config=ip.config)
20
20
21 def test_multiline_prompt(self):
21 def test_multiline_prompt(self):
22 self.pm.in_template = "[In]\n>>>"
22 self.pm.in_template = "[In]\n>>>"
23 self.pm.render('in')
23 self.pm.render('in')
24 self.assertEqual(self.pm.width, 3)
24 self.assertEqual(self.pm.width, 3)
25 self.assertEqual(self.pm.txtwidth, 3)
25 self.assertEqual(self.pm.txtwidth, 3)
26
26
27 self.pm.in_template = '[In]\n'
27 self.pm.in_template = '[In]\n'
28 self.pm.render('in')
28 self.pm.render('in')
29 self.assertEqual(self.pm.width, 0)
29 self.assertEqual(self.pm.width, 0)
30 self.assertEqual(self.pm.txtwidth, 0)
30 self.assertEqual(self.pm.txtwidth, 0)
31
31
32 def test_translate_abbreviations(self):
32 def test_translate_abbreviations(self):
33 def do_translate(template):
33 def do_translate(template):
34 self.pm.in_template = template
34 self.pm.in_template = template
35 return self.pm.templates['in']
35 return self.pm.templates['in']
36
36
37 pairs = [(r'%n>', '{color.number}{count}{color.prompt}>'),
37 pairs = [(r'%n>', '{color.number}{count}{color.prompt}>'),
38 (r'\T', '{time}'),
38 (r'\T', '{time}'),
39 (r'\n', '\n')
39 (r'\n', '\n')
40 ]
40 ]
41
41
42 tt.check_pairs(do_translate, pairs)
42 tt.check_pairs(do_translate, pairs)
43
43
44 def test_user_ns(self):
44 def test_user_ns(self):
45 self.pm.color_scheme = 'NoColor'
45 self.pm.color_scheme = 'NoColor'
46 ip.ex("foo='bar'")
46 ip.ex("foo='bar'")
47 self.pm.in_template = "In [{foo}]"
47 self.pm.in_template = "In [{foo}]"
48 prompt = self.pm.render('in')
48 prompt = self.pm.render('in')
49 self.assertEquals(prompt, u'In [bar]')
49 self.assertEquals(prompt, u'In [bar]')
50
50
51 def test_builtins(self):
51 def test_builtins(self):
52 self.pm.color_scheme = 'NoColor'
52 self.pm.color_scheme = 'NoColor'
53 self.pm.in_template = "In [{int}]"
53 self.pm.in_template = "In [{int}]"
54 prompt = self.pm.render('in')
54 prompt = self.pm.render('in')
55 self.assertEquals(prompt, u"In [%r]" % int)
55 self.assertEquals(prompt, u"In [%r]" % int)
56
56
57 def test_undefined(self):
57 def test_undefined(self):
58 self.pm.color_scheme = 'NoColor'
58 self.pm.color_scheme = 'NoColor'
59 self.pm.in_template = "In [{foo_dne}]"
59 self.pm.in_template = "In [{foo_dne}]"
60 prompt = self.pm.render('in')
60 prompt = self.pm.render('in')
61 self.assertEquals(prompt, u"In [<ERROR: 'foo_dne' not found>]")
61 self.assertEquals(prompt, u"In [<ERROR: 'foo_dne' not found>]")
62
62
63 def test_render(self):
63 def test_render(self):
64 self.pm.in_template = r'\#>'
64 self.pm.in_template = r'\#>'
65 self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count)
65 self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count)
66
66
67 def test_render_unicode(self):
67 def test_render_unicode_cwd(self):
68 td = tempfile.mkdtemp(u'ünicødé')
69 save = os.getcwdu()
68 save = os.getcwdu()
70 os.chdir(td)
69 with TemporaryDirectory(u'ünicødé') as td:
71 self.pm.in_template = r'\w [\#]'
70 os.chdir(td)
72 try:
71 self.pm.in_template = r'\w [\#]'
73 p = self.pm.render('in', color=False)
72 p = self.pm.render('in', color=False)
74 self.assertEquals(p, u"%s [%i]" % (os.getcwdu(), ip.execution_count))
73 self.assertEquals(p, u"%s [%i]" % (os.getcwdu(), ip.execution_count))
75 finally:
74 os.chdir(save)
76 os.chdir(save)
77 os.rmdir(td)
General Comments 0
You need to be logged in to leave comments. Login now