1/**
2 *  File Name:          String/match-002.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 *  This file tests cases in which regexp.global is false.  Therefore,
32 *  results should behave as regexp.exec with string passed as a parameter.
33 *
34 */
35
36    var SECTION = "String/match-002.js";
37    var VERSION = "ECMA_2";
38    var TITLE   = "String.prototype.match( regexp )";
39
40    startTest();
41
42    // the regexp argument is not a RegExp object
43    // this is not a string object
44
45    AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/,
46                          "/([\d]{5})([-\ ]?[\d]{4})?$/",
47                          "Boston, Mass. 02134",
48                          14,
49                          ["02134", "02134", undefined]);
50
51    AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g,
52                          "/([\d]{5})([-\ ]?[\d]{4})?$/g",
53                          "Boston, Mass. 02134",
54                          ["02134"]);
55
56    // set the value of lastIndex
57    re = /([\d]{5})([-\ ]?[\d]{4})?$/;
58    re.lastIndex = 0;
59
60    s = "Boston, MA 02134";
61
62    AddRegExpCases( re,
63                     "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0",
64                     s,
65                     s.lastIndexOf("0"),
66                     ["02134", "02134", undefined]);
67
68
69    re.lastIndex = s.length;
70
71    AddRegExpCases( re,
72                    "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
73                    s.length,
74                    s,
75                    s.lastIndexOf("0"),
76                    ["02134", "02134", undefined] );
77
78    re.lastIndex = s.lastIndexOf("0");
79
80    AddRegExpCases( re,
81                    "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
82                    s.lastIndexOf("0"),
83                    s,
84                    s.lastIndexOf("0"),
85                    ["02134", "02134", undefined]);
86
87    re.lastIndex = s.lastIndexOf("0") + 1;
88
89    AddRegExpCases( re,
90                    "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " +
91                    s.lastIndexOf("0") +1,
92                    s,
93                    s.lastIndexOf("0"),
94                    ["02134", "02134", undefined]);
95
96    test();
97
98function AddRegExpCases(
99    regexp, str_regexp, string, index, matches_array ) {
100
101  // prevent a runtime error
102
103    if ( regexp.exec(string) == null || matches_array == null ) {
104        AddTestCase(
105          string + ".match(" + regexp +")",
106          matches_array,
107          string.match(regexp) );
108
109        return;
110    }
111
112    AddTestCase(
113        "( " + string  + " ).match(" + str_regexp +").length",
114        matches_array.length,
115        string.match(regexp).length );
116
117    AddTestCase(
118        "( " + string + " ).match(" + str_regexp +").index",
119        index,
120        string.match(regexp).index );
121
122    AddTestCase(
123        "( " + string + " ).match(" + str_regexp +").input",
124        string,
125        string.match(regexp).input );
126
127    var limit = matches_array.length > string.match(regexp).length ?
128                matches_array.length :
129                string.match(regexp).length;
130
131    for ( var matches = 0; matches < limit; matches++ ) {
132        AddTestCase(
133            "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
134            matches_array[matches],
135            string.match(regexp)[matches] );
136    }
137}
138
139function AddGlobalRegExpCases(
140    regexp, str_regexp, string, matches_array ) {
141
142  // prevent a runtime error
143
144    if ( regexp.exec(string) == null || matches_array == null ) {
145        AddTestCase(
146          regexp + ".exec(" + string +")",
147          matches_array,
148          regexp.exec(string) );
149
150        return;
151    }
152
153    AddTestCase(
154        "( " + string  + " ).match(" + str_regexp +").length",
155        matches_array.length,
156        string.match(regexp).length );
157
158    var limit = matches_array.length > string.match(regexp).length ?
159                matches_array.length :
160                string.match(regexp).length;
161
162    for ( var matches = 0; matches < limit; matches++ ) {
163        AddTestCase(
164            "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
165            matches_array[matches],
166            string.match(regexp)[matches] );
167    }
168}
169