• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15var r;
16
17r = new RegExp ("a");
18assert (r.exec ("a") == "a");
19assert (r.exec ("b") == null);
20try {
21  r.exec.call({}, "a");
22  assert (false)
23}
24catch (e)
25{
26  assert (e instanceof TypeError);
27}
28
29assert (r.test ("a") == true);
30assert (r.test ("b") == false);
31try {
32  r.test.call({}, "a");
33  assert (false)
34}
35catch (e)
36{
37  assert (e instanceof TypeError);
38}
39
40r = new RegExp ("a", "mig");
41assert (r.toString () == "/a/gim");
42
43/* Test continous calls to the exec method to see how does the match
44 * updates the lastIndex propertyand see if the match restarts.
45 */
46var re = new RegExp("a", "g");
47result = re.exec("a");
48assert (result.length === 1);
49assert (result[0] === "a");
50assert (re.lastIndex === 1);
51
52assert (re.exec("a") === null);
53assert (re.lastIndex === 0);
54
55result = re.exec("a");
56assert (result.length === 1);
57assert (result[0] === "a");
58assert (re.lastIndex === 1);
59
60var re1 = /foo/gim;
61var re2 = /bar/g;
62
63try {
64  re2.compile("asd", "ggim");
65  assert (false);
66} catch (e) {
67  assert (e instanceof SyntaxError);
68  assert (re2 == "/bar/g");
69}
70
71try {
72  re2.compile("[", undefined);
73  assert (false);
74} catch (e) {
75  assert (e instanceof SyntaxError);
76  assert (re2 == "/bar/g");
77}
78
79try {
80  re2.compile(re1, "im");
81  assert (false);
82} catch (e) {
83  assert (e instanceof TypeError);
84  assert (re2 == "/bar/g");
85}
86
87re2.lastIndex = 2;
88assert (re2.compile("asd", "im") === re2);
89
90assert (re2 == "/asd/im");
91assert (re2.global === false);
92assert (re2.ignoreCase === true);
93assert (re2.multiline === true);
94assert (re2.source === "asd");
95assert (re2.lastIndex === 0);
96
97assert (re2.compile(re1) === re2);
98assert (re2.toString() === re1.toString());
99assert (re2.global === re1.global);
100assert (re2.ignoreCase === re1.ignoreCase);
101assert (re2.multiline === re1.multiline);
102assert (re2.source === re1.source);
103assert (re2.lastIndex === 0);
104