sublime_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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",
  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. "selectScope", hasSel(0, 8, 2, 0),
  142. "selectScope", hasSel(0, 0, 2, 1));
  143. stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}",
  144. Pos(0, 0), "goToBracket", at(0, 0),
  145. Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
  146. Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
  147. Pos(1, 2), "goToBracket", at(2, 0),
  148. Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
  149. stTest("swapLine", "1\n2\n3---\n4\n5",
  150. "swapLineDown", val("2\n1\n3---\n4\n5"),
  151. "swapLineUp", val("1\n2\n3---\n4\n5"),
  152. "swapLineUp", val("1\n2\n3---\n4\n5"),
  153. Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
  154. setSel(0, 1, 0, 1,
  155. 1, 0, 2, 0,
  156. 2, 2, 2, 2),
  157. "swapLineDown", val("4\n1\n2\n3---\n5"),
  158. hasSel(1, 1, 1, 1,
  159. 2, 0, 3, 0,
  160. 3, 2, 3, 2),
  161. "swapLineUp", val("1\n2\n3---\n4\n5"),
  162. hasSel(0, 1, 0, 1,
  163. 1, 0, 2, 0,
  164. 2, 2, 2, 2));
  165. stTest("swapLineEmptyBottomSel", "1\n2\n3",
  166. setSel(0, 1, 1, 0),
  167. "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0),
  168. "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0),
  169. "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0));
  170. stTest("swapLineUpFromEnd", "a\nb\nc",
  171. Pos(2, 1), "swapLineUp",
  172. hasSel(1, 1, 1, 1), val("a\nc\nb"));
  173. stTest("joinLines", "abc\ndef\nghi\njkl",
  174. "joinLines", val("abc def\nghi\njkl"), at(0, 4),
  175. "undo",
  176. setSel(0, 2, 1, 1), "joinLines",
  177. val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
  178. "undo",
  179. setSel(0, 1, 0, 1,
  180. 1, 1, 1, 1,
  181. 3, 1, 3, 1), "joinLines",
  182. val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
  183. 0, 8, 0, 8,
  184. 1, 3, 1, 3));
  185. stTest("duplicateLine", "abc\ndef\nghi",
  186. Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
  187. "undo",
  188. setSel(0, 1, 0, 1,
  189. 1, 1, 1, 1,
  190. 2, 1, 2, 1), "duplicateLine",
  191. val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
  192. 3, 1, 3, 1,
  193. 5, 1, 5, 1));
  194. stTest("duplicateLineSelection", "abcdef",
  195. setSel(0, 1, 0, 1,
  196. 0, 2, 0, 4,
  197. 0, 5, 0, 5),
  198. "duplicateLine",
  199. val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
  200. 2, 4, 2, 6,
  201. 2, 7, 2, 7));
  202. stTest("sortLines", "c\nb\na\nC\nB\nA",
  203. "sortLines", val("A\nB\nC\na\nb\nc"),
  204. "undo",
  205. setSel(0, 0, 2, 0,
  206. 3, 0, 5, 0),
  207. "sortLines", val("b\nc\na\nB\nC\nA"),
  208. hasSel(0, 0, 2, 0,
  209. 3, 0, 5, 0),
  210. "undo",
  211. setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
  212. stTest("bookmarks", "abc\ndef\nghi\njkl",
  213. Pos(0, 1), "toggleBookmark",
  214. setSel(1, 1, 1, 2), "toggleBookmark",
  215. setSel(2, 1, 2, 2), "toggleBookmark",
  216. "nextBookmark", hasSel(0, 1, 0, 1),
  217. "nextBookmark", hasSel(1, 1, 1, 2),
  218. "nextBookmark", hasSel(2, 1, 2, 2),
  219. "prevBookmark", hasSel(1, 1, 1, 2),
  220. "prevBookmark", hasSel(0, 1, 0, 1),
  221. "prevBookmark", hasSel(2, 1, 2, 2),
  222. "prevBookmark", hasSel(1, 1, 1, 2),
  223. "toggleBookmark",
  224. "prevBookmark", hasSel(2, 1, 2, 2),
  225. "prevBookmark", hasSel(0, 1, 0, 1),
  226. "selectBookmarks", hasSel(0, 1, 0, 1,
  227. 2, 1, 2, 2),
  228. "clearBookmarks",
  229. Pos(0, 0), "selectBookmarks", at(0, 0));
  230. stTest("smartBackspace", " foo\n bar",
  231. setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace",
  232. val("foo\n br"))
  233. stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI",
  234. setSel(0, 1, 0, 3,
  235. 1, 1, 1, 1,
  236. 1, 4, 1, 4), "upcaseAtCursor",
  237. val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3,
  238. 1, 3, 1, 3,
  239. 1, 4, 1, 4),
  240. "downcaseAtCursor",
  241. val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3,
  242. 1, 3, 1, 3,
  243. 1, 4, 1, 4));
  244. stTest("mark", "abc\ndef\nghi",
  245. Pos(1, 1), "setSublimeMark",
  246. Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
  247. Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
  248. "deleteToSublimeMark", val("aef\nghi"),
  249. "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
  250. stTest("findUnder", "foo foobar a",
  251. "findUnder", hasSel(0, 4, 0, 7),
  252. "findUnder", hasSel(0, 0, 0, 3),
  253. "findUnderPrevious", hasSel(0, 4, 0, 7),
  254. "findUnderPrevious", hasSel(0, 0, 0, 3),
  255. Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
  256. Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
  257. })();