• 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 search = RegExp.prototype[Symbol.search];
16
17try {
18  search.call (0, "string");
19  assert (false);
20} catch (e) {
21  assert (e instanceof TypeError);
22}
23
24try {
25  search.call (new RegExp(), {
26    toString: () => {
27      throw "abrupt string"
28    }
29  });
30  assert (false);
31} catch (e) {
32  assert (e === "abrupt string");
33}
34
35try {
36  search.call ({
37    get lastIndex() {
38      throw "abrupt get lastIndex"
39    }
40  }, "string");
41  assert (false);
42} catch (e) {
43  assert (e === "abrupt get lastIndex");
44}
45
46try {
47  search.call ({
48    get lastIndex() {
49      return 3;
50    },
51    set lastIndex(idx) {
52      throw "abrupt set lastIndex"
53    }
54  }, "string");
55  assert (false);
56} catch (e) {
57  assert (e === "abrupt set lastIndex");
58}
59
60try {
61  search.call ({
62    get exec() {
63      throw "abrupt exec"
64    }
65  }, "string");
66  assert (false);
67} catch (e) {
68  assert (e === "abrupt exec");
69}
70
71try {
72  search.call ({
73    exec: RegExp.prototype.exec
74  }, "string");
75  assert (false);
76} catch (e) {
77  assert (e instanceof TypeError);
78}
79
80try {
81  search.call ({
82    exec: 42
83  }, "string");
84  assert (false);
85} catch (e) {
86  assert (e instanceof TypeError);
87}
88
89try {
90  search.call ({
91    exec: () => {
92      throw "abrupt exec result"
93    }
94  }, "string");
95  assert (false);
96} catch (e) {
97  assert (e === "abrupt exec result");
98}
99
100try {
101  search.call ({
102    exec: () => {
103      return 1
104    }
105  }, "string");
106  assert (false);
107} catch (e) {
108  assert (e instanceof TypeError);
109}
110
111try {
112  search.call ({
113    exec: () => {
114      return {
115        get index() {
116          throw "abrupt index"
117        }
118      }
119    }
120  }, "string");
121  assert (false);
122} catch (e) {
123  assert (e === "abrupt index");
124}
125
126assert (search.call (/abc/, "abc") === 0);
127assert (search.call (/abc/, "strabc") === 3);
128assert (search.call (/abc/, "bcd") === -1);
129
130class Regexplike {
131  constructor() {
132    this.index = 0;
133    this.global = true;
134  }
135
136  exec() {
137    if (this.index > 0) {
138      return null;
139    }
140
141    this.index = 42;
142    var result = {
143      length: 1,
144      0: "Duck",
145      index: this.index
146    };
147    return result;
148  }
149}
150
151re = new Regexplike();
152assert (search.call (re, "str") === 42);
153
154/* Object with custom @@search method */
155var o = {}
156o[Symbol.search] = function () {
157  return 4;
158};
159assert ("string".search (o) === 4);
160
161o[Symbol.search] = 42;
162try {
163  "string".search (o);
164  assert (false);
165} catch (e) {
166  assert (e instanceof TypeError);
167}
168
169Object.defineProperty (o, Symbol.search, {
170  get: () => {
171    throw "abrupt @@search get"
172  },
173  set: (v) => {}
174});
175
176try {
177  "string".search (o);
178  assert (false);
179} catch (e) {
180  assert (e === "abrupt @@search get");
181}
182
183o = {};
184o[Symbol.search] = function () {
185  throw "abrupt @@search"
186};
187try {
188  "string".search (o);
189  assert (false);
190} catch (e) {
191  assert (e === "abrupt @@search")
192}
193
194o = {
195  exec: function () { return {index: "Duck"}; },
196};
197assert ("string".search (o) === 1);
198
199o[Symbol.search] = RegExp.prototype[Symbol.search];
200assert ("string".search (o) === "Duck");
201
202o = {
203  lastIndex: "Duck",
204  exec: () => {
205    return "Duck";
206  }
207}
208
209try {
210  RegExp.prototype[Symbol.search].call (o, "Duck");
211  assert (false);
212} catch (e) {
213  assert (e instanceof TypeError);
214}
215
216o = {
217  exec: () => {
218    return { 0: "Duck", index: 0 };
219  },
220  get lastIndex () {
221    return "Duck";
222  },
223  set lastIndex (v) {
224    return "Duck";
225  }
226}
227
228assert (RegExp.prototype[Symbol.search].call (o, "str") === 0);
229