editor_text_edit_test.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. if (typeof process !== "undefined") {
  31. require("amd-loader");
  32. require("./test/mockdom");
  33. }
  34. define(function(require, exports, module) {
  35. "use strict";
  36. var EditSession = require("./edit_session").EditSession;
  37. var Editor = require("./editor").Editor;
  38. var JavaScriptMode = require("./mode/javascript").Mode;
  39. var UndoManager = require("./undomanager").UndoManager;
  40. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  41. var assert = require("./test/assertions");
  42. var whitespace = require("./ext/whitespace");
  43. module.exports = {
  44. "test: delete line from the middle" : function() {
  45. var session = new EditSession(["a", "b", "c", "d"].join("\n"));
  46. var editor = new Editor(new MockRenderer(), session);
  47. editor.moveCursorTo(1, 1);
  48. editor.removeLines();
  49. assert.equal(session.toString(), "a\nc\nd");
  50. assert.position(editor.getCursorPosition(), 1, 0);
  51. editor.removeLines();
  52. assert.equal(session.toString(), "a\nd");
  53. assert.position(editor.getCursorPosition(), 1, 0);
  54. editor.removeLines();
  55. assert.equal(session.toString(), "a");
  56. assert.position(editor.getCursorPosition(), 0, 1);
  57. editor.removeLines();
  58. assert.equal(session.toString(), "");
  59. assert.position(editor.getCursorPosition(), 0, 0);
  60. },
  61. "test: delete multiple selected lines" : function() {
  62. var session = new EditSession(["a", "b", "c", "d"].join("\n"));
  63. var editor = new Editor(new MockRenderer(), session);
  64. editor.moveCursorTo(1, 1);
  65. editor.getSelection().selectDown();
  66. editor.removeLines();
  67. assert.equal(session.toString(), "a\nd");
  68. assert.position(editor.getCursorPosition(), 1, 0);
  69. },
  70. "test: delete first line" : function() {
  71. var session = new EditSession(["a", "b", "c"].join("\n"));
  72. var editor = new Editor(new MockRenderer(), session);
  73. editor.removeLines();
  74. assert.equal(session.toString(), "b\nc");
  75. assert.position(editor.getCursorPosition(), 0, 0);
  76. },
  77. "test: delete last should also delete the new line of the previous line" : function() {
  78. var session = new EditSession(["a", "b", "c", ""].join("\n"));
  79. var editor = new Editor(new MockRenderer(), session);
  80. editor.moveCursorTo(3, 0);
  81. editor.removeLines();
  82. assert.equal(session.toString(), "a\nb\nc");
  83. assert.position(editor.getCursorPosition(), 2, 1);
  84. editor.removeLines();
  85. assert.equal(session.toString(), "a\nb");
  86. assert.position(editor.getCursorPosition(), 1, 1);
  87. },
  88. "test: indent block" : function() {
  89. var session = new EditSession(["a12345", "b12345", "c12345"].join("\n"));
  90. var editor = new Editor(new MockRenderer(), session);
  91. editor.moveCursorTo(1, 3);
  92. editor.getSelection().selectDown();
  93. editor.indent();
  94. assert.equal(["a12345", " b12345", " c12345"].join("\n"), session.toString());
  95. assert.position(editor.getCursorPosition(), 2, 7);
  96. var range = editor.getSelectionRange();
  97. assert.position(range.start, 1, 7);
  98. assert.position(range.end, 2, 7);
  99. },
  100. "test: indent selected lines" : function() {
  101. var session = new EditSession(["a12345", "b12345", "c12345"].join("\n"));
  102. var editor = new Editor(new MockRenderer(), session);
  103. editor.moveCursorTo(1, 0);
  104. editor.getSelection().selectDown();
  105. editor.indent();
  106. assert.equal(["a12345", " b12345", "c12345"].join("\n"), session.toString());
  107. },
  108. "test: no auto indent if cursor is before the {" : function() {
  109. var session = new EditSession("{", new JavaScriptMode());
  110. var editor = new Editor(new MockRenderer(), session);
  111. editor.moveCursorTo(0, 0);
  112. editor.onTextInput("\n");
  113. assert.equal(["", "{"].join("\n"), session.toString());
  114. },
  115. "test: outdent block" : function() {
  116. var session = new EditSession([" a12345", " b12345", " c12345"].join("\n"));
  117. var editor = new Editor(new MockRenderer(), session);
  118. editor.moveCursorTo(0, 5);
  119. editor.getSelection().selectDown();
  120. editor.getSelection().selectDown();
  121. editor.blockOutdent();
  122. assert.equal(session.toString(), [" a12345", "b12345", " c12345"].join("\n"));
  123. assert.position(editor.getCursorPosition(), 2, 1);
  124. var range = editor.getSelectionRange();
  125. assert.position(range.start, 0, 1);
  126. assert.position(range.end, 2, 1);
  127. editor.blockOutdent();
  128. assert.equal(session.toString(), ["a12345", "b12345", "c12345"].join("\n"));
  129. var range = editor.getSelectionRange();
  130. assert.position(range.start, 0, 0);
  131. assert.position(range.end, 2, 0);
  132. },
  133. "test: outent without a selection should update cursor" : function() {
  134. var session = new EditSession(" 12");
  135. var editor = new Editor(new MockRenderer(), session);
  136. editor.moveCursorTo(0, 3);
  137. editor.blockOutdent(" ");
  138. assert.equal(session.toString(), " 12");
  139. assert.position(editor.getCursorPosition(), 0, 0);
  140. },
  141. "test: comment lines should perserve selection" : function() {
  142. var session = new EditSession([" abc", "cde"].join("\n"), new JavaScriptMode());
  143. var editor = new Editor(new MockRenderer(), session);
  144. whitespace.detectIndentation(session);
  145. editor.moveCursorTo(0, 2);
  146. editor.getSelection().selectDown();
  147. editor.toggleCommentLines();
  148. assert.equal(["// abc", "// cde"].join("\n"), session.toString());
  149. var selection = editor.getSelectionRange();
  150. assert.position(selection.start, 0, 5);
  151. assert.position(selection.end, 1, 5);
  152. },
  153. "test: uncomment lines should perserve selection" : function() {
  154. var session = new EditSession(["// abc", "//cde"].join("\n"), new JavaScriptMode());
  155. var editor = new Editor(new MockRenderer(), session);
  156. session.setTabSize(2);
  157. editor.moveCursorTo(0, 1);
  158. editor.getSelection().selectDown();
  159. editor.getSelection().selectRight();
  160. editor.getSelection().selectRight();
  161. editor.toggleCommentLines();
  162. assert.equal([" abc", "cde"].join("\n"), session.toString());
  163. assert.range(editor.getSelectionRange(), 0, 0, 1, 1);
  164. },
  165. "test: toggle comment lines twice should return the original text" : function() {
  166. var session = new EditSession([" abc", "cde", "fg"], new JavaScriptMode());
  167. var editor = new Editor(new MockRenderer(), session);
  168. editor.moveCursorTo(0, 0);
  169. editor.getSelection().selectDown();
  170. editor.getSelection().selectDown();
  171. editor.toggleCommentLines();
  172. editor.toggleCommentLines();
  173. assert.equal([" abc", "cde", "fg"].join("\n"), session.toString());
  174. },
  175. "test: comment lines - if the selection end is at the line start it should stay there": function() {
  176. //select down
  177. var session = new EditSession(["abc", "cde"].join("\n"), new JavaScriptMode());
  178. var editor = new Editor(new MockRenderer(), session);
  179. editor.moveCursorTo(0, 0);
  180. editor.getSelection().selectDown();
  181. editor.toggleCommentLines();
  182. assert.range(editor.getSelectionRange(), 0, 3, 1, 0);
  183. // select up
  184. var session = new EditSession(["abc", "cde"].join("\n"), new JavaScriptMode());
  185. var editor = new Editor(new MockRenderer(), session);
  186. editor.moveCursorTo(1, 0);
  187. editor.getSelection().selectUp();
  188. editor.toggleCommentLines();
  189. assert.range(editor.getSelectionRange(), 0, 3, 1, 0);
  190. },
  191. "test: move lines down should keep selection on moved lines" : function() {
  192. var session = new EditSession(["11", "22", "33", "44"].join("\n"));
  193. var editor = new Editor(new MockRenderer(), session);
  194. editor.moveCursorTo(0, 1);
  195. editor.getSelection().selectDown();
  196. editor.moveLinesDown();
  197. assert.equal(["33", "11", "22", "44"].join("\n"), session.toString());
  198. assert.position(editor.getCursorPosition(), 2, 1);
  199. assert.position(editor.getSelection().getSelectionAnchor(), 1, 1);
  200. assert.position(editor.getSelection().getSelectionLead(), 2, 1);
  201. editor.moveLinesDown();
  202. assert.equal(["33", "44", "11", "22"].join("\n"), session.toString());
  203. assert.position(editor.getCursorPosition(), 3, 1);
  204. assert.position(editor.getSelection().getSelectionAnchor(), 2, 1);
  205. assert.position(editor.getSelection().getSelectionLead(), 3, 1);
  206. // moving again should have no effect
  207. editor.moveLinesDown();
  208. assert.equal(["33", "44", "11", "22"].join("\n"), session.toString());
  209. assert.position(editor.getCursorPosition(), 3, 1);
  210. assert.position(editor.getSelection().getSelectionAnchor(), 2, 1);
  211. assert.position(editor.getSelection().getSelectionLead(), 3, 1);
  212. },
  213. "test: move lines up should keep selection on moved lines" : function() {
  214. var session = new EditSession(["11", "22", "33", "44"].join("\n"));
  215. var editor = new Editor(new MockRenderer(), session);
  216. editor.moveCursorTo(2, 1);
  217. editor.getSelection().selectDown();
  218. editor.moveLinesUp();
  219. assert.equal(session.toString(), ["11", "33", "44", "22"].join("\n"));
  220. assert.position(editor.getCursorPosition(), 2, 1);
  221. assert.position(editor.getSelection().getSelectionAnchor(), 1, 1);
  222. assert.position(editor.getSelection().getSelectionLead(), 2, 1);
  223. editor.moveLinesUp();
  224. assert.equal(session.toString(), ["33", "44", "11", "22"].join("\n"));
  225. assert.position(editor.getCursorPosition(), 1, 1);
  226. assert.position(editor.getSelection().getSelectionAnchor(), 0, 1);
  227. assert.position(editor.getSelection().getSelectionLead(), 1, 1);
  228. },
  229. "test: move line without active selection should not move cursor relative to the moved line" : function() {
  230. var session = new EditSession(["11", "22", "33", "44"].join("\n"));
  231. var editor = new Editor(new MockRenderer(), session);
  232. editor.moveCursorTo(1, 1);
  233. editor.clearSelection();
  234. editor.moveLinesDown();
  235. assert.equal(["11", "33", "22", "44"].join("\n"), session.toString());
  236. assert.position(editor.getCursorPosition(), 2, 1);
  237. editor.clearSelection();
  238. editor.moveLinesUp();
  239. assert.equal(["11", "22", "33", "44"].join("\n"), session.toString());
  240. assert.position(editor.getCursorPosition(), 1, 1);
  241. },
  242. "test: copy lines down should keep selection" : function() {
  243. var session = new EditSession(["11", "22", "33", "44"].join("\n"));
  244. var editor = new Editor(new MockRenderer(), session);
  245. editor.moveCursorTo(1, 1);
  246. editor.getSelection().selectDown();
  247. editor.copyLinesDown();
  248. assert.equal(["11", "22", "33", "22", "33", "44"].join("\n"), session.toString());
  249. assert.position(editor.getCursorPosition(), 4, 1);
  250. assert.position(editor.getSelection().getSelectionAnchor(), 3, 1);
  251. assert.position(editor.getSelection().getSelectionLead(), 4, 1);
  252. },
  253. "test: copy lines up should keep selection" : function() {
  254. var session = new EditSession(["11", "22", "33", "44"].join("\n"));
  255. var editor = new Editor(new MockRenderer(), session);
  256. editor.moveCursorTo(1, 1);
  257. editor.getSelection().selectDown();
  258. editor.copyLinesUp();
  259. assert.equal(["11", "22", "33", "22", "33", "44"].join("\n"), session.toString());
  260. assert.position(editor.getCursorPosition(), 2, 1);
  261. assert.position(editor.getSelection().getSelectionAnchor(), 1, 1);
  262. assert.position(editor.getSelection().getSelectionLead(), 2, 1);
  263. },
  264. "test: input a tab with soft tab should convert it to spaces" : function() {
  265. var session = new EditSession("");
  266. var editor = new Editor(new MockRenderer(), session);
  267. session.setTabSize(2);
  268. session.setUseSoftTabs(true);
  269. editor.onTextInput("\t");
  270. assert.equal(session.toString(), " ");
  271. session.setTabSize(5);
  272. editor.onTextInput("\t");
  273. assert.equal(session.toString(), " ");
  274. },
  275. "test: input tab without soft tabs should keep the tab character" : function() {
  276. var session = new EditSession("");
  277. var editor = new Editor(new MockRenderer(), session);
  278. session.setUseSoftTabs(false);
  279. editor.onTextInput("\t");
  280. assert.equal(session.toString(), "\t");
  281. },
  282. "test: undo/redo for delete line" : function() {
  283. var session = new EditSession(["111", "222", "333"]);
  284. var undoManager = new UndoManager();
  285. session.setUndoManager(undoManager);
  286. var initialText = session.toString();
  287. var editor = new Editor(new MockRenderer(), session);
  288. editor.removeLines();
  289. var step1 = session.toString();
  290. assert.equal(step1, "222\n333");
  291. session.$syncInformUndoManager();
  292. editor.removeLines();
  293. var step2 = session.toString();
  294. assert.equal(step2, "333");
  295. session.$syncInformUndoManager();
  296. editor.removeLines();
  297. var step3 = session.toString();
  298. assert.equal(step3, "");
  299. session.$syncInformUndoManager();
  300. undoManager.undo();
  301. session.$syncInformUndoManager();
  302. assert.equal(session.toString(), step2);
  303. undoManager.undo();
  304. session.$syncInformUndoManager();
  305. assert.equal(session.toString(), step1);
  306. undoManager.undo();
  307. session.$syncInformUndoManager();
  308. assert.equal(session.toString(), initialText);
  309. undoManager.undo();
  310. session.$syncInformUndoManager();
  311. assert.equal(session.toString(), initialText);
  312. },
  313. "test: remove left should remove character left of the cursor" : function() {
  314. var session = new EditSession(["123", "456"]);
  315. var editor = new Editor(new MockRenderer(), session);
  316. editor.moveCursorTo(1, 1);
  317. editor.remove("left");
  318. assert.equal(session.toString(), "123\n56");
  319. },
  320. "test: remove left should remove line break if cursor is at line start" : function() {
  321. var session = new EditSession(["123", "456"]);
  322. var editor = new Editor(new MockRenderer(), session);
  323. editor.moveCursorTo(1, 0);
  324. editor.remove("left");
  325. assert.equal(session.toString(), "123456");
  326. },
  327. "test: remove left should remove tabsize spaces if cursor is on a tab stop and preceeded by spaces" : function() {
  328. var session = new EditSession(["123", " 456"]);
  329. session.setUseSoftTabs(true);
  330. session.setTabSize(4);
  331. var editor = new Editor(new MockRenderer(), session);
  332. editor.moveCursorTo(1, 8);
  333. editor.remove("left");
  334. assert.equal(session.toString(), "123\n 456");
  335. },
  336. "test: transpose at line start should be a noop": function() {
  337. var session = new EditSession(["123", "4567", "89"]);
  338. var editor = new Editor(new MockRenderer(), session);
  339. editor.moveCursorTo(1, 0);
  340. editor.transposeLetters();
  341. assert.equal(session.getValue(), ["123", "4567", "89"].join("\n"));
  342. },
  343. "test: transpose in line should swap the charaters before and after the cursor": function() {
  344. var session = new EditSession(["123", "4567", "89"]);
  345. var editor = new Editor(new MockRenderer(), session);
  346. editor.moveCursorTo(1, 2);
  347. editor.transposeLetters();
  348. assert.equal(session.getValue(), ["123", "4657", "89"].join("\n"));
  349. },
  350. "test: transpose at line end should swap the last two characters": function() {
  351. var session = new EditSession(["123", "4567", "89"]);
  352. var editor = new Editor(new MockRenderer(), session);
  353. editor.moveCursorTo(1, 4);
  354. editor.transposeLetters();
  355. assert.equal(session.getValue(), ["123", "4576", "89"].join("\n"));
  356. },
  357. "test: transpose with non empty selection should be a noop": function() {
  358. var session = new EditSession(["123", "4567", "89"]);
  359. var editor = new Editor(new MockRenderer(), session);
  360. editor.moveCursorTo(1, 1);
  361. editor.getSelection().selectRight();
  362. editor.transposeLetters();
  363. assert.equal(session.getValue(), ["123", "4567", "89"].join("\n"));
  364. },
  365. "test: transpose should move the cursor behind the last swapped character": function() {
  366. var session = new EditSession(["123", "4567", "89"]);
  367. var editor = new Editor(new MockRenderer(), session);
  368. editor.moveCursorTo(1, 2);
  369. editor.transposeLetters();
  370. assert.position(editor.getCursorPosition(), 1, 3);
  371. },
  372. "test: remove to line end": function() {
  373. var session = new EditSession(["123", "4567", "89"]);
  374. var editor = new Editor(new MockRenderer(), session);
  375. editor.moveCursorTo(1, 2);
  376. editor.removeToLineEnd();
  377. assert.equal(session.getValue(), ["123", "45", "89"].join("\n"));
  378. },
  379. "test: remove to line end at line end should remove the new line": function() {
  380. var session = new EditSession(["123", "4567", "89"]);
  381. var editor = new Editor(new MockRenderer(), session);
  382. editor.moveCursorTo(1, 4);
  383. editor.removeToLineEnd();
  384. assert.position(editor.getCursorPosition(), 1, 4);
  385. assert.equal(session.getValue(), ["123", "456789"].join("\n"));
  386. },
  387. "test: transform selection to uppercase": function() {
  388. var session = new EditSession(["ajax", "dot", "org"]);
  389. var editor = new Editor(new MockRenderer(), session);
  390. editor.moveCursorTo(1, 0);
  391. editor.getSelection().selectLineEnd();
  392. editor.toUpperCase()
  393. assert.equal(session.getValue(), ["ajax", "DOT", "org"].join("\n"));
  394. },
  395. "test: transform word to uppercase": function() {
  396. var session = new EditSession(["ajax", "dot", "org"]);
  397. var editor = new Editor(new MockRenderer(), session);
  398. editor.moveCursorTo(1, 0);
  399. editor.toUpperCase()
  400. assert.equal(session.getValue(), ["ajax", "DOT", "org"].join("\n"));
  401. assert.position(editor.getCursorPosition(), 1, 0);
  402. },
  403. "test: transform selection to lowercase": function() {
  404. var session = new EditSession(["AJAX", "DOT", "ORG"]);
  405. var editor = new Editor(new MockRenderer(), session);
  406. editor.moveCursorTo(1, 0);
  407. editor.getSelection().selectLineEnd();
  408. editor.toLowerCase()
  409. assert.equal(session.getValue(), ["AJAX", "dot", "ORG"].join("\n"));
  410. },
  411. "test: transform word to lowercase": function() {
  412. var session = new EditSession(["AJAX", "DOT", "ORG"]);
  413. var editor = new Editor(new MockRenderer(), session);
  414. editor.moveCursorTo(1, 0);
  415. editor.toLowerCase()
  416. assert.equal(session.getValue(), ["AJAX", "dot", "ORG"].join("\n"));
  417. assert.position(editor.getCursorPosition(), 1, 0);
  418. }
  419. };
  420. });
  421. if (typeof module !== "undefined" && module === require.main) {
  422. require("asyncjs").test.testcase(module.exports).exec()
  423. }