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 24 description("KDE JS Test"); 25 shouldBe("(new RegExp()).source", "'(?:)'"); 26 shouldBe("Boolean(new RegExp())", "true"); 27 shouldBeTrue("isNaN(Number(new RegExp()))"); 28 29 // RegExp constructor called as a function 30 shouldBe("RegExp(/x/).source", "'x'"); 31 //shouldBe("RegExp(/x/, 'g').source", "'/x/'"); // can't supply flags when constructing one RegExp from another, says mozilla 32 shouldBe("RegExp('x', 'g').global", "true"); 33 shouldBe("RegExp('x').source", "'x'"); 34 35 // RegExp constructor 36 shouldBe("new RegExp('x').source", "'x'"); 37 38 var ri = /a/i; 39 var rm = /a/m; 40 var rg = /a/g; 41 42 shouldBeFalse("(/a/).global"); 43 shouldBe("typeof (/a/).global", "'boolean'"); 44 shouldBeTrue("rg.global"); 45 shouldBeFalse("(/a/).ignoreCase"); 46 shouldBeTrue("ri.ignoreCase"); 47 shouldBeFalse("(/a/).multiline"); 48 shouldBeTrue("rm.multiline"); 49 shouldBe("rg.toString()", "'/a/g'"); 50 shouldBe("ri.toString()", "'/a/i'"); 51 shouldBe("rm.toString()", "'/a/m'"); 52 53 // check properety attributes 54 rg.global = false; 55 shouldBeTrue("rg.global"); 56 ri.ignoreCase = false; 57 shouldBeTrue("ri.ignoreCase"); 58 rm.multiline = false; 59 shouldBeTrue("rm.multiline"); 60 61 shouldBe("Boolean(/a/.test)", "true"); 62 shouldBe("/(b)c/.exec('abcd').toString()", "\"bc,b\""); 63 shouldBe("/(b)c/.exec('abcd').length", "2"); 64 shouldBe("/(b)c/.exec('abcd').index", "1"); 65 shouldBe("/(b)c/.exec('abcd').input", "'abcd'"); 66 67 var rs = /foo/; 68 rs.source = "bar"; 69 shouldBe("rs.source", "'foo'"); 70 71 shouldBe("var r = new RegExp(/x/); r.global=true; r.lastIndex = -1; typeof r.test('a')", "'boolean'"); 72 73 shouldBe("'abcdefghi'.match(/(abc)def(ghi)/).toString()","'abcdefghi,abc,ghi'"); 74 shouldBe("/(abc)def(ghi)/.exec('abcdefghi').toString()","'abcdefghi,abc,ghi'"); 75 shouldBe("RegExp.$1","'abc'"); 76 shouldBe("RegExp.$2","'ghi'"); 77 shouldBe("RegExp.$3","''"); 78 79 shouldBe("'abcdefghi'.match(/(a(b(c(d(e)f)g)h)i)/).toString()", "'abcdefghi,abcdefghi,bcdefgh,cdefg,def,e'"); 80 shouldBe("RegExp.$1","'abcdefghi'"); 81 shouldBe("RegExp.$2","'bcdefgh'"); 82 shouldBe("RegExp.$3","'cdefg'"); 83 shouldBe("RegExp.$4","'def'"); 84 shouldBe("RegExp.$5","'e'"); 85 shouldBe("RegExp.$6","''"); 86 87 shouldBe("'(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'"); 88 shouldBe("RegExp.$1","'100'"); 89 shouldBe("RegExp.$3","'200'"); 90 shouldBe("RegExp.$5","'150'"); 91 shouldBe("RegExp.$7","'15'"); 92 shouldBe("''.match(/\((\\d+)(px)* (\\d+)(px)* (\\d+)(px)* (\\d+)(px)*\)/)","null"); 93 // After a failed match, cached results on the RegExp object are unchanged. 94 shouldBe("RegExp.$1","'100'"); 95 shouldBe("RegExp.$3","'200'"); 96 shouldBe("RegExp.$5","'150'"); 97 shouldBe("RegExp.$7","'15'"); 98 99 var invalidChars = /[^@\.\w]/g; // #47092 100 shouldBe("'faure (a] kde.org'.match(invalidChars) == null", "true"); 101 shouldBe("'faure-kde (a] kde.org'.match(invalidChars) == null", "false"); 102 103 shouldBe("'test1test2'.replace('test','X')","'X1test2'"); 104 shouldBe("'test1test2'.replace(/\\d/,'X')","'testXtest2'"); 105 shouldBe("'1test2test3'.replace(/\\d/,'')","'test2test3'"); 106 shouldBe("'test1test2'.replace(/test/g,'X')","'X1X2'"); 107 shouldBe("'1test2test3'.replace(/\\d/g,'')","'testtest'"); 108 shouldBe("'1test2test3'.replace(/x/g,'')","'1test2test3'"); 109 shouldBe("'test1test2'.replace(/(te)(st)/g,'$2$1')","'stte1stte2'"); 110 shouldBe("'foo+bar'.replace(/\\+/g,'%2B')", "'foo%2Bbar'"); 111 var caught = false; try { new RegExp("+"); } catch (e) { caught = true; } 112 shouldBeTrue("caught"); // #40435 113 shouldBe("'foo'.replace(/z?/g,'x')", "'xfxoxox'"); 114 shouldBe("'test test'.replace(/\\s*/g,'')","'testtest'"); // #50985 115 shouldBe("'abc$%@'.replace(/[^0-9a-z]*/gi,'')","'abc'"); // #50848 116 shouldBe("'ab'.replace(/[^\\d\\.]*/gi,'')","''"); // #75292 117 shouldBe("'1ab'.replace(/[^\\d\\.]*/gi,'')","'1'"); // #75292 118 119 shouldBe("'1test2test3blah'.split(/test/).toString()","'1,2,3blah'"); 120 var reg = /(\d\d )/g; 121 var str = new String('98 76 blah'); 122 shouldBe("reg.exec(str).toString()","'98 ,98 '"); 123 shouldBe("reg.lastIndex","3"); 124 shouldBe("RegExp.$1","'98 '"); 125 shouldBe("RegExp.$2","''"); 126 127 shouldBe("reg.exec(str).toString()","'76 ,76 '"); 128 shouldBe("reg.lastIndex","6"); 129 shouldBe("RegExp.$1","'76 '"); 130 shouldBe("RegExp.$2","''"); 131 132 shouldBe("reg.exec(str)","null"); 133 shouldBe("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 141 shouldBe( "myRe=/d(b+)d/g; myArray = myRe.exec('cdbbdbsbz'); myRe.lastIndex", "5" ); 142 143 reg = /^u/i; 144 shouldBeTrue("reg.ignoreCase == true"); 145 shouldBeTrue("reg.global === false"); 146 shouldBeTrue("reg.multiline === false"); 147 shouldBeTrue("reg.test('UGO')"); 148 149 // regexp are writable ? 150 shouldBe("reg.x = 1; reg.x", "1"); 151 // shared data ? 152 shouldBe("var r2 = reg; r2.x = 2; reg.x", "2"); 153 154 var str = new String("For more information, see Chapter 3.4.5.1"); 155 re = /(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) 159 shouldBe("str.match(re).toString()","'Chapter 3.4.5.1,Chapter 3.4.5.1,.1'"); 160 161 str = "abcDdcba"; 162 // The returned array contains D, d. 163 shouldBe("str.match(/d/gi).toString()","'D,d'"); 164 165 // unicode escape sequence 166 shouldBe("/\\u0061/.source", "'\\\\u0061'"); 167 shouldBe("'abc'.match(/\\u0062/).toString()", "'b'"); 168 169 shouldBe("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 174 shouldBe("typeof RegExp.prototype.toString()", "'string'"); 175 176 // Empty regular expressions have string representation /(?:)/ 177 shouldBe("new RegExp().toString()", "'/(?:)/'"); 178 shouldBe("(new RegExp('(?:)')).source", "'(?:)'"); 179 shouldBe("/(?:)/.toString()", "'/(?:)/'"); 180 shouldBe("/(?:)/.source", "'(?:)'"); 181 182 debug("Done."); 183