4
4
5
5
casper . notebook_test ( function ( ) {
6
6
7
+ this . compare_outputs = function ( results , expected ) {
8
+ for ( var i = 0 ; i < results . length ; i ++ ) {
9
+ var r = results [ i ] ;
10
+ var ex = expected [ i ] ;
11
+ this . test . assertEquals ( r . output_type , ex . output_type , "output " + i + " = " + r . output_type ) ;
12
+ if ( r . output_type === 'stream' ) {
13
+ this . test . assertEquals ( r . name , ex . name , "stream " + i + " = " + r . name ) ;
14
+ this . test . assertEquals ( r . text , ex . text , "content " + i ) ;
15
+ }
16
+ }
17
+ }
7
18
this . test_coalesced_output = function ( msg , code , expected ) {
8
19
this . then ( function ( ) {
9
20
this . echo ( "Test coalesced output: " + msg ) ;
@@ -24,31 +35,23 @@ casper.notebook_test(function () {
24
35
return cell . output_area . outputs ;
25
36
} ) ;
26
37
this . test . assertEquals ( results . length , expected . length , "correct number of outputs" ) ;
27
- for ( var i = 0 ; i < results . length ; i ++ ) {
28
- var r = results [ i ] ;
29
- var ex = expected [ i ] ;
30
- this . test . assertEquals ( r . output_type , ex . output_type , "output " + i ) ;
31
- if ( r . output_type === 'stream' ) {
32
- this . test . assertEquals ( r . name , ex . name , "stream " + i ) ;
33
- this . test . assertEquals ( r . text , ex . text , "content " + i ) ;
34
- }
35
- }
38
+ this . compare_outputs ( results , expected ) ;
36
39
} ) ;
37
40
38
41
} ;
39
-
42
+
40
43
this . thenEvaluate ( function ( ) {
41
44
IPython . notebook . insert_cell_at_index ( "code" , 0 ) ;
42
45
var cell = IPython . notebook . get_cell ( 0 ) ;
43
46
cell . set_text ( [
44
47
"from __future__ import print_function" ,
45
48
"import sys" ,
46
- "from IPython.display import display"
49
+ "from IPython.display import display, clear_output "
47
50
] . join ( "\n" )
48
51
) ;
49
52
cell . execute ( ) ;
50
53
} ) ;
51
-
54
+
52
55
this . test_coalesced_output ( "stdout" , [
53
56
"print(1)" ,
54
57
"sys.stdout.flush()" ,
@@ -123,4 +126,134 @@ casper.notebook_test(function () {
123
126
} ,
124
127
} ]
125
128
) ;
129
+
130
+ this . then ( function ( ) {
131
+ this . echo ( "Test output callback overrides" ) ;
132
+ } ) ;
133
+
134
+ this . thenEvaluate ( function ( ) {
135
+ IPython . notebook . insert_cell_at_index ( "code" , 0 ) ;
136
+ var cell = IPython . notebook . get_cell ( 0 ) ;
137
+ cell . set_text ( [ "print(1)" ,
138
+ "sys.stdout.flush()" ,
139
+ "print(2)" ,
140
+ "sys.stdout.flush()" ,
141
+ "print(3, file=sys.stderr)" ,
142
+ "sys.stdout.flush()" ,
143
+ "display(2)" ,
144
+ "clear_output()" ,
145
+ "sys.stdout.flush()" ,
146
+ "print('remove handler')" ,
147
+ "sys.stdout.flush()" ,
148
+ "print('back to cell')" ,
149
+ "sys.stdout.flush()" ,
150
+ ] . join ( '\n' ) ) ;
151
+ cell . execute ( ) ;
152
+ var kernel = IPython . notebook . kernel ;
153
+ var msg_id = cell . last_msg_id ;
154
+ var callback_id = 'mycallbackid'
155
+ cell . iopub_messages = [ ] ;
156
+ var add_msg = function ( msg ) {
157
+ if ( msg . content . text === "remove handler\n" ) {
158
+ kernel . output_callback_overrides_pop ( msg_id ) ;
159
+ }
160
+ msg . content . output_type = msg . msg_type ;
161
+ cell . iopub_messages . push ( msg . content ) ;
162
+ } ;
163
+ kernel . set_callbacks_for_msg ( callback_id , {
164
+ iopub : {
165
+ output : add_msg ,
166
+ clear_output : add_msg ,
167
+ }
168
+ } , false ) ;
169
+ kernel . output_callback_overrides_push ( msg_id , callback_id ) ;
170
+ } ) ;
171
+
172
+ this . wait_for_idle ( ) ;
173
+
174
+ this . then ( function ( ) {
175
+ var expected_callback = [ {
176
+ output_type : "stream" ,
177
+ name : "stdout" ,
178
+ text : "1\n"
179
+ } , {
180
+ output_type : "stream" ,
181
+ name : "stdout" ,
182
+ text : "2\n"
183
+ } , {
184
+ output_type : "stream" ,
185
+ name : "stderr" ,
186
+ text : "3\n"
187
+ } , {
188
+ output_type : "display_data" ,
189
+ } , {
190
+ output_type : "clear_output" ,
191
+ } , {
192
+ output_type : "stream" ,
193
+ name : "stdout" ,
194
+ text : "remove handler\n"
195
+ } , ]
196
+ var expected_cell = [ {
197
+ output_type : "stream" ,
198
+ name : "stdout" ,
199
+ text : "back to cell\n"
200
+ } ]
201
+ var returned = this . evaluate ( function ( ) {
202
+ var cell = IPython . notebook . get_cell ( 0 ) ;
203
+ return [ cell . output_area . outputs , cell . iopub_messages ] ;
204
+ } ) ;
205
+ var cell_results = returned [ 0 ] ;
206
+ var callback_results = returned [ 1 ] ;
207
+ this . test . assertEquals ( cell_results . length , expected_cell . length , "correct number of cell outputs" ) ;
208
+ this . test . assertEquals ( callback_results . length , expected_callback . length , "correct number of callback outputs" ) ;
209
+ this . compare_outputs ( cell_results , expected_cell ) ;
210
+ this . compare_outputs ( callback_results , expected_callback ) ;
211
+ } ) ;
212
+
213
+ this . then ( function ( ) {
214
+ this . echo ( "Test output callback overrides get execute_results messages too" ) ;
215
+ } ) ;
216
+
217
+ this . thenEvaluate ( function ( ) {
218
+ IPython . notebook . insert_cell_at_index ( "code" , 0 ) ;
219
+ var cell = IPython . notebook . get_cell ( 0 ) ;
220
+ cell . set_text ( "'end'" ) ;
221
+ cell . execute ( ) ;
222
+ var kernel = IPython . notebook . kernel ;
223
+ var msg_id = cell . last_msg_id ;
224
+ var callback_id = 'mycallbackid2'
225
+ cell . iopub_messages = [ ] ;
226
+ var add_msg = function ( msg ) {
227
+ msg . content . output_type = msg . msg_type ;
228
+ cell . iopub_messages . push ( msg . content ) ;
229
+ } ;
230
+ kernel . set_callbacks_for_msg ( callback_id , {
231
+ iopub : {
232
+ output : add_msg ,
233
+ clear_output : add_msg ,
234
+ }
235
+ } , false ) ;
236
+ kernel . output_callback_overrides_push ( msg_id , callback_id ) ;
237
+ } ) ;
238
+
239
+ this . wait_for_idle ( ) ;
240
+
241
+ this . then ( function ( ) {
242
+ var expected_callback = [ {
243
+ output_type : "execute_result" ,
244
+ data : {
245
+ "text/plain" : "'end'"
246
+ }
247
+ } ] ;
248
+ var expected_cell = [ ] ;
249
+ var returned = this . evaluate ( function ( ) {
250
+ var cell = IPython . notebook . get_cell ( 0 ) ;
251
+ return [ cell . output_area . outputs , cell . iopub_messages ] ;
252
+ } ) ;
253
+ var cell_results = returned [ 0 ] ;
254
+ var callback_results = returned [ 1 ] ;
255
+ this . test . assertEquals ( cell_results . length , expected_cell . length , "correct number of cell outputs" ) ;
256
+ this . test . assertEquals ( callback_results . length , expected_callback . length , "correct number of callback outputs" ) ;
257
+ this . compare_outputs ( callback_results , expected_callback ) ;
258
+ } ) ;
126
259
} ) ;
0 commit comments