From d1a936b783828d918103473c79458e8e41090432 Mon Sep 17 00:00:00 2001 From: Narciso Jaramillo Date: Fri, 7 Mar 2014 18:01:17 -0800 Subject: [PATCH 1/2] Fix unit tests for getSelectedText() to reflect new behavior --- src/editor/Editor.js | 4 ++-- test/spec/Editor-test.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/editor/Editor.js b/src/editor/Editor.js index b475eb8eb86..60b522d39a4 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -1042,8 +1042,8 @@ define(function (require, exports, module) { * Returns the currently selected text, or "" if no selection. Includes \n if the * selection spans multiple lines (does NOT reflect the Document's line-endings style). By * default, returns only the contents of the primary selection, unless `allSelections` is true. - * @param {boolean=} allSelections Whether to return the contents of all selections instead - * of just the primary selection. Default false. + * @param {boolean=} allSelections Whether to return the contents of all selections (separated + * by newlines) instead of just the primary selection. Default false. * @return {!string} The selected text. */ Editor.prototype.getSelectedText = function (allSelections) { diff --git a/test/spec/Editor-test.js b/test/spec/Editor-test.js index 5b096c13033..05516cefedd 100644 --- a/test/spec/Editor-test.js +++ b/test/spec/Editor-test.js @@ -608,24 +608,40 @@ define(function (require, exports, module) { }); it("should return the contents of a single selection", function () { - myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 12}); - expect(myEditor.getSelectedText()).toEqual("line"); + myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 14}); + expect(myEditor.getSelectedText()).toEqual("line 0"); }); - it("should return the contents of a multiple selection, separated by newlines", function () { - myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 12}}, - {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}}, - {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 12}} + it("should return the primary selection by default", function () { + myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}}, + {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, + {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} ]); - expect(myEditor.getSelectedText()).toEqual("line\nline\nline"); + expect(myEditor.getSelectedText()).toEqual("line 2"); + }); + + it("should return a primary selection other than the last", function () { + myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}}, + {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, + {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} + ], 1); + expect(myEditor.getSelectedText()).toEqual("line 1"); + }); + + it("should return the contents of a multiple selection separated by newlines if allSelections is true", function () { + myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}}, + {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, + {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} + ]); + expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2"); }); it("should return the contents of a multiple selection when some selections are reversed", function () { - myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 12}, head: {line: 0, ch: 8}}, - {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}}, - {anchor: {line: 2, ch: 12}, head: {line: 2, ch: 8}} + myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 14}, head: {line: 0, ch: 8}}, + {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, + {anchor: {line: 2, ch: 14}, head: {line: 2, ch: 8}} ]); - expect(myEditor.getSelectedText()).toEqual("line\nline\nline"); + expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2"); }); }); From ceda0807d4831e01aa3629554c5a564e66fbc59f Mon Sep 17 00:00:00 2001 From: Narciso Jaramillo Date: Mon, 10 Mar 2014 11:01:31 -0700 Subject: [PATCH 2/2] Collapse some test cases of getSelectedText() and check both values of argument for each of them --- test/spec/Editor-test.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/test/spec/Editor-test.js b/test/spec/Editor-test.js index 05516cefedd..f83b855b450 100644 --- a/test/spec/Editor-test.js +++ b/test/spec/Editor-test.js @@ -605,19 +605,22 @@ define(function (require, exports, module) { it("should return empty string for a cursor", function () { myEditor._codeMirror.setCursor(0, 2); expect(myEditor.getSelectedText()).toEqual(""); + expect(myEditor.getSelectedText(true)).toEqual(""); }); it("should return the contents of a single selection", function () { myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 14}); expect(myEditor.getSelectedText()).toEqual("line 0"); + expect(myEditor.getSelectedText(true)).toEqual("line 0"); }); - it("should return the primary selection by default", function () { + it("should return the primary selection by default, but concatenate contents if allSelections is true", function () { myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}}, {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} ]); expect(myEditor.getSelectedText()).toEqual("line 2"); + expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2"); }); it("should return a primary selection other than the last", function () { @@ -626,13 +629,6 @@ define(function (require, exports, module) { {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} ], 1); expect(myEditor.getSelectedText()).toEqual("line 1"); - }); - - it("should return the contents of a multiple selection separated by newlines if allSelections is true", function () { - myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}}, - {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, - {anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}} - ]); expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2"); }); @@ -641,6 +637,7 @@ define(function (require, exports, module) { {anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}}, {anchor: {line: 2, ch: 14}, head: {line: 2, ch: 8}} ]); + expect(myEditor.getSelectedText()).toEqual("line 2"); expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2"); }); });