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: 28 December 2000
     21 *
     22 * SUMMARY: Testing regular expressions containing the ? character.
     23 * Arose from Bugzilla bug 57572: "RegExp with ? matches incorrectly"
     24 *
     25 * See http://bugzilla.mozilla.org/show_bug.cgi?id=57572
     26 *
     27 */
     28 //-----------------------------------------------------------------------------
     29 var i = 0;
     30 var bug = 57572;
     31 var summary = 'Testing regular expressions containing "?"';
     32 var cnEmptyString = ''; 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 status = inSection(1);
     46 pattern = /(\S+)?(.*)/;
     47 string = 'Test this';
     48 actualmatch = string.match(pattern);
     49 expectedmatch = Array(string, 'Test', ' this');  //single space in front of 'this'
     50 addThis();
     51 
     52 status = inSection(2);
     53 pattern = /(\S+)? ?(.*)/;  //single space between the ? characters
     54 string= 'Test this';
     55 actualmatch = string.match(pattern);
     56 expectedmatch = Array(string, 'Test', 'this');  //NO space in front of 'this'
     57 addThis();
     58 
     59 status = inSection(3);
     60 pattern = /(\S+)?(.*)/;
     61 string = 'Stupid phrase, with six - (short) words';
     62 actualmatch = string.match(pattern);
     63 expectedmatch = Array(string, 'Stupid', ' phrase, with six - (short) words');  //single space in front of 'phrase'
     64 addThis();
     65 
     66 status = inSection(4);
     67 pattern = /(\S+)? ?(.*)/;  //single space between the ? characters
     68 string = 'Stupid phrase, with six - (short) words';
     69 actualmatch = string.match(pattern);
     70 expectedmatch = Array(string, 'Stupid', 'phrase, with six - (short) words');  //NO space in front of 'phrase'
     71 addThis();
     72 
     73 
     74 // let's add an extra back-reference this time - three instead of two -
     75 status = inSection(5);
     76 pattern = /(\S+)?( ?)(.*)/;  //single space before second ? character
     77 string = 'Stupid phrase, with six - (short) words';
     78 actualmatch = string.match(pattern);
     79 expectedmatch = Array(string, 'Stupid', cnSingleSpace, 'phrase, with six - (short) words');
     80 addThis();
     81 
     82 status = inSection(6);
     83 pattern = /^(\S+)?( ?)(B+)$/;  //single space before second ? character
     84 string = 'AAABBB';
     85 actualmatch = string.match(pattern);
     86 expectedmatch = Array(string, 'AAABB', cnEmptyString, 'B');
     87 addThis();
     88 
     89 status = inSection(7);
     90 pattern = /(\S+)?(!?)(.*)/;
     91 string = 'WOW !!! !!!';
     92 actualmatch = string.match(pattern);
     93 expectedmatch = Array(string, 'WOW', cnEmptyString, ' !!! !!!');
     94 addThis();
     95 
     96 status = inSection(8);
     97 pattern = /(.+)?(!?)(!+)/;
     98 string = 'WOW !!! !!!';
     99 actualmatch = string.match(pattern);
    100 expectedmatch = Array(string, 'WOW !!! !!', cnEmptyString, '!');
    101 addThis();
    102 
    103 
    104 
    105 //-----------------------------------------------------------------------------
    106 test();
    107 //-----------------------------------------------------------------------------
    108 
    109 
    110 
    111 function addThis()
    112 {
    113   statusmessages[i] = status;
    114   patterns[i] = pattern;
    115   strings[i] = string;
    116   actualmatches[i] = actualmatch;
    117   expectedmatches[i] = expectedmatch;
    118   i++;
    119 }
    120 
    121 
    122 function test()
    123 {
    124   enterFunc ('test');
    125   printBugNumber (bug);
    126   printStatus (summary);
    127   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
    128   exitFunc ('test');
    129 }
    130