summaryrefslogtreecommitdiffstats
path: root/devtools/client/sourceeditor/test/codemirror/search_test.js
blob: 04a1e685abb5583b27837591675ad4128e74f300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(function() {
  "use strict";

  function test(name) {
    var text = Array.prototype.slice.call(arguments, 1, arguments.length - 1).join("\n");
    var body = arguments[arguments.length - 1];
    return window.test("search_" + name, function() {
      body(new CodeMirror.Doc(text));
    });
  }

  function run(doc, query, insensitive) {
    var cursor = doc.getSearchCursor(query, null, insensitive);
    for (var i = 3; i < arguments.length; i += 4) {
      var found = cursor.findNext();
      is(found, "not enough results (forward)");
      eqPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, forward, " + (i - 3) / 4);
      eqPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, forward, " + (i - 3) / 4);
    }
    is(!cursor.findNext(), "too many matches (forward)");
    for (var i = arguments.length - 4; i >= 3; i -= 4) {
      var found = cursor.findPrevious();
      is(found, "not enough results (backwards)");
      eqPos(Pos(arguments[i], arguments[i + 1]), cursor.from(), "from, backwards, " + (i - 3) / 4);
      eqPos(Pos(arguments[i + 2], arguments[i + 3]), cursor.to(), "to, backwards, " + (i - 3) / 4);
    }
    is(!cursor.findPrevious(), "too many matches (backwards)");
  }

  test("simple", "abcdefg", "abcdefg", function(doc) {
    run(doc, "cde", false, 0, 2, 0, 5, 1, 2, 1, 5);
  });

  test("multiline", "hallo", "goodbye", function(doc) {
    run(doc, "llo\ngoo", false, 0, 2, 1, 3);
    run(doc, "blah\nhall", false);
    run(doc, "bye\neye", false);
  });

  test("regexp", "abcde", "abcde", function(doc) {
    run(doc, /bcd/, false, 0, 1, 0, 4, 1, 1, 1, 4);
    run(doc, /BCD/, false);
    run(doc, /BCD/i, false, 0, 1, 0, 4, 1, 1, 1, 4);
  });

  test("insensitive", "hallo", "HALLO", "oink", "hAllO", function(doc) {
    run(doc, "All", false, 3, 1, 3, 4);
    run(doc, "All", true, 0, 1, 0, 4, 1, 1, 1, 4, 3, 1, 3, 4);
  });

  test("multilineInsensitive", "zie ginds komT", "De Stoomboot", "uit Spanje weer aan", function(doc) {
    run(doc, "komt\nde stoomboot\nuit", false);
    run(doc, "komt\nde stoomboot\nuit", true, 0, 10, 2, 3);
    run(doc, "kOMt\ndE stOOmboot\nuiT", true, 0, 10, 2, 3);
  });

  test("expandingCaseFold", "<b>İİ İİ</b>", "<b>uu uu</b>", function(doc) {
    if (phantom) return; // A Phantom bug makes this hang
    run(doc, "</b>", true, 0, 8, 0, 12, 1, 8, 1, 12);
    run(doc, "İİ", true, 0, 3, 0, 5, 0, 6, 0, 8);
  });
})();