behaviour_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 assert = require("../../test/assertions");
  38. var Range = require("../../range").Range;
  39. var Editor = require("../../editor").Editor;
  40. var EditSession = require("../../edit_session").EditSession;
  41. var MockRenderer = require("../../test/mockrenderer").MockRenderer;
  42. var JavaScriptMode = require("../javascript").Mode;
  43. var XMLMode = require("../xml").Mode;
  44. var editor;
  45. var exec = function(name, times, args) {
  46. do {
  47. editor.commands.exec(name, editor, args);
  48. } while(times --> 1);
  49. };
  50. var testRanges = function(str) {
  51. assert.equal(editor.selection.getAllRanges() + "", str + "");
  52. };
  53. module.exports = {
  54. "test: cstyle": function() {
  55. function testValue(line) {
  56. assert.equal(editor.getValue(), Array(4).join(line + "\n"));
  57. }
  58. function testSelection(line, col, inc) {
  59. editor.selection.rangeList.ranges.forEach(function(r) {
  60. assert.range(r, line, col, line, col);
  61. line += (inc || 1);
  62. });
  63. }
  64. var doc = new EditSession([
  65. "",
  66. "",
  67. "",
  68. ""
  69. ], new JavaScriptMode());
  70. editor = new Editor(new MockRenderer(), doc);
  71. editor.setOption("behavioursEnabled", true);
  72. editor.navigateFileStart();
  73. exec("addCursorBelow", 2);
  74. exec("insertstring", 1, "if ");
  75. // pairing (
  76. exec("insertstring", 1, "(");
  77. testValue("if ()");
  78. testSelection(0, 4);
  79. exec("insertstring", 1, ")");
  80. testValue("if ()");
  81. testSelection(0, 5);
  82. // pairing [
  83. exec("gotoleft", 1);
  84. exec("insertstring", 1, "[");
  85. testValue("if ([])");
  86. testSelection(0, 5);
  87. exec("insertstring", 1, "]");
  88. testValue("if ([])");
  89. testSelection(0, 6);
  90. // test deletion
  91. exec("gotoleft", 1);
  92. exec("backspace", 1);
  93. testValue("if ()");
  94. testSelection(0, 4);
  95. exec("gotolineend", 1);
  96. exec("insertstring", 1, "{");
  97. testValue("if (){}");
  98. testSelection(0, 6);
  99. exec("insertstring", 1, "}");
  100. testValue("if (){}");
  101. testSelection(0, 7);
  102. exec("gotolinestart", 1);
  103. exec("insertstring", 1, "(");
  104. testValue("(if (){}");
  105. exec("backspace", 1);
  106. editor.setValue("");
  107. exec("insertstring", 1, "{");
  108. assert.equal(editor.getValue(), "{");
  109. exec("insertstring", 1, "\n");
  110. assert.equal(editor.getValue(), "{\n \n}");
  111. editor.setValue("");
  112. exec("insertstring", 1, "(");
  113. exec("insertstring", 1, '"');
  114. exec("insertstring", 1, '"');
  115. assert.equal(editor.getValue(), '("")');
  116. exec("backspace", 1);
  117. exec("insertstring", 1, '"');
  118. assert.equal(editor.getValue(), '("")');
  119. editor.setValue("('foo')", 1);
  120. exec("gotoleft", 1);
  121. exec("selectleft", 1);
  122. exec("selectMoreBefore", 1);
  123. exec("insertstring", 1, "'");
  124. assert.equal(editor.getValue(), "('foo')");
  125. exec("selectleft", 1);
  126. exec("insertstring", 1, '"');
  127. assert.equal(editor.getValue(), '("foo")');
  128. exec("selectleft", 1);
  129. exec("insertstring", 1, '"');
  130. assert.equal(editor.getValue(), '("foo")');
  131. editor.setValue("", 1);
  132. exec("selectleft", 1);
  133. exec("insertstring", 1, '"');
  134. assert.equal(editor.getValue(), '""');
  135. exec("insertstring", 1, '\\');
  136. exec("insertstring", 1, 'n');
  137. exec("insertstring", 1, '"');
  138. assert.equal(editor.getValue(), '"\\n"');
  139. },
  140. "test: xml": function() {
  141. editor = new Editor(new MockRenderer());
  142. editor.setValue(["<OuterTag>",
  143. " <SelfClosingTag />"
  144. ].join("\n"));
  145. editor.session.setMode(new XMLMode);
  146. exec("gotolinedown", 1);
  147. exec("gotolineend", 1);
  148. exec("insertstring", 1, '\n');
  149. assert.equal(editor.session.getLine(2), " ");
  150. exec("gotolineup", 1);
  151. exec("gotolineend", 1);
  152. exec("insertstring", 1, '\n');
  153. assert.equal(editor.session.getLine(2), " ");
  154. }
  155. };
  156. });
  157. if (typeof module !== "undefined" && module === require.main) {
  158. require("asyncjs").test.testcase(module.exports).exec();
  159. }