-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathreplace_test.go
66 lines (59 loc) · 1.53 KB
/
replace_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package strutil
import (
"fmt"
"testing"
)
func TestReplaceAllToOne(t *testing.T) {
tests := []struct {
input string
from []string
to string
expected string
}{
{"", []string{"a"}, "b", ""},
{"lorem", []string{""}, "-", "-l-o-r-e-m-"},
{"lorem", []string{"lo", "em"}, "", "r"},
{"a b c a c f", []string{" ", "a", "b"}, "-", "----c---c-f"},
}
for i, test := range tests {
output := ReplaceAllToOne(test.input, test.from, test.to)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
func ExampleReplaceAllToOne() {
fmt.Println(ReplaceAllToOne("lorem", []string{"lo", "em"}, "x"))
// Output: xrx
}
func TestSplice(t *testing.T) {
tests := []struct {
input string
newStr string
start int
end int
mustPanic bool
expected string
}{
{"lorem", "x", 0, 2, false, "xrem"},
{"lorem", "", 0, 2, false, "rem"},
{"", "x", 0, 2, false, ""},
{"lorem", "x", 4, 5, false, "lorex"},
{"lorem", "ipsum", 2, 3, false, "loipsumem"},
{"lorem", "x", 5, 6, true, ""},
{"lorem", "x", 4, 4, true, ""},
{"lorem", "x", 4, 3, true, ""},
}
for i, test := range tests {
if test.mustPanic {
AssertPanics(t, func() {
_ = Splice(test.input, test.newStr, test.start, test.end)
}, "Test case %d is not successful\n", i)
} else {
output := Splice(test.input, test.newStr, test.start, test.end)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
}
}
func ExampleSplice() {
fmt.Println(Splice("Lorem", "ipsum", 2, 3))
// Output: Loipsumem
}