Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/match-001.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-001.js';
7
8 /**
9 * File Name: String/match-001.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 var SECTION = "String/match-001.js";
40 var VERSION = "ECMA_2";
41 var TITLE = "String.prototype.match( regexp )";
42
43 startTest();
44
45 // the regexp argument is not a RegExp object
46 // this is not a string object
47
48 // cases in which the regexp global property is false
49
50 AddRegExpCases( 3, "3", "1234567890", 1, 2, ["3"] );
51
52 // cases in which the regexp object global property is true
53
54 AddGlobalRegExpCases( /34/g, "/34/g", "343443444", 3, ["34", "34", "34"] );
55 AddGlobalRegExpCases( /\d{1}/g, "/d{1}/g", "123456abcde7890", 10,
56 ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] );
57
58 AddGlobalRegExpCases( /\d{2}/g, "/d{2}/g", "123456abcde7890", 5,
59 ["12", "34", "56", "78", "90"] );
60
61 AddGlobalRegExpCases( /\D{2}/g, "/d{2}/g", "123456abcde7890", 2,
62 ["ab", "cd"] );
63
64 test();
65
66
67 function AddRegExpCases(
68 regexp, str_regexp, string, length, index, matches_array ) {
69
70 AddTestCase(
71 "( " + string + " ).match(" + str_regexp +").length",
72 length,
73 string.match(regexp).length );
74
75 AddTestCase(
76 "( " + string + " ).match(" + str_regexp +").index",
77 index,
78 string.match(regexp).index );
79
80 AddTestCase(
81 "( " + string + " ).match(" + str_regexp +").input",
82 string,
83 string.match(regexp).input );
84
85 for ( var matches = 0; matches < matches_array.length; matches++ ) {
86 AddTestCase(
87 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
88 matches_array[matches],
89 string.match(regexp)[matches] );
90 }
91 }
92
93 function AddGlobalRegExpCases(
94 regexp, str_regexp, string, length, matches_array ) {
95
96 AddTestCase(
97 "( " + string + " ).match(" + str_regexp +").length",
98 length,
99 string.match(regexp).length );
100
101 for ( var matches = 0; matches < matches_array.length; matches++ ) {
102 AddTestCase(
103 "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
104 matches_array[matches],
105 string.match(regexp)[matches] );
106 }
107 }