occur_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 EditSession = ace_require("./edit_session").EditSession;
  36. var Editor = ace_require("./editor").Editor;
  37. var MockRenderer = ace_require("./test/mockrenderer").MockRenderer;
  38. var Range = ace_require("./range").Range;
  39. var assert = ace_require("./test/assertions");
  40. var Occur = ace_require("./occur").Occur;
  41. var occurStartCommand = ace_require("./commands/occur_commands").occurStartCommand;
  42. var editor, occur;
  43. module.exports = {
  44. name: "ACE occur.js",
  45. setUp: function() {
  46. var session = new EditSession('');
  47. editor = new Editor(new MockRenderer(), session);
  48. occur = new Occur();
  49. },
  50. "test: find lines matching" : function() {
  51. editor.session.insert({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc');
  52. var result = occur.matchingLines(editor.session, {needle: 'bc'}),
  53. expected = [{row: 0, content: 'abc'}, {row: 3, content: 'bcxbc'}];
  54. assert.deepEqual(result, expected);
  55. },
  56. "test: display occurrences" : function() {
  57. var text = 'abc\ndef\nxyz\nbcx\n';
  58. editor.session.insert({row: 0, column: 0}, text);
  59. occur.displayOccurContent(editor, {needle: 'bc'});
  60. assert.equal(editor.getValue(), 'abc\nbcx');
  61. occur.displayOriginalContent(editor);
  62. assert.equal(editor.getValue(), text);
  63. },
  64. "test: original position from occur doc" : function() {
  65. var text = 'abc\ndef\nxyz\nbcx\n';
  66. editor.session.insert({row: 0, column: 0}, text);
  67. occur.displayOccurContent(editor, {needle: 'bc'});
  68. assert.equal(editor.getValue(), 'abc\nbcx');
  69. var pos = occur.occurToOriginalPosition(editor.session, {row: 1, column: 2});
  70. assert.position(pos, 3, 2);
  71. },
  72. "test: occur command" : function() {
  73. // setup
  74. var text = 'hel\nlo\n\nwo\nrld\n';
  75. editor.session.insert({row: 0, column: 0}, text);
  76. editor.commands.addCommand(occurStartCommand);
  77. // run occur for lines including 'o'
  78. editor.execCommand('occur', {needle: 'o'});
  79. assert.equal(editor.getValue(), 'lo\nwo');
  80. // command install OK?
  81. // assert.ok(editor.getReadOnly(), 'occur doc not marked as read only');
  82. assert.ok(editor.getKeyboardHandler().isOccurHandler, 'no occur handler installed');
  83. assert.ok(editor.commands.byName.occurexit, 'no exitoccur command installed');
  84. // exit occur
  85. editor.execCommand('occurexit');
  86. assert.equal(editor.getValue(), text);
  87. // editor state cleaned up?
  88. // assert.ok(!editor.getReadOnly(), 'original doc is marked as read only');
  89. assert.ok(!editor.getKeyboardHandler().isOccurHandler, 'occur handler installed after detach');
  90. assert.ok(!editor.commands.byName.occurexit, 'exitoccur installed after exiting occur');
  91. },
  92. "test: occur navigation" : function() {
  93. // setup
  94. var text = 'hel\nlo\n\nwo\nrld\n';
  95. editor.session.insert({row: 0, column: 0}, text);
  96. editor.commands.addCommand(occurStartCommand);
  97. editor.moveCursorToPosition({row: 1, column: 1});
  98. // run occur for lines including 'o'
  99. editor.execCommand('occur', {needle: 'o'});
  100. assert.equal(editor.getValue(), 'lo\nwo');
  101. assert.position(editor.getCursorPosition(), 0, 1, 'original -> occur pos');
  102. // move to second line and accept
  103. editor.moveCursorToPosition({row: 1, column: 1});
  104. editor.execCommand('occuraccept');
  105. assert.position(editor.getCursorPosition(), 3, 1, 'occur -> original pos');
  106. },
  107. "test: recursive occur" : function() {
  108. // setup
  109. var text = 'x\nabc1\nx\nabc2\n';
  110. editor.session.insert({row: 0, column: 0}, text);
  111. editor.commands.addCommand(occurStartCommand);
  112. // orig -> occur1
  113. editor.execCommand('occur', {needle: 'abc'});
  114. assert.equal(editor.getValue(), 'abc1\nabc2', "orig -> occur1");
  115. // occur1 -> occur2
  116. editor.execCommand('occur', {needle: '2'});
  117. assert.equal(editor.getValue(), 'abc2', "occur1 -> occur2");
  118. // occur2 -> occur1
  119. editor.execCommand('occurexit');
  120. assert.equal(editor.getValue(), 'abc1\nabc2', "occur2 -> occur1");
  121. // occur1 -> orig
  122. editor.execCommand('occurexit');
  123. assert.equal(editor.getValue(), text, "occur1 -> orig");
  124. }
  125. };
  126. });
  127. if (typeof module !== "undefined" && module === ace_require.main) {
  128. ace_require("asyncjs").test.testcase(module.exports).exec();
  129. }