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

Side by Side Diff: tests/language/regex/global_test.dart

Issue 539153002: Port and integrate the irregexp engine from V8 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed Ivan's comments. Created 6 years, 2 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'util.dart';
6 import 'package:expect/expect.dart';
7
8 void main() {
9 var str = "ABX X";
10 str = str.replaceAll(new RegExp(r"(\w)?X"), "c");
11 assertEquals("Ac c", str);
12
13 // Test zero-length matches.
14 str = "Als Gregor Samsa eines Morgens";
15 str = str.replaceAll(new RegExp(r"\b"), "/");
16 assertEquals("/Als/ /Gregor/ /Samsa/ /eines/ /Morgens/", str);
17
18 // Test zero-length matches that have non-zero-length sub-captures.
19 str = "It was a pleasure to burn.";
20 str = str.replaceAllMapped(new RegExp(r"(?=(\w+))\b"), (Match m) => m.group(1) .length.toString());
21 assertEquals("2It 3was 1a 8pleasure 2to 4burn.", str);
22
23 // Test multiple captures.
24 str = "Try not. Do, or do not. There is no try.";
25 str = str.replaceAllMapped(new RegExp(r"(not?)|(do)|(try)", caseSensitive: fal se),
26 (m) {
27 if (m.group(1) != null) return "-";
28 if (m.group(2) != null) return "+";
29 if (m.group(3) != null) return "=";
30 });
31 assertEquals("= -. +, or + -. There is - =.", str);
32
33 // Test multiple alternate captures.
34 str = "FOUR LEGS GOOD, TWO LEGS BAD!";
35 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) LEGS (GOOD|BAD)"),
36 (m) {
37 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD") ;
38 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD");
39 return m.group(0).length - 10;
40 });
41 assertEquals("4, 2!", str);
42
43
44 // The same tests with UC16.
45
46 //Test that an optional capture is cleared between two matches.
47 str = "AB\u1234 \u1234";
48 str = str.replaceAll(new RegExp(r"(\w)?\u1234"), "c");
49 assertEquals("Ac c", str);
50
51 // Test zero-length matches.
52 str = "Als \u2623\u2642 eines Morgens";
53 str = str.replaceAll(new RegExp(r"\b"), "/");
54
55 // Test zero-length matches that have non-zero-length sub-captures.
56 str = "It was a pleasure to \u70e7.";
57 str = str.replaceAllMapped(new RegExp(r"(?=(\w+))\b"), (m) => "${m.group(1).le ngth}");
58 assertEquals("2It 3was 1a 8pleasure 2to \u70e7.", str);
59
60 // Test multiple captures.
61 str = "Try not. D\u26aa, or d\u26aa not. There is no try.";
62 str = str.replaceAllMapped(new RegExp(r"(not?)|(d\u26aa)|(try)", caseSensitive : false),
63 (m) {
64 if (m.group(1) != null) return "-";
65 if (m.group(2) != null) return "+";
66 if (m.group(3) != null) return "=";
67 });
68 assertEquals("= -. +, or + -. There is - =.", str);
69
70 // Test multiple alternate captures.
71 str = "FOUR \u817f GOOD, TWO \u817f BAD!";
72 str = str.replaceAllMapped(new RegExp(r"(FOUR|TWO) \u817f (GOOD|BAD)"),
73 (m) {
74 if (m.group(1) == "FOUR") assertTrue(m.group(2) == "GOOD") ;
75 if (m.group(1) == "TWO") assertTrue(m.group(2) == "BAD");
76 return m.group(0).length - 7;
77 });
78 assertEquals("4, 2!", str);
79
80 // Test capture that is a real substring.
81 str = "Beasts of England, beasts of Ireland";
82 str = str.replaceAll(new RegExp(r"(.*)"), '~');
83 assertEquals("~~", str);
84
85 // Test zero-length matches that have non-zero-length sub-captures that do not
86 // start at the match start position.
87 str = "up up up up";
88 str = str.replaceAllMapped(new RegExp(r"\b(?=u(p))"), (m) => "${m.group(1).len gth}");
89
90 assertEquals("1up 1up 1up 1up", str);
91
92
93 // Create regexp that has a *lot* of captures.
94 var re_string = "(a)";
95 for (var i = 0; i < 500; i++) {
96 re_string = "(" + re_string + ")";
97 }
98 re_string = re_string + "1";
99 // re_string = "(((...((a))...)))1"
100
101 var regexps = new List();
102 var last_match_expectations = new List();
103 var first_capture_expectations = new List();
104
105 // Atomic regexp.
106 regexps.add(new RegExp(r"a1"));
107 last_match_expectations.add("a1");
108 first_capture_expectations.add("");
109 // Small regexp (no capture);
110 regexps.add(new RegExp(r"\w1"));
111 last_match_expectations.add("a1");
112 first_capture_expectations.add("");
113 // Small regexp (one capture).
114 regexps.add(new RegExp(r"(a)1"));
115 last_match_expectations.add("a1");
116 first_capture_expectations.add("a");
117 // Large regexp (a lot of captures).
118 regexps.add(new RegExp(re_string));
119 last_match_expectations.add("a1");
120 first_capture_expectations.add("a");
121
122 dynamic test_replace(result_expectation,
123 subject,
124 regexp,
125 replacement) {
126 for (var i = 0; i < regexps.length; i++) {
127 // Conduct tests.
128 assertEquals(result_expectation, subject.replaceAll(regexps[i], replacemen t));
129 }
130 }
131
132
133 // Test for different number of matches.
134 for (var m = 0; m < 33; m++) {
135 // Create string that matches m times.
136 var subject = "";
137 var test_1_expectation = "";
138 var test_2_expectation = "";
139 var test_3_expectation = (m == 0) ? null : new List();
140 for (var i = 0; i < m; i++) {
141 subject += "a11";
142 test_1_expectation += "x1";
143 test_2_expectation += "1";
144 test_3_expectation.add("a1");
145 }
146
147 // Test 1a: String.replace with string.
148 test_replace(test_1_expectation, subject, new RegExp(r"a1"), "x");
149
150 // Test 2a: String.replace with empty string.
151 test_replace(test_2_expectation, subject, new RegExp(r"a1"), "");
152 }
153
154
155 // Test String hashing (compiling regular expression includes hashing).
156 var crosscheck = "\x80";
157 for (var i = 0; i < 12; i++) crosscheck += crosscheck;
158 new RegExp(crosscheck);
159
160 var subject = "ascii~only~string~here~";
161 var replacement = "\x80";
162 var result = subject.replaceAll(new RegExp(r"~"), replacement);
163 for (var i = 0; i < 5; i++) result += result;
164 new RegExp(result);
165 }
OLDNEW
« no previous file with comments | « tests/language/regex/find-first-asserted_test.dart ('k') | tests/language/regex/indexof_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698