sublime_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. eqPos(cm.getCursor("head"), Pos(line, ch), msg);
  25. eqPos(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. eqPos(sels[i].anchor, ranges[i].anchor, "anchor " + i);
  51. eqPos(sels[i].head, ranges[i].head, "head " + i);
  52. }
  53. };
  54. }
  55. stTest("bySubword", "the foo_bar DooDahBah \n a",
  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, 2),
  67. "goSubwordLeft", at(1, 1),
  68. "goSubwordLeft", at(1, 0),
  69. "goSubwordLeft", at(0, 22),
  70. "goSubwordLeft", at(0, 18),
  71. "goSubwordLeft", at(0, 15),
  72. "goSubwordLeft", at(0, 12),
  73. "goSubwordLeft", at(0, 8),
  74. "goSubwordLeft", at(0, 4),
  75. "goSubwordLeft", at(0, 0));
  76. stTest("splitSelectionByLine", "abc\ndef\nghi",
  77. setSel(0, 1, 2, 2),
  78. "splitSelectionByLine",
  79. hasSel(0, 1, 0, 3,
  80. 1, 0, 1, 3,
  81. 2, 0, 2, 2));
  82. stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl",
  83. setSel(0, 1, 1, 1,
  84. 1, 2, 3, 2,
  85. 3, 3, 3, 3),
  86. "splitSelectionByLine",
  87. hasSel(0, 1, 0, 3,
  88. 1, 0, 1, 1,
  89. 1, 2, 1, 3,
  90. 2, 0, 2, 3,
  91. 3, 0, 3, 2,
  92. 3, 3, 3, 3));
  93. stTest("selectLine", "abc\ndef\nghi",
  94. setSel(0, 1, 0, 1,
  95. 2, 0, 2, 1),
  96. "selectLine",
  97. hasSel(0, 0, 1, 0,
  98. 2, 0, 2, 3),
  99. setSel(0, 1, 1, 0),
  100. "selectLine",
  101. hasSel(0, 0, 2, 0));
  102. stTest("insertLineAfter", "abcde\nfghijkl\nmn",
  103. setSel(0, 1, 0, 1,
  104. 0, 3, 0, 3,
  105. 1, 2, 1, 2,
  106. 1, 3, 1, 5), "insertLineAfter",
  107. hasSel(1, 0, 1, 0,
  108. 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn"));
  109. stTest("insertLineBefore", "abcde\nfghijkl\nmn",
  110. setSel(0, 1, 0, 1,
  111. 0, 3, 0, 3,
  112. 1, 2, 1, 2,
  113. 1, 3, 1, 5), "insertLineBefore",
  114. hasSel(0, 0, 0, 0,
  115. 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn"));
  116. stTest("selectNextOccurrence", "a foo bar\nfoobar foo",
  117. setSel(0, 2, 0, 5),
  118. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  119. 1, 0, 1, 3),
  120. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  121. 1, 0, 1, 3,
  122. 1, 7, 1, 10),
  123. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  124. 1, 0, 1, 3,
  125. 1, 7, 1, 10),
  126. Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5),
  127. "selectNextOccurrence", hasSel(0, 2, 0, 5,
  128. 1, 7, 1, 10),
  129. setSel(0, 6, 0, 9),
  130. "selectNextOccurrence", hasSel(0, 6, 0, 9,
  131. 1, 3, 1, 6));
  132. stTest("selectScope", "foo(a) {\n bar[1, 2];\n}",
  133. "selectScope", hasSel(0, 0, 2, 1),
  134. Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5),
  135. Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5),
  136. Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1),
  137. Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0),
  138. Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0),
  139. Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10),
  140. Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10));
  141. stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}",
  142. Pos(0, 0), "goToBracket", at(0, 0),
  143. Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
  144. Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
  145. Pos(1, 2), "goToBracket", at(2, 0),
  146. Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
  147. stTest("swapLine", "1\n2\n3---\n4\n5",
  148. "swapLineDown", val("2\n1\n3---\n4\n5"),
  149. "swapLineUp", val("1\n2\n3---\n4\n5"),
  150. "swapLineUp", val("1\n2\n3---\n4\n5"),
  151. Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
  152. setSel(0, 1, 0, 1,
  153. 1, 0, 2, 0,
  154. 2, 2, 2, 2),
  155. "swapLineDown", val("4\n1\n2\n3---\n5"),
  156. hasSel(1, 1, 1, 1,
  157. 2, 0, 3, 0,
  158. 3, 2, 3, 2),
  159. "swapLineUp", val("1\n2\n3---\n4\n5"),
  160. hasSel(0, 1, 0, 1,
  161. 1, 0, 2, 0,
  162. 2, 2, 2, 2));
  163. stTest("swapLineEmptyBottomSel", "1\n2\n3",
  164. setSel(0, 1, 1, 0),
  165. "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0),
  166. "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0),
  167. "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0));
  168. stTest("swapLineUpFromEnd", "a\nb\nc",
  169. Pos(2, 1), "swapLineUp",
  170. hasSel(1, 1, 1, 1), val("a\nc\nb"));
  171. stTest("joinLines", "abc\ndef\nghi\njkl",
  172. "joinLines", val("abc def\nghi\njkl"), at(0, 4),
  173. "undo",
  174. setSel(0, 2, 1, 1), "joinLines",
  175. val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
  176. "undo",
  177. setSel(0, 1, 0, 1,
  178. 1, 1, 1, 1,
  179. 3, 1, 3, 1), "joinLines",
  180. val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
  181. 0, 8, 0, 8,
  182. 1, 3, 1, 3));
  183. stTest("duplicateLine", "abc\ndef\nghi",
  184. Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
  185. "undo",
  186. setSel(0, 1, 0, 1,
  187. 1, 1, 1, 1,
  188. 2, 1, 2, 1), "duplicateLine",
  189. val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
  190. 3, 1, 3, 1,
  191. 5, 1, 5, 1));
  192. stTest("duplicateLineSelection", "abcdef",
  193. setSel(0, 1, 0, 1,
  194. 0, 2, 0, 4,
  195. 0, 5, 0, 5),
  196. "duplicateLine",
  197. val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
  198. 2, 4, 2, 6,
  199. 2, 7, 2, 7));
  200. stTest("selectLinesUpward", "123\n345\n789\n012",
  201. setSel(0, 1, 0, 1,
  202. 1, 1, 1, 3,
  203. 2, 0, 2, 0,
  204. 3, 0, 3, 0),
  205. "selectLinesUpward",
  206. hasSel(0, 1, 0, 1,
  207. 0, 3, 0, 3,
  208. 1, 0, 1, 0,
  209. 1, 1, 1, 3,
  210. 2, 0, 2, 0,
  211. 3, 0, 3, 0));
  212. stTest("selectLinesDownward", "123\n345\n789\n012",
  213. setSel(0, 1, 0, 1,
  214. 1, 1, 1, 3,
  215. 2, 0, 2, 0,
  216. 3, 0, 3, 0),
  217. "selectLinesDownward",
  218. hasSel(0, 1, 0, 1,
  219. 1, 1, 1, 3,
  220. 2, 0, 2, 0,
  221. 2, 3, 2, 3,
  222. 3, 0, 3, 0));
  223. stTest("sortLines", "c\nb\na\nC\nB\nA",
  224. "sortLines", val("A\nB\nC\na\nb\nc"),
  225. "undo",
  226. setSel(0, 0, 2, 0,
  227. 3, 0, 5, 0),
  228. "sortLines", val("a\nb\nc\nA\nB\nC"),
  229. hasSel(0, 0, 2, 1,
  230. 3, 0, 5, 1),
  231. "undo",
  232. setSel(1, 0, 4, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
  233. stTest("bookmarks", "abc\ndef\nghi\njkl",
  234. Pos(0, 1), "toggleBookmark",
  235. setSel(1, 1, 1, 2), "toggleBookmark",
  236. setSel(2, 1, 2, 2), "toggleBookmark",
  237. "nextBookmark", hasSel(0, 1, 0, 1),
  238. "nextBookmark", hasSel(1, 1, 1, 2),
  239. "nextBookmark", hasSel(2, 1, 2, 2),
  240. "prevBookmark", hasSel(1, 1, 1, 2),
  241. "prevBookmark", hasSel(0, 1, 0, 1),
  242. "prevBookmark", hasSel(2, 1, 2, 2),
  243. "prevBookmark", hasSel(1, 1, 1, 2),
  244. "toggleBookmark",
  245. "prevBookmark", hasSel(2, 1, 2, 2),
  246. "prevBookmark", hasSel(0, 1, 0, 1),
  247. "selectBookmarks", hasSel(0, 1, 0, 1,
  248. 2, 1, 2, 2),
  249. "clearBookmarks",
  250. Pos(0, 0), "selectBookmarks", at(0, 0));
  251. stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI",
  252. setSel(0, 1, 0, 3,
  253. 1, 1, 1, 1,
  254. 1, 4, 1, 4), "upcaseAtCursor",
  255. val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3,
  256. 1, 3, 1, 3,
  257. 1, 4, 1, 4),
  258. "downcaseAtCursor",
  259. val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3,
  260. 1, 3, 1, 3,
  261. 1, 4, 1, 4));
  262. stTest("mark", "abc\ndef\nghi",
  263. Pos(1, 1), "setSublimeMark",
  264. Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
  265. Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
  266. "deleteToSublimeMark", val("aef\nghi"),
  267. "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
  268. stTest("findUnder", "foo foobar a",
  269. "findUnder", hasSel(0, 4, 0, 7),
  270. "findUnder", hasSel(0, 0, 0, 3),
  271. "findUnderPrevious", hasSel(0, 4, 0, 7),
  272. "findUnderPrevious", hasSel(0, 0, 0, 3),
  273. Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
  274. Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
  275. })();