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