Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/match-004.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-004.js';
7
8 /**
9 * File Name: String/match-004.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 *
39 * The match function should be intentionally generic, and not require
40 * this to be a string.
41 *
42 */
43
44 var SECTION = "String/match-004.js";
45 var VERSION = "ECMA_2";
46 var TITLE = "String.prototype.match( regexp )";
47
48 var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818";
49
50 startTest();
51
52 // set the value of lastIndex
53 re = /0./;
54 s = 10203040506070809000;
55
56 Number.prototype.match = String.prototype.match;
57
58 AddRegExpCases( re,
59 "re = " + re ,
60 s,
61 String(s),
62 1,
63 ["02"]);
64
65
66 re.lastIndex = 0;
67 AddRegExpCases( re,
68 "re = " + re +" [lastIndex is " + re.lastIndex+"]",
69 s,
70 String(s),
71 1,
72 ["02"]);
73 /*
74
75 re.lastIndex = s.length;
76
77 AddRegExpCases( re,
78 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
79 s.length,
80 s,
81 s.lastIndexOf("0"),
82 null );
83
84 re.lastIndex = s.lastIndexOf("0");
85
86 AddRegExpCases( re,
87 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
88 s.lastIndexOf("0"),
89 s,
90 s.lastIndexOf("0"),
91 ["02134"]);
92
93 re.lastIndex = s.lastIndexOf("0") + 1;
94
95 AddRegExpCases( re,
96 "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
97 s.lastIndexOf("0") +1,
98 s,
99 0,
100 null);
101 */
102 test();
103
104 function AddRegExpCases(
105 regexp, str_regexp, string, str_string, index, matches_array ) {
106
107 // prevent a runtime error
108
109 if ( regexp.exec(string) == null || matches_array == null ) {
110 AddTestCase(
111 string + ".match(" + regexp +")",
112 matches_array,
113 string.match(regexp) );
114
115 return;
116 }
117
118 AddTestCase(
119 "( " + string + " ).match(" + str_regexp +").length",
120 matches_array.length,
121 string.match(regexp).length );
122
123 AddTestCase(
124 "( " + string + " ).match(" + str_regexp +").index",
125 index,
126 string.match(regexp).index );
127
128 AddTestCase(
129 "( " + string + " ).match(" + str_regexp +").input",
130 str_string,
131 string.match(regexp).input );
132
133 var limit = matches_array.length > string.match(regexp).length ?
134 matches_array.length :
135 string.match(regexp).length;
136
137 for ( var matches = 0; matches < limit; matches++ ) {
138 AddTestCase(
139 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
140 matches_array[matches],
141 string.match(regexp)[matches] );
142 }
143 }
144
145 function AddGlobalRegExpCases(
146 regexp, str_regexp, string, matches_array ) {
147
148 // prevent a runtime error
149
150 if ( regexp.exec(string) == null || matches_array == null ) {
151 AddTestCase(
152 regexp + ".exec(" + string +")",
153 matches_array,
154 regexp.exec(string) );
155
156 return;
157 }
158
159 AddTestCase(
160 "( " + string + " ).match(" + str_regexp +").length",
161 matches_array.length,
162 string.match(regexp).length );
163
164 var limit = matches_array.length > string.match(regexp).length ?
165 matches_array.length :
166 string.match(regexp).length;
167
168 for ( var matches = 0; matches < limit; matches++ ) {
169 AddTestCase(
170 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
171 matches_array[matches],
172 string.match(regexp)[matches] );
173 }
174 }