Show More
@@ -1,95 +1,101 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 | import nose.tools as nt |
|
7 | import nose.tools as nt | |
8 |
|
8 | |||
9 | from IPython.testing import tools as tt, decorators as dec |
|
9 | from IPython.testing import tools as tt, decorators as dec | |
10 | from IPython.core.prompts import PromptManager, LazyEvaluate |
|
10 | from IPython.core.prompts import PromptManager, LazyEvaluate | |
11 | from IPython.testing.globalipapp import get_ipython |
|
11 | from IPython.testing.globalipapp import get_ipython | |
12 | from IPython.utils import py3compat |
|
12 | from IPython.utils import py3compat | |
13 | from IPython.utils.tempdir import TemporaryDirectory |
|
13 | from IPython.utils.tempdir import TemporaryDirectory | |
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.assertEquals(prompt, u'In [bar]') |
|
50 | self.assertEquals(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.assertEquals(prompt, u"In [%r]" % int) |
|
56 | self.assertEquals(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.assertEquals(prompt, u"In [<ERROR: 'foo_dne' not found>]") |
|
62 | self.assertEquals(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 | def test_render_unicode_cwd(self): |
|
68 | def test_render_unicode_cwd(self): | |
69 | save = os.getcwdu() |
|
69 | save = os.getcwdu() | |
70 | with TemporaryDirectory(u'ünicødé') as td: |
|
70 | with TemporaryDirectory(u'ünicødé') as td: | |
71 | os.chdir(td) |
|
71 | os.chdir(td) | |
72 | self.pm.in_template = r'\w [\#]' |
|
72 | self.pm.in_template = r'\w [\#]' | |
73 | p = self.pm.render('in', color=False) |
|
73 | p = self.pm.render('in', color=False) | |
74 | self.assertEquals(p, u"%s [%i]" % (os.getcwdu(), ip.execution_count)) |
|
74 | self.assertEquals(p, u"%s [%i]" % (os.getcwdu(), ip.execution_count)) | |
75 | os.chdir(save) |
|
75 | os.chdir(save) | |
76 |
|
76 | |||
77 | def test_lazy_eval_unicode(self): |
|
77 | def test_lazy_eval_unicode(self): | |
78 | u = u'ünicødé' |
|
78 | u = u'ünicødé' | |
79 | lz = LazyEvaluate(lambda : u) |
|
79 | lz = LazyEvaluate(lambda : u) | |
80 | self.assertEquals(str(lz), py3compat.unicode_to_str(u)) |
|
80 | self.assertEquals(str(lz), py3compat.unicode_to_str(u)) | |
81 | self.assertEquals(unicode(lz), u) |
|
81 | self.assertEquals(unicode(lz), u) | |
82 | self.assertEquals(format(lz), u) |
|
82 | self.assertEquals(format(lz), u) | |
83 |
|
83 | |||
84 | def test_lazy_eval_nonascii_bytes(self): |
|
84 | def test_lazy_eval_nonascii_bytes(self): | |
85 | u = u'ünicødé' |
|
85 | u = u'ünicødé' | |
86 | b = u.encode('utf8') |
|
86 | b = u.encode('utf8') | |
87 | lz = LazyEvaluate(lambda : b) |
|
87 | lz = LazyEvaluate(lambda : b) | |
88 | if py3compat.PY3: |
|
88 | self.assertEquals(str(lz), str(b)) | |
89 |
|
|
89 | self.assertEquals(format(lz), str(b)) | |
90 | self.assertEquals(format(lz), str(b)) |
|
90 | if not py3compat.PY3: | |
91 | else: |
|
|||
92 | self.assertEquals(str(lz), b) |
|
|||
93 | self.assertRaises(UnicodeDecodeError, unicode, lz) |
|
91 | self.assertRaises(UnicodeDecodeError, unicode, lz) | |
94 | self.assertRaises(UnicodeDecodeError, format, lz) |
|
92 | ||
|
93 | def test_lazy_eval_float(self): | |||
|
94 | f = 0.503 | |||
|
95 | lz = LazyEvaluate(lambda : f) | |||
|
96 | ||||
|
97 | self.assertEquals(str(lz), str(f)) | |||
|
98 | self.assertEquals(unicode(lz), unicode(f)) | |||
|
99 | self.assertEquals(format(lz), str(f)) | |||
|
100 | self.assertEquals(format(lz, '.1'), '0.5') | |||
95 |
|
101 |
General Comments 0
You need to be logged in to leave comments.
Login now