Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/match-002.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'match-002.js';
7
8 /**
9 * File Name: String/match-002.js
10 * ECMA Section: 15.6.4.9
11 * Description: Based on ECMA 2 Draft 7 February 1999
12 *
13 * Author: [email protected]
14 * Date: 19 February 1999
15 */
16
17 /*
18 * String.match( regexp )
19 *
20 * If regexp is not an object of type RegExp, it is replaced with result
21 * of the expression new RegExp(regexp). Let string denote the result of
22 * converting the this value to a string. If regexp.global is false,
23 * return the result obtained by invoking RegExp.prototype.exec (see
24 * section 15.7.5.3) on regexp with string as parameter.
25 *
26 * Otherwise, set the regexp.lastIndex property to 0 and invoke
27 * RegExp.prototype.exec repeatedly until there is no match. If there is a
28 * match with an empty string (in other words, if the value of
29 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
30 * The value returned is an array with the properties 0 through n-1
31 * corresponding to the first element of the result of each matching
32 * invocation of RegExp.prototype.exec.
33 *
34 * Note that the match function is intentionally generic; it does not
35 * require that its this value be a string object. Therefore, it can be
36 * transferred to other kinds of objects for use as a method.
37 *
38 * This file tests cases in which regexp.global is false. Therefore,
39 * results should behave as regexp.exec with string passed as a parameter.
40 *
41 */
42
43 var SECTION = "String/match-002.js";
44 var VERSION = "ECMA_2";
45 var TITLE = "String.prototype.match( regexp )";
46
47 startTest();
48
49 // the regexp argument is not a RegExp object
50 // this is not a string object
51
52 AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/,
53 "/([\d]{5})([-\ ]?[\d]{4})?$/",
54 "Boston, Mass. 02134",
55 14,
56 ["02134", "02134", undefined]);
57
58 AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g,
59 "/([\d]{5})([-\ ]?[\d]{4})?$/g",
60 "Boston, Mass. 02134",
61 ["02134"]);
62
63 // set the value of lastIndex
64 re = /([\d]{5})([-\ ]?[\d]{4})?$/;
65 re.lastIndex = 0;
66
67 s = "Boston, MA 02134";
68
69 AddRegExpCases( re,
70 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0",
71 s,
72 s.lastIndexOf("0"),
73 ["02134", "02134", undefined]);
74
75
76 re.lastIndex = s.length;
77
78 AddRegExpCases( re,
79 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
80 s.length,
81 s,
82 s.lastIndexOf("0"),
83 ["02134", "02134", undefined] );
84
85 re.lastIndex = s.lastIndexOf("0");
86
87 AddRegExpCases( re,
88 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
89 s.lastIndexOf("0"),
90 s,
91 s.lastIndexOf("0"),
92 ["02134", "02134", undefined]);
93
94 re.lastIndex = s.lastIndexOf("0") + 1;
95
96 AddRegExpCases( re,
97 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
98 s.lastIndexOf("0") +1,
99 s,
100 s.lastIndexOf("0"),
101 ["02134", "02134", undefined]);
102
103 test();
104
105 function AddRegExpCases(
106 regexp, str_regexp, string, index, matches_array ) {
107
108 // prevent a runtime error
109
110 if ( regexp.exec(string) == null || matches_array == null ) {
111 AddTestCase(
112 string + ".match(" + regexp +")",
113 matches_array,
114 string.match(regexp) );
115
116 return;
117 }
118
119 AddTestCase(
120 "( " + string + " ).match(" + str_regexp +").length",
121 matches_array.length,
122 string.match(regexp).length );
123
124 AddTestCase(
125 "( " + string + " ).match(" + str_regexp +").index",
126 index,
127 string.match(regexp).index );
128
129 AddTestCase(
130 "( " + string + " ).match(" + str_regexp +").input",
131 string,
132 string.match(regexp).input );
133
134 var limit = matches_array.length > string.match(regexp).length ?
135 matches_array.length :
136 string.match(regexp).length;
137
138 for ( var matches = 0; matches < limit; matches++ ) {
139 AddTestCase(
140 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
141 matches_array[matches],
142 string.match(regexp)[matches] );
143 }
144 }
145
146 function AddGlobalRegExpCases(
147 regexp, str_regexp, string, matches_array ) {
148
149 // prevent a runtime error
150
151 if ( regexp.exec(string) == null || matches_array == null ) {
152 AddTestCase(
153 regexp + ".exec(" + string +")",
154 matches_array,
155 regexp.exec(string) );
156
157 return;
158 }
159
160 AddTestCase(
161 "( " + string + " ).match(" + str_regexp +").length",
162 matches_array.length,
163 string.match(regexp).length );
164
165 var limit = matches_array.length > string.match(regexp).length ?
166 matches_array.length :
167 string.match(regexp).length;
168
169 for ( var matches = 0; matches < limit; matches++ ) {
170 AddTestCase(
171 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
172 matches_array[matches],
173 string.match(regexp)[matches] );
174 }
175 }