forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_test.go
68 lines (52 loc) · 1.57 KB
/
strings_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
package arrutil_test
import (
"fmt"
"testing"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestStringsToSlice(t *testing.T) {
is := assert.New(t)
as := arrutil.StringsToSlice([]string{"1", "2"})
is.Eq(`[]interface {}{"1", "2"}`, fmt.Sprintf("%#v", as))
}
func TestStringsToInts(t *testing.T) {
is := assert.New(t)
ints, err := arrutil.StringsToInts([]string{"1", "2"})
is.Nil(err)
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints))
_, err = arrutil.StringsToInts([]string{"a", "b"})
is.Err(err)
ints = arrutil.StringsAsInts([]string{"1", "2"})
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints))
is.Nil(arrutil.StringsAsInts([]string{"abc"}))
}
func TestStringsRemove(t *testing.T) {
ss := []string{"a", "b", "c"}
ns := arrutil.StringsRemove(ss, "b")
assert.Contains(t, ns, "a")
assert.NotContains(t, ns, "b")
assert.Len(t, ns, 2)
}
func TestStringsFilter(t *testing.T) {
is := assert.New(t)
ss := arrutil.StringsFilter([]string{"a", "", "b", ""})
is.Eq([]string{"a", "b"}, ss)
}
func TestStringsMap(t *testing.T) {
is := assert.New(t)
ss := arrutil.StringsMap([]string{"a", "b", "c"}, func(s string) string {
return s + "1"
})
is.Eq([]string{"a1", "b1", "c1"}, ss)
}
func TestTrimStrings(t *testing.T) {
is := assert.New(t)
// TrimStrings
ss := arrutil.TrimStrings([]string{" a", "b ", " c "})
is.Eq("[a b c]", fmt.Sprint(ss))
ss = arrutil.TrimStrings([]string{",a", "b.", ",.c,"}, ",.")
is.Eq("[a b c]", fmt.Sprint(ss))
ss = arrutil.TrimStrings([]string{",a", "b.", ",.c,"}, ",", ".")
is.Eq("[a b c]", fmt.Sprint(ss))
}