summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/RegExp/sticky.js
blob: 13a6debe95880cea3bb2b3f61c72fd6b92c069ad (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var BUGNUMBER = 773687;
var summary = 'sticky flag should not break assertion behavior.';

print(BUGNUMBER + ": " + summary);

function test(re, text, expectations) {
  // Sanity check for test data itself.
  assertEq(expectations.length, text.length + 1);

  for (var i = 0; i < expectations.length; i++) {
    var result = expectations[i];

    re.lastIndex = i;
    var match = re.exec(text);
    if (result === null) {
      assertEq(re.lastIndex, 0);
      assertEq(match, null);
    } else {
      assertEq(re.lastIndex, result.lastIndex);
      assertEq(match !== null, true);
      assertEq(match.length, result.matches.length);
      for (var j = 0; j < result.matches.length; j++)
        assertEq(match[j], result.matches[j]);
      assertEq(match.index, result.index);
    }
  }
}

// simple text
test(/bc/y, "abcabd", [
  null,
  { lastIndex: 3, matches: ["bc"], index: 1 },
  null,
  null,
  null,
  null,
  null,
]);

// complex pattern
test(/bc|c|d/y, "abcabd", [
  null,
  { lastIndex: 3, matches: ["bc"], index: 1 },
  { lastIndex: 3, matches: ["c"], index: 2 },
  null,
  null,
  { lastIndex: 6, matches: ["d"], index: 5 },
  null,
]);

test(/.*(bc|c|d)/y, "abcabd", [
  { lastIndex: 6, matches: ["abcabd", "d"], index: 0 },
  { lastIndex: 6, matches: ["bcabd", "d"], index: 1 },
  { lastIndex: 6, matches: ["cabd", "d"], index: 2 },
  { lastIndex: 6, matches: ["abd", "d"], index: 3 },
  { lastIndex: 6, matches: ["bd", "d"], index: 4 },
  { lastIndex: 6, matches: ["d", "d"], index: 5 },
  null,
]);

test(/.*?(bc|c|d)/y, "abcabd", [
  { lastIndex: 3, matches: ["abc", "bc"], index: 0 },
  { lastIndex: 3, matches: ["bc", "bc"], index: 1 },
  { lastIndex: 3, matches: ["c", "c"], index: 2 },
  { lastIndex: 6, matches: ["abd", "d"], index: 3 },
  { lastIndex: 6, matches: ["bd", "d"], index: 4 },
  { lastIndex: 6, matches: ["d", "d"], index: 5 },
  null,
]);

test(/(bc|.*c|d)/y, "abcabd", [
  { lastIndex: 3, matches: ["abc", "abc"], index: 0 },
  { lastIndex: 3, matches: ["bc", "bc"], index: 1 },
  { lastIndex: 3, matches: ["c", "c"], index: 2 },
  null,
  null,
  { lastIndex: 6, matches: ["d", "d"], index: 5 },
  null,
]);

// ^ assertions
test(/^/y, "abcabc", [
  { lastIndex: 0, matches: [""], index: 0 },
  null,
  null,
  null,
  null,
  null,
  null,
]);

test(/^a/my, "abc\nabc", [
  { lastIndex: 1, matches: ["a"], index: 0 },
  null,
  null,
  null,
  { lastIndex: 5, matches: ["a"], index: 4 },
  null,
  null,
  null,
]);

// \b assertions
test(/\b/y, "abc bc", [
  { lastIndex: 0, matches: [""], index: 0 },
  null,
  null,
  { lastIndex: 3, matches: [""], index: 3 },
  { lastIndex: 4, matches: [""], index: 4 },
  null,
  { lastIndex: 6, matches: [""], index: 6 },
]);

// \B assertions
test(/\B/y, "abc bc", [
  null,
  { lastIndex: 1, matches: [""], index: 1 },
  { lastIndex: 2, matches: [""], index: 2 },
  null,
  null,
  { lastIndex: 5, matches: [""], index: 5 },
  null,
]);

if (typeof reportCompare === "function")
  reportCompare(true, true);