-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
447 lines (388 loc) · 14.3 KB
/
string.go
File metadata and controls
447 lines (388 loc) · 14.3 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package assert
import (
"fmt"
"regexp"
"strings"
"testing"
)
// ContainsString tests whether the string contains the substring or not, and it set the result to
// fail if the string does not contains the substring.
//
// a := assert.New(t)
// a.ContainsString("Hello world", "") // success
// a.ContainsString("Hello world", "Hello") // success
// a.ContainsString("Hello world", "world") // success
// a.ContainsString("Hello world", "hello") // fail
func (a *Assertion) ContainsString(str, substr string, message ...any) error {
a.Helper()
return tryContainsString(a.T, false, str, substr, message...)
}
// ContainsStringNow tests whether the string contains the substring or not, and it will terminate the
// execution if the string does not contains the substring.
//
// a := assert.New(t)
// a.ContainsStringNow("Hello world", "") // success
// a.ContainsStringNow("Hello world", "Hello") // success
// a.ContainsStringNow("Hello world", "world") // success
// a.ContainsStringNow("Hello world", "hello") // fail and stop the execution
// // never runs
func (a *Assertion) ContainsStringNow(str, substr string, message ...any) error {
a.Helper()
return tryContainsString(a.T, true, str, substr, message...)
}
// NotContainsString tests whether the string contains the substring or not, and it set the result
// to fail if the string contains the substring.
//
// a := assert.New(t)
// a.NotContainsString("Hello world", "hello") // success
// a.NotContainsString("Hello world", "") // fail
// a.NotContainsString("Hello world", "Hello") // fail
// a.NotContainsString("Hello world", "world") // fail
func (a *Assertion) NotContainsString(str, substr string, message ...any) error {
a.Helper()
return tryNotContainsString(a.T, false, str, substr, message...)
}
// NotContainsStringNow tests whether the string contains the substring or not, and it will terminate the
// execution if the string contains the substring.
//
// a := assert.New(t)
// a.NotContainsStringNow("Hello world", "hello") // success
// a.NotContainsStringNow("Hello world", "Hello") // fail and stop the execution
// // never runs
func (a *Assertion) NotContainsStringNow(str, substr string, message ...any) error {
a.Helper()
return tryNotContainsString(a.T, true, str, substr, message...)
}
// tryContainsString tries to test whether the string contains the substring or not, and it'll
// fail if the string does not contains the substring.
func tryContainsString(
t *testing.T,
failedNow bool,
str, substr string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return strings.Contains(str, substr) },
failedNow,
fmt.Sprintf(defaultErrMessageContainsString, substr),
message...,
)
}
// tryNotContainsString tries to test whether the string contains the substring or not, and it'll
// fail if the string contains the substring.
func tryNotContainsString(
t *testing.T,
failedNow bool,
str, substr string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return !strings.Contains(str, substr) },
failedNow,
fmt.Sprintf(defaultErrMessageNotContainsString, substr),
message...,
)
}
// HasPrefixString tests whether the string has the prefix string or not, and it set the result to
// fail if the string does not have the prefix string.
//
// a := assert.New(t)
// a.HasPrefixString("Hello world", "") // success
// a.HasPrefixString("Hello world", "Hello") // success
// a.HasPrefixString("Hello world", "world") // fail
// a.HasPrefixString("Hello world", "hello") // fail
func (a *Assertion) HasPrefixString(str, prefix string, message ...any) error {
a.Helper()
return tryHasPrefixString(a.T, false, str, prefix, message...)
}
// HasPrefixStringNow tests whether the string has the prefix string or not, and it will terminate
// the execution if the string does not have the prefix string.
//
// a := assert.New(t)
// a.HasPrefixStringNow("Hello world", "") // success
// a.HasPrefixStringNow("Hello world", "Hello") // success
// a.HasPrefixStringNow("Hello world", "hello") // fail and stop the execution
// // never runs
func (a *Assertion) HasPrefixStringNow(str, prefix string, message ...any) error {
a.Helper()
return tryHasPrefixString(a.T, true, str, prefix, message...)
}
// NotHasPrefixString tests whether the string has the prefix string or not, and it set the result
// to fail if the string have the prefix string.
//
// a := assert.New(t)
// a.NotHasPrefixString("Hello world", "hello") // success
// a.NotHasPrefixString("Hello world", "world") // success
// a.NotHasPrefixString("Hello world", "") // fail
// a.NotHasPrefixString("Hello world", "Hello") // fail
func (a *Assertion) NotHasPrefixString(str, prefix string, message ...any) error {
a.Helper()
return tryNotHasPrefixString(a.T, false, str, prefix, message...)
}
// NotHasPrefixStringNow tests whether the string has the prefix string or not, and it will
// terminate the execution if the string have the prefix string.
//
// a := assert.New(t)
// a.NotHasPrefixStringNow("Hello world", "hello") // success
// a.NotHasPrefixStringNow("Hello world", "world") // success
// a.NotHasPrefixStringNow("Hello world", "Hello") // fail and stop the execution
// // never runs
func (a *Assertion) NotHasPrefixStringNow(str, prefix string, message ...any) error {
a.Helper()
return tryNotHasPrefixString(a.T, true, str, prefix, message...)
}
// tryHasPrefixString tries to test whether the string has the prefix string or not, and it'll fail
// if the string does not have the prefix string.
func tryHasPrefixString(
t *testing.T,
failedNow bool,
str, prefix string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return strings.HasPrefix(str, prefix) },
failedNow,
fmt.Sprintf(defaultErrMessageHasPrefixString, prefix),
message...,
)
}
// tryNotHasPrefixString tries to test whether the string has the prefix string or not, and it'll
// fail if the string has the prefix string.
func tryNotHasPrefixString(
t *testing.T,
failedNow bool,
str, prefix string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return !strings.HasPrefix(str, prefix) },
failedNow,
fmt.Sprintf(defaultErrMessageNotHasPrefixString, prefix),
message...,
)
}
// HasSuffixString tests whether the string has the suffix string or not, and it set the result to
// fail if the string does not have the suffix string.
//
// a := assert.New(t)
// a.HasSuffixString("Hello world", "") // success
// a.HasSuffixString("Hello world", "world") // success
// a.HasSuffixString("Hello world", "World") // fail
// a.HasSuffixString("Hello world", "hello") // fail
func (a *Assertion) HasSuffixString(str, suffix string, message ...any) error {
a.Helper()
return tryHasSuffixString(a.T, false, str, suffix, message...)
}
// HasSuffixStringNow tests whether the string has the suffix string or not, and it will terminate
// the execution if the string does not have the suffix string.
//
// a := assert.New(t)
// a.HasSuffixStringNow("Hello world", "") // success
// a.HasSuffixStringNow("Hello world", "world") // success
// a.HasSuffixStringNow("Hello world", "World") // fail and stop the execution
// // never runs
func (a *Assertion) HasSuffixStringNow(str, suffix string, message ...any) error {
a.Helper()
return tryHasSuffixString(a.T, true, str, suffix, message...)
}
// NotHasSuffixString tests whether the string has the suffix string or not, and it set the result
// to fail if the string have the suffix string.
//
// a := assert.New(t)
// a.NotHasSuffixString("Hello world", "Hello") // success
// a.NotHasSuffixString("Hello world", "World") // success
// a.NotHasSuffixString("Hello world", "") // fail
// a.NotHasSuffixString("Hello world", "world") // fail
func (a *Assertion) NotHasSuffixString(str, suffix string, message ...any) error {
a.Helper()
return tryNotHasSuffixString(a.T, false, str, suffix, message...)
}
// NotHasSuffixStringNow tests whether the string has the suffix string or not, and it will
// terminate the execution if the string have the suffix string.
//
// a := assert.New(t)
// a.NotHasSuffixStringNow("Hello world", "hello") // success
// a.NotHasSuffixStringNow("Hello world", "World") // success
// a.NotHasSuffixStringNow("Hello world", "world") // fail and stop the execution
// // never runs
func (a *Assertion) NotHasSuffixStringNow(str, suffix string, message ...any) error {
a.Helper()
return tryNotHasSuffixString(a.T, true, str, suffix, message...)
}
// tryHasSuffixString tries to test whether the string has the suffix string or not, and it'll fail
// if the string does not have the suffix string.
func tryHasSuffixString(
t *testing.T,
failedNow bool,
str, suffix string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return strings.HasSuffix(str, suffix) },
failedNow,
fmt.Sprintf(defaultErrMessageHasSuffixString, suffix),
message...,
)
}
// tryNotHasSuffixString tries to test whether the string has the suffix string or not, and it'll
// fail if the string has the suffix string.
func tryNotHasSuffixString(
t *testing.T,
failedNow bool,
str, suffix string,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return !strings.HasSuffix(str, suffix) },
failedNow,
fmt.Sprintf(defaultErrMessageNotHasSuffixString, suffix),
message...,
)
}
// Match tests whether the string matches the regular expression or not.
//
// a := assert.New(t)
// pattern := regexp.MustCompile(`^https?:\/\/`)
// a.Match("http://example.com", pattern) // success
// a.Match("example.com", pattern) // fail
func (a *Assertion) Match(val string, pattern *regexp.Regexp, message ...any) error {
a.Helper()
return tryMatchRegexp(a.T, false, val, pattern, "", message...)
}
// MatchNow tests whether the string matches the regular expression or not, and it will terminate
// the execution if it does not match.
//
// a := assert.New(t)
// pattern := regexp.MustCompile(`^https?:\/\/`)
// a.MatchNow("http://example.com", pattern) // success
// a.MatchNow("example.com", pattern) // fail and terminate
// // never run
func (a *Assertion) MatchNow(val string, pattern *regexp.Regexp, message ...any) error {
a.Helper()
return tryMatchRegexp(a.T, true, val, pattern, "", message...)
}
// MatchString will compile the pattern and test whether the string matches the regular expression
// or not. It will panic if the pattern is not a valid regular expression.
//
// a := assert.New(t)
// a.MatchString("http://example.com", `^https?:\/\/`) // success
// a.MatchString("example.com", `^https?:\/\/`) // fail
func (a *Assertion) MatchString(val, pattern string, message ...any) error {
a.Helper()
return tryMatchRegexp(a.T, false, val, nil, pattern, message...)
}
// MatchStringNow will compile the pattern and test whether the string matches the regular
// expression or not. It will terminate the execution if it does not match, and it will panic if
// the pattern is not a valid regular expression.
//
// a := assert.New(t)
// a.MatchStringNow("http://example.com", `^https?:\/\/`) // success
// a.MatchStringNow("example.com", `^https?:\/\/`) // fail and terminate
// // never run
func (a *Assertion) MatchStringNow(val, pattern string, message ...any) error {
a.Helper()
return tryMatchRegexp(a.T, true, val, nil, pattern, message...)
}
// NotMatch tests whether the string matches the regular expression or not, and it set the result
// to fail if the string matches the pattern.
//
// a := assert.New(t)
// pattern := regexp.MustCompile(`^https?:\/\/`)
// a.NotMatch("example.com", pattern) // success
// a.NotMatch("http://example.com", pattern) // fail
func (a *Assertion) NotMatch(val string, pattern *regexp.Regexp, message ...any) error {
a.Helper()
return tryNotMatchRegexp(a.T, false, val, pattern, "", message...)
}
// NotMatchNow tests whether the string matches the regular expression or not, and it will
// terminate the execution if the string matches the pattern.
//
// a := assert.New(t)
// pattern := regexp.MustCompile(`^https?:\/\/`)
// a.NotMatchNow("example.com", pattern) // success
// a.NotMatchNow("http://example.com", pattern) // fail and terminate
// // never run
func (a *Assertion) NotMatchNow(val string, pattern *regexp.Regexp, message ...any) error {
a.Helper()
return tryNotMatchRegexp(a.T, true, val, pattern, "", message...)
}
// MatchString will compile the pattern and test whether the string matches the regular expression
// or not, and it set the result to fail if the string matches the pattern. It will also panic if
// the pattern is not a valid regular expression.
//
// a := assert.New(t)
// a.NotMatchString("example.com", `^https?:\/\/`) // success
// a.NotMatchString("http://example.com", `^https?:\/\/`) // fail
func (a *Assertion) NotMatchString(val, pattern string, message ...any) error {
a.Helper()
return tryNotMatchRegexp(a.T, false, val, nil, pattern, message...)
}
// NotMatchStringNow will compile the pattern and test whether the string matches the regular
// expression or not, and it set the result to fail if the string matches the pattern. It will
// terminate the execution if the string matches the pattern, and it will panic if the pattern is
// not a valid regular expression.
//
// a := assert.New(t)
// a.NotMatchStringNow("example.com", `^https?:\/\/`) // success
// a.NotMatchStringNow("http://example.com", `^https?:\/\/`) // fail and terminate
// // never run
func (a *Assertion) NotMatchStringNow(val, pattern string, message ...any) error {
a.Helper()
return tryNotMatchRegexp(a.T, true, val, nil, pattern, message...)
}
// tryMatchRegexp tries to test whether the string matches the regular expression pattern or not,
// and it'll fail if the string does not match.
func tryMatchRegexp(
t *testing.T,
failedNow bool,
val string,
pattern *regexp.Regexp,
patternStr string,
message ...any,
) error {
t.Helper()
if pattern == nil {
pattern = regexp.MustCompile(patternStr)
}
return test(
t,
func() bool { return pattern.Match([]byte(val)) },
failedNow,
defaultErrMessageMatch,
message...,
)
}
// tryNotMatchRegexp tries to test whether the string matches the regular expression pattern or
// not, and it'll fail if the string matches the pattern.
func tryNotMatchRegexp(
t *testing.T,
failedNow bool,
val string,
pattern *regexp.Regexp,
patternStr string,
message ...any,
) error {
t.Helper()
if pattern == nil {
pattern = regexp.MustCompile(patternStr)
}
return test(
t,
func() bool { return !pattern.Match([]byte(val)) },
failedNow,
defaultErrMessageNotMatch,
message...,
)
}