range_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ace_require("amd-loader");
  32. }
  33. define(function(ace_require, exports, module) {
  34. "use strict";
  35. var Range = ace_require("./range").Range;
  36. var EditSession = ace_require("./edit_session").EditSession;
  37. var assert = ace_require("./test/assertions");
  38. module.exports = {
  39. name: "ACE range.js",
  40. "test: create range": function() {
  41. var range = new Range(1,2,3,4);
  42. assert.equal(range.start.row, 1);
  43. assert.equal(range.start.column, 2);
  44. assert.equal(range.end.row, 3);
  45. assert.equal(range.end.column, 4);
  46. },
  47. "test: create from points": function() {
  48. var range = Range.fromPoints({row: 1, column: 2}, {row:3, column:4});
  49. assert.equal(range.start.row, 1);
  50. assert.equal(range.start.column, 2);
  51. assert.equal(range.end.row, 3);
  52. assert.equal(range.end.column, 4);
  53. },
  54. "test: clip to rows": function() {
  55. assert.range(new Range(0, 20, 100, 30).clipRows(10, 30), 10, 0, 31, 0);
  56. assert.range(new Range(0, 20, 30, 10).clipRows(10, 30), 10, 0, 30, 10);
  57. var range = new Range(0, 20, 3, 10);
  58. var range = range.clipRows(10, 30);
  59. assert.ok(range.isEmpty());
  60. assert.range(range, 10, 0, 10, 0);
  61. },
  62. "test: isEmpty": function() {
  63. var range = new Range(1, 2, 1, 2);
  64. assert.ok(range.isEmpty());
  65. var range = new Range(1, 2, 1, 6);
  66. assert.notOk(range.isEmpty());
  67. },
  68. "test: is multi line": function() {
  69. var range = new Range(1, 2, 1, 6);
  70. assert.notOk(range.isMultiLine());
  71. var range = new Range(1, 2, 2, 6);
  72. assert.ok(range.isMultiLine());
  73. },
  74. "test: clone": function() {
  75. var range = new Range(1, 2, 3, 4);
  76. var clone = range.clone();
  77. assert.position(clone.start, 1, 2);
  78. assert.position(clone.end, 3, 4);
  79. clone.start.column = 20;
  80. assert.position(range.start, 1, 2);
  81. clone.end.column = 20;
  82. assert.position(range.end, 3, 4);
  83. },
  84. "test: contains for multi line ranges": function() {
  85. var range = new Range(1, 10, 5, 20);
  86. assert.ok(range.contains(1, 10));
  87. assert.ok(range.contains(2, 0));
  88. assert.ok(range.contains(3, 100));
  89. assert.ok(range.contains(5, 19));
  90. assert.ok(range.contains(5, 20));
  91. assert.notOk(range.contains(1, 9));
  92. assert.notOk(range.contains(0, 0));
  93. assert.notOk(range.contains(5, 21));
  94. },
  95. "test: contains for single line ranges": function() {
  96. var range = new Range(1, 10, 1, 20);
  97. assert.ok(range.contains(1, 10));
  98. assert.ok(range.contains(1, 15));
  99. assert.ok(range.contains(1, 20));
  100. assert.notOk(range.contains(0, 9));
  101. assert.notOk(range.contains(2, 9));
  102. assert.notOk(range.contains(1, 9));
  103. assert.notOk(range.contains(1, 21));
  104. },
  105. "test: extend range": function() {
  106. var range = new Range(2, 10, 2, 30);
  107. var range = range.extend(2, 5);
  108. assert.range(range, 2, 5, 2, 30);
  109. var range = range.extend(2, 35);
  110. assert.range(range, 2, 5, 2, 35);
  111. var range = range.extend(2, 15);
  112. assert.range(range, 2, 5, 2, 35);
  113. var range = range.extend(1, 4);
  114. assert.range(range, 1, 4, 2, 35);
  115. var range = range.extend(6, 10);
  116. assert.range(range, 1, 4, 6, 10);
  117. },
  118. "test: collapse rows" : function() {
  119. var range = new Range(0, 2, 1, 2);
  120. assert.range(range.collapseRows(), 0, 0, 1, 0);
  121. var range = new Range(2, 2, 3, 1);
  122. assert.range(range.collapseRows(), 2, 0, 3, 0);
  123. var range = new Range(2, 2, 3, 0);
  124. assert.range(range.collapseRows(), 2, 0, 2, 0);
  125. var range = new Range(2, 0, 2, 0);
  126. assert.range(range.collapseRows(), 2, 0, 2, 0);
  127. },
  128. "test: to screen range" : function() {
  129. var session = new EditSession([
  130. "juhu",
  131. "12\t\t34",
  132. "ぁぁa",
  133. "\t\t34"
  134. ]);
  135. var range = new Range(0, 0, 0, 3);
  136. assert.range(range.toScreenRange(session), 0, 0, 0, 3);
  137. var range = new Range(1, 1, 1, 3);
  138. assert.range(range.toScreenRange(session), 1, 1, 1, 4);
  139. var range = new Range(2, 1, 2, 2);
  140. assert.range(range.toScreenRange(session), 2, 2, 2, 4);
  141. var range = new Range(3, 0, 3, 4);
  142. assert.range(range.toScreenRange(session), 3, 0, 3, 10);
  143. }
  144. };
  145. });
  146. if (typeof module !== "undefined" && module === ace_require.main) {
  147. ace_require("asyncjs").test.testcase(module.exports).exec();
  148. }