##// END OF EJS Templates
test input / raw_input...
MinRK -
Show More
@@ -24,7 +24,7 b' import nose.tools as nt'
24 from IPython.kernel import KernelManager
24 from IPython.kernel import KernelManager
25 from IPython.kernel.tests.test_message_spec import execute, flush_channels
25 from IPython.kernel.tests.test_message_spec import execute, flush_channels
26 from IPython.testing import decorators as dec
26 from IPython.testing import decorators as dec
27 from IPython.utils import path
27 from IPython.utils import path, py3compat
28
28
29 #-------------------------------------------------------------------------------
29 #-------------------------------------------------------------------------------
30 # Tests
30 # Tests
@@ -33,6 +33,9 b' IPYTHONDIR = None'
33 save_env = None
33 save_env = None
34 save_get_ipython_dir = None
34 save_get_ipython_dir = None
35
35
36 STARTUP_TIMEOUT = 60
37 TIMEOUT = 15
38
36 def setup():
39 def setup():
37 """setup temporary IPYTHONDIR for tests"""
40 """setup temporary IPYTHONDIR for tests"""
38 global IPYTHONDIR
41 global IPYTHONDIR
@@ -75,7 +78,7 b' def new_kernel():'
75
78
76 # wait for kernel to be ready
79 # wait for kernel to be ready
77 KC.shell_channel.execute("import sys")
80 KC.shell_channel.execute("import sys")
78 KC.shell_channel.get_msg(block=True, timeout=5)
81 KC.shell_channel.get_msg(block=True, timeout=STARTUP_TIMEOUT)
79 flush_channels(KC)
82 flush_channels(KC)
80 try:
83 try:
81 yield KC
84 yield KC
@@ -116,6 +119,8 b' def _check_mp_mode(kc, expected=False, stream="stdout"):'
116 nt.assert_equal(eval(stdout.strip()), expected)
119 nt.assert_equal(eval(stdout.strip()), expected)
117
120
118
121
122 # printing tests
123
119 def test_simple_print():
124 def test_simple_print():
120 """simple print statement in kernel"""
125 """simple print statement in kernel"""
121 with new_kernel() as kc:
126 with new_kernel() as kc:
@@ -125,7 +130,6 b' def test_simple_print():'
125 nt.assert_equal(stdout, 'hi\n')
130 nt.assert_equal(stdout, 'hi\n')
126 nt.assert_equal(stderr, '')
131 nt.assert_equal(stderr, '')
127 _check_mp_mode(kc, expected=False)
132 _check_mp_mode(kc, expected=False)
128 print ('hello')
129
133
130
134
131 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
135 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
@@ -202,3 +206,47 b' def test_subprocess_error():'
202 _check_mp_mode(kc, expected=False)
206 _check_mp_mode(kc, expected=False)
203 _check_mp_mode(kc, expected=False, stream="stderr")
207 _check_mp_mode(kc, expected=False, stream="stderr")
204
208
209
210 # raw_input tests
211
212 def test_raw_input():
213 """test [raw_]input"""
214 with new_kernel() as kc:
215 iopub = kc.iopub_channel
216
217 input_f = "input" if py3compat.PY3 else "raw_input"
218 theprompt = "prompt> "
219 code = 'print({input_f}("{theprompt}"))'.format(**locals())
220 msg_id = kc.execute(code, allow_stdin=True)
221 msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT)
222 nt.assert_equal(msg['header']['msg_type'], u'input_request')
223 content = msg['content']
224 nt.assert_equal(content['prompt'], theprompt)
225 text = "some text"
226 kc.input(text)
227 reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
228 nt.assert_equal(reply['content']['status'], 'ok')
229 stdout, stderr = assemble_output(iopub)
230 nt.assert_equal(stdout, text + "\n")
231
232
233 @dec.skipif(py3compat.PY3)
234 def test_eval_input():
235 """test input() on Python 2"""
236 with new_kernel() as kc:
237 iopub = kc.iopub_channel
238
239 input_f = "input" if py3compat.PY3 else "raw_input"
240 theprompt = "prompt> "
241 code = 'print(input("{theprompt}"))'.format(**locals())
242 msg_id = kc.execute(code, allow_stdin=True)
243 msg = kc.get_stdin_msg(block=True, timeout=TIMEOUT)
244 nt.assert_equal(msg['header']['msg_type'], u'input_request')
245 content = msg['content']
246 nt.assert_equal(content['prompt'], theprompt)
247 kc.input("1+1")
248 reply = kc.get_shell_msg(block=True, timeout=TIMEOUT)
249 nt.assert_equal(reply['content']['status'], 'ok')
250 stdout, stderr = assemble_output(iopub)
251 nt.assert_equal(stdout, "2\n")
252
General Comments 0
You need to be logged in to leave comments. Login now