incremental_search_test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 emacs = ace_require('./keyboard/emacs');
  36. var EditSession = ace_require("./edit_session").EditSession;
  37. var Editor = ace_require("./editor").Editor;
  38. var MockRenderer = ace_require("./test/mockrenderer").MockRenderer;
  39. var Range = ace_require("./range").Range;
  40. var MultiSelect = ace_require("./multi_select").MultiSelect;
  41. var assert = ace_require("./test/assertions");
  42. var IncrementalSearch = ace_require("./incremental_search").IncrementalSearch;
  43. ace_require("./multi_select");
  44. var editor, iSearch;
  45. function testRanges(str, ranges) {
  46. ranges = ranges || editor.selection.getAllRanges();
  47. assert.equal(ranges + "", str + "");
  48. }
  49. // force "rerender"
  50. function callHighlighterUpdate() {
  51. var session = editor.session,
  52. ranges = [],
  53. mockMarkerLayer = {
  54. drawSingleLineMarker: function(_, markerRanges) {
  55. ranges = ranges.concat(markerRanges);
  56. }
  57. };
  58. session.$isearchHighlight.update([], mockMarkerLayer, session, {
  59. firstRow: 0, lastRow: session.getRowLength()});
  60. return ranges;
  61. }
  62. module.exports = {
  63. name: "ACE incremental_search.js",
  64. setUp: function() {
  65. var session = new EditSession(["abc123", "xyz124"]);
  66. editor = new Editor(new MockRenderer(), session);
  67. new MultiSelect(editor);
  68. iSearch = new IncrementalSearch();
  69. },
  70. "test: keyboard handler setup" : function() {
  71. iSearch.activate(editor);
  72. assert.equal(editor.getKeyboardHandler(), iSearch.$keyboardHandler);
  73. iSearch.deactivate();
  74. assert.notEqual(editor.getKeyboardHandler(), iSearch.$keyboardHandler);
  75. },
  76. "test: isearch highlight setup" : function() {
  77. var sess = editor.session;
  78. iSearch.activate(editor);
  79. iSearch.highlight('foo');
  80. var highl = sess.$isearchHighlight.id;
  81. assert.ok(sess.$isearchHighlight, 'session has no isearch highlighter');
  82. assert.equal(sess.getMarkers()[highl.id], highl.id, 'isearch highlight not in markers');
  83. iSearch.deactivate();
  84. iSearch.activate(editor);
  85. iSearch.highlight('bar');
  86. var highl2 = sess.$isearchHighlight.id;
  87. assert.equal(highl2, highl, 'multiple isearch highlights');
  88. },
  89. "test: find simple text incrementally" : function() {
  90. iSearch.activate(editor);
  91. var range = iSearch.addString('1'), // "1"
  92. highlightRanges = callHighlighterUpdate();
  93. testRanges("Range: [0/3] -> [0/4]", [range]);
  94. testRanges("Range: [0/3] -> [0/4],Range: [1/3] -> [1/4]", highlightRanges);
  95. range = iSearch.addString('2'); // "12"
  96. highlightRanges = callHighlighterUpdate();
  97. testRanges("Range: [0/3] -> [0/5]", [range]);
  98. testRanges("Range: [0/3] -> [0/5],Range: [1/3] -> [1/5]", highlightRanges);
  99. range = iSearch.addString('3'); // "123"
  100. highlightRanges = callHighlighterUpdate();
  101. testRanges("Range: [0/3] -> [0/6]", [range]);
  102. testRanges("Range: [0/3] -> [0/6]", highlightRanges);
  103. range = iSearch.removeChar(); // "12"
  104. highlightRanges = callHighlighterUpdate();
  105. testRanges("Range: [0/3] -> [0/5]", [range]);
  106. testRanges("Range: [0/3] -> [0/5],Range: [1/3] -> [1/5]", highlightRanges);
  107. },
  108. "test: forward / backward" : function() {
  109. iSearch.activate(editor);
  110. iSearch.addString('1'); iSearch.addString('2');
  111. var range = iSearch.next();
  112. testRanges("Range: [1/3] -> [1/5]", [range]);
  113. range = iSearch.next(); // nothing to find
  114. testRanges("", [range]);
  115. range = iSearch.next({backwards: true}); // backwards
  116. testRanges("Range: [1/5] -> [1/3]", [range]);
  117. },
  118. "test: cancelSearch" : function() {
  119. iSearch.activate(editor);
  120. iSearch.addString('1'); iSearch.addString('2');
  121. var range = iSearch.cancelSearch(true);
  122. testRanges("Range: [0/0] -> [0/0]", [range]);
  123. iSearch.addString('1'); range = iSearch.addString('2');
  124. testRanges("Range: [0/3] -> [0/5]", [range]);
  125. },
  126. "test: failing search keeps pos" : function() {
  127. iSearch.activate(editor);
  128. iSearch.addString('1'); iSearch.addString('2');
  129. var range = iSearch.addString('x');
  130. testRanges("", [range]);
  131. assert.position(editor.getCursorPosition(), 0, 5);
  132. },
  133. "test: backwards search" : function() {
  134. editor.moveCursorTo(1,0);
  135. iSearch.activate(editor, true);
  136. iSearch.addString('1'); var range = iSearch.addString('2');
  137. testRanges("Range: [0/5] -> [0/3]", [range]);
  138. assert.position(editor.getCursorPosition(), 0, 3);
  139. },
  140. "test: forwards then backwards, same result, reoriented range" : function() {
  141. iSearch.activate(editor);
  142. iSearch.addString('1'); var range = iSearch.addString('2');
  143. testRanges("Range: [0/3] -> [0/5]", [range]);
  144. assert.position(editor.getCursorPosition(), 0, 5);
  145. range = iSearch.next({backwards: true});
  146. testRanges("Range: [0/5] -> [0/3]", [range]);
  147. assert.position(editor.getCursorPosition(), 0, 3);
  148. },
  149. "test: reuse prev search via option" : function() {
  150. iSearch.activate(editor);
  151. iSearch.addString('1'); iSearch.addString('2');
  152. assert.position(editor.getCursorPosition(), 0, 5);
  153. iSearch.deactivate();
  154. iSearch.activate(editor);
  155. iSearch.next({backwards: false, useCurrentOrPrevSearch: true});
  156. assert.position(editor.getCursorPosition(), 1, 5);
  157. },
  158. "test: don't extend selection range if selection is empty" : function() {
  159. iSearch.activate(editor);
  160. iSearch.addString('1'); iSearch.addString('2');
  161. testRanges("Range: [0/5] -> [0/5]", [editor.getSelectionRange()]);
  162. },
  163. "test: extend selection range if selection exists" : function() {
  164. iSearch.activate(editor);
  165. editor.selection.selectTo(0, 1);
  166. iSearch.addString('1'); iSearch.addString('2');
  167. testRanges("Range: [0/0] -> [0/5]", [editor.getSelectionRange()]);
  168. },
  169. "test: extend selection in emacs mark mode" : function() {
  170. var emacs = ace_require('./keyboard/emacs');
  171. editor.keyBinding.addKeyboardHandler(emacs.handler);
  172. emacs.handler.commands.setMark.exec(editor);
  173. iSearch.activate(editor);
  174. iSearch.addString('1'); iSearch.addString('2');
  175. testRanges("Range: [0/0] -> [0/5]", [editor.getSelectionRange()]);
  176. }
  177. };
  178. });
  179. if (typeof module !== "undefined" && module === ace_require.main) {
  180. ace_require("asyncjs").test.testcase(module.exports).exec();
  181. }