emacs_test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. if (typeof process !== "undefined") {
  31. require("amd-loader");
  32. require("../test/mockdom");
  33. }
  34. define(function(require, exports, module) {
  35. "use strict";
  36. require("../multi_select");
  37. var EditSession = require("./../edit_session").EditSession,
  38. Editor = require("./../editor").Editor,
  39. Range = require("./../range").Range,
  40. MockRenderer = require("./../test/mockrenderer").MockRenderer,
  41. emacs = require('./emacs'),
  42. assert = require("./../test/assertions"),
  43. editor, sel;
  44. function initEditor(docString) {
  45. var doc = new EditSession(docString.split("\n"));
  46. editor = new Editor(new MockRenderer(), doc);
  47. editor.setKeyboardHandler(emacs.handler);
  48. sel = editor.selection;
  49. }
  50. function print(obj) {
  51. return JSON.stringify(obj, null, 2);
  52. }
  53. function pluck(arr, what) {
  54. return arr.map(function(ea) { return ea[what]; });
  55. }
  56. module.exports = {
  57. "test: detach removes emacs commands from command manager": function() {
  58. initEditor('');
  59. assert.ok(!!editor.commands.byName["keyboardQuit"], 'setup error: emacs commands not installed');
  60. editor.keyBinding.removeKeyboardHandler(editor.getKeyboardHandler());
  61. assert.ok(!editor.commands.byName["keyboardQuit"], 'emacs commands not removed');
  62. },
  63. "test: keyboardQuit clears selection": function() {
  64. initEditor('foo');
  65. editor.selectAll();
  66. editor.execCommand('keyboardQuit');
  67. assert.ok(editor.selection.isEmpty(), 'selection non-empty');
  68. },
  69. "test: exchangePointAndMark without mark set": function() {
  70. initEditor('foo');
  71. sel.setRange(Range.fromPoints({row: 0, column: 1}, {row: 0, column: 3}));
  72. editor.execCommand('exchangePointAndMark');
  73. assert.deepEqual({row: 0, column: 1}, editor.getCursorPosition(), print(editor.getCursorPosition()));
  74. },
  75. "test: exchangePointAndMark with mark set": function() {
  76. initEditor('foo');
  77. editor.pushEmacsMark({row: 0, column: 1});
  78. editor.pushEmacsMark({row: 0, column: 2});
  79. editor.execCommand('exchangePointAndMark', {count: 4});
  80. assert.deepEqual({row: 0, column: 2}, editor.getCursorPosition(), print(editor.getCursorPosition()));
  81. assert.deepEqual([{row: 0, column: 1}, {row: 0, column: 0}], editor.session.$emacsMarkRing, print(editor.session.$emacsMarkRing));
  82. },
  83. "test: exchangePointAndMark with selection": function() {
  84. initEditor('foo');
  85. editor.pushEmacsMark({row: 0, column: 1});
  86. editor.pushEmacsMark({row: 0, column: 2});
  87. sel.setRange(Range.fromPoints({row: 0, column: 0}, {row: 0, column: 1}), true);
  88. editor.execCommand('exchangePointAndMark');
  89. assert.deepEqual({row: 0, column: 1}, editor.getCursorPosition(), print(editor.getCursorPosition()));
  90. assert.deepEqual([{row: 0, column: 1}, {row: 0, column: 2}], editor.session.$emacsMarkRing, print(editor.session.$emacsMarkRing));
  91. },
  92. "test: exchangePointAndMark with multi selection": function() {
  93. initEditor('foo\nhello world\n123');
  94. var ranges = [[{row: 0, column: 0}, {row: 0, column: 3}],
  95. [{row: 1, column: 0}, {row: 1, column: 5}],
  96. [{row: 1, column: 6}, {row: 1, column: 11}]]
  97. ranges.forEach(function(r) {
  98. sel.addRange(Range.fromPoints(r[0], r[1]));
  99. });
  100. assert.equal("foo\nhello\nworld", editor.getSelectedText());
  101. editor.execCommand('exchangePointAndMark');
  102. assert.equal("foo\nhello\nworld", editor.getSelectedText());
  103. assert.deepEqual(pluck(ranges, 0), pluck(sel.getAllRanges(), 'cursor'), "selections dir not inverted");
  104. },
  105. "test: exchangePointAndMark with multi cursors": function() {
  106. initEditor('foo\nhello world\n123');
  107. var ranges = [[{row: 0, column: 0}, {row: 0, column: 3}],
  108. [{row: 1, column: 0}, {row: 1, column: 5}],
  109. [{row: 1, column: 6}, {row: 1, column: 11}]];
  110. // move cursors to the start of each range and set a mark to its end
  111. // without selecting anything
  112. ranges.forEach(function(r) {
  113. editor.pushEmacsMark(r[1]);
  114. sel.addRange(Range.fromPoints(r[0], r[0]));
  115. });
  116. assert.deepEqual(pluck(ranges, 0), pluck(sel.getAllRanges(), 'cursor'), print(sel.getAllRanges()));
  117. editor.execCommand('exchangePointAndMark');
  118. assert.deepEqual(pluck(ranges, 1), pluck(sel.getAllRanges(), 'cursor'), "not inverted: " + print(sel.getAllRanges()));
  119. },
  120. "test: setMark with multi cursors": function() {
  121. initEditor('foo\nhello world\n123');
  122. var positions = [{row: 0, column: 0},
  123. {row: 1, column: 0},
  124. {row: 1, column: 6}];
  125. positions.forEach(function(p) { sel.addRange(Range.fromPoints(p,p)); });
  126. editor.execCommand('setMark');
  127. assert.deepEqual(positions, editor.session.$emacsMarkRing, print(editor.session.$emacsMarkRing));
  128. },
  129. "test: killLine": function() {
  130. initEditor("foo \n Hello world\n \n 123");
  131. sel.setRange(new Range(0, 0, 0, 2));
  132. editor.endOperation();
  133. editor.execCommand("killLine");
  134. assert.equal(editor.getValue(),"fo\n Hello world\n \n 123");
  135. editor.execCommand("killLine");
  136. assert.equal(editor.getValue(),"fo Hello world\n \n 123");
  137. editor.execCommand("killLine");
  138. assert.equal(editor.getValue(),"fo\n \n 123");
  139. editor.execCommand("killLine");
  140. assert.equal(editor.getValue(),"fo\n 123");
  141. editor.execCommand("killLine");
  142. assert.equal(editor.getValue(),"fo 123");
  143. editor.execCommand("killLine");
  144. assert.equal(editor.getValue(),"fo");
  145. editor.execCommand("killLine");
  146. editor.execCommand("yank");
  147. assert.equal(editor.getValue(),"foo \n Hello world\n \n 123");
  148. },
  149. };
  150. });
  151. if (typeof module !== "undefined" && module === require.main) {
  152. require("asyncjs").test.testcase(module.exports).exec()
  153. }