Show More
@@ -1,112 +1,130 b'' | |||||
1 | // Test opening a rich notebook, saving it, and reopening it again. |
|
1 | // Test opening a rich notebook, saving it, and reopening it again. | |
2 | // |
|
2 | // | |
3 | //toJSON fromJSON toJSON and do a string comparison |
|
3 | //toJSON fromJSON toJSON and do a string comparison | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js |
|
6 | // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js | |
7 | mime = { |
|
7 | mime = { | |
8 | "text" : "text/plain", |
|
8 | "text" : "text/plain", | |
9 | "html" : "text/html", |
|
9 | "html" : "text/html", | |
10 | "svg" : "image/svg+xml", |
|
10 | "svg" : "image/svg+xml", | |
11 | "png" : "image/png", |
|
11 | "png" : "image/png", | |
12 | "jpeg" : "image/jpeg", |
|
12 | "jpeg" : "image/jpeg", | |
13 | "latex" : "text/latex", |
|
13 | "latex" : "text/latex", | |
14 | "json" : "application/json", |
|
14 | "json" : "application/json", | |
15 | "javascript" : "application/javascript", |
|
15 | "javascript" : "application/javascript", | |
16 | }; |
|
16 | }; | |
17 |
|
17 | |||
18 | // helper function to ensure that the short_name is found in the toJSON |
|
18 | // helper function to ensure that the short_name is found in the toJSON | |
19 | // represetnation, while the original in-memory cell retains its long mimetype |
|
19 | // represetnation, while the original in-memory cell retains its long mimetype | |
20 | // name, and that fromJSON also gets its long mimetype name |
|
20 | // name, and that fromJSON also gets its long mimetype name | |
21 | function assert_has(short_name, json, result, result2) { |
|
21 | function assert_has(short_name, json, result, result2) { | |
22 | long_name = mime[short_name]; |
|
22 | long_name = mime[short_name]; | |
23 | this.test.assertTrue(json[0].hasOwnProperty(short_name), |
|
23 | this.test.assertTrue(json[0].hasOwnProperty(short_name), | |
24 | 'toJSON() representation uses ' + short_name); |
|
24 | 'toJSON() representation uses ' + short_name); | |
25 | this.test.assertTrue(result.hasOwnProperty(long_name), |
|
25 | this.test.assertTrue(result.hasOwnProperty(long_name), | |
26 | 'toJSON() original embeded JSON keeps ' + long_name); |
|
26 | 'toJSON() original embeded JSON keeps ' + long_name); | |
27 | this.test.assertTrue(result2.hasOwnProperty(long_name), |
|
27 | this.test.assertTrue(result2.hasOwnProperty(long_name), | |
28 | 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name); |
|
28 | 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | // helper function for checkout that the first two cells have a particular |
|
31 | // helper function for checkout that the first two cells have a particular | |
32 | // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON |
|
32 | // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON | |
33 | // for a set of mimetype keys, using their short names ('javascript', 'text', |
|
33 | // for a set of mimetype keys, using their short names ('javascript', 'text', | |
34 | // 'png', etc). |
|
34 | // 'png', etc). | |
35 | function check_output_area(output_type, keys) { |
|
35 | function check_output_area(output_type, keys) { | |
36 | json = this.evaluate(function() { |
|
36 | json = this.evaluate(function() { | |
37 | var json = IPython.notebook.get_cell(0).output_area.toJSON(); |
|
37 | var json = IPython.notebook.get_cell(0).output_area.toJSON(); | |
38 | // appended cell will initially be empty, lets add it some output |
|
38 | // appended cell will initially be empty, lets add it some output | |
39 | var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json); |
|
39 | var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json); | |
40 | return json; |
|
40 | return json; | |
41 | }); |
|
41 | }); | |
42 | var result = this.get_output_cell(0); |
|
42 | var result = this.get_output_cell(0); | |
43 | var result2 = this.get_output_cell(1); |
|
43 | var result2 = this.get_output_cell(1); | |
44 | this.test.assertEquals(result.output_type, output_type, |
|
44 | this.test.assertEquals(result.output_type, output_type, | |
45 | 'testing ' + output_type + ' for ' + keys.join(' and ')); |
|
45 | 'testing ' + output_type + ' for ' + keys.join(' and ')); | |
46 |
|
46 | |||
47 | for (var idx in keys) { |
|
47 | for (var idx in keys) { | |
48 | assert_has.apply(this, [keys[idx], json, result, result2]); |
|
48 | assert_has.apply(this, [keys[idx], json, result, result2]); | |
49 | } |
|
49 | } | |
50 | } |
|
50 | } | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | // helper function to clear the first two cells, set the text of and execute |
|
53 | // helper function to clear the first two cells, set the text of and execute | |
54 | // the first one |
|
54 | // the first one | |
55 | function clear_and_execute(that, code) { |
|
55 | function clear_and_execute(that, code) { | |
56 | that.evaluate(function() { |
|
56 | that.evaluate(function() { | |
57 | IPython.notebook.get_cell(0).clear_output(); |
|
57 | IPython.notebook.get_cell(0).clear_output(); | |
58 | IPython.notebook.get_cell(1).clear_output(); |
|
58 | IPython.notebook.get_cell(1).clear_output(); | |
59 | }); |
|
59 | }); | |
60 | that.set_cell_text(0, code); |
|
60 | that.set_cell_text(0, code); | |
61 | that.execute_cell(0); |
|
61 | that.execute_cell(0); | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | casper.notebook_test(function () { |
|
64 | casper.notebook_test(function () { | |
65 | this.evaluate(function () { |
|
65 | this.evaluate(function () { | |
66 | var cell = IPython.notebook.get_cell(0); |
|
66 | var cell = IPython.notebook.get_cell(0); | |
67 | // "we have to make messes to find out who we are" |
|
67 | // "we have to make messes to find out who we are" | |
68 | cell.set_text([ |
|
68 | cell.set_text([ | |
69 | "%%javascript", |
|
69 | "%%javascript", | |
70 | "IPython.notebook.insert_cell_below('code')" |
|
70 | "IPython.notebook.insert_cell_below('code')" | |
71 | ].join('\n') |
|
71 | ].join('\n') | |
72 | ); |
|
72 | ); | |
73 |
|
73 | |||
74 | cell.execute(); |
|
74 | cell.execute(); | |
75 | }); |
|
75 | }); | |
76 |
|
76 | |||
77 | this.wait_for_output(0); |
|
77 | this.wait_for_output(0); | |
78 |
|
78 | |||
79 | this.then(function ( ) { |
|
79 | this.then(function ( ) { | |
80 | var result = this.get_output_cell(0); |
|
80 | var result = this.get_output_cell(0); | |
81 | var num_cells = this.get_cells_length(); |
|
81 | var num_cells = this.get_cells_length(); | |
82 | this.test.assertEquals(num_cells, 2, '%%javascript magic works'); |
|
82 | this.test.assertEquals(num_cells, 2, '%%javascript magic works'); | |
83 | this.test.assertTrue(result.hasOwnProperty('application/javascript'), |
|
83 | this.test.assertTrue(result.hasOwnProperty('application/javascript'), | |
84 | 'testing JS embeded with mime key'); |
|
84 | 'testing JS embeded with mime key'); | |
85 | }); |
|
85 | }); | |
86 |
|
86 | |||
87 | //this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); |
|
87 | //this.thenEvaluate(function() { IPython.notebook.save_notebook(); }); | |
88 |
|
88 | |||
89 | this.then(function ( ) { |
|
89 | this.then(function ( ) { | |
90 | check_output_area.apply(this, ['display_data', ['javascript']]); |
|
90 | check_output_area.apply(this, ['display_data', ['javascript']]); | |
91 |
|
91 | |||
92 | }); |
|
92 | }); | |
93 |
|
93 | |||
94 | this.then(function() { |
|
94 | this.then(function() { | |
95 | clear_and_execute(this, '%lsmagic'); |
|
95 | clear_and_execute(this, '%lsmagic'); | |
96 | }); |
|
96 | }); | |
97 |
|
97 | |||
98 | this.then(function () { |
|
98 | this.then(function () { | |
99 | check_output_area.apply(this, ['pyout', ['text', 'json']]); |
|
99 | check_output_area.apply(this, ['pyout', ['text', 'json']]); | |
100 | }); |
|
100 | }); | |
101 |
|
101 | |||
102 | this.then(function() { |
|
102 | this.then(function() { | |
103 | clear_and_execute(this, |
|
103 | clear_and_execute(this, | |
104 | "x = %lsmagic\nfrom IPython.display import display; display(x)"); |
|
104 | "x = %lsmagic\nfrom IPython.display import display; display(x)"); | |
105 | this.execute_cell(0); |
|
105 | this.execute_cell(0); | |
106 | }); |
|
106 | }); | |
107 |
|
107 | |||
108 | this.then(function ( ) { |
|
108 | this.then(function ( ) { | |
109 | check_output_area.apply(this, ['display_data', ['text', 'json']]); |
|
109 | check_output_area.apply(this, ['display_data', ['text', 'json']]); | |
110 | }); |
|
110 | }); | |
|
111 | ||||
|
112 | this.then(function() { | |||
|
113 | clear_and_execute(this, | |||
|
114 | "from IPython.display import Latex; Latex('$X^2$')"); | |||
|
115 | }); | |||
|
116 | ||||
|
117 | this.then(function ( ) { | |||
|
118 | check_output_area.apply(this, ['pyout', ['text', 'latex']]); | |||
|
119 | }); | |||
|
120 | ||||
|
121 | this.then(function() { | |||
|
122 | clear_and_execute(this, | |||
|
123 | "from IPython.display import Latex, display; display(Latex('$X^2$'))"); | |||
|
124 | }); | |||
|
125 | ||||
|
126 | this.then(function ( ) { | |||
|
127 | check_output_area.apply(this, ['display_data', ['text', 'latex']]); | |||
|
128 | }); | |||
111 |
|
129 | |||
112 | }); |
|
130 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now