search_test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (function() {
  2. "use strict";
  3. function run(doc, query, options) {
  4. var cursor = doc.getSearchCursor(query, null, options);
  5. for (var i = 3; i < arguments.length; i += 4) {
  6. var found = cursor.findNext();
  7. is(found, "not enough results (forward)");
  8. eqCharPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, forward, " + (i - 3) / 4);
  9. eqCharPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, forward, " + (i - 3) / 4);
  10. }
  11. is(!cursor.findNext(), "too many matches (forward)");
  12. for (var i = arguments.length - 4; i >= 3; i -= 4) {
  13. var found = cursor.findPrevious();
  14. is(found, "not enough results (backwards)");
  15. eqCharPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, backwards, " + (i - 3) / 4);
  16. eqCharPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, backwards, " + (i - 3) / 4);
  17. }
  18. is(!cursor.findPrevious(), "too many matches (backwards)");
  19. }
  20. function test(name, f) { window.test("search_" + name, f) }
  21. test("simple", function() {
  22. var doc = new CodeMirror.Doc("abcdefg\nabcdefg")
  23. run(doc, "cde", false, 0, 2, 0, 5, 1, 2, 1, 5);
  24. });
  25. test("multiline", function() {
  26. var doc = new CodeMirror.Doc("hallo\na\nb\ngoodbye")
  27. run(doc, "llo\na\nb\ngoo", false, 0, 2, 3, 3);
  28. run(doc, "blah\na\nb\nhall", false);
  29. run(doc, "bye\nx\neye", false);
  30. });
  31. test("regexp", function() {
  32. var doc = new CodeMirror.Doc("abcde\nabcde")
  33. run(doc, /bcd/, false, 0, 1, 0, 4, 1, 1, 1, 4);
  34. run(doc, /BCD/, false);
  35. run(doc, /BCD/i, false, 0, 1, 0, 4, 1, 1, 1, 4);
  36. });
  37. test("regexpMultiline", function() {
  38. var doc = new CodeMirror.Doc("foo foo\nbar\nbaz")
  39. run(doc, /fo[^]*az/, {multiline: true}, 0, 0, 2, 3)
  40. run(doc, /[oa][^u]/, {multiline: true}, 0, 1, 0, 3, 0, 5, 0, 7, 1, 1, 1, 3, 2, 1, 2, 3)
  41. run(doc, /[a][^u]{2}/, {multiline: true}, 1, 1, 2, 0)
  42. })
  43. test("insensitive", function() {
  44. var doc = new CodeMirror.Doc("hallo\nHALLO\noink\nhAllO")
  45. run(doc, "All", false, 3, 1, 3, 4);
  46. run(doc, "All", true, 0, 1, 0, 4, 1, 1, 1, 4, 3, 1, 3, 4);
  47. });
  48. test("multilineInsensitive", function() {
  49. var doc = new CodeMirror.Doc("zie ginds komT\nDe Stoomboot\nuit Spanje weer aan")
  50. run(doc, "komt\nde stoomboot\nuit", false);
  51. run(doc, "komt\nde stoomboot\nuit", {caseFold: true}, 0, 10, 2, 3);
  52. run(doc, "kOMt\ndE stOOmboot\nuiT", {caseFold: true}, 0, 10, 2, 3);
  53. });
  54. test("multilineInsensitiveSlow", function() {
  55. var text = ""
  56. for (var i = 0; i < 1000; i++) text += "foo\nbar\n"
  57. var doc = new CodeMirror.Doc("find\nme\n" + text + "find\nme\n")
  58. var t0 = +new Date
  59. run(doc, /find\nme/, {multiline: true}, 0, 0, 1, 2, 2002, 0, 2003, 2)
  60. is(+new Date - t0 < 100)
  61. })
  62. test("expandingCaseFold", function() {
  63. var doc = new CodeMirror.Doc("<b>İİ İİ</b>\n<b>uu uu</b>")
  64. run(doc, "</b>", true, 0, 8, 0, 12, 1, 8, 1, 12);
  65. run(doc, "İİ", true, 0, 3, 0, 5, 0, 6, 0, 8);
  66. });
  67. test("normalize", function() {
  68. if (!String.prototype.normalize) return
  69. var doc = new CodeMirror.Doc("yılbaşı\n수 있을까\nLe taux d'humidité à London")
  70. run(doc, "s", false, 0, 5, 0, 6)
  71. run(doc, "이", false, 1, 2, 1, 3)
  72. run(doc, "a", false, 0, 4, 0, 5, 2, 4, 2, 5, 2, 19, 2, 20)
  73. })
  74. })();