##// END OF EJS Templates
Fix #4777 and #7887...
Fix #4777 and #7887 The function in charge of actually converting cursor offset to CodeMirror line number and character number was actually crashing when the cursor was at the last character (loop until undefined, then access length of variable, which is undefined). This was hiding a bug in which when you would completer to a single completion pressing tab after as-you-type filtering, the completion would be completed twice. The logic that was supposed to detect whether or not all completions had a common prefix was actually faulty as the common prefix used to be a string but was then changed to an object. Hence the logic to check whether or not there was actually a common prefix was always true, even for empty string, leading to the deletion of the line (replace by '') in some cases.

File last commit:

r17999:bfe2f0f8
r20538:ae7f6d6a
Show More
dualmode_merge.js
135 lines | 6.0 KiB | application/javascript | JavascriptLexer
// Test
casper.notebook_test(function () {
var a = 'ab\ncd';
var b = 'print("b")';
var c = 'print("c")';
var that = this;
var cell_is_mergeable = function (index) {
// Get the mergeable status of a cell.
return that.evaluate(function (index) {
var cell = IPython.notebook.get_cell(index);
return cell.is_mergeable();
}, index);
};
var cell_is_splittable = function (index) {
// Get the splittable status of a cell.
return that.evaluate(function (index) {
var cell = IPython.notebook.get_cell(index);
return cell.is_splittable();
}, index);
};
var close_dialog = function () {
this.evaluate(function(){
$('div.modal-footer button.btn-default').click();
}, {});
};
this.then(function () {
// Split and merge cells
this.select_cell(0);
this.trigger_keydown('a', 'enter'); // Create cell above and enter edit mode.
this.validate_notebook_state('a, enter', 'edit', 0);
this.set_cell_text(0, 'abcd');
this.set_cell_editor_cursor(0, 0, 2);
this.test.assertEquals(this.get_cell_text(0), 'abcd', 'Verify that cell 0 has the new contents.');
this.trigger_keydown('ctrl-shift-subtract'); // Split
this.test.assertEquals(this.get_cell_text(0), 'ab', 'split; Verify that cell 0 has the first half.');
this.test.assertEquals(this.get_cell_text(1), 'cd', 'split; Verify that cell 1 has the second half.');
this.validate_notebook_state('split', 'edit', 1);
this.select_cell(0); // Move up to cell 0
this.trigger_keydown('shift-m'); // Merge
this.validate_notebook_state('merge', 'command', 0);
this.test.assertEquals(this.get_cell_text(0), a, 'merge; Verify that cell 0 has the merged contents.');
});
// add some more cells and test splitting/merging when a cell is not deletable
this.then(function () {
this.append_cell(b);
this.append_cell(c);
});
this.thenEvaluate(function() {
IPython.notebook.get_cell(1).metadata.deletable = false;
});
// Check that merge/split status are correct
this.then(function () {
this.test.assert(cell_is_splittable(0), 'Cell 0 is splittable');
this.test.assert(cell_is_mergeable(0), 'Cell 0 is mergeable');
this.test.assert(!cell_is_splittable(1), 'Cell 1 is not splittable');
this.test.assert(!cell_is_mergeable(1), 'Cell 1 is not mergeable');
this.test.assert(cell_is_splittable(2), 'Cell 2 is splittable');
this.test.assert(cell_is_mergeable(2), 'Cell 2 is mergeable');
});
// Try to merge cell 0 below with cell 1
this.then(function () {
this.select_cell(0);
this.trigger_keydown('esc');
this.trigger_keydown('shift-m');
this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 0 down: There are still 3 cells');
this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 0 down: Cell 0 is unchanged');
this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 0 down: Cell 1 is unchanged');
this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 0 down: Cell 2 is unchanged');
this.validate_notebook_state('shift-m', 'command', 0);
});
// Try to merge cell 1 above with cell 0
this.then(function () {
this.select_cell(1);
});
this.thenEvaluate(function () {
IPython.notebook.merge_cell_above();
});
this.then(function () {
this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 1 up: There are still 3 cells');
this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 1 up: Cell 0 is unchanged');
this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 1 up: Cell 1 is unchanged');
this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 1 up: Cell 2 is unchanged');
this.validate_notebook_state('merge up', 'command', 1);
});
// Try to split cell 1
this.then(function () {
this.select_cell(1);
this.trigger_keydown('enter');
this.set_cell_editor_cursor(1, 0, 2);
this.trigger_keydown('ctrl-shift-subtract'); // Split
this.test.assertEquals(this.get_cells_length(), 3, 'Split cell 1: There are still 3 cells');
this.test.assertEquals(this.get_cell_text(0), a, 'Split cell 1: Cell 0 is unchanged');
this.test.assertEquals(this.get_cell_text(1), b, 'Split cell 1: Cell 1 is unchanged');
this.test.assertEquals(this.get_cell_text(2), c, 'Split cell 1: Cell 2 is unchanged');
this.validate_notebook_state('ctrl-shift-subtract', 'edit', 1);
});
// Try to merge cell 1 down
this.then(function () {
this.select_cell(1);
this.trigger_keydown('esc');
this.trigger_keydown('shift-m');
this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 1 down: There are still 3 cells');
this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 1 down: Cell 0 is unchanged');
this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 1 down: Cell 1 is unchanged');
this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 1 down: Cell 2 is unchanged');
this.validate_notebook_state('shift-m', 'command', 1);
});
// Try to merge cell 2 above with cell 1
this.then(function () {
this.select_cell(2);
});
this.thenEvaluate(function () {
IPython.notebook.merge_cell_above();
});
this.then(function () {
this.test.assertEquals(this.get_cells_length(), 3, 'Merge cell 2 up: There are still 3 cells');
this.test.assertEquals(this.get_cell_text(0), a, 'Merge cell 2 up: Cell 0 is unchanged');
this.test.assertEquals(this.get_cell_text(1), b, 'Merge cell 2 up: Cell 1 is unchanged');
this.test.assertEquals(this.get_cell_text(2), c, 'Merge cell 2 up: Cell 2 is unchanged');
this.validate_notebook_state('merge up', 'command', 2);
});
});