Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | var re = /foo.bar/; |
| 29 | |
| 30 | assertTrue(!!"foo*bar".match(re)); |
| 31 | assertTrue(!!"..foo*bar".match(re)); |
| 32 | |
| 33 | var plain = /foobar/; |
| 34 | |
| 35 | assertTrue(!!"foobar".match(plain)); |
| 36 | assertTrue(!!"..foobar".match(plain)); |
| 37 | |
| 38 | var sticky = /foo.bar/y; |
| 39 | |
| 40 | assertTrue(!!"foo*bar".match(sticky)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | assertEquals(7, sticky.lastIndex); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | assertFalse(!!"..foo*bar".match(sticky)); |
| 43 | |
| 44 | var stickyplain = /foobar/y; |
| 45 | |
| 46 | assertTrue(!!"foobar".match(stickyplain)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | assertEquals(6, stickyplain.lastIndex); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | assertFalse(!!"..foobar".match(stickyplain)); |
| 49 | |
| 50 | var global = /foo.bar/g; |
| 51 | |
| 52 | assertTrue(global.test("foo*bar")); |
| 53 | assertFalse(global.test("..foo*bar")); |
| 54 | global.lastIndex = 0; |
| 55 | assertTrue(global.test("..foo*bar")); |
| 56 | |
| 57 | var plainglobal = /foobar/g; |
| 58 | |
| 59 | assertTrue(plainglobal.test("foobar")); |
| 60 | assertFalse(plainglobal.test("foobar")); |
| 61 | plainglobal.lastIndex = 0; |
| 62 | assertTrue(plainglobal.test("foobar")); |
| 63 | |
| 64 | var stickyglobal = /foo.bar/gy; |
| 65 | |
| 66 | assertTrue(stickyglobal.test("foo*bar")); |
| 67 | assertEquals(7, stickyglobal.lastIndex); |
| 68 | assertFalse(stickyglobal.test("..foo*bar")); |
| 69 | stickyglobal.lastIndex = 0; |
| 70 | assertFalse(stickyglobal.test("..foo*bar")); |
| 71 | stickyglobal.lastIndex = 2; |
| 72 | assertTrue(stickyglobal.test("..foo*bar")); |
| 73 | assertEquals(9, stickyglobal.lastIndex); |
| 74 | |
| 75 | var stickyplainglobal = /foobar/yg; |
| 76 | assertTrue(stickyplainglobal.sticky); |
| 77 | stickyplainglobal.sticky = false; |
| 78 | |
| 79 | assertTrue(stickyplainglobal.test("foobar")); |
| 80 | assertEquals(6, stickyplainglobal.lastIndex); |
| 81 | assertFalse(stickyplainglobal.test("..foobar")); |
| 82 | stickyplainglobal.lastIndex = 0; |
| 83 | assertFalse(stickyplainglobal.test("..foobar")); |
| 84 | stickyplainglobal.lastIndex = 2; |
| 85 | assertTrue(stickyplainglobal.test("..foobar")); |
| 86 | assertEquals(8, stickyplainglobal.lastIndex); |
| 87 | |
| 88 | assertEquals("/foo.bar/gy", "" + stickyglobal); |
| 89 | assertEquals("/foo.bar/g", "" + global); |
| 90 | |
| 91 | assertTrue(stickyglobal.sticky); |
| 92 | stickyglobal.sticky = false; |
| 93 | assertTrue(stickyglobal.sticky); |
| 94 | |
| 95 | var stickyglobal2 = new RegExp("foo.bar", "gy"); |
| 96 | assertTrue(stickyglobal2.test("foo*bar")); |
| 97 | assertEquals(7, stickyglobal2.lastIndex); |
| 98 | assertFalse(stickyglobal2.test("..foo*bar")); |
| 99 | stickyglobal2.lastIndex = 0; |
| 100 | assertFalse(stickyglobal2.test("..foo*bar")); |
| 101 | stickyglobal2.lastIndex = 2; |
| 102 | assertTrue(stickyglobal2.test("..foo*bar")); |
| 103 | assertEquals(9, stickyglobal2.lastIndex); |
| 104 | |
| 105 | assertEquals("/foo.bar/gy", "" + stickyglobal2); |
| 106 | |
| 107 | assertTrue(stickyglobal2.sticky); |
| 108 | stickyglobal2.sticky = false; |
| 109 | assertTrue(stickyglobal2.sticky); |
| 110 | |
| 111 | sticky.lastIndex = -1; // Causes sticky regexp to fail fast |
| 112 | assertFalse(sticky.test("..foo.bar")); |
| 113 | assertEquals(0, sticky.lastIndex); |
| 114 | |
| 115 | sticky.lastIndex = -1; // Causes sticky regexp to fail fast |
| 116 | assertFalse(!!sticky.exec("..foo.bar")); |
| 117 | assertEquals(0, sticky.lastIndex); |
| 118 | |
| 119 | // ES6 draft says: Even when the y flag is used with a pattern, ^ always |
| 120 | // matches only at the beginning of Input, or (if Multiline is true) at the |
| 121 | // beginning of a line. |
| 122 | var hat = /^foo/y; |
| 123 | hat.lastIndex = 2; |
| 124 | assertFalse(hat.test("..foo")); |
| 125 | |
| 126 | var mhat = /^foo/my; |
| 127 | mhat.lastIndex = 2; |
| 128 | assertFalse(mhat.test("..foo")); |
| 129 | mhat.lastIndex = 2; |
| 130 | assertTrue(mhat.test(".\nfoo")); |