• 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
15// check properties
16
17function length_configurable()
18{
19  function is_es51() {
20    return (typeof g === "function");
21    { function g() {} }
22  }
23  return is_es51() ? false : true;
24}
25
26assert(Object.getOwnPropertyDescriptor(String.prototype.match, 'length').configurable === length_configurable());
27assert(Object.getOwnPropertyDescriptor(String.prototype.match, 'length').enumerable === false);
28assert(Object.getOwnPropertyDescriptor(String.prototype.match, 'length').writable === false);
29
30function match_equals (match_result, expected)
31{
32  if (match_result.length !== expected.length)
33  {
34    return false;
35  }
36
37  for(var i = 0; i < expected.length; i++)
38  {
39    if (match_result[i] !== expected[i])
40    {
41      return false;
42    }
43  }
44
45  return true;
46}
47
48assert (match_equals ("hello".match("o"), ["o"]));
49assert ("hello".match(/ /g) == void 0);
50
51assert (match_equals ("hello".match(/o/), ["o"]));
52
53assert (match_equals ("hello".match(/l/), ["l"]));
54assert (match_equals ("hello".match(/l/g), ["l", "l"]));
55
56assert ("".match(/a/g) == void 0);
57
58assert ("".match() !== void 0 );
59
60assert (match_equals ("".match(), [""]));
61assert (match_equals ("".match(undefined), [""]));
62assert (match_equals ("".match(""), [""]));
63
64assert (match_equals ("test 1, test 2, test 3, test 45".match(/[0-9]+/g), ["1", "2", "3", "45"]));
65
66var re = new RegExp("", "g");
67assert (match_equals ("a".match(re), ["", ""]));
68
69
70/* Check Object coercible */
71try {
72  String.prototype.match.call(undefined, "");
73  assert (false);
74}
75catch (e)
76{
77  assert (e instanceof TypeError);
78}
79
80/* Check toString conversion */
81try {
82  var obj = { toString: function() { throw new ReferenceError("foo"); } };
83  String.prototype.match.call(obj, "");
84  assert (false);
85}
86catch (e)
87{
88  assert (e instanceof ReferenceError);
89  assert (e.message === "foo");
90}
91
92/* Check Invalid RegExp */
93try {
94  var obj = { toString: function() { throw new ReferenceError("foo"); } };
95  "".match (obj);
96  assert (false);
97}
98catch (e)
99{
100  assert (e instanceof ReferenceError);
101  assert (e.message === "foo");
102}
103
104/* Check if lastIndex is set to 0 on start */
105var re = /a/g;
106re.lastIndex = 3;
107
108assert (match_equals ("a".match(re), ["a"]));
109