This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstringsutil_test.go
272 lines (246 loc) · 8.81 KB
/
stringsutil_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package stringsutil
import (
"testing"
"github.com/stretchr/testify/require"
)
type betweentest struct {
After string
Before string
Result string
WantError bool
}
func TestBetween(t *testing.T) {
tests := map[string]betweentest{
"a b c": {After: "a", Before: "c", Result: " b ", WantError: false},
"this is a test": {After: "this", Before: "test", Result: " is a ", WantError: false},
"this is a test bbb test": {After: "test", Before: "test", Result: " bbb ", WantError: false},
"this is a test with before error": {After: "test", Before: "testt", Result: "this is a test with before error", WantError: true},
"this is a test with after error": {After: "testt", Before: "test", Result: "this is a test with after error", WantError: true},
}
for str, test := range tests {
res, err := Between(str, test.After, test.Before)
if test.WantError {
require.Error(t, err)
}
require.Equalf(t, test.Result, res, "test: %s after: %s before: %s result: %s", str, test.After, test.Before, res)
}
}
func TestBefore(t *testing.T) {
tests := map[string]betweentest{
"a b c": {Before: "c", Result: "a b ", WantError: false},
"this is a test": {Before: "test", Result: "this is a ", WantError: false},
"this is a test with second t": {Before: "testt", Result: "this is a test with second t", WantError: true},
}
for str, test := range tests {
res, err := Before(str, test.Before)
if test.WantError {
require.Error(t, err)
}
require.Equalf(t, test.Result, res, "test: %s before: %s result: %s", str, test.Before, res)
}
}
func TestAfter(t *testing.T) {
tests := map[string]betweentest{
"a b c": {After: "a", Result: " b c", WantError: false},
"this is a test": {After: "this", Result: " is a test", WantError: false},
"this is a test with second t": {After: "testt", Result: "this is a test with second t", WantError: true},
}
for str, test := range tests {
res, err := After(str, test.After)
if test.WantError {
require.Error(t, err)
}
require.Equalf(t, test.Result, res, "test: %s after: %s result: %s", str, test.After, res)
}
}
type prefixsuffixtest struct {
Prefixes []string
Suffixes []string
Result interface{}
}
func TestHasPrefixAny(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Prefixes: []string{"a"}, Result: true},
"a b c d": {Prefixes: []string{"a b", "a"}, Result: true},
"a b c d e": {Prefixes: []string{"b", "o", "a"}, Result: true},
"test test": {Prefixes: []string{"a", "b"}, Result: false},
}
for str, test := range tests {
res := HasPrefixAny(str, test.Prefixes...)
require.Equalf(t, test.Result, res, "test: %s prefixes: %+v result: %s", str, test.Prefixes, res)
}
}
func TestHasSuffixAny(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Suffixes: []string{"c"}, Result: true},
"a b c d": {Suffixes: []string{"c d", "a"}, Result: true},
"a b c d e": {Suffixes: []string{"c", "d", "e"}, Result: true},
"test test": {Suffixes: []string{"a", "b"}, Result: false},
}
for str, test := range tests {
res := HasSuffixAny(str, test.Suffixes...)
require.Equalf(t, test.Result, res, "test: %s suffixes: %+v result: %s", str, test.Suffixes, res)
}
}
func TestTrimPrefixAny(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Prefixes: []string{"a"}, Result: " b c"},
"a b c d": {Prefixes: []string{"a b", "a"}, Result: " c d"},
"a b c d e": {Prefixes: []string{"b", "o", "a"}, Result: " b c d e"},
"test test": {Prefixes: []string{"a", "b"}, Result: "test test"},
}
for str, test := range tests {
res := TrimPrefixAny(str, test.Prefixes...)
require.Equalf(t, test.Result, res, "test: %s prefixes: %+v result: %s", str, test.Prefixes, res)
}
}
func TestTrimSuffixAny(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Suffixes: []string{"c"}, Result: "a b "},
"a b c d": {Suffixes: []string{"c d", "a"}, Result: "a b "},
"a b c d e": {Suffixes: []string{"e"}, Result: "a b c d "},
"test test": {Suffixes: []string{"a", "b"}, Result: "test test"},
}
for str, test := range tests {
res := TrimSuffixAny(str, test.Suffixes...)
require.Equalf(t, test.Result, res, "test: %s suffixes: %+v result: %s", str, test.Suffixes, res)
}
}
type jointest struct {
Items []interface{}
Separator string
Result string
}
func TestJoin(t *testing.T) {
tests := []jointest{
{Items: []interface{}{"a"}, Separator: "", Result: "a"},
{Items: []interface{}{"a", "b"}, Separator: ",", Result: "a,b"},
{Items: []interface{}{"a", "b", 1}, Separator: ",", Result: "a,b,1"},
{Items: []interface{}{2, "b", 1}, Separator: "", Result: "2b1"},
}
for _, test := range tests {
res := Join(test.Items, test.Separator)
require.Equalf(t, test.Result, res, "test: %+v", test)
}
}
func TestHasPrefixI(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Prefixes: []string{"a"}, Result: true},
"A b c d": {Prefixes: []string{"a"}, Result: true},
"Ab c d": {Prefixes: []string{"b"}, Result: false},
}
for str, test := range tests {
res := HasPrefixI(str, test.Prefixes[0])
require.Equalf(t, test.Result, res, "test: %s prefixes: %+v result: %s", str, test.Prefixes, res)
}
}
func TestHasSuffixI(t *testing.T) {
tests := map[string]prefixsuffixtest{
"a b c": {Prefixes: []string{"c"}, Result: true},
"A b C": {Prefixes: []string{"c"}, Result: true},
"Ab c d": {Prefixes: []string{"c"}, Result: false},
}
for str, test := range tests {
res := HasSuffixI(str, test.Prefixes[0])
require.Equalf(t, test.Result, res, "test: %s suffixes: %+v result: %s", str, test.Suffixes, res)
}
}
func TestReverse(t *testing.T) {
tests := map[string]string{
"abc": "cba",
"A b C": "C b A",
"Ab c d": "d c bA",
"hello world!": "!dlrow olleh",
"!@#$%^&*()": ")(*&^%$#@!",
"明日は晴天り": "り天晴は日明",
}
for str, expRes := range tests {
res := Reverse(str)
require.Equalf(t, expRes, res, "test: %s expected: %+v result: %s", str, expRes, res)
}
}
type containstest struct {
Items []string
Result bool
}
type replacealltest struct {
Old string
New string
Result string
}
func TestContainsAny(t *testing.T) {
tests := map[string]containstest{
"abc": {Items: []string{"a", "b"}, Result: true},
"abcd": {Items: []string{"x", "b"}, Result: true},
"A b C": {Items: []string{"x"}, Result: false},
}
for str, test := range tests {
res := ContainsAny(str, test.Items...)
require.Equalf(t, test.Result, res, "test: %+v", res)
}
}
func TestEqualFoldAny(t *testing.T) {
tests := map[string]containstest{
"abc": {Items: []string{"a", "Abc"}, Result: true},
"abcd": {Items: []string{"x", "ABcD"}, Result: true},
"A b C": {Items: []string{"x"}, Result: false},
"hello": {Items: []string{"llo", "heLLo"}, Result: true},
"world": {Items: []string{"Hello", "WoRld"}, Result: true},
}
for str, test := range tests {
res := EqualFoldAny(str, test.Items...)
require.Equalf(t, test.Result, res, "test: %+v", res)
}
}
type attest struct {
After int
Search string
Result interface{}
}
func TestIndexAt(t *testing.T) {
tests := map[string]attest{
"a a b": {After: 1, Search: "a", Result: 2},
"test": {After: 1, Search: "t", Result: 3},
"test test": {After: 4, Search: "test", Result: 5},
"test test test": {After: 0, Search: "test", Result: 0},
}
for str, test := range tests {
res := IndexAt(str, test.Search, test.After)
require.Equalf(t, test.Result, res, "test: %s after: %d search: %s result: %d", str, test.After, test.Search, res)
}
}
type splitanytest struct {
Splitset []string
Result interface{}
}
func TestSplitAny(t *testing.T) {
tests := map[string]splitanytest{
"a a b": {Splitset: []string{" "}, Result: []string{"a", "a", "b"}},
"test1test2 3": {Splitset: []string{"1", "2", " "}, Result: []string{"test", "test", "3"}},
}
for str, test := range tests {
res := SplitAny(str, test.Splitset...)
require.Equalf(t, test.Result, res, "test: %s splitset: %v result: %v got: %v", str, test.Splitset, test.Result, res)
}
}
func TestSlideWithLength(t *testing.T) {
var res []string
c := SlideWithLength("test123", 4)
require.NotNil(t, c)
for cc := range c {
res = append(res, cc)
}
require.Equal(t, []string{"test", "est1", "st12", "t123", "123"}, res)
}
func TestReplaceAll(t *testing.T) {
tests := map[string]replacealltest{
"hello": {Old: "l", New: "k", Result: "hekko"},
"abcd": {Old: "a", New: "b", Result: "bbcd"},
"A b C": {Old: " ", New: "", Result: "AbC"},
"!@#$%^&*()": {Old: "@", New: "_", Result: "!_#$%^&*()"},
}
for str, test := range tests {
res := ReplaceAll(str, test.New, test.Old)
require.Equalf(t, test.Result, res, "test: %s, new: %s, old: %s, expected: %s, got: %s", str, test.New, test.Old, test.Result, res)
}
}