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

Unified Diff: tests/corelib/string_replace_test.dart

Issue 920453002: Add String.replaceFirstMapped. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: With change to _interpolate methods. Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: tests/corelib/string_replace_test.dart
diff --git a/tests/corelib/string_replace_test.dart b/tests/corelib/string_replace_test.dart
index df426fa545b3480dc5f96a2bb58823666dd93474..b8486ae0363e37df4c6f5b4888bea4c9db6a3d2a 100644
--- a/tests/corelib/string_replace_test.dart
+++ b/tests/corelib/string_replace_test.dart
@@ -4,92 +4,215 @@
import "package:expect/expect.dart";
-class StringReplaceTest {
- static testMain() {
- Expect.equals(
- "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to"));
+main() {
+ // Test replaceFirst.
+ Expect.equals(
+ "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to"));
+
+ // Test with the replaced string at the begining.
+ Expect.equals(
+ "toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to"));
+
+ // Test with the replaced string at the end.
+ Expect.equals(
+ "toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to"));
+
+ // Test when there are no occurence of the string to replace.
+ Expect.equals("ABC", "ABC".replaceFirst("from", "to"));
+
+ // Test when the string to change is the empty string.
+ Expect.equals("", "".replaceFirst("from", "to"));
+
+ // Test when the string to change is a substring of the string to
+ // replace.
+ Expect.equals("fro", "fro".replaceFirst("from", "to"));
+
+ // Test when the string to change is the replaced string.
+ Expect.equals("to", "from".replaceFirst("from", "to"));
+
+ // Test when the string to change is the replacement string.
+ Expect.equals("to", "to".replaceFirst("from", "to"));
+
+ // Test replacing by the empty string.
+ Expect.equals("", "from".replaceFirst("from", ""));
+ Expect.equals("AB", "AfromB".replaceFirst("from", ""));
+
+ // Test changing the empty string.
+ Expect.equals("to", "".replaceFirst("", "to"));
+
+ // Test replacing the empty string.
+ Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to"));
+
+ // Test startIndex.
+ Expect.equals(
+ "foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4));
+
+ // Test startIndex skipping one case at the begining.
+ Expect.equals(
+ "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1));
+
+ // Test startIndex skipping one case at the begining.
+ Expect.equals(
+ "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5));
- // Test with the replaced string at the begining.
- Expect.equals(
- "toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to"));
+ // Test startIndex replacing with the empty string.
+ Expect.equals(
+ "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1));
- // Test with the replaced string at the end.
- Expect.equals(
- "toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to"));
+ // Test startIndex with a RegExp with carat
+ Expect.equals(
+ "foo-bar-foo-bar",
+ "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8));
- // Test when there are no occurence of the string to replace.
- Expect.equals("ABC", "ABC".replaceFirst("from", "to"));
+ // Test startIndex with a RegExp
+ Expect.equals(
+ "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1));
- // Test when the string to change is the empty string.
- Expect.equals("", "".replaceFirst("from", "to"));
+ // Test startIndex with regexp-looking String
+ Expect.equals(
+ "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3));
- // Test when the string to change is a substring of the string to
- // replace.
- Expect.equals("fro", "fro".replaceFirst("from", "to"));
+ // Test negative startIndex
+ Expect.throws(
+ () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError);
- // Test when the string to change is the replaced string.
- Expect.equals("to", "from".replaceFirst("from", "to"));
+ // Test startIndex too large
+ Expect.throws(
+ () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError);
- // Test when the string to change is the replacement string.
- Expect.equals("to", "to".replaceFirst("from", "to"));
+ // Test null startIndex
+ Expect.throws(
+ () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError);
- // Test replacing by the empty string.
- Expect.equals("", "from".replaceFirst("from", ""));
- Expect.equals("AB", "AfromB".replaceFirst("from", ""));
+ // Test object startIndex
+ Expect.throws(
+ () => "hello".replaceFirst("h", "X", new Object()));
- // Test changing the empty string.
- Expect.equals("to", "".replaceFirst("", "to"));
- // Test replacing the empty string.
- Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to"));
+ // Test replaceFirstMapped.
- // Test startIndex.
- Expect.equals(
- "foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4));
+ Expect.equals(
+ "AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex skipping one case at the begining.
- Expect.equals(
- "foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1));
+ // Test with the replaced string at the begining.
+ Expect.equals(
+ "toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex skipping one case at the begining.
- Expect.equals(
- "foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5));
+ // Test with the replaced string at the end.
+ Expect.equals(
+ "toABtoCDtoEto",
+ "fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex replacing with the empty string.
- Expect.equals(
- "foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1));
+ // Test when there are no occurence of the string to replace.
+ Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex with a RegExp with carat
- Expect.equals(
- "foo-bar-foo-bar",
- "foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8));
+ // Test when the string to change is the empty string.
+ Expect.equals("", "".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex with a RegExp
- Expect.equals(
- "aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1));
+ // Test when the string to change is a substring of the string to
+ // replace.
+ Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex with regexp-looking String
- Expect.equals(
- "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3));
+ // Test when the string to change is the replaced string.
+ Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to"));
- // Test negative startIndex
- Expect.throws(
- () => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError);
+ // Test when the string to change is the replacement string.
+ Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to"));
- // Test startIndex too large
- Expect.throws(
- () => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError);
+ // Test replacing by the empty string.
+ Expect.equals("", "from".replaceFirstMapped("from", (_) => ""));
+ Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => ""));
- // Test null startIndex
- Expect.throws(
- () => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError);
+ // Test changing the empty string.
+ Expect.equals("to", "".replaceFirstMapped("", (_) => "to"));
- // Test object startIndex
- Expect.throws(
- () => "hello".replaceFirst("h", "X", new Object()));
- }
+ // Test replacing the empty string.
+ Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to"));
+
+ // Test startIndex.
+ Expect.equals(
+ "foo-AAA-foo-bar",
+ "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4));
+
+ // Test startIndex skipping one case at the begining.
+ Expect.equals(
+ "foo-bar-AAA-bar",
+ "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1));
+
+ // Test startIndex skipping one case at the begining.
+ Expect.equals(
+ "foo-bar-foo-AAA",
+ "foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5));
+
+ // Test startIndex replacing with the empty string.
+ Expect.equals(
+ "foo-bar--bar", "foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1));
+
+ // Test startIndex with a RegExp with carat
+ Expect.equals(
+ "foo-bar-foo-bar",
+ "foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8));
+
+ // Test startIndex with a RegExp
+ Expect.equals(
+ "aaa{3}X{3}",
+ "aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1));
+
+ // Test startIndex with regexp-looking String
+ Expect.equals(
+ "aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3));
+
+ // Test negative startIndex
+ Expect.throws(
+ () => "hello".replaceFirstMapped("h", (_) => "X", -1),
+ (e) => e is RangeError);
+
+ // Test startIndex too large
+ Expect.throws(
+ () => "hello".replaceFirstMapped("h", (_) => "X", 6),
+ (e) => e is RangeError);
+
+ // Test null startIndex
+ Expect.throws(
+ () => "hello".replaceFirstMapped("h", (_) => "X", null),
+ (e) => e is ArgumentError);
+
+ // Test object startIndex
+ Expect.throws(
+ () => "hello".replaceFirstMapped("h", (_) => "X", new Object()));
+
+ // Test replacement depending on argument.
+ Expect.equals(
+ "foo-BAR-foo-bar",
+ "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase()));
+
+ Expect.equals(
+ "foo-[bar]-foo-bar",
+ "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]"));
+
+ Expect.equals("foo-foo-bar-foo-bar-foo-bar",
+ "foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input));
+
+ // Test replacement throwing.
+ Expect.throws(
+ () => "foo-bar".replaceFirstMapped("bar", (v) => throw 42),
+ (e) => e == 42);
+
+ // Test replacement returning non-String.
+ var o = new Object();
+ Expect.equals("foo-$o", "foo-bar".replaceFirstMapped("bar",
+ (v) { return o; }));
+
+ Expect.equals("foo-42", "foo-bar".replaceFirstMapped("bar",
+ (v) { return 42; }));
+
+ // Test replacement returning object throwing on string-conversion.
+ var n = new Naughty();
+ Expect.throws(
+ () => "foo-bar".replaceFirstMapped("bar", (v) { return n; }));
}
-main() {
- StringReplaceTest.testMain();
+// Fails to return a String on toString, throws if converted by "$naughty".
+class Naughty {
+ toString() => this;
}
« sdk/lib/_internal/compiler/js_lib/string_helper.dart ('K') | « sdk/lib/core/string.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698