Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: tests/corelib_strong/regexp/global_test.dart

Issue 2989863002: Migrated test block 19 to Dart 2.0. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. All rights reserved.
2 // Copyright 2012 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 var str = "ABX X";
34 str = str.replaceAll(new RegExp(r"(\w)?X"), "c");
35 assertEquals("Ac c", str);
36
37 // Test zero-length matches.
38 str = "Als Gregor Samsa eines Morgens";
39 str = str.replaceAll(new RegExp(r"\b"), "/");
40 assertEquals("/Als/ /Gregor/ /Samsa/ /eines/ /Morgens/", str);
41
42 // Test zero-length matches that have non-zero-length sub-captures.
43 str = "It was a pleasure to burn.";
44 str = str.replaceAllMapped(
45 new RegExp(r"(?=(\w+))\b"), (Match m) => m.group(1).length.toString());
46 assertEquals("2It 3was 1a 8pleasure 2to 4burn.", str);
47
48 // Test multiple captures.
49 str = "Try not. Do, or do not. There is no try.";
50 str = str.replaceAllMapped(
51 new RegExp(r"(not?)|(do)|(try)", caseSensitive: false), (m) {
52 if (m.group(1) != null) return "-";
53 if (m.group(2) != null) return "+";
54 if (m.group(3) != null) return "=";
55 });
56 assertEquals("= -. +, or + -. There is - =.", str);
57
58 // Test multiple alternate captures.
59 str = "FOUR LEGS GOOD, TWO LEGS BAD!";
60 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) LEGS (GOOD|BAD)"), (m) {
61 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD");
62 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD");
63 return m.group(0).length - 10;
64 });
65 assertEquals("4, 2!", str);
66
67 // The same tests with UC16.
68
69 //Test that an optional capture is cleared between two matches.
70 str = "AB\u1234 \u1234";
71 str = str.replaceAll(new RegExp(r"(\w)?\u1234"), "c");
72 assertEquals("Ac c", str);
73
74 // Test zero-length matches.
75 str = "Als \u2623\u2642 eines Morgens";
76 str = str.replaceAll(new RegExp(r"\b"), "/");
77
78 // Test zero-length matches that have non-zero-length sub-captures.
79 str = "It was a pleasure to \u70e7.";
80 str = str.replaceAllMapped(
81 new RegExp(r"(?=(\w+))\b"), (m) => "${m.group(1).length}");
82 assertEquals("2It 3was 1a 8pleasure 2to \u70e7.", str);
83
84 // Test multiple captures.
85 str = "Try not. D\u26aa, or d\u26aa not. There is no try.";
86 str = str.replaceAllMapped(
87 new RegExp(r"(not?)|(d\u26aa)|(try)", caseSensitive: false), (m) {
88 if (m.group(1) != null) return "-";
89 if (m.group(2) != null) return "+";
90 if (m.group(3) != null) return "=";
91 });
92 assertEquals("= -. +, or + -. There is - =.", str);
93
94 // Test multiple alternate captures.
95 str = "FOUR \u817f GOOD, TWO \u817f BAD!";
96 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) \u817f (GOOD|BAD)"), (m) {
97 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD");
98 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD");
99 return m.group(0).length - 7;
100 });
101 assertEquals("4, 2!", str);
102
103 // Test capture that is a real substring.
104 str = "Beasts of England, beasts of Ireland";
105 str = str.replaceAll(new RegExp(r"(.*)"), '~');
106 assertEquals("~~", str);
107
108 // Test zero-length matches that have non-zero-length sub-captures that do not
109 // start at the match start position.
110 str = "up up up up";
111 str = str.replaceAllMapped(
112 new RegExp(r"\b(?=u(p))"), (m) => "${m.group(1).length}");
113
114 assertEquals("1up 1up 1up 1up", str);
115
116 // Create regexp that has a *lot* of captures.
117 var re_string = "(a)";
118 for (var i = 0; i < 500; i++) {
119 re_string = "(" + re_string + ")";
120 }
121 re_string = re_string + "1";
122 // re_string = "(((...((a))...)))1"
123
124 var regexps = new List();
125 var last_match_expectations = new List();
126 var first_capture_expectations = new List();
127
128 // Atomic regexp.
129 regexps.add(new RegExp(r"a1"));
130 last_match_expectations.add("a1");
131 first_capture_expectations.add("");
132 // Small regexp (no capture);
133 regexps.add(new RegExp(r"\w1"));
134 last_match_expectations.add("a1");
135 first_capture_expectations.add("");
136 // Small regexp (one capture).
137 regexps.add(new RegExp(r"(a)1"));
138 last_match_expectations.add("a1");
139 first_capture_expectations.add("a");
140 // Large regexp (a lot of captures).
141 regexps.add(new RegExp(re_string));
142 last_match_expectations.add("a1");
143 first_capture_expectations.add("a");
144
145 dynamic test_replace(result_expectation, subject, regexp, replacement) {
146 for (var i = 0; i < regexps.length; i++) {
147 // Conduct tests.
148 assertEquals(
149 result_expectation, subject.replaceAll(regexps[i], replacement));
150 }
151 }
152
153 // Test for different number of matches.
154 for (var m = 0; m < 33; m++) {
155 // Create string that matches m times.
156 var subject = "";
157 var test_1_expectation = "";
158 var test_2_expectation = "";
159 var test_3_expectation = (m == 0) ? null : new List();
160 for (var i = 0; i < m; i++) {
161 subject += "a11";
162 test_1_expectation += "x1";
163 test_2_expectation += "1";
164 test_3_expectation.add("a1");
165 }
166
167 // Test 1a: String.replace with string.
168 test_replace(test_1_expectation, subject, new RegExp(r"a1"), "x");
169
170 // Test 2a: String.replace with empty string.
171 test_replace(test_2_expectation, subject, new RegExp(r"a1"), "");
172 }
173
174 // Test String hashing (compiling regular expression includes hashing).
175 var crosscheck = "\x80";
176 for (var i = 0; i < 12; i++) crosscheck += crosscheck;
177 new RegExp(crosscheck);
178
179 var subject = "ascii~only~string~here~";
180 var replacement = "\x80";
181 var result = subject.replaceAll(new RegExp(r"~"), replacement);
182 for (var i = 0; i < 5; i++) result += result;
183 new RegExp(result);
184 }
OLDNEW
« no previous file with comments | « tests/corelib_strong/regexp/find-first-asserted_test.dart ('k') | tests/corelib_strong/regexp/indexof_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698