multi_test.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. (function() {
  2. namespace = "multi_";
  3. function hasSelections(cm) {
  4. var sels = cm.listSelections();
  5. var given = (arguments.length - 1) / 4;
  6. if (sels.length != given)
  7. throw new Failure("expected " + given + " selections, found " + sels.length);
  8. for (var i = 0, p = 1; i < given; i++, p += 4) {
  9. var anchor = Pos(arguments[p], arguments[p + 1]);
  10. var head = Pos(arguments[p + 2], arguments[p + 3]);
  11. eqCharPos(sels[i].anchor, anchor, "anchor of selection " + i);
  12. eqCharPos(sels[i].head, head, "head of selection " + i);
  13. }
  14. }
  15. function hasCursors(cm) {
  16. var sels = cm.listSelections();
  17. var given = (arguments.length - 1) / 2;
  18. if (sels.length != given)
  19. throw new Failure("expected " + given + " selections, found " + sels.length);
  20. for (var i = 0, p = 1; i < given; i++, p += 2) {
  21. eqCursorPos(sels[i].anchor, sels[i].head, "something selected for " + i);
  22. var head = Pos(arguments[p], arguments[p + 1]);
  23. eqCharPos(sels[i].head, head, "selection " + i);
  24. }
  25. }
  26. testCM("getSelection", function(cm) {
  27. select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)});
  28. eq(cm.getSelection(), "1234\n56\n90");
  29. eq(cm.getSelection(false).join("|"), "1234|56|90");
  30. eq(cm.getSelections().join("|"), "1234\n56|90");
  31. }, {value: "1234\n5678\n90"});
  32. testCM("setSelection", function(cm) {
  33. select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)});
  34. hasSelections(cm, 0, 0, 0, 0,
  35. 2, 5, 1, 0,
  36. 3, 0, 3, 0);
  37. cm.setSelection(Pos(1, 2), Pos(1, 1));
  38. hasSelections(cm, 1, 2, 1, 1);
  39. select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)},
  40. {anchor: Pos(0, 0), head: Pos(1, 3)},
  41. Pos(3, 0), Pos(2, 2));
  42. hasSelections(cm, 0, 0, 2, 4,
  43. 3, 0, 3, 0);
  44. cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)},
  45. {anchor: Pos(1, 1), head: Pos(1, 2)},
  46. {anchor: Pos(2, 1), head: Pos(2, 2)}], 1);
  47. eqCharPos(cm.getCursor("head"), Pos(1, 2));
  48. eqCharPos(cm.getCursor("anchor"), Pos(1, 1));
  49. eqCharPos(cm.getCursor("from"), Pos(1, 1));
  50. eqCharPos(cm.getCursor("to"), Pos(1, 2));
  51. cm.setCursor(Pos(1, 1));
  52. hasCursors(cm, 1, 1);
  53. }, {value: "abcde\nabcde\nabcde\n"});
  54. testCM("somethingSelected", function(cm) {
  55. select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)});
  56. eq(cm.somethingSelected(), true);
  57. select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5));
  58. eq(cm.somethingSelected(), false);
  59. }, {value: "123456789"});
  60. testCM("extendSelection", function(cm) {
  61. select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1));
  62. cm.setExtending(true);
  63. cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]);
  64. hasSelections(cm, 0, 1, 0, 2,
  65. 1, 1, 1, 0,
  66. 2, 1, 2, 3);
  67. cm.extendSelection(Pos(2, 4), Pos(2, 0));
  68. hasSelections(cm, 2, 4, 2, 0);
  69. }, {value: "1234\n1234\n1234"});
  70. testCM("addSelection", function(cm) {
  71. select(cm, Pos(0, 1), Pos(1, 1));
  72. cm.addSelection(Pos(0, 0), Pos(0, 4));
  73. hasSelections(cm, 0, 0, 0, 4,
  74. 1, 1, 1, 1);
  75. cm.addSelection(Pos(2, 2));
  76. hasSelections(cm, 0, 0, 0, 4,
  77. 1, 1, 1, 1,
  78. 2, 2, 2, 2);
  79. }, {value: "1234\n1234\n1234"});
  80. testCM("replaceSelection", function(cm) {
  81. var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)},
  82. {anchor: Pos(0, 2), head: Pos(0, 3)},
  83. {anchor: Pos(0, 4), head: Pos(0, 5)},
  84. {anchor: Pos(2, 1), head: Pos(2, 4)},
  85. {anchor: Pos(2, 5), head: Pos(2, 6)}];
  86. var val = "123456\n123456\n123456";
  87. cm.setValue(val);
  88. cm.setSelections(selections);
  89. cm.replaceSelection("ab", "around");
  90. eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab");
  91. hasSelections(cm, 0, 0, 0, 2,
  92. 0, 3, 0, 5,
  93. 0, 6, 0, 8,
  94. 2, 1, 2, 3,
  95. 2, 4, 2, 6);
  96. cm.setValue(val);
  97. cm.setSelections(selections);
  98. cm.replaceSelection("", "around");
  99. eq(cm.getValue(), "246\n123456\n15");
  100. hasSelections(cm, 0, 0, 0, 0,
  101. 0, 1, 0, 1,
  102. 0, 2, 0, 2,
  103. 2, 1, 2, 1,
  104. 2, 2, 2, 2);
  105. cm.setValue(val);
  106. cm.setSelections(selections);
  107. cm.replaceSelection("X\nY\nZ", "around");
  108. hasSelections(cm, 0, 0, 2, 1,
  109. 2, 2, 4, 1,
  110. 4, 2, 6, 1,
  111. 8, 1, 10, 1,
  112. 10, 2, 12, 1);
  113. cm.replaceSelection("a", "around");
  114. hasSelections(cm, 0, 0, 0, 1,
  115. 0, 2, 0, 3,
  116. 0, 4, 0, 5,
  117. 2, 1, 2, 2,
  118. 2, 3, 2, 4);
  119. cm.replaceSelection("xy", "start");
  120. hasSelections(cm, 0, 0, 0, 0,
  121. 0, 3, 0, 3,
  122. 0, 6, 0, 6,
  123. 2, 1, 2, 1,
  124. 2, 4, 2, 4);
  125. cm.replaceSelection("z\nf");
  126. hasSelections(cm, 1, 1, 1, 1,
  127. 2, 1, 2, 1,
  128. 3, 1, 3, 1,
  129. 6, 1, 6, 1,
  130. 7, 1, 7, 1);
  131. eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy");
  132. });
  133. function select(cm) {
  134. var sels = [];
  135. for (var i = 1; i < arguments.length; i++) {
  136. var arg = arguments[i];
  137. if (arg.head) sels.push(arg);
  138. else sels.push({head: arg, anchor: arg});
  139. }
  140. cm.setSelections(sels, sels.length - 1);
  141. }
  142. testCM("indentSelection", function(cm) {
  143. select(cm, Pos(0, 1), Pos(1, 1));
  144. cm.indentSelection(4);
  145. eq(cm.getValue(), " foo\n bar\nbaz");
  146. select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4));
  147. cm.indentSelection(-2);
  148. eq(cm.getValue(), " foo\n bar\nbaz");
  149. select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)},
  150. {anchor: Pos(1, 3), head: Pos(2, 0)});
  151. cm.indentSelection(-2);
  152. eq(cm.getValue(), "foo\n bar\nbaz");
  153. }, {value: "foo\nbar\nbaz"});
  154. testCM("killLine", function(cm) {
  155. select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1));
  156. cm.execCommand("killLine");
  157. eq(cm.getValue(), "f\nb\nbaz");
  158. cm.execCommand("killLine");
  159. eq(cm.getValue(), "fbbaz");
  160. cm.setValue("foo\nbar\nbaz");
  161. select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)});
  162. cm.execCommand("killLine");
  163. eq(cm.getValue(), "faz");
  164. }, {value: "foo\nbar\nbaz"});
  165. testCM("deleteLine", function(cm) {
  166. select(cm, Pos(0, 0),
  167. {head: Pos(0, 1), anchor: Pos(2, 0)},
  168. Pos(4, 0));
  169. cm.execCommand("deleteLine");
  170. eq(cm.getValue(), "4\n6\n7");
  171. select(cm, Pos(2, 1));
  172. cm.execCommand("deleteLine");
  173. eq(cm.getValue(), "4\n6\n");
  174. }, {value: "1\n2\n3\n4\n5\n6\n7"});
  175. testCM("deleteH", function(cm) {
  176. select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)});
  177. cm.execCommand("delWordAfter");
  178. eq(cm.getValue(), "foo bar baz\nabc ef ghi\n");
  179. cm.execCommand("delWordAfter");
  180. eq(cm.getValue(), "foo baz\nabc ghi\n");
  181. cm.execCommand("delCharBefore");
  182. cm.execCommand("delCharBefore");
  183. eq(cm.getValue(), "fo baz\nab ghi\n");
  184. select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5));
  185. cm.execCommand("delWordAfter");
  186. eq(cm.getValue(), "fo \nab ghi\n");
  187. }, {value: "foo bar baz\nabc def ghi\n"});
  188. testCM("goLineStart", function(cm) {
  189. select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1));
  190. cm.execCommand("goLineStart");
  191. hasCursors(cm, 0, 0, 1, 0);
  192. select(cm, Pos(1, 1), Pos(0, 1));
  193. cm.setExtending(true);
  194. cm.execCommand("goLineStart");
  195. hasSelections(cm, 0, 1, 0, 0,
  196. 1, 1, 1, 0);
  197. }, {value: "foo\nbar\nbaz"});
  198. testCM("moveV", function(cm) {
  199. select(cm, Pos(0, 2), Pos(1, 2));
  200. cm.execCommand("goLineDown");
  201. hasCursors(cm, 1, 2, 2, 2);
  202. cm.execCommand("goLineUp");
  203. hasCursors(cm, 0, 2, 1, 2);
  204. cm.execCommand("goLineUp");
  205. hasCursors(cm, 0, 0, 0, 2);
  206. cm.execCommand("goLineUp");
  207. hasCursors(cm, 0, 0);
  208. select(cm, Pos(0, 2), Pos(1, 2));
  209. cm.setExtending(true);
  210. cm.execCommand("goLineDown");
  211. hasSelections(cm, 0, 2, 2, 2);
  212. }, {value: "12345\n12345\n12345"});
  213. testCM("moveH", function(cm) {
  214. select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3));
  215. cm.execCommand("goCharRight");
  216. hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4);
  217. cm.execCommand("goCharLeft");
  218. hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3);
  219. for (var i = 0; i < 15; i++)
  220. cm.execCommand("goCharRight");
  221. hasCursors(cm, 2, 4, 2, 5);
  222. }, {value: "12345\n12345\n12345"});
  223. testCM("newlineAndIndent", function(cm) {
  224. select(cm, Pos(0, 5), Pos(1, 5));
  225. cm.execCommand("newlineAndIndent");
  226. hasCursors(cm, 1, 2, 3, 2);
  227. eq(cm.getValue(), "x = [\n 1];\ny = [\n 2];");
  228. cm.undo();
  229. eq(cm.getValue(), "x = [1];\ny = [2];");
  230. hasCursors(cm, 0, 5, 1, 5);
  231. select(cm, Pos(0, 5), Pos(0, 6));
  232. cm.execCommand("newlineAndIndent");
  233. hasCursors(cm, 1, 2, 2, 0);
  234. eq(cm.getValue(), "x = [\n 1\n];\ny = [2];");
  235. }, {value: "x = [1];\ny = [2];", mode: "javascript"});
  236. testCM("goDocStartEnd", function(cm) {
  237. select(cm, Pos(0, 1), Pos(1, 1));
  238. cm.execCommand("goDocStart");
  239. hasCursors(cm, 0, 0);
  240. select(cm, Pos(0, 1), Pos(1, 1));
  241. cm.execCommand("goDocEnd");
  242. hasCursors(cm, 1, 3);
  243. select(cm, Pos(0, 1), Pos(1, 1));
  244. cm.setExtending(true);
  245. cm.execCommand("goDocEnd");
  246. hasSelections(cm, 1, 1, 1, 3);
  247. }, {value: "abc\ndef"});
  248. testCM("selectionHistory", function(cm) {
  249. for (var i = 0; i < 3; ++i)
  250. cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1));
  251. cm.execCommand("undoSelection");
  252. eq(cm.getSelection(), "1\n2");
  253. cm.execCommand("undoSelection");
  254. eq(cm.getSelection(), "1");
  255. cm.execCommand("undoSelection");
  256. eq(cm.getSelection(), "");
  257. eqCharPos(cm.getCursor(), Pos(0, 0));
  258. cm.execCommand("redoSelection");
  259. eq(cm.getSelection(), "1");
  260. cm.execCommand("redoSelection");
  261. eq(cm.getSelection(), "1\n2");
  262. cm.execCommand("redoSelection");
  263. eq(cm.getSelection(), "1\n2\n3");
  264. }, {value: "1 2 3"});
  265. })();