Debian Codegraph @

rhino/testsrc/tests/ecma_3/RegExp/octal-002.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 *
8 * Date: 31 July 2002
9 * SUMMARY: Testing regexps containing octal escape sequences
10 * This is an elaboration of mozilla/js/tests/ecma_2/RegExp/octal-003.js
11 *
12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=141078
13 * for a reference on octal escape sequences in regexps.
14 *
15 * NOTE:
16 * We will use the identities '\011' === '\u0009' === '\x09' === '\t'
17 *
18 * The first is an octal escape sequence (\(0-3)OO; O an octal digit).
19 * See ECMA-262 Edition 2, Section 7.7.4 "String Literals". These were
20 * dropped in Edition 3 but we support them for backward compatibility.
21 *
22 * The second is a Unicode escape sequence (\uHHHH; H a hex digit).
23 * Since octal 11 = hex 9, the two escapes define the same character.
24 *
25 * The third is a hex escape sequence (\xHH; H a hex digit).
26 * Since hex 09 = hex 0009, this defines the same character.
27 *
28 * The fourth is the familiar escape sequence for a horizontal tab,
29 * defined in the ECMA spec as having Unicode value \u0009.
30 */
31 //-----------------------------------------------------------------------------
32 var gTestfile = 'octal-002.js';
33 var i = 0;
34 var BUGNUMBER = 141078;
35 var summary = 'Testing regexps containing octal escape sequences';
36 var status = '';
37 var statusmessages = new Array();
38 var pattern = '';
39 var patterns = new Array();
40 var string = '';
41 var strings = new Array();
42 var actualmatch = '';
43 var actualmatches = new Array();
44 var expectedmatch = '';
45 var expectedmatches = new Array();
46
47
48 /*
49 * Test a string containing the null character '\0' followed by the string '11'
50 *
51 * 'a' + String.fromCharCode(0) + '11';
52 *
53 * Note we can't simply write 'a\011', because '\011' would be interpreted
54 * as the octal escape sequence for the tab character (see above).
55 *
56 * We should get no match from the regexp /.\011/, because it should be
57 * looking for the octal escape sequence \011, i.e. the tab character -
58 *
59 */
60 status = inSection(1);
61 pattern = /.\011/;
62 string = 'a' + String.fromCharCode(0) + '11';
63 actualmatch = string.match(pattern);
64 expectedmatch = null;
65 addThis();
66
67
68 /*
69 * Try same thing with 'xx' in place of '11'.
70 *
71 * Should get a match now, because the octal escape sequence in the regexp
72 * has been reduced from \011 to \0, and '\0' is present in the string -
73 */
74 status = inSection(2);
75 pattern = /.\0xx/;
76 string = 'a' + String.fromCharCode(0) + 'xx';
77 actualmatch = string.match(pattern);
78 expectedmatch = Array(string);
79 addThis();
80
81
82 /*
83 * Same thing; don't use |String.fromCharCode(0)| this time.
84 * There is no ambiguity in '\0xx': it is the null character
85 * followed by two x's, no other interpretation is possible.
86 */
87 status = inSection(3);
88 pattern = /.\0xx/;
89 string = 'a\0xx';
90 actualmatch = string.match(pattern);
91 expectedmatch = Array(string);
92 addThis();
93
94
95 /*
96 * This one should produce a match. The two-character string
97 * 'a' + '\011' is duplicated in the pattern and test string:
98 */
99 status = inSection(4);
100 pattern = /.\011/;
101 string = 'a\011';
102 actualmatch = string.match(pattern);
103 expectedmatch = Array(string);
104 addThis();
105
106
107 /*
108 * Same as above, only now, for the second character of the string,
109 * use the Unicode escape '\u0009' instead of the octal escape '\011'
110 */
111 status = inSection(5);
112 pattern = /.\011/;
113 string = 'a\u0009';
114 actualmatch = string.match(pattern);
115 expectedmatch = Array(string);
116 addThis();
117
118
119 /*
120 * Same as above, only now for the second character of the string,
121 * use the hex escape '\x09' instead of the octal escape '\011'
122 */
123 status = inSection(6);
124 pattern = /.\011/;
125 string = 'a\x09';
126 actualmatch = string.match(pattern);
127 expectedmatch = Array(string);
128 addThis();
129
130
131 /*
132 * Same as above, only now for the second character of the string,
133 * use the escape '\t' instead of the octal escape '\011'
134 */
135 status = inSection(7);
136 pattern = /.\011/;
137 string = 'a\t';
138 actualmatch = string.match(pattern);
139 expectedmatch = Array(string);
140 addThis();
141
142
143 /*
144 * Return to the string from Section 1.
145 *
146 * Unlike Section 1, use the RegExp() function to create the
147 * regexp pattern: null character followed by the string '11'.
148 *
149 * Since this is exactly what the string is, we should get a match -
150 */
151 status = inSection(8);
152 string = 'a' + String.fromCharCode(0) + '11';
153 pattern = RegExp(string);
154 actualmatch = string.match(pattern);
155 expectedmatch = Array(string);
156 addThis();
157
158
159
160
161 //-------------------------------------------------------------------------------------------------
162 test();
163 //-------------------------------------------------------------------------------------------------
164
165
166
167 function addThis()
168 {
169 statusmessages[i] = status;
170 patterns[i] = pattern;
171 strings[i] = string;
172 actualmatches[i] = actualmatch;
173 expectedmatches[i] = expectedmatch;
174 i++;
175 }
176
177
178 function test()
179 {
180 enterFunc ('test');
181 printBugNumber(BUGNUMBER);
182 printStatus (summary);
183 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
184 exitFunc ('test');
185 }