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
33assertTrue(/^bar/.test("bar"));
34assertTrue(/^bar/.test("bar\nfoo"));
35assertFalse(/^bar/.test("foo\nbar"));
36assertTrue(/^bar/m.test("bar"));
37assertTrue(/^bar/m.test("bar\nfoo"));
38assertTrue(/^bar/m.test("foo\nbar"));
39
40assertTrue(/bar$/.test("bar"));
41assertFalse(/bar$/.test("bar\nfoo"));
42assertTrue(/bar$/.test("foo\nbar"));
43assertTrue(/bar$/m.test("bar"));
44assertTrue(/bar$/m.test("bar\nfoo"));
45assertTrue(/bar$/m.test("foo\nbar"));
46
47assertFalse(/^bxr/.test("bar"));
48assertFalse(/^bxr/.test("bar\nfoo"));
49assertFalse(/^bxr/m.test("bar"));
50assertFalse(/^bxr/m.test("bar\nfoo"));
51assertFalse(/^bxr/m.test("foo\nbar"));
52
53assertFalse(/bxr$/.test("bar"));
54assertFalse(/bxr$/.test("foo\nbar"));
55assertFalse(/bxr$/m.test("bar"));
56assertFalse(/bxr$/m.test("bar\nfoo"));
57assertFalse(/bxr$/m.test("foo\nbar"));
58
59
60assertTrue(/^.*$/.test(""));
61assertTrue(/^.*$/.test("foo"));
62assertFalse(/^.*$/.test("\n"));
63assertTrue(/^.*$/m.test("\n"));
64
65assertTrue(/^[\s]*$/.test(" "));
66assertTrue(/^[\s]*$/.test("\n"));
67
68assertTrue(/^[^]*$/.test(""));
69assertTrue(/^[^]*$/.test("foo"));
70assertTrue(/^[^]*$/.test("\n"));
71
72assertTrue(/^([()\s]|.)*$/.test("()\n()"));
73assertTrue(/^([()\n]|.)*$/.test("()\n()"));
74assertFalse(/^([()]|.)*$/.test("()\n()"));
75assertTrue(/^([()]|.)*$/m.test("()\n()"));
76assertTrue(/^([()]|.)*$/m.test("()\n"));
77assertTrue(/^[()]*$/m.test("()\n."));
78
79assertTrue(/^[\].]*$/.test("...]..."));
80
81
82function 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
105check_case("a", "A");
106// Aring
107check_case(String.fromCharCode(229), String.fromCharCode(197));
108// Russian G
109check_case(String.fromCharCode(0x413), String.fromCharCode(0x433));
110
111
112assertThrows("a = new RegExp('[z-a]');");
113