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  IS"
      8 * basis, WITHOUT WARRANTY OF ANY KIND, either expressed
      9 * or 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.
     17 * All Rights Reserved.
     18 *
     19 * Contributor(s): mozilla (at) pdavis.cx, pschwartau (at) netscape.com
     20 * Date: 22 October 2001
     21 *
     22 * SUMMARY: Regression test for Bugzilla bug 105972:
     23 * "/^.*?$/ will not match anything"
     24 *
     25 * See http://bugzilla.mozilla.org/show_bug.cgi?id=105972
     26 */
     27 //-----------------------------------------------------------------------------
     28 var i = 0;
     29 var bug = 105972;
     30 var summary = 'Regression test for Bugzilla bug 105972';
     31 var cnEmptyString = '';
     32 var status = '';
     33 var statusmessages = new Array();
     34 var pattern = '';
     35 var patterns = new Array();
     36 var string = '';
     37 var strings = new Array();
     38 var actualmatch = '';
     39 var actualmatches = new Array();
     40 var expectedmatch = '';
     41 var expectedmatches = new Array();
     42 
     43 
     44 /*
     45  * The bug: this match was coming up null in Rhino and SpiderMonkey.
     46  * It should match the whole string. The reason:
     47  *
     48  * The * operator is greedy, but *? is non-greedy: it will stop
     49  * at the simplest match it can find. But the pattern here asks us
     50  * to match till the end of the string. So the simplest match must
     51  * go all the way out to the end, and *? has no choice but to do it.
     52  */
     53 status = inSection(1);
     54 pattern = /^.*?$/;
     55 string = 'Hello World';
     56 actualmatch = string.match(pattern);
     57 expectedmatch = Array(string);
     58 addThis();
     59 
     60 
     61 /*
     62  * Leave off the '$' condition - here we expect the empty string.
     63  * Unlike the above pattern, we don't have to match till the end of
     64  * the string, so the non-greedy operator *? doesn't try to...
     65  */
     66 status = inSection(2);
     67 pattern = /^.*?/;
     68 string = 'Hello World';
     69 actualmatch = string.match(pattern);
     70 expectedmatch = Array(cnEmptyString);
     71 addThis();
     72 
     73 
     74 /*
     75  * Try '$' combined with an 'or' operator.
     76  *
     77  * The operator *? will consume the string from left to right,
     78  * attempting to satisfy the condition (:|$). When it hits ':',
     79  * the match will stop because the operator *? is non-greedy.
     80  *
     81  * The submatch $1 = (:|$) will contain the ':'
     82  */
     83 status = inSection(3);
     84 pattern = /^.*?(:|$)/;
     85 string = 'Hello: World';
     86 actualmatch = string.match(pattern);
     87 expectedmatch = Array('Hello:', ':');
     88 addThis();
     89 
     90 
     91 /*
     92  * Again, '$' combined with an 'or' operator.
     93  *
     94  * The operator * will consume the string from left to right,
     95  * attempting to satisfy the condition (:|$). When it hits ':',
     96  * the match will not stop since * is greedy. The match will
     97  * continue until it hits $, the end-of-string boundary.
     98  *
     99  * The submatch $1 = (:|$) will contain the empty string
    100  * conceived to exist at the end-of-string boundary.
    101  */
    102 status = inSection(4);
    103 pattern = /^.*(:|$)/;
    104 string = 'Hello: World';
    105 actualmatch = string.match(pattern);
    106 expectedmatch = Array(string, cnEmptyString);
    107 addThis();
    108 
    109 
    110 
    111 
    112 //-----------------------------------------------------------------------------
    113 test();
    114 //-----------------------------------------------------------------------------
    115 
    116 
    117 
    118 function addThis()
    119 {
    120   statusmessages[i] = status;
    121   patterns[i] = pattern;
    122   strings[i] = string;
    123   actualmatches[i] = actualmatch;
    124   expectedmatches[i] = expectedmatch;
    125   i++;
    126 }
    127 
    128 
    129 function test()
    130 {
    131   enterFunc ('test');
    132   printBugNumber (bug);
    133   printStatus (summary);
    134   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
    135   exitFunc ('test');
    136 }
    137