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