1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_2/String/match-004.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + * File Name: String/match-004.js 1.12 + * ECMA Section: 15.6.4.9 1.13 + * Description: Based on ECMA 2 Draft 7 February 1999 1.14 + * 1.15 + * Author: christine@netscape.com 1.16 + * Date: 19 February 1999 1.17 + */ 1.18 + 1.19 +/* 1.20 + * String.match( regexp ) 1.21 + * 1.22 + * If regexp is not an object of type RegExp, it is replaced with result 1.23 + * of the expression new RegExp(regexp). Let string denote the result of 1.24 + * converting the this value to a string. If regexp.global is false, 1.25 + * return the result obtained by invoking RegExp.prototype.exec (see 1.26 + * section 15.7.5.3) on regexp with string as parameter. 1.27 + * 1.28 + * Otherwise, set the regexp.lastIndex property to 0 and invoke 1.29 + * RegExp.prototype.exec repeatedly until there is no match. If there is a 1.30 + * match with an empty string (in other words, if the value of 1.31 + * regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1. 1.32 + * The value returned is an array with the properties 0 through n-1 1.33 + * corresponding to the first element of the result of each matching 1.34 + * invocation of RegExp.prototype.exec. 1.35 + * 1.36 + * Note that the match function is intentionally generic; it does not 1.37 + * require that its this value be a string object. Therefore, it can be 1.38 + * transferred to other kinds of objects for use as a method. 1.39 + * 1.40 + * 1.41 + * The match function should be intentionally generic, and not require 1.42 + * this to be a string. 1.43 + * 1.44 + */ 1.45 + 1.46 +var SECTION = "String/match-004.js"; 1.47 +var VERSION = "ECMA_2"; 1.48 +var TITLE = "String.prototype.match( regexp )"; 1.49 + 1.50 +var BUGNUMBER="http://scopus/bugsplat/show_bug.cgi?id=345818"; 1.51 + 1.52 +startTest(); 1.53 + 1.54 +// set the value of lastIndex 1.55 +re = /0./; 1.56 +s = 10203040506070809000; 1.57 + 1.58 +Number.prototype.match = String.prototype.match; 1.59 + 1.60 +AddRegExpCases( re, 1.61 + "re = " + re , 1.62 + s, 1.63 + String(s), 1.64 + 1, 1.65 + ["02"]); 1.66 + 1.67 + 1.68 +re.lastIndex = 0; 1.69 +AddRegExpCases( re, 1.70 + "re = " + re +" [lastIndex is " + re.lastIndex+"]", 1.71 + s, 1.72 + String(s), 1.73 + 1, 1.74 + ["02"]); 1.75 +/* 1.76 + 1.77 +re.lastIndex = s.length; 1.78 + 1.79 +AddRegExpCases( re, 1.80 +"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.81 +s.length, 1.82 +s, 1.83 +s.lastIndexOf("0"), 1.84 +null ); 1.85 + 1.86 +re.lastIndex = s.lastIndexOf("0"); 1.87 + 1.88 +AddRegExpCases( re, 1.89 +"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.90 +s.lastIndexOf("0"), 1.91 +s, 1.92 +s.lastIndexOf("0"), 1.93 +["02134"]); 1.94 + 1.95 +re.lastIndex = s.lastIndexOf("0") + 1; 1.96 + 1.97 +AddRegExpCases( re, 1.98 +"re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.99 +s.lastIndexOf("0") +1, 1.100 +s, 1.101 +0, 1.102 +null); 1.103 +*/ 1.104 +test(); 1.105 + 1.106 +function AddRegExpCases( 1.107 + regexp, str_regexp, string, str_string, index, matches_array ) { 1.108 + 1.109 + // prevent a runtime error 1.110 + 1.111 + if ( regexp.exec(string) == null || matches_array == null ) { 1.112 + AddTestCase( 1.113 + string + ".match(" + regexp +")", 1.114 + matches_array, 1.115 + string.match(regexp) ); 1.116 + 1.117 + return; 1.118 + } 1.119 + 1.120 + AddTestCase( 1.121 + "( " + string + " ).match(" + str_regexp +").length", 1.122 + matches_array.length, 1.123 + string.match(regexp).length ); 1.124 + 1.125 + AddTestCase( 1.126 + "( " + string + " ).match(" + str_regexp +").index", 1.127 + index, 1.128 + string.match(regexp).index ); 1.129 + 1.130 + AddTestCase( 1.131 + "( " + string + " ).match(" + str_regexp +").input", 1.132 + str_string, 1.133 + string.match(regexp).input ); 1.134 + 1.135 + var limit = matches_array.length > string.match(regexp).length ? 1.136 + matches_array.length : 1.137 + string.match(regexp).length; 1.138 + 1.139 + for ( var matches = 0; matches < limit; matches++ ) { 1.140 + AddTestCase( 1.141 + "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 1.142 + matches_array[matches], 1.143 + string.match(regexp)[matches] ); 1.144 + } 1.145 +} 1.146 + 1.147 +function AddGlobalRegExpCases( 1.148 + regexp, str_regexp, string, matches_array ) { 1.149 + 1.150 + // prevent a runtime error 1.151 + 1.152 + if ( regexp.exec(string) == null || matches_array == null ) { 1.153 + AddTestCase( 1.154 + regexp + ".exec(" + string +")", 1.155 + matches_array, 1.156 + regexp.exec(string) ); 1.157 + 1.158 + return; 1.159 + } 1.160 + 1.161 + AddTestCase( 1.162 + "( " + string + " ).match(" + str_regexp +").length", 1.163 + matches_array.length, 1.164 + string.match(regexp).length ); 1.165 + 1.166 + var limit = matches_array.length > string.match(regexp).length ? 1.167 + matches_array.length : 1.168 + string.match(regexp).length; 1.169 + 1.170 + for ( var matches = 0; matches < limit; matches++ ) { 1.171 + AddTestCase( 1.172 + "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 1.173 + matches_array[matches], 1.174 + string.match(regexp)[matches] ); 1.175 + } 1.176 +}