##// END OF EJS Templates
Merge pull request #6488 from jhamrick/lock-cells...
Min RK -
r18107:0b33b807 merge
parent child Browse files
Show More
@@ -0,0 +1,107 b''
1
2 // Test
3 casper.notebook_test(function () {
4 var that = this;
5 var cell_is_deletable = function (index) {
6 // Get the deletable status of a cell.
7 return that.evaluate(function (index) {
8 var cell = IPython.notebook.get_cell(index);
9 return cell.is_deletable();
10 }, index);
11 };
12
13 var a = 'print("a")';
14 var index = this.append_cell(a);
15
16 var b = 'print("b")';
17 index = this.append_cell(b);
18
19 var c = 'print("c")';
20 index = this.append_cell(c);
21
22 this.thenEvaluate(function() {
23 IPython.notebook.get_cell(1).metadata.deletable = false;
24 IPython.notebook.get_cell(2).metadata.deletable = 0; // deletable only when exactly false
25 IPython.notebook.get_cell(3).metadata.deletable = true;
26 });
27
28 this.then(function () {
29 // Check deletable status of the cells
30 this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable');
31 this.test.assert(!cell_is_deletable(1), 'Cell 1 is not deletable');
32 this.test.assert(cell_is_deletable(2), 'Cell 2 is deletable');
33 this.test.assert(cell_is_deletable(3), 'Cell 3 is deletable');
34 });
35
36 // Try to delete cell 0 (should succeed)
37 this.then(function () {
38 this.select_cell(0);
39 this.trigger_keydown('esc');
40 this.trigger_keydown('d', 'd');
41 this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are now 3 cells');
42 this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 1 is now cell 0');
43 this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 2 is now cell 1');
44 this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 3 is now cell 2');
45 this.validate_notebook_state('dd', 'command', 0);
46 });
47
48 // Try to delete cell 0 (should fail)
49 this.then(function () {
50 this.select_cell(0);
51 this.trigger_keydown('d', 'd');
52 this.test.assertEquals(this.get_cells_length(), 3, 'Delete cell 0: There are still 3 cells');
53 this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 0: Cell 0 was not deleted');
54 this.test.assertEquals(this.get_cell_text(1), b, 'Delete cell 0: Cell 1 was not affected');
55 this.test.assertEquals(this.get_cell_text(2), c, 'Delete cell 0: Cell 2 was not affected');
56 this.validate_notebook_state('dd', 'command', 0);
57 });
58
59 // Try to delete cell 1 (should succeed)
60 this.then(function () {
61 this.select_cell(1);
62 this.trigger_keydown('d', 'd');
63 this.test.assertEquals(this.get_cells_length(), 2, 'Delete cell 1: There are now 2 cells');
64 this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 1: Cell 0 was not affected');
65 this.test.assertEquals(this.get_cell_text(1), c, 'Delete cell 1: Cell 1 was not affected');
66 this.validate_notebook_state('dd', 'command', 1);
67 });
68
69 // Try to delete cell 1 (should succeed)
70 this.then(function () {
71 this.select_cell(1);
72 this.trigger_keydown('d', 'd');
73 this.test.assertEquals(this.get_cells_length(), 1, 'Delete cell 1: There is now 1 cell');
74 this.test.assertEquals(this.get_cell_text(0), a, 'Delete cell 2: Cell 0 was not affected');
75 this.validate_notebook_state('dd', 'command', 0);
76 });
77
78 // Change the deletable status of the last cells
79 this.thenEvaluate(function() {
80 IPython.notebook.get_cell(0).metadata.deletable = true;
81 });
82
83 this.then(function () {
84 // Check deletable status of the cell
85 this.test.assert(cell_is_deletable(0), 'Cell 0 is deletable');
86
87 // Try to delete the last cell (should succeed)
88 this.select_cell(0);
89 this.trigger_keydown('d', 'd');
90 this.test.assertEquals(this.get_cells_length(), 1, 'Delete last cell: There is still 1 cell');
91 this.test.assertEquals(this.get_cell_text(0), "", 'Delete last cell: Cell 0 was deleted');
92 this.validate_notebook_state('dd', 'command', 0);
93 });
94
95 // Make sure copied cells are deletable
96 this.thenEvaluate(function() {
97 IPython.notebook.get_cell(0).metadata.deletable = false;
98 });
99 this.then(function () {
100 this.select_cell(0);
101 this.trigger_keydown('c', 'v');
102 this.test.assertEquals(this.get_cells_length(), 2, 'Copy cell: There are 2 cells');
103 this.test.assert(!cell_is_deletable(0), 'Cell 0 is not deletable');
104 this.test.assert(cell_is_deletable(1), 'Cell 1 is deletable');
105 this.validate_notebook_state('cv', 'command', 1);
106 });
107 });
@@ -385,7 +385,8 b' define(['
385 **/
385 **/
386 Cell.prototype.toJSON = function () {
386 Cell.prototype.toJSON = function () {
387 var data = {};
387 var data = {};
388 data.metadata = this.metadata;
388 // deepcopy the metadata so copied cells don't share the same object
389 data.metadata = JSON.parse(JSON.stringify(this.metadata));
389 data.cell_type = this.cell_type;
390 data.cell_type = this.cell_type;
390 return data;
391 return data;
391 };
392 };
@@ -404,22 +405,35 b' define(['
404
405
405
406
406 /**
407 /**
407 * can the cell be split into two cells
408 * can the cell be split into two cells (false if not deletable)
408 * @method is_splittable
409 * @method is_splittable
409 **/
410 **/
410 Cell.prototype.is_splittable = function () {
411 Cell.prototype.is_splittable = function () {
411 return true;
412 return this.is_deletable();
412 };
413 };
413
414
414
415
415 /**
416 /**
416 * can the cell be merged with other cells
417 * can the cell be merged with other cells (false if not deletable)
417 * @method is_mergeable
418 * @method is_mergeable
418 **/
419 **/
419 Cell.prototype.is_mergeable = function () {
420 Cell.prototype.is_mergeable = function () {
420 return true;
421 return this.is_deletable();
421 };
422 };
422
423
424 /**
425 * is the cell deletable? only false (undeletable) if
426 * metadata.deletable is explicitly false -- everything else
427 * counts as true
428 *
429 * @method is_deletable
430 **/
431 Cell.prototype.is_deletable = function () {
432 if (this.metadata.deletable === false) {
433 return false;
434 }
435 return true;
436 };
423
437
424 /**
438 /**
425 * @return {String} - the text before the cursor
439 * @return {String} - the text before the cursor
@@ -760,7 +760,11 b' define(['
760 */
760 */
761 Notebook.prototype.delete_cell = function (index) {
761 Notebook.prototype.delete_cell = function (index) {
762 var i = this.index_or_selected(index);
762 var i = this.index_or_selected(index);
763 var cell = this.get_selected_cell();
763 var cell = this.get_cell(i);
764 if (!cell.is_deletable()) {
765 return this;
766 }
767
764 this.undelete_backup = cell.toJSON();
768 this.undelete_backup = cell.toJSON();
765 $('#undelete_cell').removeClass('disabled');
769 $('#undelete_cell').removeClass('disabled');
766 if (this.is_valid_cell_index(i)) {
770 if (this.is_valid_cell_index(i)) {
@@ -1188,6 +1192,10 b' define(['
1188 Notebook.prototype.copy_cell = function () {
1192 Notebook.prototype.copy_cell = function () {
1189 var cell = this.get_selected_cell();
1193 var cell = this.get_selected_cell();
1190 this.clipboard = cell.toJSON();
1194 this.clipboard = cell.toJSON();
1195 // remove undeletable status from the copied cell
1196 if (this.clipboard.metadata.deletable !== undefined) {
1197 delete this.clipboard.metadata.deletable;
1198 }
1191 this.enable_paste();
1199 this.enable_paste();
1192 };
1200 };
1193
1201
@@ -1,6 +1,33 b''
1
1
2 // Test
2 // Test
3 casper.notebook_test(function () {
3 casper.notebook_test(function () {
4 var a = 'ab\ncd';
5 var b = 'print("b")';
6 var c = 'print("c")';
7
8 var that = this;
9 var cell_is_mergeable = function (index) {
10 // Get the mergeable status of a cell.
11 return that.evaluate(function (index) {
12 var cell = IPython.notebook.get_cell(index);
13 return cell.is_mergeable();
14 }, index);
15 };
16
17 var cell_is_splittable = function (index) {
18 // Get the splittable status of a cell.
19 return that.evaluate(function (index) {
20 var cell = IPython.notebook.get_cell(index);
21 return cell.is_splittable();
22 }, index);
23 };
24
25 var close_dialog = function () {
26 this.evaluate(function(){
27 $('div.modal-footer button.btn-default').click();
28 }, {});
29 };
30
4 this.then(function () {
31 this.then(function () {
5 // Split and merge cells
32 // Split and merge cells
6 this.select_cell(0);
33 this.select_cell(0);
@@ -16,6 +43,93 b' casper.notebook_test(function () {'
16 this.select_cell(0); // Move up to cell 0
43 this.select_cell(0); // Move up to cell 0
17 this.trigger_keydown('shift-m'); // Merge
44 this.trigger_keydown('shift-m'); // Merge
18 this.validate_notebook_state('merge', 'command', 0);
45 this.validate_notebook_state('merge', 'command', 0);
19 this.test.assertEquals(this.get_cell_text(0), 'ab\ncd', 'merge; Verify that cell 0 has the merged contents.');
46 this.test.assertEquals(this.get_cell_text(0), a, 'merge; Verify that cell 0 has the merged contents.');
47 });
48
49 // add some more cells and test splitting/merging when a cell is not deletable
50 this.then(function () {
51 this.append_cell(b);
52 this.append_cell(c);
53 });
54
55 this.thenEvaluate(function() {
56 IPython.notebook.get_cell(1).metadata.deletable = false;
57 });
58
59 // Check that merge/split status are correct
60 this.then(function () {
61 this.test.assert(cell_is_splittable(0), 'Cell 0 is splittable');
62 this.test.assert(cell_is_mergeable(0), 'Cell 0 is mergeable');
63 this.test.assert(!cell_is_splittable(1), 'Cell 1 is not splittable');
64 this.test.assert(!cell_is_mergeable(1), 'Cell 1 is not mergeable');
65 this.test.assert(cell_is_splittable(2), 'Cell 2 is splittable');
66 this.test.assert(cell_is_mergeable(2), 'Cell 2 is mergeable');
67 });
68
69 // Try to merge cell 0 below with cell 1
70 this.then(function () {
71 this.select_cell(0);
72 this.trigger_keydown('esc');
73 this.trigger_keydown('shift-m');
74 this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 0 down: There are still 3 cells');
75 this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 0 down: Cell 0 is unchanged');
76 this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 0 down: Cell 1 is unchanged');
77 this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 0 down: Cell 2 is unchanged');
78 this.validate_notebook_state('shift-m', 'command', 0);
79 });
80
81 // Try to merge cell 1 above with cell 0
82 this.then(function () {
83 this.select_cell(1);
84 });
85 this.thenEvaluate(function () {
86 IPython.notebook.merge_cell_above();
87 });
88 this.then(function () {
89 this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 1 up: There are still 3 cells');
90 this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 1 up: Cell 0 is unchanged');
91 this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 1 up: Cell 1 is unchanged');
92 this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 1 up: Cell 2 is unchanged');
93 this.validate_notebook_state('merge up', 'command', 1);
94 });
95
96 // Try to split cell 1
97 this.then(function () {
98 this.select_cell(1);
99 this.trigger_keydown('enter');
100 this.set_cell_editor_cursor(1, 0, 2);
101 this.trigger_keydown('ctrl-shift-subtract'); // Split
102 this.test.assertEquals(this.get_cells_length(), 3, 'Split cell 1: There are still 3 cells');
103 this.test.assertEquals(this.get_cell_text(0), a, 'Split cell 1: Cell 0 is unchanged');
104 this.test.assertEquals(this.get_cell_text(1), b, 'Split cell 1: Cell 1 is unchanged');
105 this.test.assertEquals(this.get_cell_text(2), c, 'Split cell 1: Cell 2 is unchanged');
106 this.validate_notebook_state('ctrl-shift-subtract', 'edit', 1);
107 });
108
109 // Try to merge cell 1 down
110 this.then(function () {
111 this.select_cell(1);
112 this.trigger_keydown('esc');
113 this.trigger_keydown('shift-m');
114 this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 1 down: There are still 3 cells');
115 this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 1 down: Cell 0 is unchanged');
116 this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 1 down: Cell 1 is unchanged');
117 this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 1 down: Cell 2 is unchanged');
118 this.validate_notebook_state('shift-m', 'command', 1);
119 });
120
121 // Try to merge cell 2 above with cell 1
122 this.then(function () {
123 this.select_cell(2);
124 });
125 this.thenEvaluate(function () {
126 IPython.notebook.merge_cell_above();
127 });
128 this.then(function () {
129 this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 2 up: There are still 3 cells');
130 this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 2 up: Cell 0 is unchanged');
131 this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 2 up: Cell 1 is unchanged');
132 this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 2 up: Cell 2 is unchanged');
133 this.validate_notebook_state('merge up', 'command', 2);
20 });
134 });
21 }); No newline at end of file
135 });
General Comments 0
You need to be logged in to leave comments. Login now