Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Test that we throw exceptions when calling test and exec with no
     29 // input.  This is not part of the spec, but we do it for
     30 // compatibility with JSC.
     31 assertThrows("/a/.test()");
     32 assertThrows("/a/.exec()");
     33 
     34 // Test that we do not throw exceptions once the static RegExp.input
     35 // field has been set.
     36 RegExp.input = "a";
     37 assertDoesNotThrow("/a/.test()");
     38 assertDoesNotThrow("/a/.exec()");
     39 
     40 // Test the (deprecated as of JS 1.5) properties of the RegExp function.
     41 var re = /((\d+)\.(\d+))/;
     42 var s = 'abc123.456def';
     43 
     44 re.exec(s);
     45 
     46 assertEquals(s, RegExp.input);
     47 assertEquals('123.456', RegExp.lastMatch);
     48 assertEquals('456', RegExp.lastParen);
     49 assertEquals('abc', RegExp.leftContext);
     50 assertEquals('def', RegExp.rightContext);
     51 
     52 assertEquals(s, RegExp['$_']);
     53 assertEquals('123.456', RegExp['$&']);
     54 assertEquals('456', RegExp['$+']);
     55 assertEquals('abc', RegExp['$`']);
     56 assertEquals('def', RegExp["$'"]);
     57 
     58 assertEquals('123.456', RegExp['$1']);
     59 assertEquals('123', RegExp['$2']);
     60 assertEquals('456', RegExp['$3']);
     61 for (var i = 4; i < 10; ++i) {
     62   assertEquals('', RegExp['$' + i]);
     63 }
     64 
     65 // They should be read only.
     66 RegExp['$1'] = 'fisk';
     67 assertEquals('123.456', RegExp['$1']);
     68 
     69 // String.prototype.match and String.prototype.replace (when given a
     70 // regexp) and also RegExp.prototype.test should all behave as if
     71 // RegExp.prototype.exec were called.
     72 s = 'ghi789.012jkl';
     73 s.match(re);
     74 assertEquals(s, RegExp.input);
     75 assertEquals('789.012', RegExp.lastMatch);
     76 assertEquals('012', RegExp.lastParen);
     77 assertEquals('ghi', RegExp.leftContext);
     78 assertEquals('jkl', RegExp.rightContext);
     79 assertEquals(s, RegExp['$_']);
     80 assertEquals('789.012', RegExp['$&']);
     81 assertEquals('012', RegExp['$+']);
     82 assertEquals('ghi', RegExp['$`']);
     83 assertEquals('jkl', RegExp["$'"]);
     84 assertEquals('789.012', RegExp['$1']);
     85 assertEquals('789', RegExp['$2']);
     86 assertEquals('012', RegExp['$3']);
     87 for (var i = 4; i < 10; ++i) {
     88   assertEquals('', RegExp['$' + i]);
     89 }
     90 
     91 s = 'abc123.456def';
     92 s.replace(re, 'whocares');
     93 assertEquals(s, RegExp.input);
     94 assertEquals('123.456', RegExp.lastMatch);
     95 assertEquals('456', RegExp.lastParen);
     96 assertEquals('abc', RegExp.leftContext);
     97 assertEquals('def', RegExp.rightContext);
     98 assertEquals(s, RegExp['$_']);
     99 assertEquals('123.456', RegExp['$&']);
    100 assertEquals('456', RegExp['$+']);
    101 assertEquals('abc', RegExp['$`']);
    102 assertEquals('def', RegExp["$'"]);
    103 assertEquals('123.456', RegExp['$1']);
    104 assertEquals('123', RegExp['$2']);
    105 assertEquals('456', RegExp['$3']);
    106 for (var i = 4; i < 10; ++i) {
    107   assertEquals('', RegExp['$' + i]);
    108 }
    109 
    110 s = 'ghi789.012jkl';
    111 re.test(s);
    112 assertEquals(s, RegExp.input);
    113 assertEquals('789.012', RegExp.lastMatch);
    114 assertEquals('012', RegExp.lastParen);
    115 assertEquals('ghi', RegExp.leftContext);
    116 assertEquals('jkl', RegExp.rightContext);
    117 assertEquals(s, RegExp['$_']);
    118 assertEquals('789.012', RegExp['$&']);
    119 assertEquals('012', RegExp['$+']);
    120 assertEquals('ghi', RegExp['$`']);
    121 assertEquals('jkl', RegExp["$'"]);
    122 assertEquals('789.012', RegExp['$1']);
    123 assertEquals('789', RegExp['$2']);
    124 assertEquals('012', RegExp['$3']);
    125 for (var i = 4; i < 10; ++i) {
    126   assertEquals('', RegExp['$' + i]);
    127 }
    128 
    129 // String.prototype.replace must interleave matching and replacing when a
    130 // global regexp is matched and replaced with the result of a function, in
    131 // case the function uses the static properties of the regexp constructor.
    132 re = /(.)/g;
    133 function f() { return RegExp.$1; };
    134 assertEquals('abcd', 'abcd'.replace(re, f));
    135 
    136 // lastParen where the last parenthesis didn't match.
    137 assertEquals("foo,", /foo(?:a(x))?/.exec("foobx"), "lastParen setup");
    138 assertEquals("", RegExp.lastParen, "lastParen");
    139 
    140 // The same test for $1 to $9.
    141 for (var i = 1; i <= 9; i++) {
    142   var haystack = "foo";
    143   var re_text = "^foo";
    144   for (var j = 0; j < i - 1; j++) {
    145     haystack += "x";
    146     re_text += "(x)";
    147   }
    148   re_text += "(?:a(x))?";
    149   haystack += "bx";
    150   var re = new RegExp(re_text);
    151   assertTrue(re.test(haystack), "$" + i + " setup");
    152   for (var j = 1; j < i - 1; j++) {
    153     assertEquals("x", RegExp['$' + j], "$" + j + " in $" + i + " setup");
    154   }
    155   assertEquals("", RegExp['$' + (i)], "$" + i);
    156 }
    157 
    158 RegExp.multiline = "foo";
    159 assertTrue(typeof RegExp.multiline == typeof Boolean(), "RegExp.multiline coerces values to booleans");
    160 RegExp.input = Number();
    161 assertTrue(typeof RegExp.input == typeof String(), "RegExp.input coerces values to booleans");
    162 
    163 // Ensure that we save the correct string as the last subject when
    164 // we do a match on a sliced string (the top one not the underlying).
    165 var foo = "lsdfj sldkfj sdklfj lsdfjl sdkfjlsdk fjsdl fjsdljskdj flsj flsdkj flskd regexp: /foobar/\nldkfj sdlkfj sdkl";
    166 assertTrue(/^([a-z]+): (.*)/.test(foo.substring(foo.indexOf("regexp:"))), "regexp: setup");
    167 assertEquals("regexp", RegExp.$1, "RegExp.$1");
    168