Home | History | Annotate | Download | only in RegExp
      1 /*
      2 * The contents of this file are subject to the Netscape Public
      3 * License Version 1.1 (the "License"); you may not use this file
      4 * except in compliance with the License. You may obtain a copy of
      5 * the License at http://www.mozilla.org/NPL/
      6 *
      7 * Software distributed under the License is distributed on an "AS
      8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      9 * implied. See the License for the specific language governing
     10 * rights and limitations under the License.
     11 *
     12 * The Original Code is mozilla.org code.
     13 *
     14 * The Initial Developer of the Original Code is Netscape
     15 * Communications Corporation.  Portions created by Netscape are
     16 * Copyright (C) 1998 Netscape Communications Corporation. All
     17 * Rights Reserved.
     18 *
     19 * Contributor(s): pschwartau (at) netscape.com
     20 * Date: 26 November 2000
     21 *
     22 *
     23 *SUMMARY: Passing a RegExp object to a RegExp() constructor.
     24 *This test arose from Bugzilla bug 61266. The ECMA3 section is:
     25 *
     26 *  15.10.4.1 new RegExp(pattern, flags)
     27 *
     28 *  If pattern is an object R whose [[Class]] property is "RegExp" and
     29 *  flags is undefined, then let P be the pattern used to construct R
     30 *  and let F be the flags used to construct R. If pattern is an object R
     31 *  whose [[Class]] property is "RegExp" and flags is not undefined,
     32 *  then throw a TypeError exception. Otherwise, let P be the empty string
     33 *  if pattern is undefined and ToString(pattern) otherwise, and let F be
     34 *  the empty string if flags is undefined and ToString(flags) otherwise.
     35 *
     36 *
     37 *The current test will check the first scenario outlined above:
     38 *
     39 *   "pattern" is itself a RegExp object R
     40 *   "flags"  is undefined
     41 *
     42 * We check that a new RegExp object obj2 defined from these parameters
     43 * is morally the same as the original RegExp object obj1. Of course, they
     44 * can't be equal as objects - so we check their enumerable properties...
     45 *
     46 * In this test, the initial RegExp object obj1 will include a flag. This test is
     47 * identical to test 15.10.4.1-3.js, except that here we use this syntax:
     48 *
     49 *                  obj2 = new RegExp(obj1, undefined);
     50 *
     51 * instead of:
     52 *
     53 *                  obj2 = new RegExp(obj1);
     54 */
     55 //-------------------------------------------------------------------------------------------------
     56 var bug = '61266';
     57 var summary = 'Passing a RegExp object to a RegExp() constructor';
     58 var statprefix = 'Applying RegExp() twice to pattern ';
     59 var statmiddle = ' and flag ';
     60 var statsuffix =  '; testing property ';
     61 var singlequote = "'";
     62 var i = -1; var j = -1; var s = '';
     63 var obj1 = {}; var obj2 = {};
     64 var status = ''; var actual = ''; var expect = ''; var msg = '';
     65 var patterns = new Array();
     66 var flags = new Array();
     67 
     68 
     69 // various regular expressions to try -
     70 patterns[0] = '';
     71 patterns[1] = 'abc';
     72 patterns[2] = '(.*)(3-1)\s\w';
     73 patterns[3] = '(.*)(...)\\s\\w';
     74 patterns[4] = '[^A-Za-z0-9_]';
     75 patterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)';
     76 
     77 // various flags to try -
     78 flags[0] = 'i';
     79 flags[1] = 'g';
     80 flags[2] = 'm';
     81 flags[3] = undefined;
     82 
     83 
     84 
     85 //-------------------------------------------------------------------------------------------------
     86 test();
     87 //-------------------------------------------------------------------------------------------------
     88 
     89 
     90 function test()
     91 {
     92   enterFunc ('test');
     93   printBugNumber (bug);
     94   printStatus (summary);
     95 
     96   for (i in patterns)
     97   {
     98     s = patterns[i];
     99 
    100     for (j in flags)
    101     {
    102       f = flags[j];
    103       status = getStatus(s, f);
    104       obj1 = new RegExp(s, f);
    105       obj2 = new RegExp(obj1, undefined);  // see introduction to bug
    106 
    107       for (prop in obj2)
    108       {
    109         msg  = status +  quote(prop);
    110         actual = obj2[prop];
    111         expect = obj1[prop];
    112         reportCompare (expect, actual, msg);
    113       }
    114     }
    115   }
    116 
    117   exitFunc ('test');
    118 }
    119 
    120 
    121 function getStatus(regexp, flag)
    122 {
    123   return (statprefix  +  quote(regexp) +  statmiddle  +  flag  +  statsuffix);
    124 }
    125 
    126 
    127 function quote(text)
    128 {
    129   return (singlequote  +  text  + singlequote);
    130 }