OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. All rights reserved. | |
2 // Copyright 2009 the V8 project authors. All rights reserved. | |
3 // Redistribution and use in source and binary forms, with or without | |
4 // modification, are permitted provided that the following conditions are | |
5 // met: | |
6 // | |
7 // * Redistributions of source code must retain the above copyright | |
8 // notice, this list of conditions and the following disclaimer. | |
9 // * Redistributions in binary form must reproduce the above | |
10 // copyright notice, this list of conditions and the following | |
11 // disclaimer in the documentation and/or other materials provided | |
12 // with the distribution. | |
13 // * Neither the name of Google Inc. nor the names of its | |
14 // contributors may be used to endorse or promote products derived | |
15 // from this software without specific prior written permission. | |
16 // | |
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 import 'v8_regexp_utils.dart'; | |
30 import 'package:expect/expect.dart'; | |
31 | |
32 void main() { | |
33 // Tests captures in positive and negative look-ahead in regular expressions. | |
34 | |
35 dynamic testRE(re, input, expected_result) { | |
36 if (expected_result) { | |
37 assertTrue(re.hasMatch(input)); | |
38 } else { | |
39 assertFalse(re.hasMatch(input)); | |
40 } | |
41 } | |
42 | |
43 dynamic execRE(re, input, expected_result) { | |
44 shouldBe(re.firstMatch(input), expected_result); | |
45 } | |
46 | |
47 // Test of simple positive lookahead. | |
48 | |
49 var re = new RegExp(r"^(?=a)"); | |
50 testRE(re, "a", true); | |
51 testRE(re, "b", false); | |
52 execRE(re, "a", [""]); | |
53 | |
54 re = new RegExp(r"^(?=\woo)f\w"); | |
55 testRE(re, "foo", true); | |
56 testRE(re, "boo", false); | |
57 testRE(re, "fao", false); | |
58 testRE(re, "foa", false); | |
59 execRE(re, "foo", ["fo"]); | |
60 | |
61 re = new RegExp(r"(?=\w).(?=\W)"); | |
62 testRE(re, ".a! ", true); | |
63 testRE(re, ".! ", false); | |
64 testRE(re, ".ab! ", true); | |
65 execRE(re, ".ab! ", ["b"]); | |
66 | |
67 re = new RegExp(r"(?=f(?=[^f]o)).."); | |
68 testRE(re, ", foo!", true); | |
69 testRE(re, ", fo!", false); | |
70 testRE(re, ", ffo", false); | |
71 execRE(re, ", foo!", ["fo"]); | |
72 | |
73 // Positive lookahead with captures. | |
74 re = new RegExp("^[^\'\"]*(?=([\'\"])).*\\1(\\w+)\\1"); | |
75 testRE(re, " 'foo' ", true); | |
76 testRE(re, ' "foo" ', true); | |
77 testRE(re, " \" 'foo' ", false); | |
78 testRE(re, " ' \"foo\" ", false); | |
79 testRE(re, " 'foo\" ", false); | |
80 testRE(re, " \"foo' ", false); | |
81 execRE(re, " 'foo' ", [" 'foo'", "'", "foo"]); | |
82 execRE(re, ' "foo" ', [' "foo"', '"', 'foo']); | |
83 | |
84 // Captures are cleared on backtrack past the look-ahead. | |
85 re = new RegExp(r"^(?:(?=(.))a|b)\1$"); | |
86 testRE(re, "aa", true); | |
87 testRE(re, "b", true); | |
88 testRE(re, "bb", false); | |
89 testRE(re, "a", false); | |
90 execRE(re, "aa", ["aa", "a"]); | |
91 execRE(re, "b", ["b", null]); | |
92 | |
93 re = new RegExp(r"^(?=(.)(?=(.)\1\2)\2\1)\1\2"); | |
94 testRE(re, "abab", true); | |
95 testRE(re, "ababxxxxxxxx", true); | |
96 testRE(re, "aba", false); | |
97 execRE(re, "abab", ["ab", "a", "b"]); | |
98 | |
99 re = new RegExp(r"^(?:(?=(.))a|b|c)$"); | |
100 testRE(re, "a", true); | |
101 testRE(re, "b", true); | |
102 testRE(re, "c", true); | |
103 testRE(re, "d", false); | |
104 execRE(re, "a", ["a", "a"]); | |
105 execRE(re, "b", ["b", null]); | |
106 execRE(re, "c", ["c", null]); | |
107 | |
108 execRE(new RegExp(r"^(?=(b))b"), "b", ["b", "b"]); | |
109 execRE(new RegExp(r"^(?:(?=(b))|a)b"), "ab", ["ab", null]); | |
110 execRE(new RegExp(r"^(?:(?=(b)(?:(?=(c))|d))|)bd"), "bd", ["bd", "b", null]); | |
111 | |
112 // Test of Negative Look-Ahead. | |
113 | |
114 re = new RegExp(r"(?!x)."); | |
115 testRE(re, "y", true); | |
116 testRE(re, "x", false); | |
117 execRE(re, "y", ["y"]); | |
118 | |
119 re = new RegExp(r"(?!(\d))|\d"); | |
120 testRE(re, "4", true); | |
121 execRE(re, "4", ["4", null]); | |
122 execRE(re, "x", ["", null]); | |
123 | |
124 // Test mixed nested look-ahead with captures. | |
125 | |
126 re = new RegExp(r"^(?=(x)(?=(y)))"); | |
127 testRE(re, "xy", true); | |
128 testRE(re, "xz", false); | |
129 execRE(re, "xy", ["", "x", "y"]); | |
130 | |
131 re = new RegExp(r"^(?!(x)(?!(y)))"); | |
132 testRE(re, "xy", true); | |
133 testRE(re, "xz", false); | |
134 execRE(re, "xy", ["", null, null]); | |
135 | |
136 re = new RegExp(r"^(?=(x)(?!(y)))"); | |
137 testRE(re, "xz", true); | |
138 testRE(re, "xy", false); | |
139 execRE(re, "xz", ["", "x", null]); | |
140 | |
141 re = new RegExp(r"^(?!(x)(?=(y)))"); | |
142 testRE(re, "xz", true); | |
143 testRE(re, "xy", false); | |
144 execRE(re, "xz", ["", null, null]); | |
145 | |
146 re = new RegExp(r"^(?=(x)(?!(y)(?=(z))))"); | |
147 testRE(re, "xaz", true); | |
148 testRE(re, "xya", true); | |
149 testRE(re, "xyz", false); | |
150 testRE(re, "a", false); | |
151 execRE(re, "xaz", ["", "x", null, null]); | |
152 execRE(re, "xya", ["", "x", null, null]); | |
153 | |
154 re = new RegExp(r"^(?!(x)(?=(y)(?!(z))))"); | |
155 testRE(re, "a", true); | |
156 testRE(re, "xa", true); | |
157 testRE(re, "xyz", true); | |
158 testRE(re, "xya", false); | |
159 execRE(re, "a", ["", null, null, null]); | |
160 execRE(re, "xa", ["", null, null, null]); | |
161 execRE(re, "xyz", ["", null, null, null]); | |
162 } | |
OLD | NEW |