js/src/tests/ecma_2/String/match-001.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:0b445d20fca7
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
7 /**
8 * File Name: String/match-001.js
9 * ECMA Section: 15.6.4.9
10 * Description: Based on ECMA 2 Draft 7 February 1999
11 *
12 * Author: christine@netscape.com
13 * Date: 19 February 1999
14 */
15
16 /*
17 * String.match( regexp )
18 *
19 * If regexp is not an object of type RegExp, it is replaced with result
20 * of the expression new RegExp(regexp). Let string denote the result of
21 * converting the this value to a string. If regexp.global is false,
22 * return the result obtained by invoking RegExp.prototype.exec (see
23 * section 15.7.5.3) on regexp with string as parameter.
24 *
25 * Otherwise, set the regexp.lastIndex property to 0 and invoke
26 * RegExp.prototype.exec repeatedly until there is no match. If there is a
27 * match with an empty string (in other words, if the value of
28 * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
29 * The value returned is an array with the properties 0 through n-1
30 * corresponding to the first element of the result of each matching
31 * invocation of RegExp.prototype.exec.
32 *
33 * Note that the match function is intentionally generic; it does not
34 * require that its this value be a string object. Therefore, it can be
35 * transferred to other kinds of objects for use as a method.
36 */
37
38 var SECTION = "String/match-001.js";
39 var VERSION = "ECMA_2";
40 var TITLE = "String.prototype.match( regexp )";
41
42 startTest();
43
44 // the regexp argument is not a RegExp object
45 // this is not a string object
46
47 // cases in which the regexp global property is false
48
49 AddRegExpCases( 3, "3", "1234567890", 1, 2, ["3"] );
50
51 // cases in which the regexp object global property is true
52
53 AddGlobalRegExpCases( /34/g, "/34/g", "343443444", 3, ["34", "34", "34"] );
54 AddGlobalRegExpCases( /\d{1}/g, "/d{1}/g", "123456abcde7890", 10,
55 ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] );
56
57 AddGlobalRegExpCases( /\d{2}/g, "/d{2}/g", "123456abcde7890", 5,
58 ["12", "34", "56", "78", "90"] );
59
60 AddGlobalRegExpCases( /\D{2}/g, "/d{2}/g", "123456abcde7890", 2,
61 ["ab", "cd"] );
62
63 test();
64
65
66 function AddRegExpCases(
67 regexp, str_regexp, string, length, index, matches_array ) {
68
69 AddTestCase(
70 "( " + string + " ).match(" + str_regexp +").length",
71 length,
72 string.match(regexp).length );
73
74 AddTestCase(
75 "( " + string + " ).match(" + str_regexp +").index",
76 index,
77 string.match(regexp).index );
78
79 AddTestCase(
80 "( " + string + " ).match(" + str_regexp +").input",
81 string,
82 string.match(regexp).input );
83
84 for ( var matches = 0; matches < matches_array.length; matches++ ) {
85 AddTestCase(
86 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
87 matches_array[matches],
88 string.match(regexp)[matches] );
89 }
90 }
91
92 function AddGlobalRegExpCases(
93 regexp, str_regexp, string, length, matches_array ) {
94
95 AddTestCase(
96 "( " + string + " ).match(" + str_regexp +").length",
97 length,
98 string.match(regexp).length );
99
100 for ( var matches = 0; matches < matches_array.length; matches++ ) {
101 AddTestCase(
102 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
103 matches_array[matches],
104 string.match(regexp)[matches] );
105 }
106 }

mercurial