You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
11 KiB

7 months ago
  1. (function() {
  2. "use strict";
  3. var Pos = CodeMirror.Pos;
  4. namespace = "sublime_";
  5. function stTest(name) {
  6. var actions = Array.prototype.slice.call(arguments, 1);
  7. testCM(name, function(cm) {
  8. for (var i = 0; i < actions.length; i++) {
  9. var action = actions[i];
  10. if (typeof action == "string" && i == 0)
  11. cm.setValue(action);
  12. else if (typeof action == "string")
  13. cm.execCommand(action);
  14. else if (action instanceof Pos)
  15. cm.setCursor(action);
  16. else
  17. action(cm);
  18. }
  19. });
  20. }
  21. function at(line, ch, msg) {
  22. return function(cm) {
  23. eq(cm.listSelections().length, 1);
  24. eqCursorPos(cm.getCursor("head"), Pos(line, ch), msg);
  25. eqCursorPos(cm.getCursor("anchor"), Pos(line, ch), msg);
  26. };
  27. }
  28. function val(content, msg) {
  29. return function(cm) { eq(cm.getValue(), content, msg); };
  30. }
  31. function argsToRanges(args) {
  32. if (args.length % 4) throw new Error("Wrong number of arguments for ranges.");
  33. var ranges = [];
  34. for (var i = 0; i < args.length; i += 4)
  35. ranges.push({anchor: Pos(args[i], args[i + 1]),
  36. head: Pos(args[i + 2], args[i + 3])});
  37. return ranges;
  38. }
  39. function setSel() {
  40. var ranges = argsToRanges(arguments);
  41. return function(cm) { cm.setSelections(ranges, 0); };
  42. }
  43. function hasSel() {
  44. var ranges = argsToRanges(arguments);
  45. return function(cm) {
  46. var sels = cm.listSelections();
  47. if (sels.length != ranges.length)
  48. throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length);
  49. for (var i = 0; i < sels.length; i++) {
  50. eqCharPos(sels[i].anchor, ranges[i].anchor, "anchor " + i);
  51. eqCharPos(sels[i].head, ranges[i].head, "head " + i);
  52. }
  53. };
  54. }
  55. stTest("bySubword", "the foo_bar DooDahBah \n a FOOBar",
  56. "goSubwordLeft", at(0, 0),
  57. "goSubwordRight", at(0, 3),
  58. "goSubwordRight", at(0, 7),
  59. "goSubwordRight", at(0, 11),
  60. "goSubwordRight", at(0, 15),
  61. "goSubwordRight", at(0, 18),
  62. "goSubwordRight", at(0, 21),
  63. "goSubwordRight", at(0, 22),
  64. "goSubwordRight", at(1, 0),
  65. "goSubwordRight", at(1, 2),
  66. "goSubwordRight", at(1, 6),
  67. "goSubwordRight", at(1, 9),
  68. "goSubwordLeft", at(1, 6),
  69. "goSubwordLeft", at(1, 3),
  70. "goSubwordLeft", at(1, 1),
  71. "goSubwordLeft", at(1, 0),
  72. "goSubwordLeft", at(0, 22),
  73. "goSubwordLeft", at(0, 18),
  74. "goSubwordLeft", at(0, 15),
  75. "goSubwordLeft", at(0, 12),
  76. "goSubwordLeft", at(0, 8),
  77. "goSubwordLeft", at(0, 4),
  78. "goSubwordLeft", at(0, 0));
  79. stTest("splitSelectionByLine", "abc\ndef\nghi",
  80. setSel(0, 1, 2, 2),
  81. "splitSelectionByLine",
  82. hasSel(0, 1, 0, 3,
  83. 1, 0, 1, 3,
  84. 2, 0, 2, 2));
  85. stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl",
  86. setSel(0, 1, 1, 1,
  87. 1, 2, 3, 2,
  88. 3, 3, 3, 3),
  89. "splitSelectionByLine",
  90. hasSel(0, 1, 0, 3,
  91. 1, 0, 1, 1,
  92. 1, 2, 1, 3,
  93. 2, 0, 2, 3,
  94. 3, 0, 3, 2,
  95. 3, 3, 3, 3));
  96. stTest("selectLine", "abc\ndef\nghi",
  97. setSel(0, 1, 0, 1,
  98. 2, 0, 2, 1),
  99. "selectLine",
  100. hasSel(0, 0, 1, 0,
  101. 2, 0, 2, 3),
  102. setSel(0, 1, 1, 0),
  103. "selectLine",
  104. hasSel(0, 0, 2, 0));
  105. stTest("insertLineAfter", "abcde\nfghijkl\nmn",
  106. setSel(0, 1, 0, 1,
  107. 0, 3, 0, 3,
  108. 1, 2, 1, 2,
  109. 1, 3, 1, 5), "insertLineAfter",
  110. hasSel(1, 0, 1, 0,
  111. 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn"));
  112. stTest("insertLineBefore", "abcde\nfghijkl\nmn",
  113. setSel(0, 1, 0, 1,
  114. 0, 3, 0, 3,
  115. 1, 2, 1, 2,
  116. 1, 3, 1, 5), "insertLineBefore",
  117. hasSel(0, 0, 0, 0,
  118. 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn"));
  119. stTest("skipAndSelectNextOccurrence", "a foo bar\nfoobar foo",
  120. setSel(0, 2, 0, 5), "skipAndSelectNextOccurrence", hasSel(1, 0, 1, 3),
  121. "skipAndSelectNextOccurrence", hasSel(1, 7, 1, 10),
  122. "skipAndSelectNextOccurrence", hasSel(0, 2, 0, 5),
  123. Pos(0, 3), "skipAndSelectNextOccurrence", hasSel(0, 2, 0, 5),
  124. "skipAndSelectNextOccurrence", hasSel(1, 7, 1, 10),
  125. setSel(0, 6, 0, 9), "skipAndSelectNextOccurrence", hasSel(1, 3, 1, 6));
  126. stTest("selectNextOccurrence", "a foo bar\nfoobar foo",
  127. setSel(0, 2, 0, 5),
  128. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  129. 1, 0, 1, 3),
  130. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  131. 1, 0, 1, 3,
  132. 1, 7, 1, 10),
  133. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  134. 1, 0, 1, 3,
  135. 1, 7, 1, 10),
  136. Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5),
  137. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  138. 1, 7, 1, 10),
  139. setSel(0, 6, 0, 9),
  140. "selectNextOccurrence", hasSel(0, 6, 0, 9,
  141. 1, 3, 1, 6));
  142. stTest("selectScope", "foo(a) {\n bar[1, 2];\n}",
  143. "selectScope", hasSel(0, 0, 2, 1),
  144. Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5),
  145. Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5),
  146. Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1),
  147. Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0),
  148. Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0),
  149. Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10),
  150. Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10),
  151. "selectScope", hasSel(0, 8, 2, 0),
  152. "selectScope", hasSel(0, 0, 2, 1));
  153. stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}",
  154. Pos(0, 0), "goToBracket", at(0, 0),
  155. Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
  156. Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
  157. Pos(1, 2), "goToBracket", at(2, 0),
  158. Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
  159. stTest("swapLine", "1\n2\n3---\n4\n5",
  160. "swapLineDown", val("2\n1\n3---\n4\n5"),
  161. "swapLineUp", val("1\n2\n3---\n4\n5"),
  162. "swapLineUp", val("1\n2\n3---\n4\n5"),
  163. Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
  164. setSel(0, 1, 0, 1,
  165. 1, 0, 2, 0,
  166. 2, 2, 2, 2),
  167. "swapLineDown", val("4\n1\n2\n3---\n5"),
  168. hasSel(1, 1, 1, 1,
  169. 2, 0, 3, 0,
  170. 3, 2, 3, 2),
  171. "swapLineUp", val("1\n2\n3---\n4\n5"),
  172. hasSel(0, 1, 0, 1,
  173. 1, 0, 2, 0,
  174. 2, 2, 2, 2));
  175. stTest("swapLineEmptyBottomSel", "1\n2\n3",
  176. setSel(0, 1, 1, 0),
  177. "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0),
  178. "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0),
  179. "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0));
  180. stTest("swapLineUpFromEnd", "a\nb\nc",
  181. Pos(2, 1), "swapLineUp",
  182. hasSel(1, 1, 1, 1), val("a\nc\nb"));
  183. stTest("joinLines", "abc\ndef\nghi\njkl",
  184. "joinLines", val("abc def\nghi\njkl"), at(0, 4),
  185. "undo",
  186. setSel(0, 2, 1, 1), "joinLines",
  187. val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
  188. "undo",
  189. setSel(0, 1, 0, 1,
  190. 1, 1, 1, 1,
  191. 3, 1, 3, 1), "joinLines",
  192. val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
  193. 0, 8, 0, 8,
  194. 1, 3, 1, 3));
  195. stTest("duplicateLine", "abc\ndef\nghi",
  196. Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
  197. "undo",
  198. setSel(0, 1, 0, 1,
  199. 1, 1, 1, 1,
  200. 2, 1, 2, 1), "duplicateLine",
  201. val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
  202. 3, 1, 3, 1,
  203. 5, 1, 5, 1));
  204. stTest("duplicateLineSelection", "abcdef",
  205. setSel(0, 1, 0, 1,
  206. 0, 2, 0, 4,
  207. 0, 5, 0, 5),
  208. "duplicateLine",
  209. val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
  210. 2, 4, 2, 6,
  211. 2, 7, 2, 7));
  212. stTest("sortLines", "c\nb\na\nC\nB\nA",
  213. "sortLines", val("A\nB\nC\na\nb\nc"),
  214. "undo",
  215. setSel(0, 0, 2, 0,
  216. 3, 0, 5, 0),
  217. "sortLines", val("b\nc\na\nB\nC\nA"),
  218. hasSel(0, 0, 2, 0,
  219. 3, 0, 5, 0),
  220. "undo",
  221. setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
  222. stTest("bookmarks", "abc\ndef\nghi\njkl",
  223. Pos(0, 1), "toggleBookmark",
  224. setSel(1, 1, 1, 2), "toggleBookmark",
  225. setSel(2, 1, 2, 2), "toggleBookmark",
  226. "nextBookmark", hasSel(0, 1, 0, 1),
  227. "nextBookmark", hasSel(1, 1, 1, 2),
  228. "nextBookmark", hasSel(2, 1, 2, 2),
  229. "prevBookmark", hasSel(1, 1, 1, 2),
  230. "prevBookmark", hasSel(0, 1, 0, 1),
  231. "prevBookmark", hasSel(2, 1, 2, 2),
  232. "prevBookmark", hasSel(1, 1, 1, 2),
  233. "toggleBookmark",
  234. "prevBookmark", hasSel(2, 1, 2, 2),
  235. "prevBookmark", hasSel(0, 1, 0, 1),
  236. "selectBookmarks", hasSel(0, 1, 0, 1,
  237. 2, 1, 2, 2),
  238. "clearBookmarks",
  239. Pos(0, 0), "selectBookmarks", at(0, 0));
  240. stTest("smartBackspace", " foo\n bar",
  241. setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace",
  242. val("foo\n br"))
  243. stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI",
  244. setSel(0, 1, 0, 3,
  245. 1, 1, 1, 1,
  246. 1, 4, 1, 4), "upcaseAtCursor",
  247. val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3,
  248. 1, 3, 1, 3,
  249. 1, 4, 1, 4),
  250. "downcaseAtCursor",
  251. val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3,
  252. 1, 3, 1, 3,
  253. 1, 4, 1, 4));
  254. stTest("mark", "abc\ndef\nghi",
  255. Pos(1, 1), "setSublimeMark",
  256. Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
  257. Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
  258. "deleteToSublimeMark", val("aef\nghi"),
  259. "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
  260. stTest("findUnder", "foo foobar a",
  261. "findUnder", hasSel(0, 4, 0, 7),
  262. "findUnder", hasSel(0, 0, 0, 3),
  263. "findUnderPrevious", hasSel(0, 4, 0, 7),
  264. "findUnderPrevious", hasSel(0, 0, 0, 3),
  265. Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
  266. Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
  267. })();