44
55casper . notebook_test ( function ( ) {
66
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+ }
718 this . test_coalesced_output = function ( msg , code , expected ) {
819 this . then ( function ( ) {
920 this . echo ( "Test coalesced output: " + msg ) ;
@@ -24,31 +35,23 @@ casper.notebook_test(function () {
2435 return cell . output_area . outputs ;
2536 } ) ;
2637 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 ) ;
3639 } ) ;
3740
3841 } ;
39-
42+
4043 this . thenEvaluate ( function ( ) {
4144 IPython . notebook . insert_cell_at_index ( "code" , 0 ) ;
4245 var cell = IPython . notebook . get_cell ( 0 ) ;
4346 cell . set_text ( [
4447 "from __future__ import print_function" ,
4548 "import sys" ,
46- "from IPython.display import display"
49+ "from IPython.display import display, clear_output "
4750 ] . join ( "\n" )
4851 ) ;
4952 cell . execute ( ) ;
5053 } ) ;
51-
54+
5255 this . test_coalesced_output ( "stdout" , [
5356 "print(1)" ,
5457 "sys.stdout.flush()" ,
@@ -123,4 +126,134 @@ casper.notebook_test(function () {
123126 } ,
124127 } ]
125128 ) ;
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+ } ) ;
126259} ) ;
0 commit comments