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) 2003
     18 * the Initial Developer. All Rights Reserved.
     19 *
     20 * Contributor(s): scole (at) planetweb.com, 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:    21 January 2003
     38 * SUMMARY: Invalid use of regexp quantifiers should generate SyntaxErrors
     39 *
     40 * See http://bugzilla.mozilla.org/show_bug.cgi?id=188206
     41 * and http://bugzilla.mozilla.org/show_bug.cgi?id=85721#c48 etc.
     42 * and http://bugzilla.mozilla.org/show_bug.cgi?id=190685
     43 * and http://bugzilla.mozilla.org/show_bug.cgi?id=197451
     44 */
     45 //-----------------------------------------------------------------------------
     46 var UBound = 0;
     47 var bug = 188206;
     48 var summary = 'Invalid use of regexp quantifiers should generate SyntaxErrors';
     49 var TEST_PASSED = 'SyntaxError';
     50 var TEST_FAILED = 'Generated an error, but NOT a SyntaxError!';
     51 var TEST_FAILED_BADLY = 'Did not generate ANY error!!!';
     52 var CHECK_PASSED = 'Should not generate an error';
     53 var CHECK_FAILED = 'Generated an error!';
     54 var status = '';
     55 var statusitems = [];
     56 var actual = '';
     57 var actualvalues = [];
     58 var expect= '';
     59 var expectedvalues = [];
     60 
     61 
     62 /*
     63  * All the following are invalid uses of regexp quantifiers and
     64  * should generate SyntaxErrors. That's what we're testing for.
     65  *
     66  * To allow the test to compile and run, we have to hide the errors
     67  * inside eval strings, and check they are caught at run-time -
     68  *
     69  */
     70 status = inSection(1);
     71 testThis(' /a**/ ');
     72 
     73 status = inSection(2);
     74 testThis(' /a***/ ');
     75 
     76 status = inSection(3);
     77 testThis(' /a++/ ');
     78 
     79 status = inSection(4);
     80 testThis(' /a+++/ ');
     81 
     82 /*
     83  * The ? quantifier, unlike * or +, may appear twice in succession.
     84  * Thus we need at least three in a row to provoke a SyntaxError -
     85  */
     86 
     87 status = inSection(5);
     88 testThis(' /a???/ ');
     89 
     90 status = inSection(6);
     91 testThis(' /a????/ ');
     92 
     93 
     94 /*
     95  * Now do some weird things on the left side of the regexps -
     96  */
     97 status = inSection(7);
     98 testThis(' /*a/ ');
     99 
    100 status = inSection(8);
    101 testThis(' /**a/ ');
    102 
    103 status = inSection(9);
    104 testThis(' /+a/ ');
    105 
    106 status = inSection(10);
    107 testThis(' /++a/ ');
    108 
    109 status = inSection(11);
    110 testThis(' /?a/ ');
    111 
    112 status = inSection(12);
    113 testThis(' /??a/ ');
    114 
    115 
    116 /*
    117  * Misusing the {DecmalDigits} quantifier - according to ECMA,
    118  * but not according to Perl.
    119  *
    120  * ECMA-262 Edition 3 prohibits the use of unescaped braces in
    121  * regexp patterns, unless they form part of a quantifier.
    122  *
    123  * Hovever, Perl does not prohibit this. If not used as part
    124  * of a quantifer, Perl treats braces literally.
    125  *
    126  * We decided to follow Perl on this for backward compatibility.
    127  * See http://bugzilla.mozilla.org/show_bug.cgi?id=190685.
    128  *
    129  * Therefore NONE of the following ECMA violations should generate
    130  * a SyntaxError. Note we use checkThis() instead of testThis().
    131  */
    132 status = inSection(13);
    133 checkThis(' /a*{/ ');
    134 
    135 status = inSection(14);
    136 checkThis(' /a{}/ ');
    137 
    138 status = inSection(15);
    139 checkThis(' /{a/ ');
    140 
    141 status = inSection(16);
    142 checkThis(' /}a/ ');
    143 
    144 status = inSection(17);
    145 checkThis(' /x{abc}/ ');
    146 
    147 status = inSection(18);
    148 checkThis(' /{{0}/ ');
    149 
    150 status = inSection(19);
    151 checkThis(' /{{1}/ ');
    152 
    153 status = inSection(20);
    154 checkThis(' /x{{0}/ ');
    155 
    156 status = inSection(21);
    157 checkThis(' /x{{1}/ ');
    158 
    159 status = inSection(22);
    160 checkThis(' /x{{0}}/ ');
    161 
    162 status = inSection(23);
    163 checkThis(' /x{{1}}/ ');
    164 
    165 status = inSection(24);
    166 checkThis(' /x{{0}}/ ');
    167 
    168 status = inSection(25);
    169 checkThis(' /x{{1}}/ ');
    170 
    171 status = inSection(26);
    172 checkThis(' /x{{0}}/ ');
    173 
    174 status = inSection(27);
    175 checkThis(' /x{{1}}/ ');
    176 
    177 
    178 /*
    179  * Misusing the {DecmalDigits} quantifier - according to BOTH ECMA and Perl.
    180  *
    181  * Just as with the * and + quantifiers above, can't have two {DecmalDigits}
    182  * quantifiers in succession - it's a SyntaxError.
    183  */
    184 status = inSection(28);
    185 testThis(' /x{1}{1}/ ');
    186 
    187 status = inSection(29);
    188 testThis(' /x{1,}{1}/ ');
    189 
    190 status = inSection(30);
    191 testThis(' /x{1,2}{1}/ ');
    192 
    193 status = inSection(31);
    194 testThis(' /x{1}{1,}/ ');
    195 
    196 status = inSection(32);
    197 testThis(' /x{1,}{1,}/ ');
    198 
    199 status = inSection(33);
    200 testThis(' /x{1,2}{1,}/ ');
    201 
    202 status = inSection(34);
    203 testThis(' /x{1}{1,2}/ ');
    204 
    205 status = inSection(35);
    206 testThis(' /x{1,}{1,2}/ ');
    207 
    208 status = inSection(36);
    209 testThis(' /x{1,2}{1,2}/ ');
    210 
    211 
    212 
    213 //-----------------------------------------------------------------------------
    214 test();
    215 //-----------------------------------------------------------------------------
    216 
    217 
    218 
    219 /*
    220  * Invalid syntax should generate a SyntaxError
    221  */
    222 function testThis(sInvalidSyntax)
    223 {
    224   expect = TEST_PASSED;
    225   actual = TEST_FAILED_BADLY;
    226 
    227   try
    228   {
    229     eval(sInvalidSyntax);
    230   }
    231   catch(e)
    232   {
    233     if (e instanceof SyntaxError)
    234       actual = TEST_PASSED;
    235     else
    236       actual = TEST_FAILED;
    237   }
    238 
    239   statusitems[UBound] = status;
    240   expectedvalues[UBound] = expect;
    241   actualvalues[UBound] = actual;
    242   UBound++;
    243 }
    244 
    245 
    246 /*
    247  * Allowed syntax shouldn't generate any errors
    248  */
    249 function checkThis(sAllowedSyntax)
    250 {
    251   expect = CHECK_PASSED;
    252   actual = CHECK_PASSED;
    253 
    254   try
    255   {
    256     eval(sAllowedSyntax);
    257   }
    258   catch(e)
    259   {
    260     actual = CHECK_FAILED;
    261   }
    262 
    263   statusitems[UBound] = status;
    264   expectedvalues[UBound] = expect;
    265   actualvalues[UBound] = actual;
    266   UBound++;
    267 }
    268 
    269 
    270 function test()
    271 {
    272   enterFunc('test');
    273   printBugNumber(bug);
    274   printStatus(summary);
    275 
    276   for (var i=0; i<UBound; i++)
    277   {
    278     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
    279   }
    280 
    281   exitFunc ('test');
    282 }
    283