1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15function test_match(re, input, expected)
16{
17  var result = re.exec(input);
18
19  if (expected === null)
20  {
21    assert (result === null);
22    return;
23  }
24
25  assert (result !== null);
26  assert (result.length === expected.length);
27
28  for (var idx = 0; idx < result.length; idx++)
29  {
30    assert (result[idx] === expected[idx]);
31  }
32}
33
34test_match (new RegExp ("A{1,2}"), "B", null);
35test_match (new RegExp ("A{1,2}"), "", null);
36test_match (new RegExp ("A{1,2}"), "A", ["A"]);
37test_match (new RegExp ("A{1,2}"), "AA", ["AA"]);
38test_match (new RegExp ("A{1,2}"), "AAA", ["AA"]);
39
40test_match (new RegExp ("A{1,}"), "B", null);
41test_match (new RegExp ("A{1,}"), "GA", ["A"]);
42test_match (new RegExp ("A{1,}"), "FAAAW", ["AAA"]);
43test_match (new RegExp ("A{1,}"), "FAdAAW", ["A"]);
44
45/* Test web compatiblity (ES2015 Annex B 1.4) */
46
47test_match (new RegExp ("A{1,2"), "A", null);
48test_match (new RegExp ("A{1,2"), "AA", null);
49test_match (new RegExp ("A{1,2"), "A{1,2", ["A{1,2"]);
50test_match (new RegExp ("A{1,2"), "AA{1,2", ["A{1,2"]);
51
52test_match (new RegExp ("A{1,"), "A", null);
53test_match (new RegExp ("A{1,"), "AA", null);
54test_match (new RegExp ("A{1,"), "A{1,", ["A{1,"]);
55test_match (new RegExp ("A{1,"), "A{1,2", ["A{1,"]);
56test_match (new RegExp ("A{1,"), "AA{1,2", ["A{1,"]);
57
58test_match (new RegExp ("A{1"), "A", null);
59test_match (new RegExp ("A{1"), "AA", null);
60test_match (new RegExp ("A{1"), "A{1,", ["A{1"]);
61test_match (new RegExp ("A{1"), "A{1,2", ["A{1"]);
62test_match (new RegExp ("A{1"), "AA{1,2", ["A{1"]);
63
64test_match (new RegExp ("A{"), "A", null);
65test_match (new RegExp ("A{"), "AA", null);
66test_match (new RegExp ("A{"), "A{,", ["A{"]);
67test_match (new RegExp ("A{"), "A{1,", ["A{"]);
68test_match (new RegExp ("A{"), "A{1,2", ["A{"]);
69test_match (new RegExp ("A{"), "AA{1,2", ["A{"]);
70
71test_match (new RegExp ("{"), "", null);
72test_match (new RegExp ("{"), "AA", null);
73test_match (new RegExp ("{"), "{,", ["{"]);
74test_match (new RegExp ("{"), "{1,", ["{"]);
75test_match (new RegExp ("{"), "{1,2", ["{"]);
76test_match (new RegExp ("{"), "A{1,2", ["{"]);
77
78test_match (new RegExp ("{{2,3}"), "", null);
79test_match (new RegExp ("{{2,3}"), "AA", null);
80test_match (new RegExp ("{{2,3}"), "{{,", ["{{"]);
81test_match (new RegExp ("{{2,3}"), "{{{,", ["{{{"]);
82test_match (new RegExp ("{{2,3}"), "{{{{,", ["{{{"]);
83
84test_match (new RegExp ("{{2,3"), "{{{{,", null);
85test_match (new RegExp ("{{2,3"), "{{2,3,", ["{{2,3"]);
86
87test_match (/A{1,2/, "A", null);
88test_match (/A{1,2/, "AA", null);
89test_match (/A{1,2/, "A{1,2", ["A{1,2"]);
90test_match (/A{1,2/, "AA{1,2", ["A{1,2"]);
91
92test_match (/A{1,/, "A", null);
93test_match (/A{1,/, "AA", null);
94test_match (/A{1,/, "A{1,", ["A{1,"]);
95test_match (/A{1,/, "A{1,2", ["A{1,"]);
96test_match (/A{1,/, "AA{1,2", ["A{1,"]);
97
98test_match (/A{1/, "A", null);
99test_match (/A{1/, "AA", null);
100test_match (/A{1/, "A{1,", ["A{1"]);
101test_match (/A{1/, "A{1,2", ["A{1"]);
102test_match (/A{1/, "AA{1,2", ["A{1"]);
103
104test_match (/A{/, "A", null);
105test_match (/A{/, "AA", null);
106test_match (/A{/, "A{,", ["A{"]);
107test_match (/A{/, "A{1,", ["A{"]);
108test_match (/A{/, "A{1,2", ["A{"]);
109test_match (/A{/, "AA{1,2", ["A{"]);
110
111test_match (/{/, "", null);
112test_match (/{/, "AA", null);
113test_match (/{/, "{,", ["{"]);
114test_match (/{/, "{1,", ["{"]);
115test_match (/{/, "{1,2", ["{"]);
116test_match (/{/, "A{1,2", ["{"]);
117
118test_match (/{{2,3}/, "", null);
119test_match (/{{2,3}/, "AA", null);
120test_match (/{{2,3}/, "{{,", ["{{"]);
121test_match (/{{2,3}/, "{{{,", ["{{{"]);
122test_match (/{{2,3}/, "{{{{,", ["{{{"]);
123
124test_match (/{{2,3/, "{{{{,", null);
125test_match (/{{2,3/, "{{2,3,", ["{{2,3"]);
126
127try {
128    new RegExp ("[");
129    assert (false);
130} catch (ex) {
131    assert (ex instanceof SyntaxError);
132}
133
134try {
135    eval ("/[/");
136    assert (false);
137} catch (ex) {
138    assert (ex instanceof SyntaxError);
139}
140
141try {
142    new RegExp ("(");
143    assert (false);
144} catch (ex) {
145    assert (ex instanceof SyntaxError);
146}
147
148try {
149    eval ("/(/");
150    assert (false);
151} catch (ex) {
152    assert (ex instanceof SyntaxError);
153}
154
155test_match (new RegExp("\s+{3,4"), "s+{3,4", null);
156test_match (new RegExp("\s+{3,4"), "s{3,4", ["s{3,4"]);
157test_match (new RegExp("\s+{3,4"), "ss{3,4", ["ss{3,4"]);
158test_match (new RegExp("\\s+{3,4"), "    {3,4", ["    {3,4"]);
159test_match (new RegExp("\\s+{3,4"), "   d{3,4", null);
160
161test_match (/s+{3,4/, "s+{3,4", null);
162test_match (/s+{3,4/, "s{3,4", ["s{3,4"]);
163test_match (/s+{3,4/, "ss{3,4", ["ss{3,4"]);
164test_match (/\s+{3,4/, "    {3,4", ["    {3,4"]);
165test_match (/\s+{3,4/, "   d{3,4", null);
166
167try {
168    new RegExp ("\s+{3,4}");
169    assert (false);
170} catch (ex) {
171    assert (ex instanceof SyntaxError);
172}
173
174try {
175    eval ("/\\s+{3,4}/");
176    assert (false);
177} catch (ex) {
178    assert (ex instanceof SyntaxError);
179}
180
181try {
182    new RegExp ("a{2,3}{2,3}");
183    assert (false);
184} catch (ex) {
185    assert (ex instanceof SyntaxError);
186}
187
188try {
189    eval ("/a{2,3}{2,3}/");
190    assert (false);
191} catch (ex) {
192    assert (ex instanceof SyntaxError);
193}
194