| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
* Date: 22 June 2001 |
| 8 |
* |
| 9 |
* SUMMARY: Regression test for Bugzilla bug 87231: |
| 10 |
* "Regular expression /(A)?(A.*)/ picks 'A' twice" |
| 11 |
* |
| 12 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=87231 |
| 13 |
* Key case: |
| 14 |
* |
| 15 |
* pattern = /^(A)?(A.*)$/; |
| 16 |
* string = 'A'; |
| 17 |
* expectedmatch = Array('A', '', 'A'); |
| 18 |
* |
| 19 |
* |
| 20 |
* We expect the 1st subexpression (A)? NOT to consume the single 'A'. |
| 21 |
* Recall that "?" means "match 0 or 1 times". Here, it should NOT do |
| 22 |
* greedy matching: it should match 0 times instead of 1. This allows |
| 23 |
* the 2nd subexpression to make the only match it can: the single 'A'. |
| 24 |
* Such "altruism" is the only way there can be a successful global match... |
| 25 |
*/ |
| 26 |
//----------------------------------------------------------------------------- |
| 27 |
var i = 0; |
| 28 |
var BUGNUMBER = 87231; |
| 29 |
var cnEmptyString = ''; |
| 30 |
var summary = 'Testing regular expression /(A)?(A.*)/'; |
| 31 |
var status = ''; |
| 32 |
var statusmessages = new Array(); |
| 33 |
var pattern = ''; |
| 34 |
var patterns = new Array(); |
| 35 |
var string = ''; |
| 36 |
var strings = new Array(); |
| 37 |
var actualmatch = ''; |
| 38 |
var actualmatches = new Array(); |
| 39 |
var expectedmatch = ''; |
| 40 |
var expectedmatches = new Array(); |
| 41 |
|
| 42 |
|
| 43 |
pattern = /^(A)?(A.*)$/; |
| 44 |
status = inSection(1); |
| 45 |
string = 'AAA'; |
| 46 |
actualmatch = string.match(pattern); |
| 47 |
expectedmatch = Array('AAA', 'A', 'AA'); |
| 48 |
addThis(); |
| 49 |
|
| 50 |
status = inSection(2); |
| 51 |
string = 'AA'; |
| 52 |
actualmatch = string.match(pattern); |
| 53 |
expectedmatch = Array('AA', 'A', 'A'); |
| 54 |
addThis(); |
| 55 |
|
| 56 |
status = inSection(3); |
| 57 |
string = 'A'; |
| 58 |
actualmatch = string.match(pattern); |
| 59 |
expectedmatch = Array('A', undefined, 'A'); // 'altruistic' case: see above |
| 60 |
addThis(); |
| 61 |
|
| 62 |
|
| 63 |
pattern = /(A)?(A.*)/; |
| 64 |
var strL = 'zxcasd;fl\\\ ^'; |
| 65 |
var strR = 'aaAAaaaf;lrlrzs'; |
| 66 |
|
| 67 |
status = inSection(4); |
| 68 |
string = strL + 'AAA' + strR; |
| 69 |
actualmatch = string.match(pattern); |
| 70 |
expectedmatch = Array('AAA' + strR, 'A', 'AA' + strR); |
| 71 |
addThis(); |
| 72 |
|
| 73 |
status = inSection(5); |
| 74 |
string = strL + 'AA' + strR; |
| 75 |
actualmatch = string.match(pattern); |
| 76 |
expectedmatch = Array('AA' + strR, 'A', 'A' + strR); |
| 77 |
addThis(); |
| 78 |
|
| 79 |
status = inSection(6); |
| 80 |
string = strL + 'A' + strR; |
| 81 |
actualmatch = string.match(pattern); |
| 82 |
expectedmatch = Array('A' + strR, undefined, 'A' + strR); // 'altruistic' case: see above |
| 83 |
addThis(); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
//------------------------------------------------------------------------------------------------- |
| 88 |
test(); |
| 89 |
//------------------------------------------------------------------------------------------------- |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
function addThis() |
| 94 |
{ |
| 95 |
statusmessages[i] = status; |
| 96 |
patterns[i] = pattern; |
| 97 |
strings[i] = string; |
| 98 |
actualmatches[i] = actualmatch; |
| 99 |
expectedmatches[i] = expectedmatch; |
| 100 |
i++; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function test() |
| 105 |
{ |
| 106 |
printBugNumber(BUGNUMBER); |
| 107 |
printStatus (summary); |
| 108 |
testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); |
| 109 |
} |