OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 |
| 7 main() { |
| 8 testSplitString(); |
| 9 testSplitRegExp(); |
| 10 testSplitPattern(); |
| 11 } |
| 12 |
| 13 |
| 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)'); |
| 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 } |
OLD | NEW |