1/* ***** BEGIN LICENSE BLOCK *****
2* Version: NPL 1.1/GPL 2.0/LGPL 2.1
3*
4* The contents of this file are subject to the Netscape Public License
5* Version 1.1 (the "License"); you may not use this file except in
6* compliance with the License. You may obtain a copy of the License at
7* http://www.mozilla.org/NPL/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is JavaScript Engine testing utilities.
15*
16* The Initial Developer of the Original Code is Netscape Communications Corp.
17* Portions created by the Initial Developer are Copyright (C) 2002
18* the Initial Developer. All Rights Reserved.
19*
20* Contributor(s): pschwartau@netscape.com
21*
22* Alternatively, the contents of this file may be used under the terms of
23* either the GNU General Public License Version 2 or later (the "GPL"), or
24* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25* in which case the provisions of the GPL or the LGPL are applicable instead
26* of those above. If you wish to allow use of your version of this file only
27* under the terms of either the GPL or the LGPL, and not to allow others to
28* use your version of this file under the terms of the NPL, indicate your
29* decision by deleting the provisions above and replace them with the notice
30* and other provisions required by the GPL or the LGPL. If you do not delete
31* the provisions above, a recipient may use your version of this file under
32* the terms of any one of the NPL, the GPL or the LGPL.
33*
34* ***** END LICENSE BLOCK *****
35*
36*
37* Date:    31 July 2002
38* SUMMARY: Testing regexps containing octal escape sequences
39* This is an elaboration of mozilla/js/tests/ecma_2/RegExp/octal-003.js
40*
41* See http://bugzilla.mozilla.org/show_bug.cgi?id=141078
42* for a reference on octal escape sequences in regexps.
43*
44* NOTE:
45* We will use the identities '\011' === '\u0009' === '\x09' === '\t'
46*
47* The first is an octal escape sequence (\(0-3)OO; O an octal digit).
48* See ECMA-262 Edition 2, Section 7.7.4 "String Literals". These were
49* dropped in Edition 3 but we support them for backward compatibility.
50*
51* The second is a Unicode escape sequence (\uHHHH; H a hex digit).
52* Since octal 11 = hex 9, the two escapes define the same character.
53*
54* The third is a hex escape sequence (\xHH; H a hex digit).
55* Since hex 09 = hex 0009, this defines the same character.
56*
57* The fourth is the familiar escape sequence for a horizontal tab,
58* defined in the ECMA spec as having Unicode value \u0009.
59*/
60//-----------------------------------------------------------------------------
61var i = 0;
62var bug = 141078;
63var summary = 'Testing regexps containing octal escape sequences';
64var status = '';
65var statusmessages = new Array();
66var pattern = '';
67var patterns = new Array();
68var string = '';
69var strings = new Array();
70var actualmatch = '';
71var actualmatches = new Array();
72var expectedmatch = '';
73var expectedmatches = new Array();
74
75
76/*
77 * Test a string containing the null character '\0' followed by the string '11'
78 *
79 *               'a' + String.fromCharCode(0) + '11';
80 *
81 * Note we can't simply write 'a\011', because '\011' would be interpreted
82 * as the octal escape sequence for the tab character (see above).
83 *
84 * We should get no match from the regexp /.\011/, because it should be
85 * looking for the octal escape sequence \011, i.e. the tab character -
86 *
87 */
88status = inSection(1);
89pattern = /.\011/;
90string = 'a' + String.fromCharCode(0) + '11';
91actualmatch = string.match(pattern);
92expectedmatch = null;
93addThis();
94
95
96/*
97 * Try same thing with 'xx' in place of '11'.
98 *
99 * Should get a match now, because the octal escape sequence in the regexp
100 * has been reduced from \011 to \0, and '\0' is present in the string -
101 */
102status = inSection(2);
103pattern = /.\0xx/;
104string = 'a' + String.fromCharCode(0) + 'xx';
105actualmatch = string.match(pattern);
106expectedmatch = Array(string);
107addThis();
108
109
110/*
111 * Same thing; don't use |String.fromCharCode(0)| this time.
112 * There is no ambiguity in '\0xx': it is the null character
113 * followed by two x's, no other interpretation is possible.
114 */
115status = inSection(3);
116pattern = /.\0xx/;
117string = 'a\0xx';
118actualmatch = string.match(pattern);
119expectedmatch = Array(string);
120addThis();
121
122
123/*
124 * This one should produce a match. The two-character string
125 * 'a' + '\011' is duplicated in the pattern and test string:
126 */
127status = inSection(4);
128pattern = /.\011/;
129string = 'a\011';
130actualmatch = string.match(pattern);
131expectedmatch = Array(string);
132addThis();
133
134
135/*
136 * Same as above, only now, for the second character of the string,
137 * use the Unicode escape '\u0009' instead of the octal escape '\011'
138 */
139status = inSection(5);
140pattern = /.\011/;
141string = 'a\u0009';
142actualmatch = string.match(pattern);
143expectedmatch = Array(string);
144addThis();
145
146
147/*
148 * Same as above, only now  for the second character of the string,
149 * use the hex escape '\x09' instead of the octal escape '\011'
150 */
151status = inSection(6);
152pattern = /.\011/;
153string = 'a\x09';
154actualmatch = string.match(pattern);
155expectedmatch = Array(string);
156addThis();
157
158
159/*
160 * Same as above, only now  for the second character of the string,
161 * use the escape '\t' instead of the octal escape '\011'
162 */
163status = inSection(7);
164pattern = /.\011/;
165string = 'a\t';
166actualmatch = string.match(pattern);
167expectedmatch = Array(string);
168addThis();
169
170
171/*
172 * Return to the string from Section 1.
173 *
174 * Unlike Section 1, use the RegExp() function to create the
175 * regexp pattern: null character followed by the string '11'.
176 *
177 * Since this is exactly what the string is, we should get a match -
178 */
179status = inSection(8);
180string = 'a' + String.fromCharCode(0) + '11';
181pattern = RegExp(string);
182actualmatch = string.match(pattern);
183expectedmatch = Array(string);
184addThis();
185
186
187
188
189//-------------------------------------------------------------------------------------------------
190test();
191//-------------------------------------------------------------------------------------------------
192
193
194
195function addThis()
196{
197  statusmessages[i] = status;
198  patterns[i] = pattern;
199  strings[i] = string;
200  actualmatches[i] = actualmatch;
201  expectedmatches[i] = expectedmatch;
202  i++;
203}
204
205
206function test()
207{
208  enterFunc ('test');
209  printBugNumber (bug);
210  printStatus (summary);
211  testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
212  exitFunc ('test');
213}
214