placeholder_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. var EditSession = require("./edit_session").EditSession;
  37. var Editor = require("./editor").Editor;
  38. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  39. var assert = require("./test/assertions");
  40. var JavaScriptMode = require("./mode/javascript").Mode;
  41. var PlaceHolder = require("./placeholder").PlaceHolder;
  42. var UndoManager = require("./undomanager").UndoManager;
  43. module.exports = {
  44. "test: simple at the end appending of text" : function() {
  45. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  46. var editor = new Editor(new MockRenderer(), session);
  47. new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  48. editor.moveCursorTo(0, 5);
  49. editor.insert('b');
  50. assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);");
  51. editor.insert('cd');
  52. assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(abcd, abcd);");
  53. editor.remove('left');
  54. editor.remove('left');
  55. editor.remove('left');
  56. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);");
  57. },
  58. "test: inserting text outside placeholder" : function() {
  59. var session = new EditSession("var a = 10;\nconsole.log(a, a);\n", new JavaScriptMode());
  60. var editor = new Editor(new MockRenderer(), session);
  61. new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  62. editor.moveCursorTo(2, 0);
  63. editor.insert('b');
  64. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);\nb");
  65. },
  66. "test: insertion at the beginning" : function(next) {
  67. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  68. var editor = new Editor(new MockRenderer(), session);
  69. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  70. editor.moveCursorTo(0, 4);
  71. editor.insert('$');
  72. assert.equal(session.doc.getValue(), "var $a = 10;\nconsole.log($a, $a);");
  73. editor.moveCursorTo(0, 4);
  74. // Have to put this in a setTimeout because the anchor is only fixed later.
  75. setTimeout(function() {
  76. editor.insert('v');
  77. assert.equal(session.doc.getValue(), "var v$a = 10;\nconsole.log(v$a, v$a);");
  78. next();
  79. }, 10);
  80. },
  81. "test: detaching placeholder" : function() {
  82. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  83. var editor = new Editor(new MockRenderer(), session);
  84. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  85. editor.moveCursorTo(0, 5);
  86. editor.insert('b');
  87. assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);");
  88. p.detach();
  89. editor.insert('cd');
  90. assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(ab, ab);");
  91. },
  92. "test: events" : function() {
  93. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  94. var editor = new Editor(new MockRenderer(), session);
  95. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  96. var entered = false;
  97. var left = false;
  98. p.on("cursorEnter", function() {
  99. entered = true;
  100. });
  101. p.on("cursorLeave", function() {
  102. left = true;
  103. });
  104. editor.moveCursorTo(0, 0);
  105. editor.moveCursorTo(0, 4);
  106. p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
  107. assert.ok(entered);
  108. editor.moveCursorTo(1, 0);
  109. p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
  110. assert.ok(left);
  111. },
  112. "test: cancel": function(next) {
  113. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  114. session.setUndoManager(new UndoManager());
  115. var editor = new Editor(new MockRenderer(), session);
  116. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  117. editor.moveCursorTo(0, 5);
  118. editor.insert('b');
  119. editor.insert('cd');
  120. editor.remove('left');
  121. assert.equal(session.doc.getValue(), "var abc = 10;\nconsole.log(abc, abc);");
  122. // Wait a little for the changes to enter the undo stack
  123. setTimeout(function() {
  124. p.cancel();
  125. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);");
  126. next();
  127. }, 80);
  128. }
  129. };
  130. });
  131. if (typeof module !== "undefined" && module === require.main) {
  132. require("asyncjs").test.testcase(module.exports).exec()
  133. }