From 28bd4151a6739bf2e5605f2ef5c5e73fd69b5b32 Mon Sep 17 00:00:00 2001 From: Peter Flynn Date: Thu, 13 Dec 2012 00:14:03 -0800 Subject: [PATCH] Update block-comment unit tests to account for pull reqs #2121 (handle line comments differently), #2133 (single-line block comment mode when Line Comment invoked in CSS/HTML/etc. - enable test coverage), and #2148 (don't replace whole selection - which means tests must work around #2335). --- test/spec/EditorCommandHandlers-test.js | 665 +++++++++++++----------- 1 file changed, 367 insertions(+), 298 deletions(-) diff --git a/test/spec/EditorCommandHandlers-test.js b/test/spec/EditorCommandHandlers-test.js index 018559ff723..3e330c7250c 100644 --- a/test/spec/EditorCommandHandlers-test.js +++ b/test/spec/EditorCommandHandlers-test.js @@ -22,7 +22,7 @@ */ /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ -/*global define, describe, it, expect, beforeEach, afterEach, waitsFor, runs, $ */ +/*global define, describe, it, expect, beforeEach, afterEach, waitsFor, waits, runs, $ */ define(function (require, exports, module) { 'use strict'; @@ -407,25 +407,54 @@ define(function (require, exports, module) { }); + /** + * Invokes Toggle Block Comment, expects the given selection/cursor & document text, invokes + * it a 2nd time, and then expects the original selection/cursor & document text again. + * @param {!string} expectedCommentedText + * @param {!{ch:number,line:number}|{start:{ch:number,line:number},end:{ch:number,line:number}}} expectedCommentedSel + */ + function testToggleBlock(expectedCommentedText, expectedCommentedSel) { + function expectSel(sel) { + if (sel.start) { + expectSelection(sel); + } else { + expectCursorAt(sel); + } + } + + var startingContent = myDocument.getText(); + var startingSel = myEditor.getSelection(); + + // Toggle comment on + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + expect(myDocument.getText()).toEqual(expectedCommentedText); + expectSel(expectedCommentedSel); + + // Toggle comment off + // Can't immediately call BLOCK_COMMENT again to uncomment because CodeMirror might not + // be done re-tokenizing in response to the first toggle, and BLOCK_COMMENT depends on + // getting correct tokens. See #2335. Ideally we'd listen for onHighlightComplete() but + // it's not clear that will always get called (if CM decides no async work was needed). + // So we just wait until after the async tokenization must have been run. + waits(200); + runs(function () { + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + expect(myDocument.getText()).toEqual(startingContent); + expectSel(startingSel); + }); + } + describe("Block comment/uncomment", function () { beforeEach(setupFullEditor); it("should block comment/uncomment, cursor at start of line", function () { myEditor.setCursorPos(0, 0); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines[0] = "/**/function foo() {"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectCursorAt({line: 0, ch: 2}); - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectCursorAt({line: 0, ch: 0}); + testToggleBlock(expectedText, {line: 0, ch: 2}); }); it("should block comment/uncomment, cursor to left of existing block comment", function () { @@ -438,36 +467,21 @@ define(function (require, exports, module) { // put cursor to left of block myEditor.setCursorPos(3, 4); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - lines[3] = " /**/ /*a();*/"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectCursorAt({line: 3, ch: 6}); - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(startingContent); - expectCursorAt({line: 3, ch: 4}); + testToggleBlock(expectedText, {line: 3, ch: 6}); }); it("should block comment/uncomment, subset of line selected", function () { myEditor.setSelection({line: 1, ch: 13}, {line: 1, ch: 18}); // select "bar()" - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines[1] = " function /*bar()*/ {"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 1, ch: 15}, end: {line: 1, ch: 20}}); // just text within block - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 1, ch: 13}, end: {line: 1, ch: 18}}); // back to original selection + // Selects just text within block + testToggleBlock(expectedText, {start: {line: 1, ch: 15}, end: {line: 1, ch: 20}}); }); it("should block uncomment, cursor within existing sub-line block comment", function () { @@ -505,94 +519,59 @@ define(function (require, exports, module) { it("should block comment/uncomment, selection from mid-line end of line", function () { myEditor.setSelection({line: 3, ch: 8}, {line: 3, ch: 12}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines[3] = " /*a();*/"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 3, ch: 10}, end: {line: 3, ch: 14}}); // just text within block - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 3, ch: 8}, end: {line: 3, ch: 12}}); // back to original selection + // Selects just text within block + testToggleBlock(expectedText, {start: {line: 3, ch: 10}, end: {line: 3, ch: 14}}); }); it("should block comment/uncomment, all of line selected but not newline", function () { myEditor.setSelection({line: 3, ch: 0}, {line: 3, ch: 12}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines[3] = "/* a();*/"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 3, ch: 2}, end: {line: 3, ch: 14}}); // just text within block - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 3, ch: 0}, end: {line: 3, ch: 12}}); // back to original selection + // Selects just text within block + testToggleBlock(expectedText, {start: {line: 3, ch: 2}, end: {line: 3, ch: 14}}); }); it("should block comment/uncomment, all of line selected including newline", function () { myEditor.setSelection({line: 3, ch: 0}, {line: 4, ch: 0}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines.splice(3, 1, "/*", lines[3], "*/"); // inserts new delimiter lines var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}); // original line, but not block-delimiter lines - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 3, ch: 0}, end: {line: 4, ch: 0}}); // back to original selection + // Selects original line, but not block-delimiter lines + testToggleBlock(expectedText, {start: {line: 4, ch: 0}, end: {line: 5, ch: 0}}); }); it("should block comment/uncomment, multiple lines selected", function () { myEditor.setSelection({line: 1, ch: 0}, {line: 6, ch: 0}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines.splice(6, 0, "*/"); // inserts new delimiter lines lines.splice(1, 0, "/*"); var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 2, ch: 0}, end: {line: 7, ch: 0}}); // original lines, but not block-delimiter lines - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}}); // back to original selection + // Selects original lines, but not block-delimiter lines + testToggleBlock(expectedText, {start: {line: 2, ch: 0}, end: {line: 7, ch: 0}}); }); it("should block comment/uncomment, multiple partial lines selected", function () { myEditor.setSelection({line: 1, ch: 13}, {line: 3, ch: 9}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = defaultContent.split("\n"); lines[1] = " function /*bar() {"; lines[3] = " a*/();"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 1, ch: 15}, end: {line: 3, ch: 9}}); // just text within block - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 1, ch: 13}, end: {line: 3, ch: 9}}); // back to original selection + // Selects just text within block + testToggleBlock(expectedText, {start: {line: 1, ch: 15}, end: {line: 3, ch: 9}}); }); // Selections mixing uncommented text and existing block comments @@ -613,7 +592,40 @@ define(function (require, exports, module) { expectSelection({start: {line: 1, ch: 4}, end: {line: 1, ch: 20}}); // range endpoints still align with same text }); - // We no-op in this case since it's ambiguous - can't nest block comments, but was multiple independent uncomments intended? + // We no-op in these cases since it's ambiguous - can't nest block comments, but was multiple independent uncomments intended? + + it("should do nothing, selection covers parts of multiple block comments", function () { + // Start with part of line 1 wrapped in a block comment + var lines = defaultContent.split("\n"); + lines[1] = " /*function*/ /*bar()*/ {"; + var startingContent = lines.join("\n"); + myDocument.setText(startingContent); + + // select end of 1st comment, start of 2nd comment, and the space between them + myEditor.setSelection({line: 1, ch: 9}, {line: 1, ch: 22}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + expect(myDocument.getText()).toEqual(startingContent); + expectSelection({start: {line: 1, ch: 9}, end: {line: 1, ch: 22}}); // no change + }); + + it("should do nothing, selection covers multiple block comments", function () { + // Start with part of line 1 wrapped in a block comment + var lines = defaultContent.split("\n"); + lines[1] = " /*function*/ /*bar()*/ {"; + var startingContent = lines.join("\n"); + myDocument.setText(startingContent); + + // select both block comments and the space between them, but nothing else + myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 26}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + expect(myDocument.getText()).toEqual(startingContent); + expectSelection({start: {line: 1, ch: 4}, end: {line: 1, ch: 26}}); // no change + }); + it("should do nothing, selection covers multiple block comments plus other text", function () { // Start with part of line 1 wrapped in a block comment var lines = defaultContent.split("\n"); @@ -632,13 +644,15 @@ define(function (require, exports, module) { }); - // In cases where a line comment is already present, the block comment command may perform line uncomment instead - describe("Block uncomment auto-switching to line uncomment", function () { + // In cases where a line comment is already present, block comment should work where possible, switch to + // line-uncomment where the selection wouldn't allow a useful block comment, and do nothing where ambiguous. + describe("Block comment around line comments", function () { beforeEach(setupFullEditor); // Selections including existing line comments - it("should switch to line uncomment mode, cursor inside line comment (whitespace to left)", function () { + // Unambiguously safe to assume line-uncomment + it("should switch to line uncomment mode, cursor inside line comment (with only whitespace to left)", function () { // Start with part of line 1 line-commented var lines = defaultContent.split("\n"); lines[1] = " //function bar() {"; @@ -653,6 +667,7 @@ define(function (require, exports, module) { expectCursorAt({line: 1, ch: 16}); }); + // Unambiguously safe to assume line-uncomment it("should switch to line uncomment mode, cursor in whitespace to left of line comment", function () { // Start with part of line 1 line-commented var lines = defaultContent.split("\n"); @@ -668,24 +683,23 @@ define(function (require, exports, module) { expectCursorAt({line: 1, ch: 0}); }); -// it("should switch to line uncomment mode, cursor inside line comment (code to left)", function () { -// // Start with comment ending line 1 -// var lines = defaultContent.split("\n"); -// lines[1] = " function bar() { // comment"; -// var startingContent = lines.join("\n"); -// myDocument.setText(startingContent); -// -// myEditor.setCursorPos(1, 24); -// -// CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); -// -// lines[1] = " function bar() { comment"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectCursorAt({line: 1, ch: 22}); -// }); + // Ambiguous: does user want to remove the line comment? Even invoking Line Comment explicitly wouldn't do that here. + it("should do nothing, cursor inside line comment (with code to left)", function () { + // Start with comment ending line 1 + var lines = defaultContent.split("\n"); + lines[1] = " function bar() { // comment"; + var startingContent = lines.join("\n"); + myDocument.setText(startingContent); + + myEditor.setCursorPos(1, 24); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + expect(myDocument.getText()).toEqual(startingContent); // no change + expectCursorAt({line: 1, ch: 24}); + }); + // Unambiguous: insert empty block comment inside code it("should stay in block comment mode, cursor in code to left of line comment", function () { // Start with comment ending line 1 var lines = defaultContent.split("\n"); @@ -704,25 +718,27 @@ define(function (require, exports, module) { expectCursorAt({line: 1, ch: 14}); }); -// it("should switch to line uncomment mode, all of line-comment line selected (following line is code)", function () { -// var content = "function foo()\n" + -// " // Comment\n" + -// "}"; -// myDocument.setText(content); -// -// myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0}); -// -// CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); -// -// var lines = content.split("\n"); -// lines[1] = " Comment"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}}); -// }); - - it("should switch to line uncomment mode, all of line-comment line selected (following line is whitespace)", function () { + // TODO (#2337): behaves differently (uncomments) if selection ends 1 char earlier + it("should block comment, all of line-comment line selected (following line is code)", function () { + var content = "function foo()\n" + + " // Comment\n" + + "}"; + myDocument.setText(content); + + myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + var lines = content.split("\n"); + lines.splice(2, 0, "*/"); // inserts new delimiter lines + lines.splice(1, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}); // just text within block + }); + + it("should block comment, all of line-comment line selected (following line is whitespace)", function () { var content = "function foo()\n" + " // Comment\n" + " \n" + @@ -734,14 +750,15 @@ define(function (require, exports, module) { CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); var lines = content.split("\n"); - lines[1] = " Comment"; + lines.splice(2, 0, "*/"); // inserts new delimiter lines + lines.splice(1, 0, "/*"); var expectedText = lines.join("\n"); expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}}); + expectSelection({start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}); // just text within block }); - it("should switch to line uncomment mode, all of line-comment line selected (following line is line comment)", function () { + it("should block comment, all of line-comment line selected (following line is line comment)", function () { var content = "function foo()\n" + " // Comment\n" + " // Comment 2\n" + @@ -753,33 +770,35 @@ define(function (require, exports, module) { CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); var lines = content.split("\n"); - lines[1] = " Comment"; + lines.splice(2, 0, "*/"); // inserts new delimiter lines + lines.splice(1, 0, "/*"); var expectedText = lines.join("\n"); expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}}); + expectSelection({start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}); }); -// it("should switch to line uncomment mode, all of line-comment line selected (following line is block comment)", function () { -// var content = "function foo()\n" + -// " // Comment\n" + -// " /* Comment 2 */\n" + -// "}"; -// myDocument.setText(content); -// -// myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0}); -// -// CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); -// -// var lines = content.split("\n"); -// lines[1] = " Comment"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}}); -// }); - - it("should switch to line uncomment mode, multiple line comments selected", function () { + it("should block comment, all of line-comment line selected (following line is block comment)", function () { + var content = "function foo()\n" + + " // Comment\n" + + " /* Comment 2 */\n" + + "}"; + myDocument.setText(content); + + myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + var lines = content.split("\n"); + lines.splice(2, 0, "*/"); // inserts new delimiter lines + lines.splice(1, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 2, ch: 0}, end: {line: 3, ch: 0}}); + }); + + it("should block comment, multiple line comments selected", function () { // Start with all of lines 1-5 line-commented var lines = defaultContent.split("\n"); lines[1] = "// function bar() {"; @@ -787,15 +806,20 @@ define(function (require, exports, module) { lines[3] = "// a();"; lines[4] = "// "; lines[5] = "// }"; - var startingContent = lines.join("\n"); - myDocument.setText(startingContent); + var content = lines.join("\n"); + myDocument.setText(content); myEditor.setSelection({line: 1, ch: 0}, {line: 6, ch: 0}); CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(defaultContent); - expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}}); + lines = content.split("\n"); + lines.splice(6, 0, "*/"); // inserts new delimiter lines + lines.splice(1, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 2, ch: 0}, end: {line: 7, ch: 0}}); }); // Selections mixing uncommented text and existing line comments @@ -817,20 +841,22 @@ define(function (require, exports, module) { " \n" + "}"; - it("should switch to line uncomment mode, selection covers line comment plus whitespace", function () { + it("should block comment, selection covers line comment plus whitespace", function () { myDocument.setText(lineCommentCode); myEditor.setSelection({line: 1, ch: 0}, {line: 3, ch: 4}); CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); var lines = lineCommentCode.split("\n"); - lines[2] = " Floating comment"; + lines[1] = "/* "; // does NOT insert new delimiter lines, since selection wasn't whole lines + lines[3] = " */"; var expectedText = lines.join("\n"); expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 1, ch: 0}, end: {line: 3, ch: 4}}); + expectSelection({start: {line: 1, ch: 2}, end: {line: 3, ch: 4}}); // text between delimiters }); + // TODO (#2337): behaves differently (block comments) if selection ends 1 char later it("should switch to line uncomment mode, selection starts in whitespace & ends in line comment", function () { myDocument.setText(lineCommentCode); myEditor.setSelection({line: 2, ch: 2}, {line: 2, ch: 10}); @@ -845,34 +871,35 @@ define(function (require, exports, module) { expectSelection({start: {line: 2, ch: 2}, end: {line: 2, ch: 8}}); }); -// it("should block comment, selection starts in code & ends in line comment", function () { -// myDocument.setText(lineCommentCode); -// myEditor.setSelection({line: 7, ch: 8}, {line: 7, ch: 28}); -// -// CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines[7] = " /*b(); // post comment*/"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 7, ch: 10}, end: {line: 7, ch: 30}}); -// }); + // TODO (#2337): behaves differently (no-ops) if selection ends 1 char earlier + it("should block comment, selection starts in code & ends in line comment", function () { + myDocument.setText(lineCommentCode); + myEditor.setSelection({line: 7, ch: 8}, {line: 7, ch: 28}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + var lines = lineCommentCode.split("\n"); + lines[7] = " /*b(); // post comment*/"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 7, ch: 10}, end: {line: 7, ch: 30}}); + }); -// it("should block comment, selection starts on line with line comment", function () { -// myDocument.setText(lineCommentCode); -// myEditor.setSelection({line: 4, ch: 0}, {line: 9, ch: 0}); -// -// CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines.splice(9, 0, "*/"); -// lines.splice(4, 0, "/*"); -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 5, ch: 0}, end: {line: 10, ch: 0}}); -// }); + it("should block comment, selection starts on line with line comment", function () { + myDocument.setText(lineCommentCode); + myEditor.setSelection({line: 4, ch: 0}, {line: 9, ch: 0}); + + CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); + + var lines = lineCommentCode.split("\n"); + lines.splice(9, 0, "*/"); + lines.splice(4, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 5, ch: 0}, end: {line: 10, ch: 0}}); + }); it("should block comment, selection ends on line with line comment", function () { myDocument.setText(lineCommentCode); @@ -889,104 +916,178 @@ define(function (require, exports, module) { expectSelection({start: {line: 11, ch: 0}, end: {line: 13, ch: 0}}); }); - it("should switch to line uncomment mode, selection convers several line comments separated by whitespace", function () { + it("should block comment, selection covers several line comments separated by whitespace", function () { myDocument.setText(lineCommentCode); myEditor.setSelection({line: 11, ch: 0}, {line: 14, ch: 0}); CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); var lines = lineCommentCode.split("\n"); - lines[11] = " Attached above"; - lines[13] = " Final floating comment"; + lines.splice(14, 0, "*/"); + lines.splice(11, 0, "/*"); var expectedText = lines.join("\n"); expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 11, ch: 0}, end: {line: 14, ch: 0}}); + expectSelection({start: {line: 12, ch: 0}, end: {line: 15, ch: 0}}); }); }); // In cases where the language only supports block comments, the line comment/uncomment command may perform block comment/uncomment instead -// describe("Line comment auto-switching to block comment", function () { -// var cssContent = "div {\n" + -// " color: red;\n" + -// "}\n" + -// "\n" + -// "/*span {\n" + -// " color: blue;\n" + -// "}*/\n"; -// -// beforeEach(function() { -// setupFullEditor(cssContent, "css"); -// }); -// -// it("should block-comment entire line (except whitespace) that cursor is in", function () { -// myEditor.setCursorPos(1, 4); -// -// CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines[1] = " /*color: red;*/"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectCursorAt({line: 1, ch: 6}); -// }); -// -// it("should block-comment sub-line selection", function () { -// myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 9}); -// -// CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines[1] = " /*color*/: red;"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 1, ch: 6}, end: {line: 1, ch: 11}}); -// }); -// -// it("should block-comment multi-line selection", function () { -// myEditor.setSelection({line: 0, ch: 0}, {line: 3, ch: 0}); -// -// CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines.splice(3, 0, "*/"); -// lines.splice(0, 0, "/*"); -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}}); -// }); -// -// it("should uncomment multi-line block comment selection", function () { -// myEditor.setSelection({line: 4, ch: 0}, {line: 7, ch: 0}); -// -// CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines[4] = "span {"; -// lines[6] = "}"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}}); -// }); -// -// it("should uncomment multi-line block comment that cursor is in", function () { -// myEditor.setCursorPos(5, 4); -// -// CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); -// -// var lines = lineCommentCode.split("\n"); -// lines[4] = "span {"; -// lines[6] = "}"; -// var expectedText = lines.join("\n"); -// -// expect(myDocument.getText()).toEqual(expectedText); -// expectCursorAt({line: 5, ch: 4}); -// }); -// }); + describe("Line comment auto-switching to block comment", function () { + var cssContent = "div {\n" + + " color: red;\n" + + "}\n" + + "\n" + + "/*span {\n" + + " color: blue;\n" + + "}*/\n"; + + beforeEach(function () { + setupFullEditor(cssContent, "css"); + }); + + it("should block-comment entire line that cursor is in", function () { + myEditor.setCursorPos(1, 4); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[1] = "/* color: red;*/"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: 1, ch: 6}); + }); + + it("should block-comment entire line that sub-line selection is in", function () { + myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 9}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[1] = "/* color: red;*/"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 1, ch: 6}, end: {line: 1, ch: 11}}); + }); + + it("should block-comment full multi-line selection", function () { + myEditor.setSelection({line: 0, ch: 0}, {line: 3, ch: 0}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines.splice(3, 0, "*/"); + lines.splice(0, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}}); + }); + + it("should block-comment partial multi-line selection as if it were full", function () { + myEditor.setSelection({line: 0, ch: 3}, {line: 1, ch: 10}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines.splice(2, 0, "*/"); + lines.splice(0, 0, "/*"); + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 1, ch: 3}, end: {line: 2, ch: 10}}); // range endpoints still align with same text + }); + + it("should uncomment multi-line block comment selection, selected exactly", function () { + myEditor.setSelection({line: 4, ch: 0}, {line: 6, ch: 3}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 4, ch: 0}, end: {line: 6, ch: 1}}); + }); + +/* + it("should uncomment multi-line block comment selection, selected including trailing newline", function () { // #2339: no-ops + myEditor.setSelection({line: 4, ch: 0}, {line: 7, ch: 0}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 4, ch: 0}, end: {line: 7, ch: 0}}); + }); +*/ + + it("should uncomment multi-line block comment selection, only start selected", function () { + myEditor.setSelection({line: 4, ch: 0}, {line: 5, ch: 8}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 4, ch: 0}, end: {line: 5, ch: 8}}); + }); + + it("should uncomment multi-line block comment selection, only middle selected", function () { + myEditor.setSelection({line: 5, ch: 0}, {line: 5, ch: 8}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 5, ch: 0}, end: {line: 5, ch: 8}}); + }); + +/* + it("should uncomment multi-line block comment selection, only end selected", function () { // #2339: no-ops & shifts selection weirdly + myEditor.setSelection({line: 5, ch: 8}, {line: 6, ch: 3}); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectSelection({start: {line: 5, ch: 8}, end: {line: 6, ch: 1}}); + }); +*/ + + it("should uncomment multi-line block comment that cursor is in", function () { + myEditor.setCursorPos(5, 4); + + CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); + + var lines = cssContent.split("\n"); + lines[4] = "span {"; + lines[6] = "}"; + var expectedText = lines.join("\n"); + + expect(myDocument.getText()).toEqual(expectedText); + expectCursorAt({line: 5, ch: 4}); + }); + }); describe("Comment/uncomment with mixed sytnax modes", function () { @@ -1017,43 +1118,27 @@ define(function (require, exports, module) { // Correct behavior for line and block comment commands - it("should block comment generic HTML code", function () { + it("should block comment/uncomment generic HTML code", function () { myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 10}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = htmlContent.split("\n"); lines[1] = " "; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: { line: 1, ch: 8 }, end: {line: 1, ch: 14}}); - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(htmlContent); - expectSelection({start: { line: 1, ch: 4 }, end: {line: 1, ch: 10}}); + testToggleBlock(expectedText, {start: { line: 1, ch: 8 }, end: {line: 1, ch: 14}}); }); - it("should block comment generic CSS code", function () { + it("should block comment/uncomment generic CSS code", function () { myEditor.setSelection({line: 4, ch: 16}, {line: 4, ch: 32}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = htmlContent.split("\n"); lines[4] = " /*font-size: 15px;*/"; var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 4, ch: 18}, end: {line: 4, ch: 34}}); - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(htmlContent); - expectSelection({start: {line: 4, ch: 16}, end: {line: 4, ch: 32}}); + testToggleBlock(expectedText, {start: {line: 4, ch: 18}, end: {line: 4, ch: 34}}); }); - it("should line comment generic JS code", function () { + it("should line comment/uncomment generic JS code", function () { myEditor.setCursorPos(10, 0); CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor); @@ -1071,42 +1156,26 @@ define(function (require, exports, module) { expectCursorAt({line: 10, ch: 0}); }); - it("should block comment generic JS code", function () { + it("should block comment/uncomment generic JS code", function () { myEditor.setSelection({line: 8, ch: 0}, {line: 13, ch: 0}); - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - var lines = htmlContent.split("\n"); lines.splice(13, 0, "*/"); lines.splice(8, 0, "/*"); var expectedText = lines.join("\n"); - expect(myDocument.getText()).toEqual(expectedText); - expectSelection({start: {line: 9, ch: 0}, end: {line: 14, ch: 0}}); - - // Uncomment - CommandManager.execute(Commands.EDIT_BLOCK_COMMENT, myEditor); - expect(myDocument.getText()).toEqual(htmlContent); - expectSelection({start: {line: 8, ch: 0}, end: {line: 13, ch: 0}}); + testToggleBlock(expectedText, {start: {line: 9, ch: 0}, end: {line: 14, ch: 0}}); }); - it("should use HTML comments on outside of