michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 31 July 2002 michael@0: * SUMMARY: Testing regexps containing octal escape sequences michael@0: * This is an elaboration of mozilla/js/tests/ecma_2/RegExp/octal-003.js michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=141078 michael@0: * for a reference on octal escape sequences in regexps. michael@0: * michael@0: * NOTE: michael@0: * We will use the identities '\011' === '\u0009' === '\x09' === '\t' michael@0: * michael@0: * The first is an octal escape sequence (\(0-3)OO; O an octal digit). michael@0: * See ECMA-262 Edition 2, Section 7.7.4 "String Literals". These were michael@0: * dropped in Edition 3 but we support them for backward compatibility. michael@0: * michael@0: * The second is a Unicode escape sequence (\uHHHH; H a hex digit). michael@0: * Since octal 11 = hex 9, the two escapes define the same character. michael@0: * michael@0: * The third is a hex escape sequence (\xHH; H a hex digit). michael@0: * Since hex 09 = hex 0009, this defines the same character. michael@0: * michael@0: * The fourth is the familiar escape sequence for a horizontal tab, michael@0: * defined in the ECMA spec as having Unicode value \u0009. michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var i = 0; michael@0: var BUGNUMBER = 141078; michael@0: var summary = 'Testing regexps containing octal escape sequences'; michael@0: var status = ''; michael@0: var statusmessages = new Array(); michael@0: var pattern = ''; michael@0: var patterns = new Array(); michael@0: var string = ''; michael@0: var strings = new Array(); michael@0: var actualmatch = ''; michael@0: var actualmatches = new Array(); michael@0: var expectedmatch = ''; michael@0: var expectedmatches = new Array(); michael@0: michael@0: michael@0: /* michael@0: * Test a string containing the null character '\0' followed by the string '11' michael@0: * michael@0: * 'a' + String.fromCharCode(0) + '11'; michael@0: * michael@0: * Note we can't simply write 'a\011', because '\011' would be interpreted michael@0: * as the octal escape sequence for the tab character (see above). michael@0: * michael@0: * We should get no match from the regexp /.\011/, because it should be michael@0: * looking for the octal escape sequence \011, i.e. the tab character - michael@0: * michael@0: */ michael@0: status = inSection(1); michael@0: pattern = /.\011/; michael@0: string = 'a' + String.fromCharCode(0) + '11'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Try same thing with 'xx' in place of '11'. michael@0: * michael@0: * Should get a match now, because the octal escape sequence in the regexp michael@0: * has been reduced from \011 to \0, and '\0' is present in the string - michael@0: */ michael@0: status = inSection(2); michael@0: pattern = /.\0xx/; michael@0: string = 'a' + String.fromCharCode(0) + 'xx'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Same thing; don't use |String.fromCharCode(0)| this time. michael@0: * There is no ambiguity in '\0xx': it is the null character michael@0: * followed by two x's, no other interpretation is possible. michael@0: */ michael@0: status = inSection(3); michael@0: pattern = /.\0xx/; michael@0: string = 'a\0xx'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * This one should produce a match. The two-character string michael@0: * 'a' + '\011' is duplicated in the pattern and test string: michael@0: */ michael@0: status = inSection(4); michael@0: pattern = /.\011/; michael@0: string = 'a\011'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Same as above, only now, for the second character of the string, michael@0: * use the Unicode escape '\u0009' instead of the octal escape '\011' michael@0: */ michael@0: status = inSection(5); michael@0: pattern = /.\011/; michael@0: string = 'a\u0009'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Same as above, only now for the second character of the string, michael@0: * use the hex escape '\x09' instead of the octal escape '\011' michael@0: */ michael@0: status = inSection(6); michael@0: pattern = /.\011/; michael@0: string = 'a\x09'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Same as above, only now for the second character of the string, michael@0: * use the escape '\t' instead of the octal escape '\011' michael@0: */ michael@0: status = inSection(7); michael@0: pattern = /.\011/; michael@0: string = 'a\t'; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Return to the string from Section 1. michael@0: * michael@0: * Unlike Section 1, use the RegExp() function to create the michael@0: * regexp pattern: null character followed by the string '11'. michael@0: * michael@0: * Since this is exactly what the string is, we should get a match - michael@0: */ michael@0: status = inSection(8); michael@0: string = 'a' + String.fromCharCode(0) + '11'; michael@0: pattern = RegExp(string); michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = Array(string); michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------------------------- michael@0: test(); michael@0: //------------------------------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusmessages[i] = status; michael@0: patterns[i] = pattern; michael@0: strings[i] = string; michael@0: actualmatches[i] = actualmatch; michael@0: expectedmatches[i] = expectedmatch; michael@0: i++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); michael@0: exitFunc ('test'); michael@0: }