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// Tests handling of flags for regexps.
29
30// We should now allow duplicates of flags.
31// (See http://code.google.com/p/v8/issues/detail?id=219)
32
33// This has been reversed by issue 1628, since other browsers have also
34// tightened their syntax.
35// (See http://code.google.com/p/v8/issues/detail?id=1628)
36
37// Base tests: we recognize the basic flags
38
39function assertFlags(re, global, multiline, ignoreCase) {
40  var name = re + " flag: ";
41  (global ? assertTrue : assertFalse)(re.global, name + "g");
42  (multiline ? assertTrue : assertFalse)(re.multiline, name + "m");
43  (ignoreCase ? assertTrue : assertFalse)(re.ignoreCase, name + "i");
44}
45
46var re = /a/;
47assertFlags(re, false, false, false)
48
49re = /a/gim;
50assertFlags(re, true, true, true)
51
52re = RegExp("a","");
53assertFlags(re, false, false, false)
54
55re = RegExp("a", "gim");
56assertFlags(re, true, true, true)
57
58// Double i's
59
60assertThrows("/a/ii");
61
62assertThrows("/a/gii");
63
64assertThrows("/a/igi");
65
66assertThrows("/a/iig");
67
68assertThrows("/a/gimi");
69
70assertThrows("/a/giim");
71
72assertThrows("/a/igim");
73
74assertThrows(function(){ return RegExp("a", "ii"); })
75
76assertThrows(function(){ return RegExp("a", "gii"); })
77
78assertThrows(function(){ return RegExp("a", "igi"); })
79
80assertThrows(function(){ return RegExp("a", "iig"); })
81
82assertThrows(function(){ return RegExp("a", "gimi"); })
83
84assertThrows(function(){ return RegExp("a", "giim"); })
85
86assertThrows(function(){ return RegExp("a", "igim"); })
87
88// Tripple i's
89
90assertThrows("/a/iii");
91
92assertThrows("/a/giii");
93
94assertThrows("/a/igii");
95
96assertThrows("/a/iigi");
97
98assertThrows("/a/iiig");
99
100assertThrows("/a/miiig");
101
102assertThrows(function(){ return RegExp("a", "iii"); })
103
104assertThrows(function(){ return RegExp("a", "giii"); })
105
106assertThrows(function(){ return RegExp("a", "igii"); })
107
108assertThrows(function(){ return RegExp("a", "iigi"); })
109
110assertThrows(function(){ return RegExp("a", "iiig"); })
111
112assertThrows(function(){ return RegExp("a", "miiig"); })
113
114// Illegal flags - valid flags late in string.
115
116assertThrows("/a/arglebargleglopglyf");
117
118assertThrows("/a/arglebargleglopglif");
119
120assertThrows("/a/arglebargleglopglym");
121
122assertThrows("/a/arglebargleglopglim");
123
124// Case of flags still matters.
125
126var re = /a/gmi;
127assertFlags(re, true, true, true)
128
129assertThrows("/a/Gmi");
130
131assertThrows("/a/gMi");
132
133assertThrows("/a/gmI");
134
135assertThrows("/a/GMi");
136
137assertThrows("/a/GmI");
138
139assertThrows("/a/gMI");
140
141assertThrows("/a/GMI");
142
143// Unicode escape sequences are not interpreted.
144
145assertThrows("/a/\\u0067");
146assertThrows("/a/\\u0069");
147assertThrows("/a/\\u006d");
148assertThrows("/a/\\u006D");
149