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: 22 June 2001
21*
22* SUMMARY:  Regression test for Bugzilla bug 87231:
23* "Regular expression /(A)?(A.*)/ picks 'A' twice"
24*
25* See http://bugzilla.mozilla.org/show_bug.cgi?id=87231
26* Key case:
27*
28*            pattern = /^(A)?(A.*)$/;
29*            string = 'A';
30*            expectedmatch = Array('A', '', 'A');
31*
32*
33* We expect the 1st subexpression (A)? NOT to consume the single 'A'.
34* Recall that "?" means "match 0 or 1 times". Here, it should NOT do
35* greedy matching: it should match 0 times instead of 1. This allows
36* the 2nd subexpression to make the only match it can: the single 'A'.
37* Such "altruism" is the only way there can be a successful global match...
38*/
39//-------------------------------------------------------------------------------------------------
40var i = 0;
41var bug = 87231;
42var cnEmptyString = '';
43var summary = 'Testing regular expression /(A)?(A.*)/';
44var status = '';
45var statusmessages = new Array();
46var pattern = '';
47var patterns = new Array();
48var string = '';
49var strings = new Array();
50var actualmatch = '';
51var actualmatches = new Array();
52var expectedmatch = '';
53var expectedmatches = new Array();
54
55
56pattern = /^(A)?(A.*)$/;
57    status = inSection(1);
58    string = 'AAA';
59    actualmatch = string.match(pattern);
60    expectedmatch = Array('AAA', 'A', 'AA');
61    addThis();
62
63    status = inSection(2);
64    string = 'AA';
65    actualmatch = string.match(pattern);
66    expectedmatch = Array('AA', 'A', 'A');
67    addThis();
68
69    status = inSection(3);
70    string = 'A';
71    actualmatch = string.match(pattern);
72    expectedmatch = Array('A', undefined, 'A'); // 'altruistic' case: see above
73    addThis();
74
75
76pattern = /(A)?(A.*)/;
77var strL = 'zxcasd;fl\\\  ^';
78var strR = 'aaAAaaaf;lrlrzs';
79
80    status = inSection(4);
81    string =  strL + 'AAA' + strR;
82    actualmatch = string.match(pattern);
83    expectedmatch = Array('AAA' + strR, 'A', 'AA' + strR);
84    addThis();
85
86    status = inSection(5);
87    string =  strL + 'AA' + strR;
88    actualmatch = string.match(pattern);
89    expectedmatch = Array('AA' + strR, 'A', 'A' + strR);
90    addThis();
91
92    status = inSection(6);
93    string =  strL + 'A' + strR;
94    actualmatch = string.match(pattern);
95    expectedmatch = Array('A' + strR, undefined, 'A' + strR); // 'altruistic' case: see above
96    addThis();
97
98
99
100//-------------------------------------------------------------------------------------------------
101test();
102//-------------------------------------------------------------------------------------------------
103
104
105
106function addThis()
107{
108  statusmessages[i] = status;
109  patterns[i] = pattern;
110  strings[i] = string;
111  actualmatches[i] = actualmatch;
112  expectedmatches[i] = expectedmatch;
113  i++;
114}
115
116
117function test()
118{
119  enterFunc ('test');
120  printBugNumber (bug);
121  printStatus (summary);
122  testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
123  exitFunc ('test');
124}
125