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