This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
EditorCommandHandlers-test.js
505 lines (372 loc) · 22.9 KB
/
EditorCommandHandlers-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, expect, beforeEach, afterEach, waitsFor, runs, $ */
define(function (require, exports, module) {
'use strict';
var Editor = require("editor/Editor").Editor,
EditorCommandHandlers = require("editor/EditorCommandHandlers"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
SpecRunnerUtils = require("spec/SpecRunnerUtils"),
EditorUtils = require("editor/EditorUtils");
describe("EditorCommandHandlers", function () {
var defaultContent = "function foo() {\n" +
" function bar() {\n" +
" \n" +
" a();\n" +
" \n" +
" }\n" +
"\n" +
"}";
var myDocument, myEditor;
beforeEach(function () {
// create dummy Document for the Editor
myDocument = SpecRunnerUtils.createMockDocument(defaultContent);
// create Editor instance (containing a CodeMirror instance)
$("body").append("<div id='editor'/>");
myEditor = new Editor(myDocument, true, "javascript", $("#editor").get(0), {});
// Must be focused so editor commands target it
myEditor.focus();
});
afterEach(function () {
myEditor.destroy();
myEditor = null;
$("#editor").remove();
myDocument = null;
});
// Helper functions for testing cursor position / selection range
function expectCursorAt(pos) {
var selection = myEditor.getSelection();
expect(selection.start).toEqual(selection.end);
expect(selection.start).toEqual(pos);
}
function expectSelection(sel) {
expect(myEditor.getSelection()).toEqual(sel);
}
describe("Line comment/uncomment", function () {
it("should comment/uncomment a single line, cursor at start", function () {
myEditor.setCursorPos(3, 0);
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
var lines = defaultContent.split("\n");
lines[3] = "// a();";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 3, ch: 2});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectCursorAt({line: 3, ch: 0});
});
it("should comment/uncomment a single line, cursor at end", function () {
myEditor.setCursorPos(3, 12);
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
var lines = defaultContent.split("\n");
lines[3] = "// a();";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 3, ch: 14});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectCursorAt({line: 3, ch: 12});
});
it("should comment/uncomment first line in file", function () {
myEditor.setCursorPos(0, 0);
CommandManager.execute(Commands.EDIT_LINE_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});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectCursorAt({line: 0, ch: 0});
});
it("should comment/uncomment a single partly-selected line", function () {
// select "function" on line 1
myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 12});
CommandManager.execute(Commands.EDIT_LINE_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: 6}, end: {line: 1, ch: 14}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 4}, end: {line: 1, ch: 12}});
});
it("should comment/uncomment a single selected line", function () {
// selection covers all of line's text, but not \n at end
myEditor.setSelection({line: 1, ch: 0}, {line: 1, ch: 20});
CommandManager.execute(Commands.EDIT_LINE_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: 0}, end: {line: 1, ch: 22}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 1, ch: 20}});
});
it("should comment/uncomment a single fully-selected line (including LF)", function () {
// selection including \n at end of line
myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_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: 0}, end: {line: 2, ch: 0}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}});
});
it("should comment/uncomment multiple selected lines", function () {
// selection including \n at end of line
myEditor.setSelection({line: 1, ch: 0}, {line: 6, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
var lines = defaultContent.split("\n");
lines[1] = "// function bar() {";
lines[2] = "// ";
lines[3] = "// a();";
lines[4] = "// ";
lines[5] = "// }";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}});
});
it("should comment/uncomment ragged multi-line selection", function () {
myEditor.setSelection({line: 1, ch: 6}, {line: 3, ch: 9});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
var lines = defaultContent.split("\n");
lines[1] = "// function bar() {";
lines[2] = "// ";
lines[3] = "// a();";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 8}, end: {line: 3, ch: 11}});
expect(myEditor.getSelectedText()).toEqual("nction bar() {\n// \n// a");
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 6}, end: {line: 3, ch: 9}});
});
it("should comment/uncomment after select all", function () {
myEditor.setSelection({line: 0, ch: 0}, {line: 7, ch: 1});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
var expectedText = "//function foo() {\n" +
"// function bar() {\n" +
"// \n" +
"// a();\n" +
"// \n" +
"// }\n" +
"//\n" +
"//}";
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 0, ch: 0}, end: {line: 7, ch: 3}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 0, ch: 0}, end: {line: 7, ch: 1}});
});
it("should comment/uncomment lines that were partially commented out already, our style", function () {
// Start with line 3 commented out, with "//" at column 0
var lines = defaultContent.split("\n");
lines[3] = "// a();";
var startingContent = lines.join("\n");
myDocument.setText(startingContent);
// select lines 1-3
myEditor.setSelection({line: 1, ch: 0}, {line: 4, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
lines = defaultContent.split("\n");
lines[1] = "// function bar() {";
lines[2] = "// ";
lines[3] = "//// a();";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(startingContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}});
});
it("should comment/uncomment lines that were partially commented out already, comment closer to code", function () {
// Start with line 3 commented out, with "//" snug against the code
var lines = defaultContent.split("\n");
lines[3] = " //a();";
var startingContent = lines.join("\n");
myDocument.setText(startingContent);
// select lines 1-3
myEditor.setSelection({line: 1, ch: 0}, {line: 4, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
lines = defaultContent.split("\n");
lines[1] = "// function bar() {";
lines[2] = "// ";
lines[3] = "// //a();";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(startingContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}});
});
it("should uncomment indented, aligned comments", function () {
// Start with lines 1-5 commented out, with "//" all aligned at column 4
var lines = defaultContent.split("\n");
lines[1] = " //function bar() {";
lines[2] = " // ";
lines[3] = " // a();";
lines[4] = " // ";
lines[5] = " //}";
var startingContent = lines.join("\n");
myDocument.setText(startingContent);
// select lines 1-5
myEditor.setSelection({line: 1, ch: 0}, {line: 6, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}});
});
it("should uncomment ragged partial comments", function () {
// Start with lines 1-5 commented out, with "//" snug up against each non-blank line's code
var lines = defaultContent.split("\n");
lines[1] = " //function bar() {";
lines[2] = " ";
lines[3] = " //a();";
lines[4] = " ";
lines[5] = " //}";
var startingContent = lines.join("\n");
myDocument.setText(startingContent);
// select lines 1-5
myEditor.setSelection({line: 1, ch: 0}, {line: 6, ch: 0});
CommandManager.execute(Commands.EDIT_LINE_COMMENT, myEditor);
expect(myDocument.getText()).toEqual(defaultContent);
expectSelection({start: {line: 1, ch: 0}, end: {line: 6, ch: 0}});
});
});
describe("Duplicate", function () {
it("should duplicate whole line if no selection", function () {
// place cursor in middle of line 1
myEditor.setCursorPos(1, 10);
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(1, 0, " function bar() {");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 2, ch: 10});
});
it("should duplicate line + \n if selected line is at end of file", function () {
var lines = defaultContent.split("\n"),
len = lines.length;
// place cursor at the beginning of the last line
myEditor.setCursorPos(len - 1, 0);
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
lines.push("}");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: len, ch: 0});
});
it("should duplicate first line", function () {
// place cursor at start of line 0
myEditor.setCursorPos(0, 0);
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(0, 0, "function foo() {");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 1, ch: 0});
});
it("should duplicate empty line", function () {
// place cursor on line 6
myEditor.setCursorPos(6, 0);
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(6, 0, "");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectCursorAt({line: 7, ch: 0});
});
it("should duplicate selection within a line", function () {
// select "bar" on line 1
myEditor.setSelection({line: 1, ch: 13}, {line: 1, ch: 16});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines[1] = " function barbar() {";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 16}, end: {line: 1, ch: 19}});
});
it("should duplicate when entire line selected, excluding newline", function () {
// select all of line 1, EXcluding trailing \n
myEditor.setSelection({line: 1, ch: 0}, {line: 1, ch: 20});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines[1] = " function bar() { function bar() {";
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 1, ch: 20}, end: {line: 1, ch: 40}});
});
it("should duplicate when entire line selected, including newline", function () {
// select all of line 1, INcluding trailing \n
myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(1, 0, " function bar() {");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 2, ch: 0}, end: {line: 3, ch: 0}});
});
it("should duplicate when multiple lines selected", function () {
// select lines 1-3
myEditor.setSelection({line: 1, ch: 0}, {line: 4, ch: 0});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(1, 0, " function bar() {",
" ",
" a();");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 4, ch: 0}, end: {line: 7, ch: 0}});
});
it("should duplicate selection crossing line boundary", function () {
// select from middle of line 1 to middle of line 3
myEditor.setSelection({line: 1, ch: 13}, {line: 3, ch: 11});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var lines = defaultContent.split("\n");
lines.splice(1, 3, " function bar() {",
" ",
" a()bar() {",
" ",
" a();");
var expectedText = lines.join("\n");
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 3, ch: 11}, end: {line: 5, ch: 11}});
});
it("should duplicate after select all", function () {
myEditor.setSelection({line: 0, ch: 0}, {line: 7, ch: 1});
CommandManager.execute(Commands.EDIT_DUPLICATE, myEditor);
var expectedText = defaultContent + defaultContent;
expect(myDocument.getText()).toEqual(expectedText);
expectSelection({start: {line: 7, ch: 1}, end: {line: 14, ch: 1}});
});
});
});
});