@@ -50,6 +50,15 @@ def _check_generated_stderr(self, stderr, n):
50
50
for ex in expect :
51
51
assert re .search (ex , line ) is not None , "Expected %r in %r" % (ex , line )
52
52
53
+ def _check_expected_lines_unordered (self , expected , lines ):
54
+ for expect in expected :
55
+ found = False
56
+ for line in lines :
57
+ found = found | (re .search (expect , line ) is not None )
58
+ if found :
59
+ break
60
+ assert found , "Expected %r in output" % (expect ,)
61
+
53
62
def test_cellpx_block_args (self ):
54
63
"""%%px --[no]block flags work"""
55
64
ip = get_ipython ()
@@ -207,8 +216,53 @@ def test_cellpx_groupby_type(self):
207
216
208
217
def test_cellpx_stream (self ):
209
218
"""%%px --stream"""
210
- # TODO
211
- pass
219
+ ip = get_ipython ()
220
+ v = self .client [:]
221
+ v .block = True
222
+ v .activate ()
223
+
224
+ v ['generate_output' ] = generate_output
225
+
226
+ with capture_output (display = False ) as io :
227
+ ip .run_cell_magic ('px' , '--stream' , 'generate_output()' )
228
+
229
+ self .assertNotIn ('\n \n ' , io .stdout )
230
+ lines = io .stdout .splitlines ()
231
+ expected = []
232
+ expected .extend (
233
+ [
234
+ r'\[stdout:\d+\] stdout' ,
235
+ r'\[stdout:\d+\] stdout2' ,
236
+ ]
237
+ * len (v )
238
+ )
239
+ expected .extend (
240
+ [
241
+ r'\[output:\d+\]' ,
242
+ r'IPython\.core\.display\.HTML' ,
243
+ r'IPython\.core\.display\.Math' ,
244
+ ]
245
+ * len (v )
246
+ )
247
+ expected .extend ([r'Out\[\d+:\d+\]:.*IPython\.core\.display\.Math' ] * len (v ))
248
+
249
+ self .assertEqual (len (lines ), len (expected ), io .stdout )
250
+ # Check that all expected lines are in the output
251
+ self ._check_expected_lines_unordered (expected , lines )
252
+
253
+ # Do the same for stderr
254
+ self .assertNotIn ('\n \n ' , io .stderr )
255
+ lines = io .stderr .splitlines ()
256
+ expected = []
257
+ expected .extend (
258
+ [
259
+ r'\[stderr:\d+\] stderr' ,
260
+ r'\[stderr:\d+\] stderr2' ,
261
+ ]
262
+ * len (v )
263
+ )
264
+ self .assertEqual (len (lines ), len (expected ), io .stderr )
265
+ self ._check_expected_lines_unordered (expected , lines )
212
266
213
267
def test_px_nonblocking (self ):
214
268
ip = get_ipython ()
0 commit comments