js/src/tests/ecma_3/RegExp/15.10.6.2-1.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_3/RegExp/15.10.6.2-1.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 + * Date: 23 October 2001
    1.11 + *
    1.12 + * SUMMARY: Testing regexps with the global flag set.
    1.13 + * NOT every substring fitting the given pattern will be matched.
    1.14 + * The parent string is CONSUMED as successive matches are found.
    1.15 + *
    1.16 + * From the ECMA-262 Final spec:
    1.17 + *
    1.18 + * 15.10.6.2 RegExp.prototype.exec(string)
    1.19 + * Performs a regular expression match of string against the regular
    1.20 + * expression and returns an Array object containing the results of
    1.21 + * the match, or null if the string did not match.
    1.22 + *
    1.23 + * The string ToString(string) is searched for an occurrence of the
    1.24 + * regular expression pattern as follows:
    1.25 + *
    1.26 + * 1.  Let S be the value of ToString(string).
    1.27 + * 2.  Let length be the length of S.
    1.28 + * 3.  Let lastIndex be the value of the lastIndex property.
    1.29 + * 4.  Let i be the value of ToInteger(lastIndex).
    1.30 + * 5.  If the global property is false, let i = 0.
    1.31 + * 6.  If i < 0 or i > length then set lastIndex to 0 and return null.
    1.32 + * 7.  Call [[Match]], giving it the arguments S and i.
    1.33 + *     If [[Match]] returned failure, go to step 8;
    1.34 + *     otherwise let r be its State result and go to step 10.
    1.35 + * 8.  Let i = i+1.
    1.36 + * 9.  Go to step 6.
    1.37 + * 10. Let e be r's endIndex value.
    1.38 + * 11. If the global property is true, set lastIndex to e.
    1.39 + *
    1.40 + *          etc.
    1.41 + *
    1.42 + *
    1.43 + * So when the global flag is set, |lastIndex| is incremented every time
    1.44 + * there is a match; not from i to i+1, but from i to "endIndex" e:
    1.45 + *
    1.46 + * e = (index of last input character matched so far by the pattern) + 1
    1.47 + *
    1.48 + * Thus in the example below, the first endIndex e occurs after the
    1.49 + * first match 'a b'. The next match will begin AFTER this, and so
    1.50 + * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched.
    1.51 + */
    1.52 +//-----------------------------------------------------------------------------
    1.53 +var i = 0;
    1.54 +var BUGNUMBER = '(none)';
    1.55 +var summary = 'Testing regexps with the global flag set';
    1.56 +var status = '';
    1.57 +var statusmessages = new Array();
    1.58 +var pattern = '';
    1.59 +var patterns = new Array();
    1.60 +var string = '';
    1.61 +var strings = new Array();
    1.62 +var actualmatch = '';
    1.63 +var actualmatches = new Array();
    1.64 +var expectedmatch = '';
    1.65 +var expectedmatches = new Array();
    1.66 +
    1.67 +
    1.68 +status = inSection(1);
    1.69 +string = 'a b c d e';
    1.70 +pattern = /\w\s\w/g;
    1.71 +actualmatch = string.match(pattern);
    1.72 +expectedmatch = ['a b','c d']; // see above explanation -
    1.73 +addThis();
    1.74 +
    1.75 +
    1.76 +status = inSection(2);
    1.77 +string = '12345678';
    1.78 +pattern = /\d\d\d/g;
    1.79 +actualmatch = string.match(pattern);
    1.80 +expectedmatch = ['123','456'];
    1.81 +addThis();
    1.82 +
    1.83 +
    1.84 +
    1.85 +//-----------------------------------------------------------------------------
    1.86 +test();
    1.87 +//-----------------------------------------------------------------------------
    1.88 +
    1.89 +
    1.90 +
    1.91 +function addThis()
    1.92 +{
    1.93 +  statusmessages[i] = status;
    1.94 +  patterns[i] = pattern;
    1.95 +  strings[i] = string;
    1.96 +  actualmatches[i] = actualmatch;
    1.97 +  expectedmatches[i] = expectedmatch;
    1.98 +  i++;
    1.99 +}
   1.100 +
   1.101 +
   1.102 +function test()
   1.103 +{
   1.104 +  enterFunc ('test');
   1.105 +  printBugNumber(BUGNUMBER);
   1.106 +  printStatus (summary);
   1.107 +  testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
   1.108 +  exitFunc ('test');
   1.109 +}

mercurial