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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial