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 /** 29 * @fileoverview Check that various regexp constructs work as intended. 30 * Particularly those regexps that use ^ and $. 31 */ 32 33 assertTrue(/^bar/.test("bar")); 34 assertTrue(/^bar/.test("bar\nfoo")); 35 assertFalse(/^bar/.test("foo\nbar")); 36 assertTrue(/^bar/m.test("bar")); 37 assertTrue(/^bar/m.test("bar\nfoo")); 38 assertTrue(/^bar/m.test("foo\nbar")); 39 40 assertTrue(/bar$/.test("bar")); 41 assertFalse(/bar$/.test("bar\nfoo")); 42 assertTrue(/bar$/.test("foo\nbar")); 43 assertTrue(/bar$/m.test("bar")); 44 assertTrue(/bar$/m.test("bar\nfoo")); 45 assertTrue(/bar$/m.test("foo\nbar")); 46 47 assertFalse(/^bxr/.test("bar")); 48 assertFalse(/^bxr/.test("bar\nfoo")); 49 assertFalse(/^bxr/m.test("bar")); 50 assertFalse(/^bxr/m.test("bar\nfoo")); 51 assertFalse(/^bxr/m.test("foo\nbar")); 52 53 assertFalse(/bxr$/.test("bar")); 54 assertFalse(/bxr$/.test("foo\nbar")); 55 assertFalse(/bxr$/m.test("bar")); 56 assertFalse(/bxr$/m.test("bar\nfoo")); 57 assertFalse(/bxr$/m.test("foo\nbar")); 58 59 60 assertTrue(/^.*$/.test("")); 61 assertTrue(/^.*$/.test("foo")); 62 assertFalse(/^.*$/.test("\n")); 63 assertTrue(/^.*$/m.test("\n")); 64 65 assertTrue(/^[\s]*$/.test(" ")); 66 assertTrue(/^[\s]*$/.test("\n")); 67 68 assertTrue(/^[^]*$/.test("")); 69 assertTrue(/^[^]*$/.test("foo")); 70 assertTrue(/^[^]*$/.test("\n")); 71 72 assertTrue(/^([()\s]|.)*$/.test("()\n()")); 73 assertTrue(/^([()\n]|.)*$/.test("()\n()")); 74 assertFalse(/^([()]|.)*$/.test("()\n()")); 75 assertTrue(/^([()]|.)*$/m.test("()\n()")); 76 assertTrue(/^([()]|.)*$/m.test("()\n")); 77 assertTrue(/^[()]*$/m.test("()\n.")); 78 79 assertTrue(/^[\].]*$/.test("...]...")); 80 81 82 function check_case(lc, uc) { 83 var a = new RegExp("^" + lc + "$"); 84 assertFalse(a.test(uc)); 85 a = new RegExp("^" + lc + "$", "i"); 86 assertTrue(a.test(uc)); 87 88 var A = new RegExp("^" + uc + "$"); 89 assertFalse(A.test(lc)); 90 A = new RegExp("^" + uc + "$", "i"); 91 assertTrue(A.test(lc)); 92 93 a = new RegExp("^[" + lc + "]$"); 94 assertFalse(a.test(uc)); 95 a = new RegExp("^[" + lc + "]$", "i"); 96 assertTrue(a.test(uc)); 97 98 A = new RegExp("^[" + uc + "]$"); 99 assertFalse(A.test(lc)); 100 A = new RegExp("^[" + uc + "]$", "i"); 101 assertTrue(A.test(lc)); 102 } 103 104 105 check_case("a", "A"); 106 // Aring 107 check_case(String.fromCharCode(229), String.fromCharCode(197)); 108 // Russian G 109 check_case(String.fromCharCode(0x413), String.fromCharCode(0x433)); 110 111 112 assertThrows("a = new RegExp('[z-a]');"); 113