##// END OF EJS Templates
more refactoring - test to/fromJSON for mimetypes
Paul Ivanov -
Show More
@@ -1,121 +1,111 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
19 // represetnation, while the original in-memory cell retains its long mimetype
20 // name, and that fromJSON also gets its long mimetype name
18 function assert_has(short_name, json, result, result2) {
21 function assert_has(short_name, json, result, result2) {
19 long_name = mime[short_name];
22 long_name = mime[short_name];
20 this.test.assertTrue(json[0].hasOwnProperty(short_name),
23 this.test.assertTrue(json[0].hasOwnProperty(short_name),
21 'toJSON() representation uses ' + short_name);
24 'toJSON() representation uses ' + short_name);
22 this.test.assertTrue(result.hasOwnProperty(long_name),
25 this.test.assertTrue(result.hasOwnProperty(long_name),
23 'toJSON() original embeded JSON keeps ' + long_name);
26 'toJSON() original embeded JSON keeps ' + long_name);
24 this.test.assertTrue(result2.hasOwnProperty(long_name),
27 this.test.assertTrue(result2.hasOwnProperty(long_name),
25 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name);
28 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name);
26 }
29 }
30
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
33 // for a set of mimetype keys, using their short names ('javascript', 'text',
34 // 'png', etc).
35 function check_output_area(output_type, keys) {
36 json = this.evaluate(function() {
37 var json = IPython.notebook.get_cell(0).output_area.toJSON();
38 // appended cell will initially be empty, lets add it some output
39 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
40 return json;
41 });
42 var result = this.get_output_cell(0);
43 var result2 = this.get_output_cell(1);
44 this.test.assertEquals(result.output_type, output_type,
45 'testing ' + output_type + ' for ' + keys.join(' and '));
46
47 for (var idx in keys) {
48 assert_has.apply(this, [keys[idx], json, result, result2]);
49 }
50 }
27
51
28 casper.notebook_test(function () {
52 casper.notebook_test(function () {
29 this.evaluate(function () {
53 this.evaluate(function () {
30 var cell = IPython.notebook.get_cell(0);
54 var cell = IPython.notebook.get_cell(0);
31 // "we have to make messes to find out who we are"
55 // "we have to make messes to find out who we are"
32 cell.set_text([
56 cell.set_text([
33 "%%javascript",
57 "%%javascript",
34 "IPython.notebook.insert_cell_below('code')"
58 "IPython.notebook.insert_cell_below('code')"
35 ].join('\n')
59 ].join('\n')
36 );
60 );
37
61
38 cell.execute();
62 cell.execute();
39 });
63 });
40
64
41 this.wait_for_output(0);
65 this.wait_for_output(0);
42
66
43 this.then(function ( ) {
67 this.then(function ( ) {
44 var result = this.get_output_cell(0);
68 var result = this.get_output_cell(0);
45 var num_cells = this.get_cells_length();
69 var num_cells = this.get_cells_length();
46 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
70 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
47 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
71 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
48 'testing JS embeded with mime key');
72 'testing JS embeded with mime key');
49 });
73 });
50
74
51 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
75 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
52
76
53 this.then(function ( ) {
77 this.then(function ( ) {
54 json = this.evaluate(function() {
78 check_output_area.apply(this, ['display_data', ['javascript']]);
55 var json = IPython.notebook.get_cell(0).output_area.toJSON();
56 // appended cell will initially be empty, lets add it some output
57 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
58 return json;
59 });
60 var result = this.get_output_cell(0);
61 var result2 = this.get_output_cell(1);
62
63 assert_has.apply(this, ['javascript', json, result, result2]);
64
79
65 });
80 });
66
81
67 this.then(function() {
82 this.then(function() {
68 // test output of text/plain and application/json keys
83 // test output of text/plain and application/json keys
69 this.evaluate(function() {
84 this.evaluate(function() {
70 IPython.notebook.get_cell(0).clear_output();
85 IPython.notebook.get_cell(0).clear_output();
71 IPython.notebook.get_cell(1).clear_output();
86 IPython.notebook.get_cell(1).clear_output();
72 });
87 });
73 this.set_cell_text(0, "%lsmagic");
88 this.set_cell_text(0, "%lsmagic");
74 this.execute_cell(0);
89 this.execute_cell(0);
75 });
90 });
76
91
77 this.then(function ( ) {
92 this.then(function () {
78 json = this.evaluate(function() {
93 check_output_area.apply(this, ['pyout', ['text', 'json']]);
79 var json = IPython.notebook.get_cell(0).output_area.toJSON();
80 // appended cell will initially be empty, lets add it some output
81 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
82 return json;
83 });
84 var result = this.get_output_cell(0);
85 var result2 = this.get_output_cell(1);
86 this.test.assertEquals(result.output_type, 'pyout',
87 'testing pyout application/json and text/plain');
88
89 assert_has.apply(this, ['json', json, result, result2]);
90 assert_has.apply(this, ['text', json, result, result2]);
91
92 });
94 });
95
93 this.then(function() {
96 this.then(function() {
94 // test display of text/plain and application/json keys
97 // test display of text/plain and application/json keys
95 this.evaluate(function() {
98 this.evaluate(function() {
96 IPython.notebook.get_cell(0).clear_output();
99 IPython.notebook.get_cell(0).clear_output();
97 IPython.notebook.get_cell(1).clear_output();
100 IPython.notebook.get_cell(1).clear_output();
98 });
101 });
99 this.set_cell_text(0,
102 this.set_cell_text(0,
100 "x = %lsmagic\nfrom IPython.display import display; display(x)");
103 "x = %lsmagic\nfrom IPython.display import display; display(x)");
101 this.execute_cell(0);
104 this.execute_cell(0);
102 });
105 });
103
106
104 this.then(function ( ) {
107 this.then(function ( ) {
105 json = this.evaluate(function() {
108 check_output_area.apply(this, ['display_data', ['text', 'json']]);
106 var json = IPython.notebook.get_cell(0).output_area.toJSON();
107 // appended cell will initially be empty, lets add it some output
108 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
109 return json;
110 });
111 var result = this.get_output_cell(0);
112 var result2 = this.get_output_cell(1);
113 this.test.assertEquals(result.output_type, 'display_data',
114 'testing display_data application/json and text/plain');
115
116 assert_has.apply(this, ['json', json, result, result2]);
117 assert_has.apply(this, ['text', json, result, result2]);
118
119 });
109 });
120
110
121 });
111 });
General Comments 0
You need to be logged in to leave comments. Login now