blob: c0633f9173eb645a71b1718fbae6dabc1b7af9ab [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdochb8a8cc12014-11-26 15:28:44 +000028var re = /foo.bar/;
29
30assertTrue(!!"foo*bar".match(re));
31assertTrue(!!"..foo*bar".match(re));
32
33var plain = /foobar/;
34
35assertTrue(!!"foobar".match(plain));
36assertTrue(!!"..foobar".match(plain));
37
38var sticky = /foo.bar/y;
39
40assertTrue(!!"foo*bar".match(sticky));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041assertEquals(7, sticky.lastIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042assertFalse(!!"..foo*bar".match(sticky));
43
44var stickyplain = /foobar/y;
45
46assertTrue(!!"foobar".match(stickyplain));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047assertEquals(6, stickyplain.lastIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048assertFalse(!!"..foobar".match(stickyplain));
49
50var global = /foo.bar/g;
51
52assertTrue(global.test("foo*bar"));
53assertFalse(global.test("..foo*bar"));
54global.lastIndex = 0;
55assertTrue(global.test("..foo*bar"));
56
57var plainglobal = /foobar/g;
58
59assertTrue(plainglobal.test("foobar"));
60assertFalse(plainglobal.test("foobar"));
61plainglobal.lastIndex = 0;
62assertTrue(plainglobal.test("foobar"));
63
64var stickyglobal = /foo.bar/gy;
65
66assertTrue(stickyglobal.test("foo*bar"));
67assertEquals(7, stickyglobal.lastIndex);
68assertFalse(stickyglobal.test("..foo*bar"));
69stickyglobal.lastIndex = 0;
70assertFalse(stickyglobal.test("..foo*bar"));
71stickyglobal.lastIndex = 2;
72assertTrue(stickyglobal.test("..foo*bar"));
73assertEquals(9, stickyglobal.lastIndex);
74
75var stickyplainglobal = /foobar/yg;
76assertTrue(stickyplainglobal.sticky);
77stickyplainglobal.sticky = false;
78
79assertTrue(stickyplainglobal.test("foobar"));
80assertEquals(6, stickyplainglobal.lastIndex);
81assertFalse(stickyplainglobal.test("..foobar"));
82stickyplainglobal.lastIndex = 0;
83assertFalse(stickyplainglobal.test("..foobar"));
84stickyplainglobal.lastIndex = 2;
85assertTrue(stickyplainglobal.test("..foobar"));
86assertEquals(8, stickyplainglobal.lastIndex);
87
88assertEquals("/foo.bar/gy", "" + stickyglobal);
89assertEquals("/foo.bar/g", "" + global);
90
91assertTrue(stickyglobal.sticky);
92stickyglobal.sticky = false;
93assertTrue(stickyglobal.sticky);
94
95var stickyglobal2 = new RegExp("foo.bar", "gy");
96assertTrue(stickyglobal2.test("foo*bar"));
97assertEquals(7, stickyglobal2.lastIndex);
98assertFalse(stickyglobal2.test("..foo*bar"));
99stickyglobal2.lastIndex = 0;
100assertFalse(stickyglobal2.test("..foo*bar"));
101stickyglobal2.lastIndex = 2;
102assertTrue(stickyglobal2.test("..foo*bar"));
103assertEquals(9, stickyglobal2.lastIndex);
104
105assertEquals("/foo.bar/gy", "" + stickyglobal2);
106
107assertTrue(stickyglobal2.sticky);
108stickyglobal2.sticky = false;
109assertTrue(stickyglobal2.sticky);
110
111sticky.lastIndex = -1; // Causes sticky regexp to fail fast
112assertFalse(sticky.test("..foo.bar"));
113assertEquals(0, sticky.lastIndex);
114
115sticky.lastIndex = -1; // Causes sticky regexp to fail fast
116assertFalse(!!sticky.exec("..foo.bar"));
117assertEquals(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.
122var hat = /^foo/y;
123hat.lastIndex = 2;
124assertFalse(hat.test("..foo"));
125
126var mhat = /^foo/my;
127mhat.lastIndex = 2;
128assertFalse(mhat.test("..foo"));
129mhat.lastIndex = 2;
130assertTrue(mhat.test(".\nfoo"));