emacs_test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. (function() {
  2. "use strict";
  3. var Pos = CodeMirror.Pos;
  4. namespace = "emacs_";
  5. var eventCache = {};
  6. function fakeEvent(keyName) {
  7. var event = eventCache[key];
  8. if (event) return event;
  9. var ctrl, shift, alt;
  10. var key = keyName.replace(/\w+-/g, function(type) {
  11. if (type == "Ctrl-") ctrl = true;
  12. else if (type == "Alt-") alt = true;
  13. else if (type == "Shift-") shift = true;
  14. return "";
  15. });
  16. var code;
  17. for (var c in CodeMirror.keyNames)
  18. if (CodeMirror.keyNames[c] == key) { code = c; break; }
  19. if (code == null) throw new Error("Unknown key: " + key);
  20. return eventCache[keyName] = {
  21. type: "keydown", keyCode: code, ctrlKey: ctrl, shiftKey: shift, altKey: alt,
  22. preventDefault: function(){}, stopPropagation: function(){}
  23. };
  24. }
  25. function sim(name, start /*, actions... */) {
  26. var keys = Array.prototype.slice.call(arguments, 2);
  27. testCM(name, function(cm) {
  28. for (var i = 0; i < keys.length; ++i) {
  29. var cur = keys[i];
  30. if (cur instanceof Pos) cm.setCursor(cur);
  31. else if (cur.call) cur(cm);
  32. else cm.triggerOnKeyDown(fakeEvent(cur));
  33. }
  34. }, {keyMap: "emacs", value: start, mode: "javascript"});
  35. }
  36. function at(line, ch, sticky) { return function(cm) { eqCursorPos(cm.getCursor(), Pos(line, ch, sticky)); }; }
  37. function txt(str) { return function(cm) { eq(cm.getValue(), str); }; }
  38. sim("motionHSimple", "abc", "Ctrl-F", "Ctrl-F", "Ctrl-B", at(0, 1, "after"));
  39. sim("motionHMulti", "abcde",
  40. "Ctrl-4", "Ctrl-F", at(0, 4, "before"), "Ctrl--", "Ctrl-2", "Ctrl-F", at(0, 2, "after"),
  41. "Ctrl-5", "Ctrl-B", at(0, 0, "after"));
  42. sim("motionHWord", "abc. def ghi",
  43. "Alt-F", at(0, 3, "before"), "Alt-F", at(0, 8, "before"),
  44. "Ctrl-B", "Alt-B", at(0, 5, "after"), "Alt-B", at(0, 0, "after"));
  45. sim("motionHWordMulti", "abc. def ghi ",
  46. "Ctrl-3", "Alt-F", at(0, 12, "before"), "Ctrl-2", "Alt-B", at(0, 5, "after"),
  47. "Ctrl--", "Alt-B", at(0, 8, "before"));
  48. sim("motionVSimple", "a\nb\nc\n", "Ctrl-N", "Ctrl-N", "Ctrl-P", at(1, 0, "after"));
  49. sim("motionVMulti", "a\nb\nc\nd\ne\n",
  50. "Ctrl-2", "Ctrl-N", at(2, 0, "after"), "Ctrl-F", "Ctrl--", "Ctrl-N", at(1, 1, "before"),
  51. "Ctrl--", "Ctrl-3", "Ctrl-P", at(4, 1, "before"));
  52. sim("killYank", "abc\ndef\nghi",
  53. "Ctrl-F", "Ctrl-Space", "Ctrl-N", "Ctrl-N", "Ctrl-W", "Ctrl-E", "Ctrl-Y",
  54. txt("ahibc\ndef\ng"));
  55. sim("killRing", "abcdef",
  56. "Ctrl-Space", "Ctrl-F", "Ctrl-W", "Ctrl-Space", "Ctrl-F", "Ctrl-W",
  57. "Ctrl-Y", "Alt-Y",
  58. txt("acdef"));
  59. sim("copyYank", "abcd",
  60. "Ctrl-Space", "Ctrl-E", "Alt-W", "Ctrl-Y",
  61. txt("abcdabcd"));
  62. sim("killLineSimple", "foo\nbar", "Ctrl-F", "Ctrl-K", txt("f\nbar"));
  63. sim("killLineEmptyLine", "foo\n \nbar", "Ctrl-N", "Ctrl-K", txt("foo\nbar"));
  64. sim("killLineMulti", "foo\nbar\nbaz",
  65. "Ctrl-F", "Ctrl-F", "Ctrl-K", "Ctrl-K", "Ctrl-K", "Ctrl-A", "Ctrl-Y",
  66. txt("o\nbarfo\nbaz"));
  67. sim("moveByParagraph", "abc\ndef\n\n\nhij\nklm\n\n",
  68. "Ctrl-F", "Ctrl-Down", at(2, 0), "Ctrl-Down", at(6, 0),
  69. "Ctrl-N", "Ctrl-Up", at(3, 0), "Ctrl-Up", at(0, 0),
  70. Pos(1, 2), "Ctrl-Down", at(2, 0), Pos(4, 2), "Ctrl-Up", at(3, 0));
  71. sim("moveByParagraphMulti", "abc\n\ndef\n\nhij\n\nklm",
  72. "Ctrl-U", "2", "Ctrl-Down", at(3, 0),
  73. "Shift-Alt-.", "Ctrl-3", "Ctrl-Up", at(1, 0));
  74. sim("moveBySentence", "sentence one! sentence\ntwo\n\nparagraph two",
  75. "Alt-E", at(0, 13), "Alt-E", at(1, 3), "Ctrl-F", "Alt-A", at(0, 13));
  76. sim("moveByExpr", "function foo(a, b) {}",
  77. "Ctrl-Alt-F", at(0, 8), "Ctrl-Alt-F", at(0, 12), "Ctrl-Alt-F", at(0, 18),
  78. "Ctrl-Alt-B", at(0, 12), "Ctrl-Alt-B", at(0, 9));
  79. sim("moveByExprMulti", "foo bar baz bug",
  80. "Ctrl-2", "Ctrl-Alt-F", at(0, 7),
  81. "Ctrl--", "Ctrl-Alt-F", at(0, 4),
  82. "Ctrl--", "Ctrl-2", "Ctrl-Alt-B", at(0, 11));
  83. sim("delExpr", "var x = [\n a,\n b\n c\n];",
  84. Pos(0, 8), "Ctrl-Alt-K", txt("var x = ;"), "Ctrl-/",
  85. Pos(4, 1), "Ctrl-Alt-Backspace", txt("var x = ;"));
  86. sim("delExprMulti", "foo bar baz",
  87. "Ctrl-2", "Ctrl-Alt-K", txt(" baz"),
  88. "Ctrl-/", "Ctrl-E", "Ctrl-2", "Ctrl-Alt-Backspace", txt("foo "));
  89. sim("justOneSpace", "hi bye ",
  90. Pos(0, 4), "Alt-Space", txt("hi bye "),
  91. Pos(0, 4), "Alt-Space", txt("hi b ye "),
  92. "Ctrl-A", "Alt-Space", "Ctrl-E", "Alt-Space", txt(" hi b ye "));
  93. sim("openLine", "foo bar", "Alt-F", "Ctrl-O", txt("foo\n bar"))
  94. sim("transposeChar", "abcd\ne",
  95. "Ctrl-F", "Ctrl-T", "Ctrl-T", txt("bcad\ne"), at(0, 3),
  96. "Ctrl-F", "Ctrl-T", "Ctrl-T", "Ctrl-T", txt("bcda\ne"), at(0, 4),
  97. "Ctrl-F", "Ctrl-T", txt("bcde\na"), at(1, 1));
  98. sim("manipWordCase", "foo BAR bAZ",
  99. "Alt-C", "Alt-L", "Alt-U", txt("Foo bar BAZ"),
  100. "Ctrl-A", "Alt-U", "Alt-L", "Alt-C", txt("FOO bar Baz"));
  101. sim("manipWordCaseMulti", "foo Bar bAz",
  102. "Ctrl-2", "Alt-U", txt("FOO BAR bAz"),
  103. "Ctrl-A", "Ctrl-3", "Alt-C", txt("Foo Bar Baz"));
  104. sim("upExpr", "foo {\n bar[];\n baz(blah);\n}",
  105. Pos(2, 7), "Ctrl-Alt-U", at(2, 5), "Ctrl-Alt-U", at(0, 4));
  106. sim("transposeExpr", "do foo[bar] dah",
  107. Pos(0, 6), "Ctrl-Alt-T", txt("do [bar]foo dah"));
  108. sim("clearMark", "abcde", Pos(0, 2), "Ctrl-Space", "Ctrl-F", "Ctrl-F",
  109. "Ctrl-G", "Ctrl-W", txt("abcde"));
  110. sim("delRegion", "abcde", "Ctrl-Space", "Ctrl-F", "Ctrl-F", "Delete", txt("cde"));
  111. sim("backspaceRegion", "abcde", "Ctrl-Space", "Ctrl-F", "Ctrl-F", "Backspace", txt("cde"));
  112. sim("backspaceDoesntAddToRing", "foobar", "Ctrl-F", "Ctrl-F", "Ctrl-F", "Ctrl-K", "Backspace", "Backspace", "Ctrl-Y", txt("fbar"));
  113. testCM("save", function(cm) {
  114. var saved = false;
  115. CodeMirror.commands.save = function(cm) { saved = cm.getValue(); };
  116. cm.triggerOnKeyDown(fakeEvent("Ctrl-X"));
  117. cm.triggerOnKeyDown(fakeEvent("Ctrl-S"));
  118. is(saved, "hi");
  119. }, {value: "hi", keyMap: "emacs"});
  120. testCM("gotoInvalidLineFloat", function(cm) {
  121. cm.openDialog = function(_, cb) { cb("2.2"); };
  122. cm.triggerOnKeyDown(fakeEvent("Alt-G"));
  123. cm.triggerOnKeyDown(fakeEvent("G"));
  124. }, {value: "1\n2\n3\n4", keyMap: "emacs"});
  125. })();