anchor_test.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 text at anchor should not move anchor when insertRight is true": function() {
  53. var doc = new Document("juhu\nkinners");
  54. var anchor = new Anchor(doc, 1, 4);
  55. anchor.$insertRight = true;
  56. doc.insert({row: 1, column: 4}, "123");
  57. assert.position(anchor.getPosition(), 1, 4);
  58. },
  59. "test insert lines before cursor should move anchor row": function() {
  60. var doc = new Document("juhu\nkinners");
  61. var anchor = new Anchor(doc, 1, 4);
  62. doc.insertFullLines(1, ["123", "456"]);
  63. assert.position(anchor.getPosition(), 3, 4);
  64. },
  65. "test insert lines at anchor position should move anchor down": function() {
  66. var doc = new Document("juhu\nkinners");
  67. var anchor = new Anchor(doc, 1, 0);
  68. doc.insertLines(1, ["line"]);
  69. assert.position(anchor.getPosition(), 2, 0);
  70. },
  71. "test insert lines at anchor position should not move anchor down when insertRight is true and column is 0": function() {
  72. var doc = new Document("juhu\nkinners");
  73. var anchor = new Anchor(doc, 1, 0);
  74. anchor.$insertRight = true;
  75. doc.insertLines(1, ["line"]);
  76. assert.position(anchor.getPosition(), 1, 0);
  77. },
  78. "test insert lines at anchor row should move anchor down when column > 0": function() {
  79. var doc = new Document("juhu\nkinners");
  80. var anchor = new Anchor(doc, 1, 2);
  81. anchor.$insertRight = true;
  82. doc.insertLines(1, ["line"]);
  83. assert.position(anchor.getPosition(), 2, 2);
  84. },
  85. "test insert new line before cursor should move anchor column": function() {
  86. var doc = new Document("juhu\nkinners");
  87. var anchor = new Anchor(doc, 1, 4);
  88. doc.insertMergedLines({row: 0, column: 0}, ['', '']);
  89. assert.position(anchor.getPosition(), 2, 4);
  90. },
  91. "test insert new line in anchor line before anchor should move anchor column and row": function() {
  92. var doc = new Document("juhu\nkinners");
  93. var anchor = new Anchor(doc, 1, 4);
  94. doc.insertMergedLines({row: 1, column: 2}, ['', '']);
  95. assert.position(anchor.getPosition(), 2, 2);
  96. },
  97. "test delete text in anchor line before anchor should move anchor column": function() {
  98. var doc = new Document("juhu\nkinners");
  99. var anchor = new Anchor(doc, 1, 4);
  100. doc.remove(new Range(1, 1, 1, 3));
  101. assert.position(anchor.getPosition(), 1, 2);
  102. },
  103. "test remove range which contains the anchor should move the anchor to the start of the range": function() {
  104. var doc = new Document("juhu\nkinners");
  105. var anchor = new Anchor(doc, 0, 3);
  106. doc.remove(new Range(0, 1, 1, 3));
  107. assert.position(anchor.getPosition(), 0, 1);
  108. },
  109. "test delete character before the anchor should have no effect": function() {
  110. var doc = new Document("juhu\nkinners");
  111. var anchor = new Anchor(doc, 1, 4);
  112. doc.remove(new Range(1, 4, 1, 5));
  113. assert.position(anchor.getPosition(), 1, 4);
  114. },
  115. "test delete lines in anchor line before anchor should move anchor row": function() {
  116. var doc = new Document("juhu\n1\n2\nkinners");
  117. var anchor = new Anchor(doc, 3, 4);
  118. doc.removeFullLines(1, 2);
  119. assert.position(anchor.getPosition(), 1, 4);
  120. },
  121. "test remove new line before the cursor": function() {
  122. var doc = new Document("juhu\nkinners");
  123. var anchor = new Anchor(doc, 1, 4);
  124. doc.removeNewLine(0);
  125. assert.position(anchor.getPosition(), 0, 8);
  126. },
  127. "test delete range which contains the anchor should move anchor to the end of the range": function() {
  128. var doc = new Document("juhu\nkinners");
  129. var anchor = new Anchor(doc, 1, 4);
  130. doc.remove(new Range(0, 2, 1, 2));
  131. assert.position(anchor.getPosition(), 0, 4);
  132. },
  133. "test delete line which contains the anchor should move anchor to the end of the range": function() {
  134. var doc = new Document("juhu\nkinners\n123");
  135. var anchor = new Anchor(doc, 1, 5);
  136. doc.removeFullLines(1, 1);
  137. assert.position(anchor.getPosition(), 1, 0);
  138. },
  139. "test remove after the anchor should have no effect": function() {
  140. var doc = new Document("juhu\nkinners\n123");
  141. var anchor = new Anchor(doc, 1, 2);
  142. doc.remove(new Range(1, 4, 2, 2));
  143. assert.position(anchor.getPosition(), 1, 2);
  144. },
  145. "test anchor changes triggered by document changes should emit change event": function(next) {
  146. var doc = new Document("juhu\nkinners\n123");
  147. var anchor = new Anchor(doc, 1, 5);
  148. anchor.on("change", function(e) {
  149. assert.position(anchor.getPosition(), 0, 0);
  150. next();
  151. });
  152. doc.remove(new Range(0, 0, 2, 1));
  153. },
  154. "test only fire change event if position changes": function() {
  155. var doc = new Document("juhu\nkinners\n123");
  156. var anchor = new Anchor(doc, 1, 5);
  157. anchor.on("change", function(e) {
  158. assert.fail();
  159. });
  160. doc.remove(new Range(2, 0, 2, 1));
  161. },
  162. "test insert/remove lines at the end of the document": function() {
  163. var doc = new Document("juhu\nkinners\n123");
  164. var anchor = new Anchor(doc, 2, 4);
  165. doc.removeFullLines(0, 3);
  166. assert.position(anchor.getPosition(), 0, 0);
  167. doc.insertFullLines(0, ["a", "b", "c"]);
  168. assert.position(anchor.getPosition(), 3, 0);
  169. assert.equal(doc.getValue(), "a\nb\nc\n");
  170. }
  171. };
  172. });
  173. if (typeof module !== "undefined" && module === require.main) {
  174. require("asyncjs").test.testcase(module.exports).exec()
  175. }