##// END OF EJS Templates
refactor clearing and executing first cell in test
Paul Ivanov -
Show More
@@ -1,111 +1,112
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
53 // helper function to clear the first two cells, set the text of and execute
54 // the first one
55 function clear_and_execute(that, code) {
56 that.evaluate(function() {
57 IPython.notebook.get_cell(0).clear_output();
58 IPython.notebook.get_cell(1).clear_output();
59 });
60 that.set_cell_text(0, code);
61 that.execute_cell(0);
62 }
63
52 casper.notebook_test(function () {
64 casper.notebook_test(function () {
53 this.evaluate(function () {
65 this.evaluate(function () {
54 var cell = IPython.notebook.get_cell(0);
66 var cell = IPython.notebook.get_cell(0);
55 // "we have to make messes to find out who we are"
67 // "we have to make messes to find out who we are"
56 cell.set_text([
68 cell.set_text([
57 "%%javascript",
69 "%%javascript",
58 "IPython.notebook.insert_cell_below('code')"
70 "IPython.notebook.insert_cell_below('code')"
59 ].join('\n')
71 ].join('\n')
60 );
72 );
61
73
62 cell.execute();
74 cell.execute();
63 });
75 });
64
76
65 this.wait_for_output(0);
77 this.wait_for_output(0);
66
78
67 this.then(function ( ) {
79 this.then(function ( ) {
68 var result = this.get_output_cell(0);
80 var result = this.get_output_cell(0);
69 var num_cells = this.get_cells_length();
81 var num_cells = this.get_cells_length();
70 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
82 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
71 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
83 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
72 'testing JS embeded with mime key');
84 'testing JS embeded with mime key');
73 });
85 });
74
86
75 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
87 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
76
88
77 this.then(function ( ) {
89 this.then(function ( ) {
78 check_output_area.apply(this, ['display_data', ['javascript']]);
90 check_output_area.apply(this, ['display_data', ['javascript']]);
79
91
80 });
92 });
81
93
82 this.then(function() {
94 this.then(function() {
83 // test output of text/plain and application/json keys
95 clear_and_execute(this, '%lsmagic');
84 this.evaluate(function() {
85 IPython.notebook.get_cell(0).clear_output();
86 IPython.notebook.get_cell(1).clear_output();
87 });
88 this.set_cell_text(0, "%lsmagic");
89 this.execute_cell(0);
90 });
96 });
91
97
92 this.then(function () {
98 this.then(function () {
93 check_output_area.apply(this, ['pyout', ['text', 'json']]);
99 check_output_area.apply(this, ['pyout', ['text', 'json']]);
94 });
100 });
95
101
96 this.then(function() {
102 this.then(function() {
97 // test display of text/plain and application/json keys
103 clear_and_execute(this,
98 this.evaluate(function() {
99 IPython.notebook.get_cell(0).clear_output();
100 IPython.notebook.get_cell(1).clear_output();
101 });
102 this.set_cell_text(0,
103 "x = %lsmagic\nfrom IPython.display import display; display(x)");
104 "x = %lsmagic\nfrom IPython.display import display; display(x)");
104 this.execute_cell(0);
105 this.execute_cell(0);
105 });
106 });
106
107
107 this.then(function ( ) {
108 this.then(function ( ) {
108 check_output_area.apply(this, ['display_data', ['text', 'json']]);
109 check_output_area.apply(this, ['display_data', ['text', 'json']]);
109 });
110 });
110
111
111 });
112 });
General Comments 0
You need to be logged in to leave comments. Login now