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@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//----------------------------------------------------------------------------- 29var i = 0; 30var bug = 57572; 31var summary = 'Testing regular expressions containing "?"'; 32var cnEmptyString = ''; var cnSingleSpace = ' '; 33var status = ''; 34var statusmessages = new Array(); 35var pattern = ''; 36var patterns = new Array(); 37var string = ''; 38var strings = new Array(); 39var actualmatch = ''; 40var actualmatches = new Array(); 41var expectedmatch = ''; 42var expectedmatches = new Array(); 43 44 45status = inSection(1); 46pattern = /(\S+)?(.*)/; 47string = 'Test this'; 48actualmatch = string.match(pattern); 49expectedmatch = Array(string, 'Test', ' this'); //single space in front of 'this' 50addThis(); 51 52status = inSection(2); 53pattern = /(\S+)? ?(.*)/; //single space between the ? characters 54string= 'Test this'; 55actualmatch = string.match(pattern); 56expectedmatch = Array(string, 'Test', 'this'); //NO space in front of 'this' 57addThis(); 58 59status = inSection(3); 60pattern = /(\S+)?(.*)/; 61string = 'Stupid phrase, with six - (short) words'; 62actualmatch = string.match(pattern); 63expectedmatch = Array(string, 'Stupid', ' phrase, with six - (short) words'); //single space in front of 'phrase' 64addThis(); 65 66status = inSection(4); 67pattern = /(\S+)? ?(.*)/; //single space between the ? characters 68string = 'Stupid phrase, with six - (short) words'; 69actualmatch = string.match(pattern); 70expectedmatch = Array(string, 'Stupid', 'phrase, with six - (short) words'); //NO space in front of 'phrase' 71addThis(); 72 73 74// let's add an extra back-reference this time - three instead of two - 75status = inSection(5); 76pattern = /(\S+)?( ?)(.*)/; //single space before second ? character 77string = 'Stupid phrase, with six - (short) words'; 78actualmatch = string.match(pattern); 79expectedmatch = Array(string, 'Stupid', cnSingleSpace, 'phrase, with six - (short) words'); 80addThis(); 81 82status = inSection(6); 83pattern = /^(\S+)?( ?)(B+)$/; //single space before second ? character 84string = 'AAABBB'; 85actualmatch = string.match(pattern); 86expectedmatch = Array(string, 'AAABB', cnEmptyString, 'B'); 87addThis(); 88 89status = inSection(7); 90pattern = /(\S+)?(!?)(.*)/; 91string = 'WOW !!! !!!'; 92actualmatch = string.match(pattern); 93expectedmatch = Array(string, 'WOW', cnEmptyString, ' !!! !!!'); 94addThis(); 95 96status = inSection(8); 97pattern = /(.+)?(!?)(!+)/; 98string = 'WOW !!! !!!'; 99actualmatch = string.match(pattern); 100expectedmatch = Array(string, 'WOW !!! !!', cnEmptyString, '!'); 101addThis(); 102 103 104 105//----------------------------------------------------------------------------- 106test(); 107//----------------------------------------------------------------------------- 108 109 110 111function 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 122function test() 123{ 124 enterFunc ('test'); 125 printBugNumber (bug); 126 printStatus (summary); 127 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); 128 exitFunc ('test'); 129} 130