##// END OF EJS Templates
TST: add future unicode_literals test (#786)
Olivier Verdier -
Show More
@@ -1,135 +1,148 b''
1 """Tests for the key interactiveshell module.
1 """Tests for the key interactiveshell module.
2
2
3 Historically the main classes in interactiveshell have been under-tested. This
3 Historically the main classes in interactiveshell have been under-tested. This
4 module should grow as many single-method tests as possible to trap many of the
4 module should grow as many single-method tests as possible to trap many of the
5 recurring bugs we seem to encounter with high-level interaction.
5 recurring bugs we seem to encounter with high-level interaction.
6
6
7 Authors
7 Authors
8 -------
8 -------
9 * Fernando Perez
9 * Fernando Perez
10 """
10 """
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2011 The IPython Development Team
12 # Copyright (C) 2011 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # stdlib
21 # stdlib
22 import unittest
22 import unittest
23 from StringIO import StringIO
23 from StringIO import StringIO
24
24
25 from IPython.testing import decorators as dec
25 from IPython.testing import decorators as dec
26 from IPython.utils import io
26 from IPython.utils import io
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Tests
29 # Tests
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 class InteractiveShellTestCase(unittest.TestCase):
32 class InteractiveShellTestCase(unittest.TestCase):
33 def test_naked_string_cells(self):
33 def test_naked_string_cells(self):
34 """Test that cells with only naked strings are fully executed"""
34 """Test that cells with only naked strings are fully executed"""
35 ip = get_ipython()
35 ip = get_ipython()
36 # First, single-line inputs
36 # First, single-line inputs
37 ip.run_cell('"a"\n')
37 ip.run_cell('"a"\n')
38 self.assertEquals(ip.user_ns['_'], 'a')
38 self.assertEquals(ip.user_ns['_'], 'a')
39 # And also multi-line cells
39 # And also multi-line cells
40 ip.run_cell('"""a\nb"""\n')
40 ip.run_cell('"""a\nb"""\n')
41 self.assertEquals(ip.user_ns['_'], 'a\nb')
41 self.assertEquals(ip.user_ns['_'], 'a\nb')
42
42
43 def test_run_empty_cell(self):
43 def test_run_empty_cell(self):
44 """Just make sure we don't get a horrible error with a blank
44 """Just make sure we don't get a horrible error with a blank
45 cell of input. Yes, I did overlook that."""
45 cell of input. Yes, I did overlook that."""
46 ip = get_ipython()
46 ip = get_ipython()
47 old_xc = ip.execution_count
47 old_xc = ip.execution_count
48 ip.run_cell('')
48 ip.run_cell('')
49 self.assertEquals(ip.execution_count, old_xc)
49 self.assertEquals(ip.execution_count, old_xc)
50
50
51 def test_run_cell_multiline(self):
51 def test_run_cell_multiline(self):
52 """Multi-block, multi-line cells must execute correctly.
52 """Multi-block, multi-line cells must execute correctly.
53 """
53 """
54 ip = get_ipython()
54 ip = get_ipython()
55 src = '\n'.join(["x=1",
55 src = '\n'.join(["x=1",
56 "y=2",
56 "y=2",
57 "if 1:",
57 "if 1:",
58 " x += 1",
58 " x += 1",
59 " y += 1",])
59 " y += 1",])
60 ip.run_cell(src)
60 ip.run_cell(src)
61 self.assertEquals(ip.user_ns['x'], 2)
61 self.assertEquals(ip.user_ns['x'], 2)
62 self.assertEquals(ip.user_ns['y'], 3)
62 self.assertEquals(ip.user_ns['y'], 3)
63
63
64 def test_multiline_string_cells(self):
64 def test_multiline_string_cells(self):
65 "Code sprinkled with multiline strings should execute (GH-306)"
65 "Code sprinkled with multiline strings should execute (GH-306)"
66 ip = get_ipython()
66 ip = get_ipython()
67 ip.run_cell('tmp=0')
67 ip.run_cell('tmp=0')
68 self.assertEquals(ip.user_ns['tmp'], 0)
68 self.assertEquals(ip.user_ns['tmp'], 0)
69 ip.run_cell('tmp=1;"""a\nb"""\n')
69 ip.run_cell('tmp=1;"""a\nb"""\n')
70 self.assertEquals(ip.user_ns['tmp'], 1)
70 self.assertEquals(ip.user_ns['tmp'], 1)
71
71
72 def test_dont_cache_with_semicolon(self):
72 def test_dont_cache_with_semicolon(self):
73 "Ending a line with semicolon should not cache the returned object (GH-307)"
73 "Ending a line with semicolon should not cache the returned object (GH-307)"
74 ip = get_ipython()
74 ip = get_ipython()
75 oldlen = len(ip.user_ns['Out'])
75 oldlen = len(ip.user_ns['Out'])
76 a = ip.run_cell('1;')
76 a = ip.run_cell('1;')
77 newlen = len(ip.user_ns['Out'])
77 newlen = len(ip.user_ns['Out'])
78 self.assertEquals(oldlen, newlen)
78 self.assertEquals(oldlen, newlen)
79 #also test the default caching behavior
79 #also test the default caching behavior
80 ip.run_cell('1')
80 ip.run_cell('1')
81 newlen = len(ip.user_ns['Out'])
81 newlen = len(ip.user_ns['Out'])
82 self.assertEquals(oldlen+1, newlen)
82 self.assertEquals(oldlen+1, newlen)
83
83
84 def test_In_variable(self):
84 def test_In_variable(self):
85 "Verify that In variable grows with user input (GH-284)"
85 "Verify that In variable grows with user input (GH-284)"
86 ip = get_ipython()
86 ip = get_ipython()
87 oldlen = len(ip.user_ns['In'])
87 oldlen = len(ip.user_ns['In'])
88 ip.run_cell('1;')
88 ip.run_cell('1;')
89 newlen = len(ip.user_ns['In'])
89 newlen = len(ip.user_ns['In'])
90 self.assertEquals(oldlen+1, newlen)
90 self.assertEquals(oldlen+1, newlen)
91 self.assertEquals(ip.user_ns['In'][-1],'1;')
91 self.assertEquals(ip.user_ns['In'][-1],'1;')
92
92
93 def test_magic_names_in_string(self):
93 def test_magic_names_in_string(self):
94 ip = get_ipython()
94 ip = get_ipython()
95 ip.run_cell('a = """\n%exit\n"""')
95 ip.run_cell('a = """\n%exit\n"""')
96 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
96 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
97
97
98 def test_alias_crash(self):
98 def test_alias_crash(self):
99 """Errors in prefilter can't crash IPython"""
99 """Errors in prefilter can't crash IPython"""
100 ip = get_ipython()
100 ip = get_ipython()
101 ip.run_cell('%alias parts echo first %s second %s')
101 ip.run_cell('%alias parts echo first %s second %s')
102 # capture stderr:
102 # capture stderr:
103 save_err = io.stderr
103 save_err = io.stderr
104 io.stderr = StringIO()
104 io.stderr = StringIO()
105 ip.run_cell('parts 1')
105 ip.run_cell('parts 1')
106 err = io.stderr.getvalue()
106 err = io.stderr.getvalue()
107 io.stderr = save_err
107 io.stderr = save_err
108 self.assertEquals(err.split(':')[0], 'ERROR')
108 self.assertEquals(err.split(':')[0], 'ERROR')
109
109
110 def test_trailing_newline(self):
110 def test_trailing_newline(self):
111 """test that running !(command) does not raise a SyntaxError"""
111 """test that running !(command) does not raise a SyntaxError"""
112 ip = get_ipython()
112 ip = get_ipython()
113 ip.run_cell('!(true)\n', False)
113 ip.run_cell('!(true)\n', False)
114 ip.run_cell('!(true)\n\n\n', False)
114 ip.run_cell('!(true)\n\n\n', False)
115
115
116 def test_gh_597(self):
116 def test_gh_597(self):
117 """Pretty-printing lists of objects with non-ascii reprs may cause
117 """Pretty-printing lists of objects with non-ascii reprs may cause
118 problems."""
118 problems."""
119 class Spam(object):
119 class Spam(object):
120 def __repr__(self):
120 def __repr__(self):
121 return "\xe9"*50
121 return "\xe9"*50
122 import IPython.core.formatters
122 import IPython.core.formatters
123 f = IPython.core.formatters.PlainTextFormatter()
123 f = IPython.core.formatters.PlainTextFormatter()
124 f([Spam(),Spam()])
124 f([Spam(),Spam()])
125
125
126 def test_future_flags(self):
126 def test_future_flags(self):
127 """Check that future flags are used for parsing code (gh-777)"""
127 """Check that future flags are used for parsing code (gh-777)"""
128 ip = get_ipython()
128 ip = get_ipython()
129 ip.run_cell('from __future__ import print_function')
129 ip.run_cell('from __future__ import print_function')
130 try:
130 try:
131 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
131 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
132 assert 'prfunc_return_val' in ip.user_ns
132 assert 'prfunc_return_val' in ip.user_ns
133 finally:
133 finally:
134 # Reset compiler flags so we don't mess up other tests.
134 # Reset compiler flags so we don't mess up other tests.
135 ip.compile.reset_compiler_flags()
135 ip.compile.reset_compiler_flags()
136
137 def test_future_unicode(self):
138 """Check that unicode_literals is imported from __future__ (gh #786)"""
139 ip = get_ipython()
140 try:
141 ip.run_cell(u'len_byte_str = len("\xe9")')
142 assert ip.user_ns['len_byte_str'] == 2
143 ip.run_cell('from __future__ import unicode_literals')
144 ip.run_cell(u'len_unicode_str = len("\xe9")')
145 assert ip.user_ns['len_unicode_str'] == 1
146 finally:
147 # Reset compiler flags so we don't mess up other tests.
148 ip.compile.reset_compiler_flags()
General Comments 0
You need to be logged in to leave comments. Login now