##// END OF EJS Templates
Remove dependence on execution_count in test_width
Aaron Meurer -
Show More
@@ -1,129 +1,129 b''
1 # -*- coding: utf-8
1 # -*- coding: utf-8
2 """Tests for prompt generation."""
2 """Tests for prompt generation."""
3
3
4 import unittest
4 import unittest
5
5
6 import os
6 import os
7
7
8 from IPython.testing import tools as tt, decorators as dec
8 from IPython.testing import tools as tt, decorators as dec
9 from IPython.core.prompts import PromptManager, LazyEvaluate, _invisible_characters
9 from IPython.core.prompts import PromptManager, LazyEvaluate, _invisible_characters
10 from IPython.testing.globalipapp import get_ipython
10 from IPython.testing.globalipapp import get_ipython
11 from IPython.utils.tempdir import TemporaryWorkingDirectory
11 from IPython.utils.tempdir import TemporaryWorkingDirectory
12 from IPython.utils import py3compat
12 from IPython.utils import py3compat
13 from IPython.utils.py3compat import unicode_type
13 from IPython.utils.py3compat import unicode_type
14
14
15 ip = get_ipython()
15 ip = get_ipython()
16
16
17
17
18 class PromptTests(unittest.TestCase):
18 class PromptTests(unittest.TestCase):
19 def setUp(self):
19 def setUp(self):
20 self.pm = PromptManager(shell=ip, config=ip.config)
20 self.pm = PromptManager(shell=ip, config=ip.config)
21
21
22 def test_multiline_prompt(self):
22 def test_multiline_prompt(self):
23 self.pm.in_template = "[In]\n>>>"
23 self.pm.in_template = "[In]\n>>>"
24 self.pm.render('in')
24 self.pm.render('in')
25 self.assertEqual(self.pm.width, 3)
25 self.assertEqual(self.pm.width, 3)
26 self.assertEqual(self.pm.txtwidth, 3)
26 self.assertEqual(self.pm.txtwidth, 3)
27
27
28 self.pm.in_template = '[In]\n'
28 self.pm.in_template = '[In]\n'
29 self.pm.render('in')
29 self.pm.render('in')
30 self.assertEqual(self.pm.width, 0)
30 self.assertEqual(self.pm.width, 0)
31 self.assertEqual(self.pm.txtwidth, 0)
31 self.assertEqual(self.pm.txtwidth, 0)
32
32
33 def test_translate_abbreviations(self):
33 def test_translate_abbreviations(self):
34 def do_translate(template):
34 def do_translate(template):
35 self.pm.in_template = template
35 self.pm.in_template = template
36 return self.pm.templates['in']
36 return self.pm.templates['in']
37
37
38 pairs = [(r'%n>', '{color.number}{count}{color.prompt}>'),
38 pairs = [(r'%n>', '{color.number}{count}{color.prompt}>'),
39 (r'\T', '{time}'),
39 (r'\T', '{time}'),
40 (r'\n', '\n')
40 (r'\n', '\n')
41 ]
41 ]
42
42
43 tt.check_pairs(do_translate, pairs)
43 tt.check_pairs(do_translate, pairs)
44
44
45 def test_user_ns(self):
45 def test_user_ns(self):
46 self.pm.color_scheme = 'NoColor'
46 self.pm.color_scheme = 'NoColor'
47 ip.ex("foo='bar'")
47 ip.ex("foo='bar'")
48 self.pm.in_template = "In [{foo}]"
48 self.pm.in_template = "In [{foo}]"
49 prompt = self.pm.render('in')
49 prompt = self.pm.render('in')
50 self.assertEqual(prompt, u'In [bar]')
50 self.assertEqual(prompt, u'In [bar]')
51
51
52 def test_builtins(self):
52 def test_builtins(self):
53 self.pm.color_scheme = 'NoColor'
53 self.pm.color_scheme = 'NoColor'
54 self.pm.in_template = "In [{int}]"
54 self.pm.in_template = "In [{int}]"
55 prompt = self.pm.render('in')
55 prompt = self.pm.render('in')
56 self.assertEqual(prompt, u"In [%r]" % int)
56 self.assertEqual(prompt, u"In [%r]" % int)
57
57
58 def test_undefined(self):
58 def test_undefined(self):
59 self.pm.color_scheme = 'NoColor'
59 self.pm.color_scheme = 'NoColor'
60 self.pm.in_template = "In [{foo_dne}]"
60 self.pm.in_template = "In [{foo_dne}]"
61 prompt = self.pm.render('in')
61 prompt = self.pm.render('in')
62 self.assertEqual(prompt, u"In [<ERROR: 'foo_dne' not found>]")
62 self.assertEqual(prompt, u"In [<ERROR: 'foo_dne' not found>]")
63
63
64 def test_render(self):
64 def test_render(self):
65 self.pm.in_template = r'\#>'
65 self.pm.in_template = r'\#>'
66 self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count)
66 self.assertEqual(self.pm.render('in',color=False), '%d>' % ip.execution_count)
67
67
68 @dec.onlyif_unicode_paths
68 @dec.onlyif_unicode_paths
69 def test_render_unicode_cwd(self):
69 def test_render_unicode_cwd(self):
70 with TemporaryWorkingDirectory(u'ΓΌnicΓΈdΓ©'):
70 with TemporaryWorkingDirectory(u'ΓΌnicΓΈdΓ©'):
71 self.pm.in_template = r'\w [\#]'
71 self.pm.in_template = r'\w [\#]'
72 p = self.pm.render('in', color=False)
72 p = self.pm.render('in', color=False)
73 self.assertEqual(p, u"%s [%i]" % (py3compat.getcwd(), ip.execution_count))
73 self.assertEqual(p, u"%s [%i]" % (py3compat.getcwd(), ip.execution_count))
74
74
75 def test_lazy_eval_unicode(self):
75 def test_lazy_eval_unicode(self):
76 u = u'ΓΌnicΓΈdΓ©'
76 u = u'ΓΌnicΓΈdΓ©'
77 lz = LazyEvaluate(lambda : u)
77 lz = LazyEvaluate(lambda : u)
78 # str(lz) would fail
78 # str(lz) would fail
79 self.assertEqual(unicode_type(lz), u)
79 self.assertEqual(unicode_type(lz), u)
80 self.assertEqual(format(lz), u)
80 self.assertEqual(format(lz), u)
81
81
82 def test_lazy_eval_nonascii_bytes(self):
82 def test_lazy_eval_nonascii_bytes(self):
83 u = u'ΓΌnicΓΈdΓ©'
83 u = u'ΓΌnicΓΈdΓ©'
84 b = u.encode('utf8')
84 b = u.encode('utf8')
85 lz = LazyEvaluate(lambda : b)
85 lz = LazyEvaluate(lambda : b)
86 # unicode(lz) would fail
86 # unicode(lz) would fail
87 self.assertEqual(str(lz), str(b))
87 self.assertEqual(str(lz), str(b))
88 self.assertEqual(format(lz), str(b))
88 self.assertEqual(format(lz), str(b))
89
89
90 def test_lazy_eval_float(self):
90 def test_lazy_eval_float(self):
91 f = 0.503
91 f = 0.503
92 lz = LazyEvaluate(lambda : f)
92 lz = LazyEvaluate(lambda : f)
93
93
94 self.assertEqual(str(lz), str(f))
94 self.assertEqual(str(lz), str(f))
95 self.assertEqual(unicode_type(lz), unicode_type(f))
95 self.assertEqual(unicode_type(lz), unicode_type(f))
96 self.assertEqual(format(lz), str(f))
96 self.assertEqual(format(lz), str(f))
97 self.assertEqual(format(lz, '.1'), '0.5')
97 self.assertEqual(format(lz, '.1'), '0.5')
98
98
99 @dec.skip_win32
99 @dec.skip_win32
100 def test_cwd_x(self):
100 def test_cwd_x(self):
101 self.pm.in_template = r"\X0"
101 self.pm.in_template = r"\X0"
102 save = py3compat.getcwd()
102 save = py3compat.getcwd()
103 os.chdir(os.path.expanduser('~'))
103 os.chdir(os.path.expanduser('~'))
104 p = self.pm.render('in', color=False)
104 p = self.pm.render('in', color=False)
105 try:
105 try:
106 self.assertEqual(p, '~')
106 self.assertEqual(p, '~')
107 finally:
107 finally:
108 os.chdir(save)
108 os.chdir(save)
109
109
110 def test_invisible_chars(self):
110 def test_invisible_chars(self):
111 self.assertEqual(_invisible_characters('abc'), 0)
111 self.assertEqual(_invisible_characters('abc'), 0)
112 self.assertEqual(_invisible_characters('\001\033[1;37m\002'), 9)
112 self.assertEqual(_invisible_characters('\001\033[1;37m\002'), 9)
113 # Sequences must be between \001 and \002 to be counted
113 # Sequences must be between \001 and \002 to be counted
114 self.assertEqual(_invisible_characters('\033[1;37m'), 0)
114 self.assertEqual(_invisible_characters('\033[1;37m'), 0)
115 # Test custom escape sequences
115 # Test custom escape sequences
116 self.assertEqual(_invisible_characters('\001\033]133;A\a\002'), 10)
116 self.assertEqual(_invisible_characters('\001\033]133;A\a\002'), 10)
117
117
118 def test_width(self):
118 def test_width(self):
119 default_in = '\x01\x1b]133;A\x07\x02In [\\#]: \x01\x1b]133;B\x07\x02'
119 default_in = '\x01\x1b]133;A\x07\x02In [1]: \x01\x1b]133;B\x07\x02'
120 self.pm.in_template = default_in
120 self.pm.in_template = default_in
121 self.pm.render('in')
121 self.pm.render('in')
122 self.assertEqual(self.pm.width, 8)
122 self.assertEqual(self.pm.width, 8)
123 self.assertEqual(self.pm.txtwidth, 8)
123 self.assertEqual(self.pm.txtwidth, 8)
124
124
125 # Test custom escape sequences
125 # Test custom escape sequences
126 self.pm.in_template = '\001\033]133;A\a\002' + default_in + '\001\033]133;B\a\002'
126 self.pm.in_template = '\001\033]133;A\a\002' + default_in + '\001\033]133;B\a\002'
127 self.pm.render('in')
127 self.pm.render('in')
128 self.assertEqual(self.pm.width, 8)
128 self.assertEqual(self.pm.width, 8)
129 self.assertEqual(self.pm.txtwidth, 8)
129 self.assertEqual(self.pm.txtwidth, 8)
General Comments 0
You need to be logged in to leave comments. Login now