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