forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrutil_test.go
84 lines (66 loc) · 1.97 KB
/
strutil_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
package strutil_test
import (
"strings"
"testing"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestSimilarity(t *testing.T) {
is := assert.New(t)
_, ok := strutil.Similarity("hello", "he", 0.3)
is.True(ok)
}
func TestValid(t *testing.T) {
is := assert.New(t)
is.Eq("ab", strutil.Valid("ab", ""))
is.Eq("ab", strutil.Valid("ab", "cd"))
is.Eq("cd", strutil.Valid("", "cd"))
is.Empty(strutil.Valid("", ""))
is.Eq("cd", strutil.OrElse("", "cd"))
is.Eq("ab", strutil.OrElse("ab", "cd"))
is.Eq(" ", strutil.ZeroOr(" ", "cd"))
is.Eq("cd", strutil.ZeroOr("", "cd"))
is.Eq("ab", strutil.ZeroOr("ab", "cd"))
is.Eq("cd", strutil.BlankOr("", "cd"))
is.Eq("cd", strutil.BlankOr(" ", "cd"))
is.Eq("ab", strutil.BlankOr("ab", "cd"))
is.Eq("ab", strutil.BlankOr(" ab ", "cd"))
is.Eq("ab", strutil.OrCond(true, "ab", "cd"))
is.Eq("cd", strutil.OrCond(false, "ab", "cd"))
is.Eq("ab", strutil.OrHandle(" ab ", strings.TrimSpace))
is.Empty(strutil.OrHandle("", strings.TrimSpace))
}
func TestReplaces(t *testing.T) {
assert.Eq(t, "tom age is 20", strutil.Replaces(
"{name} age is {age}",
map[string]string{
"{name}": "tom",
"{age}": "20",
}))
}
func TestWrapTag(t *testing.T) {
assert.Eq(t, "", strutil.WrapTag("", "info"))
assert.Eq(t, "<info>abc</info>", strutil.WrapTag("abc", "info"))
}
func TestSubstrCount(t *testing.T) {
s := "I'm fine, thank you, and you"
substr := "you"
res, err := strutil.SubstrCount(s, substr)
assert.NoErr(t, err)
assert.Eq(t, 2, res)
res1, err := strutil.SubstrCount(s, substr, 18)
assert.NoErr(t, err)
assert.Eq(t, 1, res1)
res2, err := strutil.SubstrCount(s, substr, 17, 100)
assert.NoErr(t, err)
assert.Eq(t, 1, res2)
res, err = strutil.SubstrCount(s, substr, 16)
assert.NoErr(t, err)
assert.Eq(t, 2, res)
res, err = strutil.SubstrCount(s, substr)
assert.NoErr(t, err)
assert.Eq(t, 2, res)
res, err = strutil.SubstrCount(s, substr, 1, 2, 3)
assert.Err(t, err)
assert.Eq(t, 0, res)
}