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

Side by Side Diff: tests/corelib/string_split_test.dart

Issue 677013002: Make dart2js String.split match the VM version. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Tweak docs. Created 6 years, 1 month 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
« no previous file with comments | « tests/corelib/string_split_reg_exp_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import "package:expect/expect.dart"; 5 import "package:expect/expect.dart";
6 6
7 class StringSplitTest { 7 main() {
8 static testMain() { 8 testSplitString();
9 var list = "a b c".split(" "); 9 testSplitRegExp();
10 Expect.equals(3, list.length); 10 testSplitPattern();
11 Expect.equals("a", list[0]);
12 Expect.equals("b", list[1]);
13 Expect.equals("c", list[2]);
14
15 list = "adbdc".split("d");
16 Expect.equals(3, list.length);
17 Expect.equals("a", list[0]);
18 Expect.equals("b", list[1]);
19 Expect.equals("c", list[2]);
20
21 list = "addbddc".split("dd");
22 Expect.equals(3, list.length);
23 Expect.equals("a", list[0]);
24 Expect.equals("b", list[1]);
25 Expect.equals("c", list[2]);
26
27 list = "abc".split(" ");
28 Expect.equals(1, list.length);
29 Expect.equals("abc", list[0]);
30
31 list = "abc".split("");
32 Expect.equals(3, list.length);
33 Expect.equals("a", list[0]);
34 Expect.equals("b", list[1]);
35 Expect.equals("c", list[2]);
36
37 list = " ".split(" ");
38 Expect.equals(4, list.length);
39 Expect.equals("", list[0]);
40 Expect.equals("", list[1]);
41 Expect.equals("", list[2]);
42 Expect.equals("", list[3]);
43 }
44 } 11 }
45 12
46 main() { 13
47 StringSplitTest.testMain(); 14 testSplit(List expect, String string, Pattern pattern) {
15 String patternString;
16 if (pattern is String) {
17 patternString = '"$pattern"';
18 } else if (pattern is RegExp) {
19 patternString = "/${pattern.pattern}/";
20 } else {
21 patternString = pattern.toString();
22 }
23 Expect.listEquals(expect, string.split(pattern),
24 '"$string".split($patternString)');
48 } 25 }
26
27 /** String patterns. */
28 void testSplitString() {
29 // Normal match.
30 testSplit(["a", "b", "c"], "a b c", " ");
31 testSplit(["a", "b", "c"], "adbdc", "d");
32 testSplit(["a", "b", "c"], "addbddc", "dd");
33 // No match.
34 testSplit(["abc"], "abc", " ");
35 testSplit(["a"], "a", "b");
36 testSplit([""], "", "b");
37 // Empty match matches everywhere except start/end.
38 testSplit(["a", "b", "c"], "abc", "");
39 // All empty parts.
40 testSplit(["", "", "", "", ""], "aaaa", "a");
41 testSplit(["", "", "", "", ""], " ", " ");
42 testSplit(["", ""], "a", "a");
43 // No overlapping matches. Match as early as possible.
44 testSplit(["", "", "", "a"], "aaaaaaa", "aa");
45 // Cannot split the empty string.
46 testSplit([], "", ""); // Match.
47 testSplit([""], "", "a"); // No match.
48 }
49
50 /** RegExp patterns. */
51 void testSplitRegExp() {
52 testSplitWithRegExp((s) => new RegExp(s));
53 }
54
55 /** Non-String, non-RegExp patterns. */
56 void testSplitPattern() {
57 testSplitWithRegExp((s) => new RegExpWrap(s));
58 }
59
60 void testSplitWithRegExp(makePattern) {
61 testSplit(["a", "b", "c"], "a b c", makePattern(r" "));
62
63 testSplit(["a", "b", "c"], "adbdc", makePattern(r"[dz]"));
64
65 testSplit(["a", "b", "c"], "addbddc", makePattern(r"dd"));
66
67 testSplit(["abc"], "abc", makePattern(r"b$"));
68
69 testSplit(["a", "b", "c"], "abc", makePattern(r""));
70
71 testSplit(["", "", "", ""], " ", makePattern(r"[ ]"));
72
73 // Non-zero-length match at end.
74 testSplit(["aa", ""], "aaa", makePattern(r"a$"));
75
76 // Zero-length match at end.
77 testSplit(["aaa"], "aaa", makePattern(r"$"));
78
79 // Non-zero-length match at start.
80 testSplit(["", "aa"], "aaa", makePattern(r"^a"));
81
82 // Zero-length match at start.
83 testSplit(["aaa"], "aaa", makePattern(r"^"));
84
85 // Picks first match, not longest or shortest.
86 testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|aaa"));
87
88 testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|"));
89
90 testSplit(["", "", "a"], "aaaaaaa", makePattern(r"aaa|aa"));
91
92 // Zero-width match depending on the following.
93 testSplit(["a", "bc"], "abc", makePattern(r"(?=[ab])"));
94
95 testSplit(["a", "b", "c"], "abc", makePattern(r"(?!^)"));
96
97 // Cannot split empty string.
98 testSplit([], "", makePattern(r""));
99
100 testSplit([], "", makePattern(r"(?:)"));
101
102 testSplit([], "", makePattern(r"$|(?=.)"));
103
104 testSplit([""], "", makePattern(r"a"));
105
106 // Can split singleton string if it matches.
107 testSplit(["", ""], "a", makePattern(r"a"));
108
109 testSplit(["a"], "a", makePattern(r"b"));
110
111 // Do not include captures.
112 testSplit(["a", "", "a"], "abba", makePattern(r"(b)"));
113
114 testSplit(["a", "a"], "abba", makePattern(r"(bb)"));
115
116 testSplit(["a", "a"], "abba", makePattern(r"(b*)"));
117
118 testSplit(["a", "a"], "aa", makePattern(r"(b*)"));
119
120 // But captures are still there, and do work with backreferences.
121 testSplit(["a", "cba"], "abcba", makePattern(r"([bc])(?=.*\1)"));
122 }
123
124 // A Pattern implementation with the same capabilities as a RegExp, but not
125 // directly recognizable as a RegExp.
126 class RegExpWrap implements Pattern {
127 final regexp;
128 RegExpWrap(String source) : regexp = new RegExp(source);
129 Iterable<Match> allMatches(String string, [int start = 0]) =>
130 regexp.allMatches(string, start);
131
132 Match matchAsPrefix(String string, [int start = 0]) =>
133 regexp.matchAsPrefix(string, start);
134
135 String toString() => "Wrap(/${regexp.pattern}/)";
136 }
OLDNEW
« no previous file with comments | « tests/corelib/string_split_reg_exp_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698