1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_2/String/match-002.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 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-002.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 + * This file tests cases in which regexp.global is false. Therefore, 1.41 + * results should behave as regexp.exec with string passed as a parameter. 1.42 + * 1.43 + */ 1.44 + 1.45 +var SECTION = "String/match-002.js"; 1.46 +var VERSION = "ECMA_2"; 1.47 +var TITLE = "String.prototype.match( regexp )"; 1.48 + 1.49 +startTest(); 1.50 + 1.51 +// the regexp argument is not a RegExp object 1.52 +// this is not a string object 1.53 + 1.54 +AddRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/, 1.55 + "/([\d]{5})([-\ ]?[\d]{4})?$/", 1.56 + "Boston, Mass. 02134", 1.57 + 14, 1.58 + ["02134", "02134", undefined]); 1.59 + 1.60 +AddGlobalRegExpCases( /([\d]{5})([-\ ]?[\d]{4})?$/g, 1.61 + "/([\d]{5})([-\ ]?[\d]{4})?$/g", 1.62 + "Boston, Mass. 02134", 1.63 + ["02134"]); 1.64 + 1.65 +// set the value of lastIndex 1.66 +re = /([\d]{5})([-\ ]?[\d]{4})?$/; 1.67 +re.lastIndex = 0; 1.68 + 1.69 +s = "Boston, MA 02134"; 1.70 + 1.71 +AddRegExpCases( re, 1.72 + "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex =0", 1.73 + s, 1.74 + s.lastIndexOf("0"), 1.75 + ["02134", "02134", undefined]); 1.76 + 1.77 + 1.78 +re.lastIndex = s.length; 1.79 + 1.80 +AddRegExpCases( re, 1.81 + "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.82 + s.length, 1.83 + s, 1.84 + s.lastIndexOf("0"), 1.85 + ["02134", "02134", undefined] ); 1.86 + 1.87 +re.lastIndex = s.lastIndexOf("0"); 1.88 + 1.89 +AddRegExpCases( re, 1.90 + "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.91 + s.lastIndexOf("0"), 1.92 + s, 1.93 + s.lastIndexOf("0"), 1.94 + ["02134", "02134", undefined]); 1.95 + 1.96 +re.lastIndex = s.lastIndexOf("0") + 1; 1.97 + 1.98 +AddRegExpCases( re, 1.99 + "re = /([\d]{5})([-\ ]?[\d]{4})?$/; re.lastIndex = " + 1.100 + s.lastIndexOf("0") +1, 1.101 + s, 1.102 + s.lastIndexOf("0"), 1.103 + ["02134", "02134", undefined]); 1.104 + 1.105 +test(); 1.106 + 1.107 +function AddRegExpCases( 1.108 + regexp, str_regexp, string, index, matches_array ) { 1.109 + 1.110 + // prevent a runtime error 1.111 + 1.112 + if ( regexp.exec(string) == null || matches_array == null ) { 1.113 + AddTestCase( 1.114 + string + ".match(" + regexp +")", 1.115 + matches_array, 1.116 + string.match(regexp) ); 1.117 + 1.118 + return; 1.119 + } 1.120 + 1.121 + AddTestCase( 1.122 + "( " + string + " ).match(" + str_regexp +").length", 1.123 + matches_array.length, 1.124 + string.match(regexp).length ); 1.125 + 1.126 + AddTestCase( 1.127 + "( " + string + " ).match(" + str_regexp +").index", 1.128 + index, 1.129 + string.match(regexp).index ); 1.130 + 1.131 + AddTestCase( 1.132 + "( " + string + " ).match(" + str_regexp +").input", 1.133 + string, 1.134 + string.match(regexp).input ); 1.135 + 1.136 + var limit = matches_array.length > string.match(regexp).length ? 1.137 + matches_array.length : 1.138 + string.match(regexp).length; 1.139 + 1.140 + for ( var matches = 0; matches < limit; matches++ ) { 1.141 + AddTestCase( 1.142 + "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 1.143 + matches_array[matches], 1.144 + string.match(regexp)[matches] ); 1.145 + } 1.146 +} 1.147 + 1.148 +function AddGlobalRegExpCases( 1.149 + regexp, str_regexp, string, matches_array ) { 1.150 + 1.151 + // prevent a runtime error 1.152 + 1.153 + if ( regexp.exec(string) == null || matches_array == null ) { 1.154 + AddTestCase( 1.155 + regexp + ".exec(" + string +")", 1.156 + matches_array, 1.157 + regexp.exec(string) ); 1.158 + 1.159 + return; 1.160 + } 1.161 + 1.162 + AddTestCase( 1.163 + "( " + string + " ).match(" + str_regexp +").length", 1.164 + matches_array.length, 1.165 + string.match(regexp).length ); 1.166 + 1.167 + var limit = matches_array.length > string.match(regexp).length ? 1.168 + matches_array.length : 1.169 + string.match(regexp).length; 1.170 + 1.171 + for ( var matches = 0; matches < limit; matches++ ) { 1.172 + AddTestCase( 1.173 + "( " + string + " ).match(" + str_regexp +")[" + matches +"]", 1.174 + matches_array[matches], 1.175 + string.match(regexp)[matches] ); 1.176 + } 1.177 +}