aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/strings/strings_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-06-30 18:03:09 -0700
committerRuss Cox <rsc@golang.org>2010-06-30 18:03:09 -0700
commit0bf413ab8e24fd0c19c14782278fc94aa2d0de18 (patch)
treed380d0f33dc266caf1a33dcd6492fad0bf957ca1 /src/pkg/strings/strings_test.go
parent269df5827010fa29822d0ed655e104b559f1e870 (diff)
downloadgo-0bf413ab8e24fd0c19c14782278fc94aa2d0de18.tar.xz
bytes, strings: add Replace
This is the Replace I suggested in the review of CL 1114041. It's true that we already have regexp.MustCompile(regexp.QuoteMeta(old)).ReplaceAll(s, new) but because this Replace is doing a simpler job it is simpler to call and inherently more efficient. I will add the bytes implementation and tests to the CL after the strings one has been reviewed. R=r, cw CC=golang-dev https://golang.org/cl/1731048
Diffstat (limited to 'src/pkg/strings/strings_test.go')
-rw-r--r--src/pkg/strings/strings_test.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go
index e4134d8d67..5ac6970c6b 100644
--- a/src/pkg/strings/strings_test.go
+++ b/src/pkg/strings/strings_test.go
@@ -700,3 +700,39 @@ func TestReadRune(t *testing.T) {
}
}
}
+
+type ReplaceTest struct {
+ in string
+ old, new string
+ n int
+ out string
+}
+
+var ReplaceTests = []ReplaceTest{
+ ReplaceTest{"hello", "l", "L", 0, "heLLo"},
+ ReplaceTest{"hello", "x", "X", 0, "hello"},
+ ReplaceTest{"", "x", "X", 0, ""},
+ ReplaceTest{"radar", "r", "<r>", 0, "<r>ada<r>"},
+ ReplaceTest{"", "", "<>", 0, "<>"},
+ ReplaceTest{"banana", "a", "<>", 0, "b<>n<>n<>"},
+ ReplaceTest{"banana", "a", "<>", 1, "b<>nana"},
+ ReplaceTest{"banana", "a", "<>", 1000, "b<>n<>n<>"},
+ ReplaceTest{"banana", "an", "<>", 0, "b<><>a"},
+ ReplaceTest{"banana", "ana", "<>", 0, "b<>na"},
+ ReplaceTest{"banana", "", "<>", 0, "<>b<>a<>n<>a<>n<>a<>"},
+ ReplaceTest{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
+ ReplaceTest{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
+ ReplaceTest{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
+ ReplaceTest{"banana", "", "<>", 1, "<>banana"},
+ ReplaceTest{"banana", "a", "a", 0, "banana"},
+ ReplaceTest{"banana", "a", "a", 1, "banana"},
+ ReplaceTest{"☺☻☹", "", "<>", 0, "<>☺<>☻<>☹<>"},
+}
+
+func TestReplace(t *testing.T) {
+ for _, tt := range ReplaceTests {
+ if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
+ t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
+ }
+ }
+}