mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 10:48:25 +00:00
Add tests for String.replaceAll with general Pattern
Change-Id: Ic680dc229e014f3323945bd509184d501c381041 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/226983 Reviewed-by: Lasse Nielsen <lrn@google.com> Commit-Queue: Stephen Adams <sra@google.com>
This commit is contained in:
parent
bd04424cda
commit
7504ce34b0
144
tests/corelib/string_replace_all_common.dart
Normal file
144
tests/corelib/string_replace_all_common.dart
Normal file
|
@ -0,0 +1,144 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
testAll(Pattern Function(Pattern) wrap) {
|
||||
testReplaceAll(wrap);
|
||||
testReplaceAllMapped(wrap);
|
||||
testSplitMapJoin(wrap);
|
||||
}
|
||||
|
||||
testReplaceAll(Pattern Function(Pattern) wrap) {
|
||||
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll(wrap("b"), "XX"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll(wrap("a"), "XX"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabdaXX", "abcabdae".replaceAll(wrap("e"), "XX"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAll(wrap("f"), "XX"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("toto", "fromfrom".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAll(wrap("a"), ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll(wrap("from"), ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll(wrap(""), "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll(wrap(""), "to"));
|
||||
|
||||
// Pattern strings containing RegExp metacharacters - these are not
|
||||
// interpreted as RegExps.
|
||||
Expect.equals(r"$$", "||".replaceAll(wrap("|"), r"$"));
|
||||
Expect.equals(r"$$$$", "||".replaceAll(wrap("|"), r"$$"));
|
||||
Expect.equals(r"x$|x", "x|.|x".replaceAll(wrap("|."), r"$"));
|
||||
Expect.equals(r"$$", "..".replaceAll(wrap("."), r"$"));
|
||||
Expect.equals(r"[$$$$]", "[..]".replaceAll(wrap("."), r"$$"));
|
||||
Expect.equals(r"[$]", "[..]".replaceAll(wrap(".."), r"$"));
|
||||
Expect.equals(r"$$", r"\\".replaceAll(wrap(r"\"), r"$"));
|
||||
}
|
||||
|
||||
testReplaceAllMapped(Pattern Function(Pattern) wrap) {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped(wrap("b"), mark));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped(wrap("a"), mark));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped(wrap("e"), mark));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAllMapped(wrap("f"), mark));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals(
|
||||
"[from][from]", "fromfrom".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAllMapped(wrap("a"), (m) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceAllMapped(wrap("from"), (m) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("[]", "".replaceAllMapped(wrap(""), mark));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped(wrap(""), mark));
|
||||
}
|
||||
|
||||
testSplitMapJoin(Pattern Function(Pattern) wrap) {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
String rest(String s) => "<${s}>";
|
||||
|
||||
Expect.equals("<a>[b]<ca>[b]<dae>",
|
||||
"abcabdae".splitMapJoin(wrap("b"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
|
||||
"abcabdae".splitMapJoin(wrap("a"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("<abcabda>[e]<>",
|
||||
"abcabdae".splitMapJoin(wrap("e"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("<abcabdae>",
|
||||
"abcabdae".splitMapJoin(wrap("f"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals(
|
||||
"<>", "".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("<fro>",
|
||||
"fro".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("<>[from]<>[from]<>",
|
||||
"fromfrom".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals(
|
||||
"<>[]<>", "".splitMapJoin(wrap(""), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
|
||||
"ABC".splitMapJoin(wrap(""), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with only onMatch.
|
||||
Expect.equals(
|
||||
"[a]bc[a]bd[a]e", "abcabdae".splitMapJoin(wrap("a"), onMatch: mark));
|
||||
|
||||
// Test with only onNonMatch
|
||||
Expect.equals(
|
||||
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin(wrap("a"), onNonMatch: rest));
|
||||
}
|
25
tests/corelib/string_replace_all_pattern_test.dart
Normal file
25
tests/corelib/string_replace_all_pattern_test.dart
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "string_replace_all_common.dart";
|
||||
|
||||
main() {
|
||||
testAll(Wrapper.wrap);
|
||||
}
|
||||
|
||||
/// A wrapper that is not recognizable as a String or RegExp.
|
||||
class Wrapper implements Pattern {
|
||||
final Pattern _pattern;
|
||||
Wrapper(this._pattern);
|
||||
|
||||
static Pattern wrap(Pattern p) => Wrapper(p);
|
||||
|
||||
Iterable<Match> allMatches(String string, [int start = 0]) =>
|
||||
_pattern.allMatches(string, start);
|
||||
|
||||
Match? matchAsPrefix(String string, [int start = 0]) =>
|
||||
_pattern.matchAsPrefix(string, start);
|
||||
|
||||
String toString() => "Wrap($_pattern)";
|
||||
}
|
|
@ -1,140 +1,9 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
testReplaceAll() {
|
||||
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll("b", "XX"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll("a", "XX"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabdaXX", "abcabdae".replaceAll("e", "XX"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAll("f", "XX"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll("from", "to"));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("toto", "fromfrom".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAll("a", ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
|
||||
|
||||
// Pattern strings containing RegExp metacharacters - these are not
|
||||
// interpreted as RegExps.
|
||||
Expect.equals(r"$$", "||".replaceAll("|", r"$"));
|
||||
Expect.equals(r"$$$$", "||".replaceAll("|", r"$$"));
|
||||
Expect.equals(r"x$|x", "x|.|x".replaceAll("|.", r"$"));
|
||||
Expect.equals(r"$$", "..".replaceAll(".", r"$"));
|
||||
Expect.equals(r"[$$$$]", "[..]".replaceAll(".", r"$$"));
|
||||
Expect.equals(r"[$]", "[..]".replaceAll("..", r"$"));
|
||||
Expect.equals(r"$$", r"\\".replaceAll(r"\", r"$"));
|
||||
}
|
||||
|
||||
testReplaceAllMapped() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped("b", mark));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped("a", mark));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped("e", mark));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAllMapped("f", mark));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("[from][from]", "fromfrom".replaceAllMapped("from", mark));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAllMapped("a", (m) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceAllMapped("from", (m) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("[]", "".replaceAllMapped("", mark));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped("", mark));
|
||||
}
|
||||
|
||||
testSplitMapJoin() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
String wrap(String s) => "<${s}>";
|
||||
|
||||
Expect.equals("<a>[b]<ca>[b]<dae>",
|
||||
"abcabdae".splitMapJoin("b", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
|
||||
"abcabdae".splitMapJoin("a", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("<abcabda>[e]<>",
|
||||
"abcabdae".splitMapJoin("e", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("<abcabdae>",
|
||||
"abcabdae".splitMapJoin("f", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("<>", "".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals(
|
||||
"<fro>", "fro".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("<>[from]<>[from]<>",
|
||||
"fromfrom".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("<>[]<>", "".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
|
||||
"ABC".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with only onMatch.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".splitMapJoin("a", onMatch: mark));
|
||||
|
||||
// Test with only onNonMatch
|
||||
Expect.equals(
|
||||
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin("a", onNonMatch: wrap));
|
||||
}
|
||||
import "string_replace_all_common.dart";
|
||||
|
||||
main() {
|
||||
testReplaceAll();
|
||||
testReplaceAllMapped();
|
||||
testSplitMapJoin();
|
||||
testAll((pattern) => pattern); // unwrapped
|
||||
}
|
||||
|
|
146
tests/corelib_2/string_replace_all_common.dart
Normal file
146
tests/corelib_2/string_replace_all_common.dart
Normal file
|
@ -0,0 +1,146 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// @dart = 2.9
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
testAll(Pattern Function(Pattern) wrap) {
|
||||
testReplaceAll(wrap);
|
||||
testReplaceAllMapped(wrap);
|
||||
testSplitMapJoin(wrap);
|
||||
}
|
||||
|
||||
testReplaceAll(Pattern Function(Pattern) wrap) {
|
||||
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll(wrap("b"), "XX"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll(wrap("a"), "XX"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabdaXX", "abcabdae".replaceAll(wrap("e"), "XX"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAll(wrap("f"), "XX"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("toto", "fromfrom".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll(wrap("from"), "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAll(wrap("a"), ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll(wrap("from"), ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll(wrap(""), "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll(wrap(""), "to"));
|
||||
|
||||
// Pattern strings containing RegExp metacharacters - these are not
|
||||
// interpreted as RegExps.
|
||||
Expect.equals(r"$$", "||".replaceAll(wrap("|"), r"$"));
|
||||
Expect.equals(r"$$$$", "||".replaceAll(wrap("|"), r"$$"));
|
||||
Expect.equals(r"x$|x", "x|.|x".replaceAll(wrap("|."), r"$"));
|
||||
Expect.equals(r"$$", "..".replaceAll(wrap("."), r"$"));
|
||||
Expect.equals(r"[$$$$]", "[..]".replaceAll(wrap("."), r"$$"));
|
||||
Expect.equals(r"[$]", "[..]".replaceAll(wrap(".."), r"$"));
|
||||
Expect.equals(r"$$", r"\\".replaceAll(wrap(r"\"), r"$"));
|
||||
}
|
||||
|
||||
testReplaceAllMapped(Pattern Function(Pattern) wrap) {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped(wrap("b"), mark));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped(wrap("a"), mark));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped(wrap("e"), mark));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAllMapped(wrap("f"), mark));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals(
|
||||
"[from][from]", "fromfrom".replaceAllMapped(wrap("from"), mark));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAllMapped(wrap("a"), (m) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceAllMapped(wrap("from"), (m) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("[]", "".replaceAllMapped(wrap(""), mark));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped(wrap(""), mark));
|
||||
}
|
||||
|
||||
testSplitMapJoin(Pattern Function(Pattern) wrap) {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
String rest(String s) => "<${s}>";
|
||||
|
||||
Expect.equals("<a>[b]<ca>[b]<dae>",
|
||||
"abcabdae".splitMapJoin(wrap("b"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
|
||||
"abcabdae".splitMapJoin(wrap("a"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("<abcabda>[e]<>",
|
||||
"abcabdae".splitMapJoin(wrap("e"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("<abcabdae>",
|
||||
"abcabdae".splitMapJoin(wrap("f"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals(
|
||||
"<>", "".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("<fro>",
|
||||
"fro".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("<>[from]<>[from]<>",
|
||||
"fromfrom".splitMapJoin(wrap("from"), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals(
|
||||
"<>[]<>", "".splitMapJoin(wrap(""), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
|
||||
"ABC".splitMapJoin(wrap(""), onMatch: mark, onNonMatch: rest));
|
||||
|
||||
// Test with only onMatch.
|
||||
Expect.equals(
|
||||
"[a]bc[a]bd[a]e", "abcabdae".splitMapJoin(wrap("a"), onMatch: mark));
|
||||
|
||||
// Test with only onNonMatch
|
||||
Expect.equals(
|
||||
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin(wrap("a"), onNonMatch: rest));
|
||||
}
|
27
tests/corelib_2/string_replace_all_pattern_test.dart
Normal file
27
tests/corelib_2/string_replace_all_pattern_test.dart
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// @dart = 2.9
|
||||
|
||||
import "string_replace_all_common.dart";
|
||||
|
||||
main() {
|
||||
testAll(Wrapper.wrap);
|
||||
}
|
||||
|
||||
/// A wrapper that is not recognizable as a String or RegExp.
|
||||
class Wrapper implements Pattern {
|
||||
final Pattern _pattern;
|
||||
Wrapper(this._pattern);
|
||||
|
||||
static Pattern wrap(Pattern p) => Wrapper(p);
|
||||
|
||||
Iterable<Match> allMatches(String string, [int start = 0]) =>
|
||||
_pattern.allMatches(string, start);
|
||||
|
||||
Match matchAsPrefix(String string, [int start = 0]) =>
|
||||
_pattern.matchAsPrefix(string, start);
|
||||
|
||||
String toString() => "Wrap($_pattern)";
|
||||
}
|
|
@ -1,142 +1,11 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// @dart = 2.9
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
testReplaceAll() {
|
||||
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll("b", "XX"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll("a", "XX"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabdaXX", "abcabdae".replaceAll("e", "XX"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAll("f", "XX"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll("from", "to"));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("toto", "fromfrom".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAll("a", ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
|
||||
|
||||
// Pattern strings containing RegExp metacharacters - these are not
|
||||
// interpreted as RegExps.
|
||||
Expect.equals(r"$$", "||".replaceAll("|", r"$"));
|
||||
Expect.equals(r"$$$$", "||".replaceAll("|", r"$$"));
|
||||
Expect.equals(r"x$|x", "x|.|x".replaceAll("|.", r"$"));
|
||||
Expect.equals(r"$$", "..".replaceAll(".", r"$"));
|
||||
Expect.equals(r"[$$$$]", "[..]".replaceAll(".", r"$$"));
|
||||
Expect.equals(r"[$]", "[..]".replaceAll("..", r"$"));
|
||||
Expect.equals(r"$$", r"\\".replaceAll(r"\", r"$"));
|
||||
}
|
||||
|
||||
testReplaceAllMapped() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped("b", mark));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped("a", mark));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped("e", mark));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAllMapped("f", mark));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("[from][from]", "fromfrom".replaceAllMapped("from", mark));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAllMapped("a", (m) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceAllMapped("from", (m) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("[]", "".replaceAllMapped("", mark));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped("", mark));
|
||||
}
|
||||
|
||||
testSplitMapJoin() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
String wrap(String s) => "<${s}>";
|
||||
|
||||
Expect.equals("<a>[b]<ca>[b]<dae>",
|
||||
"abcabdae".splitMapJoin("b", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
|
||||
"abcabdae".splitMapJoin("a", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("<abcabda>[e]<>",
|
||||
"abcabdae".splitMapJoin("e", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("<abcabdae>",
|
||||
"abcabdae".splitMapJoin("f", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("<>", "".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals(
|
||||
"<fro>", "fro".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("<>[from]<>[from]<>",
|
||||
"fromfrom".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("<>[]<>", "".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
|
||||
"ABC".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with only onMatch.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".splitMapJoin("a", onMatch: mark));
|
||||
|
||||
// Test with only onNonMatch
|
||||
Expect.equals(
|
||||
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin("a", onNonMatch: wrap));
|
||||
}
|
||||
import "string_replace_all_common.dart";
|
||||
|
||||
main() {
|
||||
testReplaceAll();
|
||||
testReplaceAllMapped();
|
||||
testSplitMapJoin();
|
||||
testAll((pattern) => pattern); // unwrapped
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue