search_test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (function() {
  2. "use strict";
  3. function test(name) {
  4. var text = Array.prototype.slice.call(arguments, 1, arguments.length - 1).join("\n");
  5. var body = arguments[arguments.length - 1];
  6. return window.test("search_" + name, function() {
  7. body(new CodeMirror.Doc(text));
  8. });
  9. }
  10. function run(doc, query, insensitive) {
  11. var cursor = doc.getSearchCursor(query, null, insensitive);
  12. for (var i = 3; i < arguments.length; i += 4) {
  13. var found = cursor.findNext();
  14. is(found, "not enough results (forward)");
  15. eqPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, forward, " + (i - 3) / 4);
  16. eqPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, forward, " + (i - 3) / 4);
  17. }
  18. is(!cursor.findNext(), "too many matches (forward)");
  19. for (var i = arguments.length - 4; i >= 3; i -= 4) {
  20. var found = cursor.findPrevious();
  21. is(found, "not enough results (backwards)");
  22. eqPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, backwards, " + (i - 3) / 4);
  23. eqPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, backwards, " + (i - 3) / 4);
  24. }
  25. is(!cursor.findPrevious(), "too many matches (backwards)");
  26. }
  27. test("simple", "abcdefg", "abcdefg", function(doc) {
  28. run(doc, "cde", false, 0, 2, 0, 5, 1, 2, 1, 5);
  29. });
  30. test("multiline", "hallo", "goodbye", function(doc) {
  31. run(doc, "llo\ngoo", false, 0, 2, 1, 3);
  32. run(doc, "blah\nhall", false);
  33. run(doc, "bye\neye", false);
  34. });
  35. test("regexp", "abcde", "abcde", function(doc) {
  36. run(doc, /bcd/, false, 0, 1, 0, 4, 1, 1, 1, 4);
  37. run(doc, /BCD/, false);
  38. run(doc, /BCD/i, false, 0, 1, 0, 4, 1, 1, 1, 4);
  39. });
  40. test("insensitive", "hallo", "HALLO", "oink", "hAllO", function(doc) {
  41. run(doc, "All", false, 3, 1, 3, 4);
  42. run(doc, "All", true, 0, 1, 0, 4, 1, 1, 1, 4, 3, 1, 3, 4);
  43. });
  44. test("multilineInsensitive", "zie ginds komT", "De Stoomboot", "uit Spanje weer aan", function(doc) {
  45. run(doc, "komt\nde stoomboot\nuit", false);
  46. run(doc, "komt\nde stoomboot\nuit", true, 0, 10, 2, 3);
  47. run(doc, "kOMt\ndE stOOmboot\nuiT", true, 0, 10, 2, 3);
  48. });
  49. test("expandingCaseFold", "<b>İİ İİ</b>", "<b>uu uu</b>", function(doc) {
  50. if (phantom) return; // A Phantom bug makes this hang
  51. run(doc, "</b>", true, 0, 8, 0, 12, 1, 8, 1, 12);
  52. run(doc, "İİ", true, 0, 3, 0, 5, 0, 6, 0, 8);
  53. });
  54. })();