-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstringsx_test.go
102 lines (95 loc) · 2.95 KB
/
stringsx_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
package stringsx
import (
"reflect"
"testing"
)
func TestClean(t *testing.T) {
cases := []struct {
name string
s string
exp string
}{
{"empty", "", ""},
{"normal", "abc", "abc"},
{"contains", "a\x00bc", "abc"},
{"contains-multi-byte-runes", "\x01世\x02界\x03", "世界"},
{"all-removable", "\x01\x02\x03", ""},
{"invalid-utf-8", "\xff", "\ufffd"},
{"invalid-utf-8-2", "a\x00\xffb\x00", "a\ufffdb"},
}
for _, c := range cases {
if got := Clean(c.s); got != c.exp {
t.Errorf("[%s] Expected: %s, got: %s", c.name, c.exp, got)
}
}
}
func TestLimitRunes(t *testing.T) {
cases := []struct {
name string
s string
n int
exp string
}{
{"empty", "", 0, ""},
{"empty-2", "", 10, ""},
{"normal", "abc", 0, ""},
{"normal-2", "abc", 2, "ab"},
{"normal-3", "abc", 3, "abc"},
{"normal-3", "abc", 20, "abc"},
{"unicode", "世界世界", 0, ""},
{"unicode-2", "世界世界", 2, "世界"},
{"unicode-3", "世界世界", 20, "世界世界"},
{"invalid-utf-8", "\xff", 0, ""},
{"invalid-utf-8-2", "\xff", 1, "\xff"},
{"invalid-utf-8-3", "\xff", 2, "\xff"},
{"invalid-utf-8#2", "a\xffb", 0, ""},
{"invalid-utf-8#2-2", "a\xffb", 1, "a"},
{"invalid-utf-8#2-3", "a\xffb", 2, "a\xff"},
{"invalid-utf-8#2-4", "a\xffb", 3, "a\xffb"},
{"invalid-utf-8#2-5", "a\xffb", 4, "a\xffb"},
{"invalid-utf-8#3", "\xff\xffb", 0, ""},
{"invalid-utf-8#3-2", "\xff\xffb", 1, "\xff"},
{"invalid-utf-8#3-3", "\xff\xffb", 2, "\xff\xff"},
{"invalid-utf-8#3-4", "\xff\xffb", 3, "\xff\xffb"},
{"invalid-utf-8#3-5", "\xff\xffb", 4, "\xff\xffb"},
}
for _, c := range cases {
if got := LimitRunes(c.s, c.n); got != c.exp {
t.Errorf("[%s] Expected: %s, got: %s", c.name, c.exp, got)
}
}
}
func TestSplitQuotes(t *testing.T) {
cases := []struct {
name string
s string
exp []string
}{
{"empty", "", []string{""}},
{"part-1", "a", []string{"a"}},
{"part-2", "ab", []string{"ab"}},
{"comma", ",", []string{"", ""}},
{"commas", ",,", []string{"", "", ""}},
{"normal-1", "a,b", []string{"a", "b"}},
{"normal-2", "a,", []string{"a", ""}},
{"normal-3", ",a", []string{"", "a"}},
{"normal-3", ",a", []string{"", "a"}},
{"normal-4", "a,b,c", []string{"a", "b", "c"}},
{"normal-5", "a, b, c , ", []string{"a", " b", " c ", " "}},
{"quoted-part-1", `"`, []string{`"`}},
{"quoted-part-2", `""`, []string{`""`}},
{"quoted-part-3", `"a"`, []string{`"a"`}},
{"quoted-part-4", `"a,b"`, []string{`"a,b"`}},
{"quoted-part-4", `",`, []string{`",`}},
{"quoted-normal-1", `a,"b"`, []string{"a", `"b"`}},
{"quoted-normal-2", `a,"b,c"`, []string{"a", `"b,c"`}},
{"quoted-normal-3", `a,"b"",c"`, []string{"a", `"b"",c"`}},
{"quoted-normal-4", `a,"b,c",d`, []string{"a", `"b,c"`, "d"}},
{"quoted-normal-4", `a,"b,c","d"`, []string{"a", `"b,c"`, `"d"`}},
}
for _, c := range cases {
if got := SplitQuotes(c.s, ',', '"'); !reflect.DeepEqual(got, c.exp) {
t.Errorf("[%s] Expected: %s, got: %s", c.name, c.exp, got)
}
}
}