Home | History | Annotate | Download | only in RegExp
      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 (at) 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 //-----------------------------------------------------------------------------
     61 var i = 0;
     62 var bug = 141078;
     63 var summary = 'Testing regexps containing octal escape sequences';
     64 var status = '';
     65 var statusmessages = new Array();
     66 var pattern = '';
     67 var patterns = new Array();
     68 var string = '';
     69 var strings = new Array();
     70 var actualmatch = '';
     71 var actualmatches = new Array();
     72 var expectedmatch = '';
     73 var 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  */
     88 status = inSection(1);
     89 pattern = /.\011/;
     90 string = 'a' + String.fromCharCode(0) + '11';
     91 actualmatch = string.match(pattern);
     92 expectedmatch = null;
     93 addThis();
     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  */
    102 status = inSection(2);
    103 pattern = /.\0xx/;
    104 string = 'a' + String.fromCharCode(0) + 'xx';
    105 actualmatch = string.match(pattern);
    106 expectedmatch = Array(string);
    107 addThis();
    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  */
    115 status = inSection(3);
    116 pattern = /.\0xx/;
    117 string = 'a\0xx';
    118 actualmatch = string.match(pattern);
    119 expectedmatch = Array(string);
    120 addThis();
    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  */
    127 status = inSection(4);
    128 pattern = /.\011/;
    129 string = 'a\011';
    130 actualmatch = string.match(pattern);
    131 expectedmatch = Array(string);
    132 addThis();
    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  */
    139 status = inSection(5);
    140 pattern = /.\011/;
    141 string = 'a\u0009';
    142 actualmatch = string.match(pattern);
    143 expectedmatch = Array(string);
    144 addThis();
    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  */
    151 status = inSection(6);
    152 pattern = /.\011/;
    153 string = 'a\x09';
    154 actualmatch = string.match(pattern);
    155 expectedmatch = Array(string);
    156 addThis();
    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  */
    163 status = inSection(7);
    164 pattern = /.\011/;
    165 string = 'a\t';
    166 actualmatch = string.match(pattern);
    167 expectedmatch = Array(string);
    168 addThis();
    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  */
    179 status = inSection(8);
    180 string = 'a' + String.fromCharCode(0) + '11';
    181 pattern = RegExp(string);
    182 actualmatch = string.match(pattern);
    183 expectedmatch = Array(string);
    184 addThis();
    185 
    186 
    187 
    188 
    189 //-------------------------------------------------------------------------------------------------
    190 test();
    191 //-------------------------------------------------------------------------------------------------
    192 
    193 
    194 
    195 function 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 
    206 function test()
    207 {
    208   enterFunc ('test');
    209   printBugNumber (bug);
    210   printStatus (summary);
    211   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
    212   exitFunc ('test');
    213 }
    214