blob: b751f0f60f1e88eecb0e91b81bc943308603c898 [file] [log] [blame]
iposva@chromium.org245aa852009-02-10 00:49:54 +00001// 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
rossberg@chromium.org28a37082011-08-22 11:03:23 +000033// 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
iposva@chromium.org245aa852009-02-10 00:49:54 +000037// 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
rossberg@chromium.org28a37082011-08-22 11:03:23 +000060assertThrows("/a/ii");
iposva@chromium.org245aa852009-02-10 00:49:54 +000061
rossberg@chromium.org28a37082011-08-22 11:03:23 +000062assertThrows("/a/gii");
iposva@chromium.org245aa852009-02-10 00:49:54 +000063
rossberg@chromium.org28a37082011-08-22 11:03:23 +000064assertThrows("/a/igi");
iposva@chromium.org245aa852009-02-10 00:49:54 +000065
rossberg@chromium.org28a37082011-08-22 11:03:23 +000066assertThrows("/a/iig");
iposva@chromium.org245aa852009-02-10 00:49:54 +000067
rossberg@chromium.org28a37082011-08-22 11:03:23 +000068assertThrows("/a/gimi");
iposva@chromium.org245aa852009-02-10 00:49:54 +000069
rossberg@chromium.org28a37082011-08-22 11:03:23 +000070assertThrows("/a/giim");
iposva@chromium.org245aa852009-02-10 00:49:54 +000071
rossberg@chromium.org28a37082011-08-22 11:03:23 +000072assertThrows("/a/igim");
iposva@chromium.org245aa852009-02-10 00:49:54 +000073
rossberg@chromium.org28a37082011-08-22 11:03:23 +000074assertThrows(function(){ return RegExp("a", "ii"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000075
rossberg@chromium.org28a37082011-08-22 11:03:23 +000076assertThrows(function(){ return RegExp("a", "gii"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000077
rossberg@chromium.org28a37082011-08-22 11:03:23 +000078assertThrows(function(){ return RegExp("a", "igi"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000079
rossberg@chromium.org28a37082011-08-22 11:03:23 +000080assertThrows(function(){ return RegExp("a", "iig"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000081
rossberg@chromium.org28a37082011-08-22 11:03:23 +000082assertThrows(function(){ return RegExp("a", "gimi"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000083
rossberg@chromium.org28a37082011-08-22 11:03:23 +000084assertThrows(function(){ return RegExp("a", "giim"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000085
rossberg@chromium.org28a37082011-08-22 11:03:23 +000086assertThrows(function(){ return RegExp("a", "igim"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +000087
88// Tripple i's
89
rossberg@chromium.org28a37082011-08-22 11:03:23 +000090assertThrows("/a/iii");
iposva@chromium.org245aa852009-02-10 00:49:54 +000091
rossberg@chromium.org28a37082011-08-22 11:03:23 +000092assertThrows("/a/giii");
iposva@chromium.org245aa852009-02-10 00:49:54 +000093
rossberg@chromium.org28a37082011-08-22 11:03:23 +000094assertThrows("/a/igii");
iposva@chromium.org245aa852009-02-10 00:49:54 +000095
rossberg@chromium.org28a37082011-08-22 11:03:23 +000096assertThrows("/a/iigi");
iposva@chromium.org245aa852009-02-10 00:49:54 +000097
rossberg@chromium.org28a37082011-08-22 11:03:23 +000098assertThrows("/a/iiig");
iposva@chromium.org245aa852009-02-10 00:49:54 +000099
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000100assertThrows("/a/miiig");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000101
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000102assertThrows(function(){ return RegExp("a", "iii"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000103
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000104assertThrows(function(){ return RegExp("a", "giii"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000105
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000106assertThrows(function(){ return RegExp("a", "igii"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000107
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000108assertThrows(function(){ return RegExp("a", "iigi"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000109
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000110assertThrows(function(){ return RegExp("a", "iiig"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000111
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000112assertThrows(function(){ return RegExp("a", "miiig"); })
iposva@chromium.org245aa852009-02-10 00:49:54 +0000113
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000114// Illegal flags - valid flags late in string.
iposva@chromium.org245aa852009-02-10 00:49:54 +0000115
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000116assertThrows("/a/arglebargleglopglyf");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000117
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000118assertThrows("/a/arglebargleglopglif");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000119
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000120assertThrows("/a/arglebargleglopglym");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000121
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000122assertThrows("/a/arglebargleglopglim");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000123
124// Case of flags still matters.
125
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000126var re = /a/gmi;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000127assertFlags(re, true, true, true)
128
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000129assertThrows("/a/Gmi");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000130
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000131assertThrows("/a/gMi");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000132
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000133assertThrows("/a/gmI");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000134
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000135assertThrows("/a/GMi");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000136
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000137assertThrows("/a/GmI");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000138
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000139assertThrows("/a/gMI");
iposva@chromium.org245aa852009-02-10 00:49:54 +0000140
rossberg@chromium.org28a37082011-08-22 11:03:23 +0000141assertThrows("/a/GMI");
142
143// Unicode escape sequences are not interpreted.
144
145assertThrows("/a/\\u0067");
146assertThrows("/a/\\u0069");
147assertThrows("/a/\\u006d");
148assertThrows("/a/\\u006D");