selection_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. }
  33. define(function(require, exports, module) {
  34. "use strict";
  35. var EditSession = require("./edit_session").EditSession;
  36. var assert = require("./test/assertions");
  37. module.exports = {
  38. createSession : function(rows, cols) {
  39. var line = new Array(cols + 1).join("a");
  40. var text = new Array(rows).join(line + "\n") + line;
  41. return new EditSession(text);
  42. },
  43. "test: move cursor to end of file should place the cursor on last row and column" : function() {
  44. var session = this.createSession(200, 10);
  45. var selection = session.getSelection();
  46. selection.moveCursorFileEnd();
  47. assert.position(selection.getCursor(), 199, 10);
  48. },
  49. "test: moveCursor to start of file should place the cursor on the first row and column" : function() {
  50. var session = this.createSession(200, 10);
  51. var selection = session.getSelection();
  52. selection.moveCursorFileStart();
  53. assert.position(selection.getCursor(), 0, 0);
  54. },
  55. "test: move selection lead to end of file" : function() {
  56. var session = this.createSession(200, 10);
  57. var selection = session.getSelection();
  58. selection.moveCursorTo(100, 5);
  59. selection.selectFileEnd();
  60. var range = selection.getRange();
  61. assert.position(range.start, 100, 5);
  62. assert.position(range.end, 199, 10);
  63. },
  64. "test: move selection lead to start of file" : function() {
  65. var session = this.createSession(200, 10);
  66. var selection = session.getSelection();
  67. selection.moveCursorTo(100, 5);
  68. selection.selectFileStart();
  69. var range = selection.getRange();
  70. assert.position(range.start, 0, 0);
  71. assert.position(range.end, 100, 5);
  72. },
  73. "test: move cursor word right" : function() {
  74. var session = new EditSession([
  75. "ab",
  76. " Juhu Kinners (abc, 12)",
  77. " cde"
  78. ].join("\n"));
  79. var selection = session.getSelection();
  80. session.$selectLongWords = true;
  81. selection.moveCursorDown();
  82. assert.position(selection.getCursor(), 1, 0);
  83. selection.moveCursorWordRight();
  84. assert.position(selection.getCursor(), 1, 5);
  85. selection.moveCursorWordRight();
  86. assert.position(selection.getCursor(), 1, 13);
  87. selection.moveCursorWordRight();
  88. assert.position(selection.getCursor(), 1, 18);
  89. selection.moveCursorWordRight();
  90. assert.position(selection.getCursor(), 1, 22);
  91. // wrap line
  92. selection.moveCursorWordRight();
  93. assert.position(selection.getCursor(), 2, 4);
  94. selection.moveCursorWordRight();
  95. assert.position(selection.getCursor(), 2, 4);
  96. },
  97. "test: select word right if cursor in word" : function() {
  98. var session = new EditSession("Juhu Kinners");
  99. var selection = session.getSelection();
  100. selection.moveCursorTo(0, 2);
  101. selection.moveCursorWordRight();
  102. assert.position(selection.getCursor(), 0, 4);
  103. },
  104. "test: moveCursor word left" : function() {
  105. var session = new EditSession([
  106. "ab",
  107. " Juhu Kinners (abc, 12)",
  108. " cde"
  109. ].join("\n"));
  110. var selection = session.getSelection();
  111. session.$selectLongWords = true;
  112. selection.moveCursorDown();
  113. selection.moveCursorLineEnd();
  114. assert.position(selection.getCursor(), 1, 23);
  115. selection.moveCursorWordLeft();
  116. assert.position(selection.getCursor(), 1, 20);
  117. selection.moveCursorWordLeft();
  118. assert.position(selection.getCursor(), 1, 15);
  119. selection.moveCursorWordLeft();
  120. assert.position(selection.getCursor(), 1, 6);
  121. selection.moveCursorWordLeft();
  122. assert.position(selection.getCursor(), 1, 1);
  123. // wrap line
  124. selection.moveCursorWordLeft();
  125. assert.position(selection.getCursor(), 0, 0);
  126. selection.moveCursorWordLeft();
  127. assert.position(selection.getCursor(), 0, 0);
  128. },
  129. "test: moveCursor word left with umlauts" : function() {
  130. var session = new EditSession(" Fuß Füße");
  131. session.$selectLongWords = true;
  132. var selection = session.getSelection();
  133. selection.moveCursorTo(0, 9)
  134. selection.moveCursorWordLeft();
  135. assert.position(selection.getCursor(), 0, 5);
  136. selection.moveCursorWordLeft();
  137. assert.position(selection.getCursor(), 0, 1);
  138. },
  139. "test: select word left if cursor in word" : function() {
  140. var session = new EditSession("Juhu Kinners");
  141. var selection = session.getSelection();
  142. session.$selectLongWords = true;
  143. selection.moveCursorTo(0, 8);
  144. selection.moveCursorWordLeft();
  145. assert.position(selection.getCursor(), 0, 5);
  146. },
  147. "test: select word right and select" : function() {
  148. var session = new EditSession("Juhu Kinners");
  149. var selection = session.getSelection();
  150. selection.moveCursorTo(0, 0);
  151. selection.selectWordRight();
  152. var range = selection.getRange();
  153. assert.position(range.start, 0, 0);
  154. assert.position(range.end, 0, 4);
  155. },
  156. "test: select word left and select" : function() {
  157. var session = new EditSession("Juhu Kinners");
  158. var selection = session.getSelection();
  159. selection.moveCursorTo(0, 3);
  160. selection.selectWordLeft();
  161. var range = selection.getRange();
  162. assert.position(range.start, 0, 0);
  163. assert.position(range.end, 0, 3);
  164. },
  165. "test: select word with cursor in word should select the word" : function() {
  166. var session = new EditSession("Juhu Kinners 123");
  167. var selection = session.getSelection();
  168. selection.moveCursorTo(0, 8);
  169. selection.selectWord();
  170. var range = selection.getRange();
  171. assert.position(range.start, 0, 5);
  172. assert.position(range.end, 0, 12);
  173. },
  174. "test: select word with cursor in word including right whitespace should select the word" : function() {
  175. var session = new EditSession("Juhu Kinners 123");
  176. var selection = session.getSelection();
  177. selection.moveCursorTo(0, 8);
  178. selection.selectAWord();
  179. var range = selection.getRange();
  180. assert.position(range.start, 0, 5);
  181. assert.position(range.end, 0, 18);
  182. },
  183. "test: select word with cursor betwen white space and word should select the word" : function() {
  184. var session = new EditSession("Juhu Kinners");
  185. var selection = session.getSelection();
  186. session.$selectLongWords = true;
  187. selection.moveCursorTo(0, 4);
  188. selection.selectWord();
  189. var range = selection.getRange();
  190. assert.position(range.start, 0, 0);
  191. assert.position(range.end, 0, 4);
  192. selection.moveCursorTo(0, 5);
  193. selection.selectWord();
  194. var range = selection.getRange();
  195. assert.position(range.start, 0, 5);
  196. assert.position(range.end, 0, 12);
  197. },
  198. "test: select word with cursor in white space should select white space" : function() {
  199. var session = new EditSession("Juhu Kinners");
  200. var selection = session.getSelection();
  201. session.$selectLongWords = true;
  202. selection.moveCursorTo(0, 5);
  203. selection.selectWord();
  204. var range = selection.getRange();
  205. assert.position(range.start, 0, 4);
  206. assert.position(range.end, 0, 6);
  207. },
  208. "test: moving cursor should fire a 'changeCursor' event" : function() {
  209. var session = new EditSession("Juhu Kinners");
  210. var selection = session.getSelection();
  211. session.$selectLongWords = true;
  212. selection.moveCursorTo(0, 5);
  213. var called = false;
  214. selection.addEventListener("changeCursor", function() {
  215. called = true;
  216. });
  217. selection.moveCursorTo(0, 6);
  218. assert.ok(called);
  219. },
  220. "test: calling setCursor with the same position should not fire an event": function() {
  221. var session = new EditSession("Juhu Kinners");
  222. var selection = session.getSelection();
  223. session.$selectLongWords = true;
  224. selection.moveCursorTo(0, 5);
  225. var called = false;
  226. selection.addEventListener("changeCursor", function() {
  227. called = true;
  228. });
  229. selection.moveCursorTo(0, 5);
  230. assert.notOk(called);
  231. },
  232. "test: moveWordright should move past || and [": function() {
  233. var session = new EditSession("||foo[");
  234. var selection = session.getSelection();
  235. session.$selectLongWords = true;
  236. // Move behind ||foo
  237. selection.moveCursorWordRight();
  238. assert.position(selection.getCursor(), 0, 5);
  239. // Move behind [
  240. selection.moveCursorWordRight();
  241. assert.position(selection.getCursor(), 0, 6);
  242. },
  243. "test: moveWordLeft should move past || and [": function() {
  244. var session = new EditSession("||foo[");
  245. var selection = session.getSelection();
  246. session.$selectLongWords = true;
  247. selection.moveCursorTo(0, 6);
  248. // Move behind [foo
  249. selection.moveCursorWordLeft();
  250. assert.position(selection.getCursor(), 0, 2);
  251. // Move behind ||
  252. selection.moveCursorWordLeft();
  253. assert.position(selection.getCursor(), 0, 0);
  254. },
  255. "test: move cursor to line start should move cursor to end of the indentation first": function() {
  256. var session = new EditSession("12\n Juhu\n12");
  257. var selection = session.getSelection();
  258. selection.moveCursorTo(1, 6);
  259. selection.moveCursorLineStart();
  260. assert.position(selection.getCursor(), 1, 4);
  261. },
  262. "test: move cursor to line start when the cursor is at the end of the indentation should move cursor to column 0": function() {
  263. var session = new EditSession("12\n Juhu\n12");
  264. var selection = session.getSelection();
  265. selection.moveCursorTo(1, 4);
  266. selection.moveCursorLineStart();
  267. assert.position(selection.getCursor(), 1, 0);
  268. },
  269. "test: move cursor to line start when the cursor is at column 0 should move cursor to the end of the indentation": function() {
  270. var session = new EditSession("12\n Juhu\n12");
  271. var selection = session.getSelection();
  272. selection.moveCursorTo(1, 0);
  273. selection.moveCursorLineStart();
  274. assert.position(selection.getCursor(), 1, 4);
  275. },
  276. // Eclipse style
  277. "test: move cursor to line start when the cursor is before the initial indentation should move cursor to the end of the indentation": function() {
  278. var session = new EditSession("12\n Juhu\n12");
  279. var selection = session.getSelection();
  280. selection.moveCursorTo(1, 2);
  281. selection.moveCursorLineStart();
  282. assert.position(selection.getCursor(), 1, 4);
  283. },
  284. "test go line up when in the middle of the first line should go to document start": function() {
  285. var session = new EditSession("juhu kinners");
  286. var selection = session.getSelection();
  287. selection.moveCursorTo(0, 4);
  288. selection.moveCursorUp();
  289. assert.position(selection.getCursor(), 0, 0);
  290. },
  291. "test: (wrap) go line up when in the middle of the first line should go to document start": function() {
  292. var session = new EditSession("juhu kinners");
  293. session.setWrapLimitRange(5, 5);
  294. session.adjustWrapLimit(80);
  295. var selection = session.getSelection();
  296. selection.moveCursorTo(0, 4);
  297. selection.moveCursorUp();
  298. assert.position(selection.getCursor(), 0, 0);
  299. },
  300. "test go line down when in the middle of the last line should go to document end": function() {
  301. var session = new EditSession("juhu kinners");
  302. var selection = session.getSelection();
  303. selection.moveCursorTo(0, 4);
  304. selection.moveCursorDown();
  305. assert.position(selection.getCursor(), 0, 12);
  306. },
  307. "test (wrap) go line down when in the middle of the last line should go to document end": function() {
  308. var session = new EditSession("juhu kinners");
  309. session.setWrapLimitRange(8, 8);
  310. session.adjustWrapLimit(80);
  311. var selection = session.getSelection();
  312. selection.moveCursorTo(0, 10);
  313. selection.moveCursorDown();
  314. assert.position(selection.getCursor(), 0, 12);
  315. },
  316. "test go line up twice and then once down when in the second should go back to the previous column": function() {
  317. var session = new EditSession("juhu\nkinners");
  318. var selection = session.getSelection();
  319. selection.moveCursorTo(1, 4);
  320. selection.moveCursorUp();
  321. selection.moveCursorUp();
  322. selection.moveCursorDown();
  323. assert.position(selection.getCursor(), 1, 4);
  324. },
  325. "test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() {
  326. var session = new EditSession("function (a) {\n \n}");
  327. var selection = session.getSelection();
  328. selection.moveCursorTo(2, 0);
  329. selection.moveCursorUp();
  330. assert.position(selection.getCursor(), 1, 0);
  331. },
  332. "test (keyboard navigation) when curLine is EOL and targetLine is shorter than current column, new column should be targetLine's EOL": function() {
  333. var session = new EditSession("function (a) {\n \n}");
  334. var selection = session.getSelection();
  335. selection.moveCursorTo(0, 14);
  336. selection.moveCursorDown();
  337. assert.position(selection.getCursor(), 1, 4);
  338. },
  339. "test fromJSON/toJSON": function() {
  340. var session = new EditSession("function (a) {\n \n}");
  341. var selection = session.getSelection();
  342. selection.moveCursorTo(0, 14);
  343. selection.moveCursorDown();
  344. assert.position(selection.getCursor(), 1, 4);
  345. var data = selection.toJSON();
  346. data = JSON.parse(JSON.stringify(data))
  347. selection.moveCursorDown();
  348. assert.position(selection.getCursor(), 2, 1);
  349. assert.ok(!selection.isEqual(data));
  350. selection.fromJSON(data);
  351. assert.position(selection.getCursor(), 1, 4);
  352. assert.ok(selection.isEqual(data));
  353. }
  354. };
  355. });
  356. if (typeof module !== "undefined" && module === require.main) {
  357. require("asyncjs").test.testcase(module.exports).exec()
  358. }