1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description("KDE JS Test");
25shouldBe("(new RegExp()).source", "'(?:)'");
26shouldBe("Boolean(new RegExp())", "true");
27shouldBeTrue("isNaN(Number(new RegExp()))");
28
29// RegExp constructor called as a function
30shouldBe("RegExp(/x/).source", "'x'");
31//shouldBe("RegExp(/x/, 'g').source", "'/x/'"); // can't supply flags when constructing one RegExp from another, says mozilla
32shouldBe("RegExp('x', 'g').global", "true");
33shouldBe("RegExp('x').source", "'x'");
34
35// RegExp constructor
36shouldBe("new RegExp('x').source", "'x'");
37
38var ri = /a/i;
39var rm = /a/m;
40var rg = /a/g;
41
42shouldBeFalse("(/a/).global");
43shouldBe("typeof (/a/).global", "'boolean'");
44shouldBeTrue("rg.global");
45shouldBeFalse("(/a/).ignoreCase");
46shouldBeTrue("ri.ignoreCase");
47shouldBeFalse("(/a/).multiline");
48shouldBeTrue("rm.multiline");
49shouldBe("rg.toString()", "'/a/g'");
50shouldBe("ri.toString()", "'/a/i'");
51shouldBe("rm.toString()", "'/a/m'");
52
53// check properety attributes
54rg.global = false;
55shouldBeTrue("rg.global");
56ri.ignoreCase = false;
57shouldBeTrue("ri.ignoreCase");
58rm.multiline = false;
59shouldBeTrue("rm.multiline");
60
61shouldBe("Boolean(/a/.test)", "true");
62shouldBe("/(b)c/.exec('abcd').toString()", "\"bc,b\"");
63shouldBe("/(b)c/.exec('abcd').length", "2");
64shouldBe("/(b)c/.exec('abcd').index", "1");
65shouldBe("/(b)c/.exec('abcd').input", "'abcd'");
66
67var rs = /foo/;
68rs.source = "bar";
69shouldBe("rs.source", "'foo'");
70
71shouldBe("var r = new RegExp(/x/); r.global=true; r.lastIndex = -1; typeof r.test('a')", "'boolean'");
72
73shouldBe("'abcdefghi'.match(/(abc)def(ghi)/).toString()","'abcdefghi,abc,ghi'");
74shouldBe("/(abc)def(ghi)/.exec('abcdefghi').toString()","'abcdefghi,abc,ghi'");
75shouldBe("RegExp.$1","'abc'");
76shouldBe("RegExp.$2","'ghi'");
77shouldBe("RegExp.$3","''");
78
79shouldBe("'abcdefghi'.match(/(a(b(c(d(e)f)g)h)i)/).toString()", "'abcdefghi,abcdefghi,bcdefgh,cdefg,def,e'");
80shouldBe("RegExp.$1","'abcdefghi'");
81shouldBe("RegExp.$2","'bcdefgh'");
82shouldBe("RegExp.$3","'cdefg'");
83shouldBe("RegExp.$4","'def'");
84shouldBe("RegExp.$5","'e'");
85shouldBe("RegExp.$6","''");
86
87shouldBe("'(100px 200px 150px 15px)'.match(/\\((\\d+)(px)* (\\d+)(px)* (\\d+)(px)* (\\d+)(px)*\\)/).toString()","'(100px 200px 150px 15px),100,px,200,px,150,px,15,px'");
88shouldBe("RegExp.$1","'100'");
89shouldBe("RegExp.$3","'200'");
90shouldBe("RegExp.$5","'150'");
91shouldBe("RegExp.$7","'15'");
92shouldBe("''.match(/\((\\d+)(px)* (\\d+)(px)* (\\d+)(px)* (\\d+)(px)*\)/)","null");
93// After a failed match, cached results on the RegExp object are unchanged.
94shouldBe("RegExp.$1","'100'");
95shouldBe("RegExp.$3","'200'");
96shouldBe("RegExp.$5","'150'");
97shouldBe("RegExp.$7","'15'");
98
99var invalidChars = /[^@\.\w]/g; // #47092
100shouldBe("'faure@kde.org'.match(invalidChars) == null", "true");
101shouldBe("'faure-kde@kde.org'.match(invalidChars) == null", "false");
102
103shouldBe("'test1test2'.replace('test','X')","'X1test2'");
104shouldBe("'test1test2'.replace(/\\d/,'X')","'testXtest2'");
105shouldBe("'1test2test3'.replace(/\\d/,'')","'test2test3'");
106shouldBe("'test1test2'.replace(/test/g,'X')","'X1X2'");
107shouldBe("'1test2test3'.replace(/\\d/g,'')","'testtest'");
108shouldBe("'1test2test3'.replace(/x/g,'')","'1test2test3'");
109shouldBe("'test1test2'.replace(/(te)(st)/g,'$2$1')","'stte1stte2'");
110shouldBe("'foo+bar'.replace(/\\+/g,'%2B')", "'foo%2Bbar'");
111var caught = false; try { new RegExp("+"); } catch (e) { caught = true; }
112shouldBeTrue("caught"); // #40435
113shouldBe("'foo'.replace(/z?/g,'x')", "'xfxoxox'");
114shouldBe("'test test'.replace(/\\s*/g,'')","'testtest'"); // #50985
115shouldBe("'abc$%@'.replace(/[^0-9a-z]*/gi,'')","'abc'"); // #50848
116shouldBe("'ab'.replace(/[^\\d\\.]*/gi,'')","''"); // #75292
117shouldBe("'1ab'.replace(/[^\\d\\.]*/gi,'')","'1'"); // #75292
118
119shouldBe("'1test2test3blah'.split(/test/).toString()","'1,2,3blah'");
120var reg = /(\d\d )/g;
121var str = new String('98 76 blah');
122shouldBe("reg.exec(str).toString()","'98 ,98 '");
123shouldBe("reg.lastIndex","3");
124shouldBe("RegExp.$1","'98 '");
125shouldBe("RegExp.$2","''");
126
127shouldBe("reg.exec(str).toString()","'76 ,76 '");
128shouldBe("reg.lastIndex","6");
129shouldBe("RegExp.$1","'76 '");
130shouldBe("RegExp.$2","''");
131
132shouldBe("reg.exec(str)","null");
133shouldBe("reg.lastIndex","0");
134
135// http://www.devguru.com/Technologies/ecmascript/quickref/regexp_lastindex.html
136// Looks IE-only though
137//shouldBe( "var re=/ships*\s/; re.exec('the hardships of traveling'); re.lastIndex", "14" );
138//shouldBe( "var re=/ships*\s/; str='the hardships of traveling'; re.exec(str); re.exec(str); re.lastIndex", "0" );
139
140// http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html
141shouldBe( "myRe=/d(b+)d/g; myArray = myRe.exec('cdbbdbsbz'); myRe.lastIndex", "5" );
142
143reg = /^u/i;
144shouldBeTrue("reg.ignoreCase == true");
145shouldBeTrue("reg.global === false");
146shouldBeTrue("reg.multiline === false");
147shouldBeTrue("reg.test('UGO')");
148
149// regexp are writable ?
150shouldBe("reg.x = 1; reg.x", "1");
151// shared data ?
152shouldBe("var r2 = reg; r2.x = 2; reg.x", "2");
153
154var str = new String("For more information, see Chapter 3.4.5.1");
155re = /(chapter \d+(\.\d)*)/i;
156// This returns the array containing Chapter 3.4.5.1,Chapter 3.4.5.1,.1
157// 'Chapter 3.4.5.1' is the first match and the first value remembered from (Chapter \d+(\.\d)*).
158// '.1' is the second value remembered from (\.\d)
159shouldBe("str.match(re).toString()","'Chapter 3.4.5.1,Chapter 3.4.5.1,.1'");
160
161str = "abcDdcba";
162// The returned array contains D, d.
163shouldBe("str.match(/d/gi).toString()","'D,d'");
164
165// unicode escape sequence
166shouldBe("/\\u0061/.source", "'\\\\u0061'");
167shouldBe("'abc'.match(/\\u0062/).toString()", "'b'");
168
169shouldBe("Object.prototype.toString.apply(RegExp.prototype)",
170         "'[object RegExp]'");
171
172// not sure what this should return. most importantly
173// it doesn't throw an exception
174shouldBe("typeof RegExp.prototype.toString()", "'string'");
175
176// Empty regular expressions have string representation /(?:)/
177shouldBe("new RegExp().toString()", "'/(?:)/'");
178shouldBe("(new RegExp('(?:)')).source", "'(?:)'");
179shouldBe("/(?:)/.toString()", "'/(?:)/'");
180shouldBe("/(?:)/.source", "'(?:)'");
181
182debug("Done.");
183