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): pschwartau (at) netscape.com
     20 * Date: 06 February 2001
     21 *
     22 * SUMMARY:  Arose from Bugzilla bug 67773:
     23 * "Regular subexpressions followed by + failing to run to completion"
     24 *
     25 * See http://bugzilla.mozilla.org/show_bug.cgi?id=67773
     26 * See http://bugzilla.mozilla.org/show_bug.cgi?id=69989
     27 */
     28 //-------------------------------------------------------------------------------------------------
     29 var i = 0;
     30 var bug = 67773;
     31 var summary = 'Testing regular subexpressions followed by ? or +\n';
     32 var cnSingleSpace = ' ';
     33 var status = '';
     34 var statusmessages = new Array();
     35 var pattern = '';
     36 var patterns = new Array();
     37 var string = '';
     38 var strings = new Array();
     39 var actualmatch = '';
     40 var actualmatches = new Array();
     41 var expectedmatch = '';
     42 var expectedmatches = new Array();
     43 
     44 
     45 pattern = /^(\S+)?( ?)(B+)$/;  //single space before second ? character
     46     status = inSection(1);
     47     string = 'AAABBB AAABBB ';  //single space at middle and at end -
     48     actualmatch = string.match(pattern);
     49     expectedmatch = null;
     50     addThis();
     51 
     52     status = inSection(2);
     53     string = 'AAABBB BBB';  //single space in the middle
     54     actualmatch = string.match(pattern);
     55     expectedmatch = Array(string,  'AAABBB', cnSingleSpace,  'BBB');
     56     addThis();
     57 
     58     status = inSection(3);
     59     string = 'AAABBB AAABBB';  //single space in the middle
     60     actualmatch = string.match(pattern);
     61     expectedmatch = null;
     62     addThis();
     63 
     64 
     65 pattern = /^(A+B)+$/;
     66     status = inSection(4);
     67     string = 'AABAAB';
     68     actualmatch = string.match(pattern);
     69     expectedmatch = Array(string,  'AAB');
     70     addThis();
     71 
     72     status = inSection(5);
     73     string = 'ABAABAAAAAAB';
     74     actualmatch = string.match(pattern);
     75     expectedmatch = Array(string,  'AAAAAAB');
     76     addThis();
     77 
     78     status = inSection(6);
     79     string = 'ABAABAABAB';
     80     actualmatch = string.match(pattern);
     81     expectedmatch = Array(string,  'AB');
     82     addThis();
     83 
     84     status = inSection(7);
     85     string = 'ABAABAABABB';
     86     actualmatch = string.match(pattern);
     87     expectedmatch = null;   // because string doesn't match at end
     88     addThis();
     89 
     90 
     91 pattern = /^(A+1)+$/;
     92     status = inSection(8);
     93     string = 'AA1AA1';
     94     actualmatch = string.match(pattern);
     95     expectedmatch = Array(string,  'AA1');
     96     addThis();
     97 
     98 
     99 pattern = /^(\w+\-)+$/;
    100     status = inSection(9);
    101     string = '';
    102     actualmatch = string.match(pattern);
    103     expectedmatch = null;
    104     addThis();
    105 
    106     status = inSection(10);
    107     string = 'bla-';
    108     actualmatch = string.match(pattern);
    109     expectedmatch = Array(string, string);
    110     addThis();
    111 
    112     status = inSection(11);
    113     string = 'bla-bla';  // hyphen missing at end -
    114     actualmatch = string.match(pattern);
    115     expectedmatch = null;  //because string doesn't match at end
    116     addThis();
    117 
    118     status = inSection(12);
    119     string = 'bla-bla-';
    120     actualmatch = string.match(pattern);
    121     expectedmatch = Array(string, 'bla-');
    122     addThis();
    123 
    124 
    125 pattern = /^(\S+)+(A+)$/;
    126     status = inSection(13);
    127     string = 'asdldflkjAAA';
    128     actualmatch = string.match(pattern);
    129     expectedmatch = Array(string, 'asdldflkjAA', 'A');
    130     addThis();
    131 
    132     status = inSection(14);
    133     string = 'asdldflkj AAA'; // space in middle
    134     actualmatch = string.match(pattern);
    135     expectedmatch = null;  //because of the space
    136     addThis();
    137 
    138 
    139 pattern = /^(\S+)+(\d+)$/;
    140     status = inSection(15);
    141     string = 'asdldflkj122211';
    142     actualmatch = string.match(pattern);
    143     expectedmatch = Array(string, 'asdldflkj12221', '1');
    144     addThis();
    145 
    146     status = inSection(16);
    147     string = 'asdldflkj1111111aaa1';
    148     actualmatch = string.match(pattern);
    149     expectedmatch = Array(string, 'asdldflkj1111111aaa', '1');
    150     addThis();
    151 
    152 
    153 /*
    154  * This one comes from Stephen Ostermiller.
    155  * See http://bugzilla.mozilla.org/show_bug.cgi?id=69989
    156  */
    157 pattern = /^[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)+$/;
    158     status = inSection(17);
    159     string = 'some.host.tld';
    160     actualmatch = string.match(pattern);
    161     expectedmatch = Array(string, '.tld', '.');
    162     addThis();
    163 
    164 
    165 
    166 //-------------------------------------------------------------------------------------------------
    167 test();
    168 //-------------------------------------------------------------------------------------------------
    169 
    170 
    171 
    172 function addThis()
    173 {
    174   statusmessages[i] = status;
    175   patterns[i] = pattern;
    176   strings[i] = string;
    177   actualmatches[i] = actualmatch;
    178   expectedmatches[i] = expectedmatch;
    179   i++;
    180 }
    181 
    182 
    183 function test()
    184 {
    185   enterFunc ('test');
    186   printBugNumber (bug);
    187   printStatus (summary);
    188   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
    189   exitFunc ('test');
    190 }
    191