document_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 Document = require("./document").Document;
  37. var Range = require("./range").Range;
  38. var assert = require("./test/assertions");
  39. module.exports = {
  40. "test: insert text in line" : function() {
  41. var doc = new Document(["12", "34"]);
  42. var deltas = [];
  43. doc.on("change", function(e) { deltas.push(e); });
  44. doc.insert({row: 0, column: 1}, "juhu");
  45. assert.equal(doc.getValue(), ["1juhu2", "34"].join("\n"));
  46. var d = deltas.concat();
  47. doc.revertDeltas(d);
  48. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  49. doc.applyDeltas(d);
  50. assert.equal(doc.getValue(), ["1juhu2", "34"].join("\n"));
  51. },
  52. "test: insert new line" : function() {
  53. var doc = new Document(["12", "34"]);
  54. var deltas = [];
  55. doc.on("change", function(e) { deltas.push(e); });
  56. doc.insertMergedLines({row: 0, column: 1}, ['', '']);
  57. assert.equal(doc.getValue(), ["1", "2", "34"].join("\n"));
  58. var d = deltas.concat();
  59. doc.revertDeltas(d);
  60. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  61. doc.applyDeltas(d);
  62. assert.equal(doc.getValue(), ["1", "2", "34"].join("\n"));
  63. },
  64. "test: insert lines at the beginning" : function() {
  65. var doc = new Document(["12", "34"]);
  66. var deltas = [];
  67. doc.on("change", function(e) { deltas.push(e); });
  68. doc.insertFullLines(0, ["aa", "bb"]);
  69. assert.equal(doc.getValue(), ["aa", "bb", "12", "34"].join("\n"));
  70. var d = deltas.concat();
  71. doc.revertDeltas(d);
  72. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  73. doc.applyDeltas(d);
  74. assert.equal(doc.getValue(), ["aa", "bb", "12", "34"].join("\n"));
  75. },
  76. "test: insert lines at the end" : function() {
  77. var doc = new Document(["12", "34"]);
  78. var deltas = [];
  79. doc.on("change", function(e) { deltas.push(e); });
  80. doc.insertFullLines(2, ["aa", "bb"]);
  81. assert.equal(doc.getValue(), ["12", "34", "aa", "bb"].join("\n"));
  82. },
  83. "test: insert lines in the middle" : function() {
  84. var doc = new Document(["12", "34"]);
  85. var deltas = [];
  86. doc.on("change", function(e) { deltas.push(e); });
  87. doc.insertFullLines(1, ["aa", "bb"]);
  88. assert.equal(doc.getValue(), ["12", "aa", "bb", "34"].join("\n"));
  89. var d = deltas.concat();
  90. doc.revertDeltas(d);
  91. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  92. doc.applyDeltas(d);
  93. assert.equal(doc.getValue(), ["12", "aa", "bb", "34"].join("\n"));
  94. },
  95. "test: insert multi line string at the start" : function() {
  96. var doc = new Document(["12", "34"]);
  97. var deltas = [];
  98. doc.on("change", function(e) { deltas.push(e); });
  99. doc.insert({row: 0, column: 0}, "aa\nbb\ncc");
  100. assert.equal(doc.getValue(), ["aa", "bb", "cc12", "34"].join("\n"));
  101. var d = deltas.concat();
  102. doc.revertDeltas(d);
  103. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  104. doc.applyDeltas(d);
  105. assert.equal(doc.getValue(), ["aa", "bb", "cc12", "34"].join("\n"));
  106. },
  107. "test: insert multi line string at the end" : function() {
  108. var doc = new Document(["12", "34"]);
  109. var deltas = [];
  110. doc.on("change", function(e) { deltas.push(e); });
  111. doc.insert({row: 1, column: 2}, "aa\nbb\ncc");
  112. assert.equal(doc.getValue(), ["12", "34aa", "bb", "cc"].join("\n"));
  113. var d = deltas.concat();
  114. doc.revertDeltas(d);
  115. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  116. doc.applyDeltas(d);
  117. assert.equal(doc.getValue(), ["12", "34aa", "bb", "cc"].join("\n"));
  118. },
  119. "test: insert multi line string in the middle" : function() {
  120. var doc = new Document(["12", "34"]);
  121. var deltas = [];
  122. doc.on("change", function(e) { deltas.push(e); });
  123. doc.insert({row: 0, column: 1}, "aa\nbb\ncc");
  124. assert.equal(doc.getValue(), ["1aa", "bb", "cc2", "34"].join("\n"));
  125. var d = deltas.concat();
  126. doc.revertDeltas(d);
  127. assert.equal(doc.getValue(), ["12", "34"].join("\n"));
  128. doc.applyDeltas(d);
  129. assert.equal(doc.getValue(), ["1aa", "bb", "cc2", "34"].join("\n"));
  130. },
  131. "test: delete in line" : function() {
  132. var doc = new Document(["1234", "5678"]);
  133. var deltas = [];
  134. doc.on("change", function(e) { deltas.push(e); });
  135. doc.remove(new Range(0, 1, 0, 3));
  136. assert.equal(doc.getValue(), ["14", "5678"].join("\n"));
  137. var d = deltas.concat();
  138. doc.revertDeltas(d);
  139. assert.equal(doc.getValue(), ["1234", "5678"].join("\n"));
  140. doc.applyDeltas(d);
  141. assert.equal(doc.getValue(), ["14", "5678"].join("\n"));
  142. },
  143. "test: delete new line" : function() {
  144. var doc = new Document(["1234", "5678"]);
  145. var deltas = [];
  146. doc.on("change", function(e) { deltas.push(e); });
  147. doc.remove(new Range(0, 4, 1, 0));
  148. assert.equal(doc.getValue(), ["12345678"].join("\n"));
  149. var d = deltas.concat();
  150. doc.revertDeltas(d);
  151. assert.equal(doc.getValue(), ["1234", "5678"].join("\n"));
  152. doc.applyDeltas(d);
  153. assert.equal(doc.getValue(), ["12345678"].join("\n"));
  154. },
  155. "test: delete multi line range line" : function() {
  156. var doc = new Document(["1234", "5678", "abcd"]);
  157. var deltas = [];
  158. doc.on("change", function(e) { deltas.push(e); });
  159. doc.remove(new Range(0, 2, 2, 2));
  160. assert.equal(doc.getValue(), ["12cd"].join("\n"));
  161. var d = deltas.concat();
  162. doc.revertDeltas(d);
  163. assert.equal(doc.getValue(), ["1234", "5678", "abcd"].join("\n"));
  164. doc.applyDeltas(d);
  165. assert.equal(doc.getValue(), ["12cd"].join("\n"));
  166. },
  167. "test: delete full lines" : function() {
  168. var doc = new Document(["1234", "5678", "abcd"]);
  169. var deltas = [];
  170. doc.on("change", function(e) { deltas.push(e); });
  171. doc.remove(new Range(1, 0, 3, 0));
  172. assert.equal(doc.getValue(), ["1234", ""].join("\n"));
  173. },
  174. "test: remove lines should return the removed lines" : function() {
  175. var doc = new Document(["1234", "5678", "abcd"]);
  176. var removed = doc.removeFullLines(1, 2);
  177. assert.equal(removed.join("\n"), ["5678", "abcd"].join("\n"));
  178. },
  179. "test: should handle unix style new lines" : function() {
  180. var doc = new Document(["1", "2", "3"]);
  181. assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
  182. },
  183. "test: should handle windows style new lines" : function() {
  184. var doc = new Document(["1", "2", "3"].join("\r\n"));
  185. doc.setNewLineMode("unix");
  186. assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
  187. },
  188. "test: set new line mode to 'windows' should use '\\r\\n' as new lines": function() {
  189. var doc = new Document(["1", "2", "3"].join("\n"));
  190. doc.setNewLineMode("windows");
  191. assert.equal(doc.getValue(), ["1", "2", "3"].join("\r\n"));
  192. },
  193. "test: set new line mode to 'unix' should use '\\n' as new lines": function() {
  194. var doc = new Document(["1", "2", "3"].join("\r\n"));
  195. doc.setNewLineMode("unix");
  196. assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
  197. },
  198. "test: set new line mode to 'auto' should detect the incoming nl type": function() {
  199. var doc = new Document(["1", "2", "3"].join("\n"));
  200. doc.setNewLineMode("auto");
  201. assert.equal(doc.getValue(), ["1", "2", "3"].join("\n"));
  202. var doc = new Document(["1", "2", "3"].join("\r\n"));
  203. doc.setNewLineMode("auto");
  204. assert.equal(doc.getValue(), ["1", "2", "3"].join("\r\n"));
  205. doc.replace(new Range(0, 0, 2, 1), ["4", "5", "6"].join("\n"));
  206. assert.equal(["4", "5", "6"].join("\n"), doc.getValue());
  207. },
  208. "test: set value": function() {
  209. var doc = new Document("1");
  210. assert.equal("1", doc.getValue());
  211. doc.setValue(doc.getValue());
  212. assert.equal("1", doc.getValue());
  213. var doc = new Document("1\n2");
  214. assert.equal("1\n2", doc.getValue());
  215. doc.setValue(doc.getValue());
  216. assert.equal("1\n2", doc.getValue());
  217. },
  218. "test: empty document has to contain one line": function() {
  219. var doc = new Document("");
  220. assert.equal(doc.$lines.length, 1);
  221. },
  222. "test: ignore empty delta": function() {
  223. var doc = new Document("");
  224. doc.on("change", function() {
  225. throw "should ignore empty delta";
  226. })
  227. doc.insert({row: 0, column: 0}, "");
  228. doc.insert({row: 1, column: 1}, "");
  229. doc.remove({start: {row: 1, column: 1}, end: {row: 1, column: 1}});
  230. },
  231. "test: inserting huge delta": function() {
  232. var doc = new Document("");
  233. var val = "";
  234. var MAX = 0xF000;
  235. for (var i = 0; i < 10 * MAX; i++) {
  236. val += i + "\n"
  237. }
  238. doc.setValue(val);
  239. assert.equal(doc.getValue(), val);
  240. for (var i = 3 * MAX + 2; i >= 3 * MAX - 2; i--) {
  241. val = doc.getLines(0, i).join("\n");
  242. doc.setValue("\nab");
  243. assert.equal(doc.getValue(), "\nab");
  244. doc.insert({row: 1, column: 1}, val);
  245. assert.equal(doc.getValue(), "\na" + val + "b");
  246. }
  247. }
  248. };
  249. });
  250. if (typeof module !== "undefined" && module === require.main) {
  251. require("asyncjs").test.testcase(module.exports).exec()
  252. }