js/src/tests/ecma_2/String/match-001.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-001.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     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-001.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-001.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 +// cases in which the regexp global property is false
    1.51 +
    1.52 +AddRegExpCases( 3, "3",   "1234567890", 1, 2, ["3"] );
    1.53 +
    1.54 +// cases in which the regexp object global property is true
    1.55 +
    1.56 +AddGlobalRegExpCases( /34/g, "/34/g", "343443444",  3, ["34", "34", "34"] );
    1.57 +AddGlobalRegExpCases( /\d{1}/g,  "/d{1}/g",  "123456abcde7890", 10,
    1.58 +		      ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] );
    1.59 +
    1.60 +AddGlobalRegExpCases( /\d{2}/g,  "/d{2}/g",  "123456abcde7890", 5,
    1.61 +		      ["12", "34", "56", "78", "90"] );
    1.62 +
    1.63 +AddGlobalRegExpCases( /\D{2}/g,  "/d{2}/g",  "123456abcde7890", 2,
    1.64 +		      ["ab", "cd"] );
    1.65 +
    1.66 +test();
    1.67 +
    1.68 +
    1.69 +function AddRegExpCases(
    1.70 +  regexp, str_regexp, string, length, index, matches_array ) {
    1.71 +
    1.72 +  AddTestCase(
    1.73 +    "( " + string  + " ).match(" + str_regexp +").length",
    1.74 +    length,
    1.75 +    string.match(regexp).length );
    1.76 +
    1.77 +  AddTestCase(
    1.78 +    "( " + string + " ).match(" + str_regexp +").index",
    1.79 +    index,
    1.80 +    string.match(regexp).index );
    1.81 +
    1.82 +  AddTestCase(
    1.83 +    "( " + string + " ).match(" + str_regexp +").input",
    1.84 +    string,
    1.85 +    string.match(regexp).input );
    1.86 +
    1.87 +  for ( var matches = 0; matches < matches_array.length; matches++ ) {
    1.88 +    AddTestCase(
    1.89 +      "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
    1.90 +      matches_array[matches],
    1.91 +      string.match(regexp)[matches] );
    1.92 +  }
    1.93 +}
    1.94 +
    1.95 +function AddGlobalRegExpCases(
    1.96 +  regexp, str_regexp, string, length, matches_array ) {
    1.97 +
    1.98 +  AddTestCase(
    1.99 +    "( " + string  + " ).match(" + str_regexp +").length",
   1.100 +    length,
   1.101 +    string.match(regexp).length );
   1.102 +
   1.103 +  for ( var matches = 0; matches < matches_array.length; matches++ ) {
   1.104 +    AddTestCase(
   1.105 +      "( " + string + " ).match(" + str_regexp +")[" + matches +"]",
   1.106 +      matches_array[matches],
   1.107 +      string.match(regexp)[matches] );
   1.108 +  }
   1.109 +}

mercurial