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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_2/String/match-003.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     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-003.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 +var SECTION = "String/match-003.js";
    1.42 +var VERSION = "ECMA_2";
    1.43 +var TITLE   = "String.prototype.match( regexp )";
    1.44 +
    1.45 +startTest();
    1.46 +
    1.47 +// the regexp argument is not a RegExp object
    1.48 +// this is not a string object
    1.49 +
    1.50 +
    1.51 +//  [if regexp.global is true] set the regexp.lastIndex property to 0 and
    1.52 +//  invoke RegExp.prototype.exec repeatedly until there is no match. If
    1.53 +//  there is a match with an empty string (in other words, if the value of
    1.54 +//  regexp.lastIndex is left unchanged) increment regexp.lastIndex by 1.
    1.55 +//  The value returned is an array with the properties 0 through n-1
    1.56 +//  corresponding to the first element of the result of each matching invocation
    1.57 +//  of RegExp.prototype.exec.
    1.58 +
    1.59 +
    1.60 +// set the value of lastIndex
    1.61 +re = /([\d]{5})([-\ ]?[\d]{4})?$/g;
    1.62 +
    1.63 +
    1.64 +s = "Boston, MA 02134";
    1.65 +
    1.66 +AddGlobalRegExpCases( re,
    1.67 +		      "re = " + re,
    1.68 +		      s,
    1.69 +		      ["02134" ]);
    1.70 +
    1.71 +re.lastIndex = 0;
    1.72 +
    1.73 +AddGlobalRegExpCases(
    1.74 +  re,
    1.75 +  "re = " + re + "; re.lastIndex = 0 ",
    1.76 +  s,
    1.77 +  ["02134"]);
    1.78 +
    1.79 +
    1.80 +re.lastIndex = s.length;
    1.81 +
    1.82 +AddGlobalRegExpCases(
    1.83 +  re,
    1.84 +  "re = " + re + "; re.lastIndex = " + s.length,
    1.85 +  s,
    1.86 +  ["02134"] );
    1.87 +
    1.88 +re.lastIndex = s.lastIndexOf("0");
    1.89 +
    1.90 +AddGlobalRegExpCases(
    1.91 +  re,
    1.92 +  "re = "+ re +"; re.lastIndex = " + s.lastIndexOf("0"),
    1.93 +  s,
    1.94 +  ["02134"]);
    1.95 +
    1.96 +re.lastIndex = s.lastIndexOf("0") + 1;
    1.97 +
    1.98 +AddGlobalRegExpCases(
    1.99 +  re,
   1.100 +  "re = " +re+ "; re.lastIndex = " + (s.lastIndexOf("0") +1),
   1.101 +  s,
   1.102 +  ["02134"]);
   1.103 +
   1.104 +test();
   1.105 +
   1.106 +function AddGlobalRegExpCases(
   1.107 +  regexp, str_regexp, string, matches_array ) {
   1.108 +
   1.109 +  // prevent a runtime error
   1.110 +
   1.111 +  if ( string.match(regexp) == null || matches_array == null ) {
   1.112 +    AddTestCase(
   1.113 +      string + ".match(" + str_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 +  var limit = matches_array.length > string.match(regexp).length ?
   1.126 +    matches_array.length :
   1.127 +    string.match(regexp).length;
   1.128 +
   1.129 +  for ( var matches = 0; matches < limit; matches++ ) {
   1.130 +    AddTestCase(
   1.131 +      "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
   1.132 +      matches_array[matches],
   1.133 +      string.match(regexp)[matches] );
   1.134 +  }
   1.135 +}

mercurial