anchor_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. }
  33. define(function(require, exports, module) {
  34. "use strict";
  35. var Document = require("./document").Document;
  36. var Anchor = require("./anchor").Anchor;
  37. var Range = require("./range").Range;
  38. var assert = require("./test/assertions");
  39. module.exports = {
  40. "test create anchor" : function() {
  41. var doc = new Document("juhu");
  42. var anchor = new Anchor(doc, 0, 0);
  43. assert.position(anchor.getPosition(), 0, 0);
  44. assert.equal(anchor.getDocument(), doc);
  45. },
  46. "test insert text in same row before cursor should move anchor column": function() {
  47. var doc = new Document("juhu\nkinners");
  48. var anchor = new Anchor(doc, 1, 4);
  49. doc.insert({row: 1, column: 1}, "123");
  50. assert.position(anchor.getPosition(), 1, 7);
  51. },
  52. "test insert lines before cursor should move anchor row": function() {
  53. var doc = new Document("juhu\nkinners");
  54. var anchor = new Anchor(doc, 1, 4);
  55. doc.insertLines(1, ["123", "456"]);
  56. assert.position(anchor.getPosition(), 3, 4);
  57. },
  58. "test insert new line before cursor should move anchor column": function() {
  59. var doc = new Document("juhu\nkinners");
  60. var anchor = new Anchor(doc, 1, 4);
  61. doc.insertNewLine({row: 0, column: 0});
  62. assert.position(anchor.getPosition(), 2, 4);
  63. },
  64. "test insert new line in anchor line before anchor should move anchor column and row": function() {
  65. var doc = new Document("juhu\nkinners");
  66. var anchor = new Anchor(doc, 1, 4);
  67. doc.insertNewLine({row: 1, column: 2});
  68. assert.position(anchor.getPosition(), 2, 2);
  69. },
  70. "test delete text in anchor line before anchor should move anchor column": function() {
  71. var doc = new Document("juhu\nkinners");
  72. var anchor = new Anchor(doc, 1, 4);
  73. doc.remove(new Range(1, 1, 1, 3));
  74. assert.position(anchor.getPosition(), 1, 2);
  75. },
  76. "test remove range which contains the anchor should move the anchor to the start of the range": function() {
  77. var doc = new Document("juhu\nkinners");
  78. var anchor = new Anchor(doc, 0, 3);
  79. doc.remove(new Range(0, 1, 1, 3));
  80. assert.position(anchor.getPosition(), 0, 1);
  81. },
  82. "test delete character before the anchor should have no effect": function() {
  83. var doc = new Document("juhu\nkinners");
  84. var anchor = new Anchor(doc, 1, 4);
  85. doc.remove(new Range(1, 4, 1, 5));
  86. assert.position(anchor.getPosition(), 1, 4);
  87. },
  88. "test delete lines in anchor line before anchor should move anchor row": function() {
  89. var doc = new Document("juhu\n1\n2\nkinners");
  90. var anchor = new Anchor(doc, 3, 4);
  91. doc.removeLines(1, 2);
  92. assert.position(anchor.getPosition(), 1, 4);
  93. },
  94. "test remove new line before the cursor": function() {
  95. var doc = new Document("juhu\nkinners");
  96. var anchor = new Anchor(doc, 1, 4);
  97. doc.removeNewLine(0);
  98. assert.position(anchor.getPosition(), 0, 8);
  99. },
  100. "test delete range which contains the anchor should move anchor to the end of the range": function() {
  101. var doc = new Document("juhu\nkinners");
  102. var anchor = new Anchor(doc, 1, 4);
  103. doc.remove(new Range(0, 2, 1, 2));
  104. assert.position(anchor.getPosition(), 0, 4);
  105. },
  106. "test delete line which contains the anchor should move anchor to the end of the range": function() {
  107. var doc = new Document("juhu\nkinners\n123");
  108. var anchor = new Anchor(doc, 1, 5);
  109. doc.removeLines(1, 1);
  110. assert.position(anchor.getPosition(), 1, 0);
  111. },
  112. "test remove after the anchor should have no effect": function() {
  113. var doc = new Document("juhu\nkinners\n123");
  114. var anchor = new Anchor(doc, 1, 2);
  115. doc.remove(new Range(1, 4, 2, 2));
  116. assert.position(anchor.getPosition(), 1, 2);
  117. },
  118. "test anchor changes triggered by document changes should emit change event": function(next) {
  119. var doc = new Document("juhu\nkinners\n123");
  120. var anchor = new Anchor(doc, 1, 5);
  121. anchor.on("change", function(e) {
  122. assert.position(anchor.getPosition(), 0, 0);
  123. next();
  124. });
  125. doc.remove(new Range(0, 0, 2, 1));
  126. },
  127. "test only fire change event if position changes": function() {
  128. var doc = new Document("juhu\nkinners\n123");
  129. var anchor = new Anchor(doc, 1, 5);
  130. anchor.on("change", function(e) {
  131. assert.fail();
  132. });
  133. doc.remove(new Range(2, 0, 2, 1));
  134. },
  135. "test insert/remove lines at the end of the document": function() {
  136. var doc = new Document("juhu\nkinners\n123");
  137. var anchor = new Anchor(doc, 2, 4);
  138. doc.removeLines(0, 3);
  139. assert.position(anchor.getPosition(), 0, 0);
  140. doc.insertLines(0, ["a", "b", "c"]);
  141. assert.position(anchor.getPosition(), 3, 0);
  142. assert.equal(doc.getValue(), "a\nb\nc\n");
  143. }
  144. };
  145. });
  146. if (typeof module !== "undefined" && module === require.main) {
  147. require("asyncjs").test.testcase(module.exports).exec()
  148. }