1
2
3
4 package xstrings
5
6 import (
7 "sort"
8 "strings"
9 "testing"
10 )
11
12 func TestToSnakeCaseAndToKebabCase(t *testing.T) {
13 cases := _M{
14 "HTTPServer": "http_server",
15 "_camelCase": "_camel_case",
16 "NoHTTPS": "no_https",
17 "Wi_thF": "wi_th_f",
18 "_AnotherTES_TCaseP": "_another_tes_t_case_p",
19 "ALL": "all",
20 "_HELLO_WORLD_": "_hello_world_",
21 "HELLO_WORLD": "hello_world",
22 "HELLO____WORLD": "hello____world",
23 "TW": "tw",
24 "_C": "_c",
25 "http2xx": "http_2xx",
26 "HTTP2XX": "http2_xx",
27 "HTTP20xOK": "http_20x_ok",
28 "HTTP20xStatus": "http_20x_status",
29 "HTTP-20xStatus": "http_20x_status",
30 "a": "a",
31 "Duration2m3s": "duration_2m3s",
32 "Bld4Floor3rd": "bld4_floor_3rd",
33 " _-_ ": "_____",
34 "a1b2c3d": "a_1b2c3d",
35 "A//B%%2c": "a//b%%2c",
36
37 "HTTP状态码404/502Error": "http_状态码404/502_error",
38 "中文(字符)": "中文(字符)",
39 "混合ABCWords与123数字456": "混合_abc_words_与123_数字456",
40
41 " sentence case ": "__sentence_case__",
42 " Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case",
43 "FROM CamelCase to snake/kebab-case": "from_camel_case_to_snake/kebab_case",
44
45 "": "",
46 "Abc\uFFFDE\uFFFDf\uFFFDd\uFFFD2\uFFFD00z\uFFFDZZ\uFFFDZZ": "abc_\uFFFDe\uFFFDf\uFFFDd_\uFFFD2\uFFFD00z_\uFFFDzz\uFFFDzz",
47 "\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD": "\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD",
48
49 "abc_123_def": "abc_123_def",
50 }
51
52 runTestCases(t, ToSnakeCase, cases)
53
54 for k, v := range cases {
55 cases[k] = strings.Replace(v, "_", "-", -1)
56 }
57
58 runTestCases(t, ToKebabCase, cases)
59 }
60
61 func TestToCamelCase(t *testing.T) {
62 runTestCases(t, ToCamelCase, _M{
63 "http_server": "HttpServer",
64 "_camel_case": "_CamelCase",
65 "no_https": "NoHttps",
66 "_complex__case_": "_Complex_Case_",
67 " complex -case ": " Complex Case ",
68 "all": "All",
69 "GOLANG_IS_GREAT": "GOLANGISGREAT",
70 "GOLANG": "GOLANG",
71 "a": "A",
72 "好": "好",
73
74 "FROM CamelCase to snake/kebab-case": "FROMCamelCaseToSnake/kebabCase",
75
76 "": "",
77 })
78 }
79
80 func TestSwapCase(t *testing.T) {
81 runTestCases(t, SwapCase, _M{
82 "swapCase": "SWAPcASE",
83 "Θ~λa云Ξπ": "θ~ΛA云ξΠ",
84 "a": "A",
85
86 "": "",
87 })
88 }
89
90 func TestFirstRuneToUpper(t *testing.T) {
91 runTestCases(t, FirstRuneToUpper, _M{
92 "hello, world!": "Hello, world!",
93 "Hello, world!": "Hello, world!",
94 "你好,世界!": "你好,世界!",
95 "a": "A",
96
97 "": "",
98 })
99 }
100
101 func TestFirstRuneToLower(t *testing.T) {
102 runTestCases(t, FirstRuneToLower, _M{
103 "hello, world!": "hello, world!",
104 "Hello, world!": "hello, world!",
105 "你好,世界!": "你好,世界!",
106 "a": "a",
107 "A": "a",
108
109 "": "",
110 })
111 }
112
113 func TestShuffle(t *testing.T) {
114
115
116 runner := func(str string) string {
117 s := Shuffle(str)
118 slice := sort.StringSlice(strings.Split(s, ""))
119 slice.Sort()
120 return strings.Join(slice, "")
121 }
122
123 runTestCases(t, runner, _M{
124 "": "",
125 "facgbheidjk": "abcdefghijk",
126 "尝试中文": "中尝文试",
127 "zh英文hun排": "hhnuz排文英",
128 })
129 }
130
131 type testShuffleSource int
132
133
134 var testShuffleTable = []int64{
135 1874068156324778273,
136 3328451335138149956,
137 5263531936693774911,
138 7955079406183515637,
139 2703501726821866378,
140 2740103009342231109,
141 6941261091797652072,
142 1905388747193831650,
143 7981306761429961588,
144 6426100070888298971,
145 4831389563158288344,
146 261049867304784443,
147 1460320609597786623,
148 5600924393587988459,
149 8995016276575641803,
150 732830328053361739,
151 5486140987150761883,
152 545291762129038907,
153 6382800227808658932,
154 2781055864473387780,
155 1598098976185383115,
156 4990765271833742716,
157 5018949295715050020,
158 2568779411109623071,
159 3902890183311134652,
160 4893789450120281907,
161 2338498362660772719,
162 2601737961087659062,
163 7273596521315663110,
164 3337066551442961397,
165 8121576815539813105,
166 2740376916591569721,
167 8249030965139585917,
168 898860202204764712,
169 9010467728050264449,
170 685213522303989579,
171 2050257992909156333,
172 6281838661429879825,
173 2227583514184312746,
174 2873287401706343734,
175 }
176
177 func (src *testShuffleSource) Int63() int64 {
178 n := testShuffleTable[int(*src)%len(testShuffleTable)]
179 (*src)++
180 return n
181 }
182
183 func (*testShuffleSource) Seed(int64) {}
184
185 func TestShuffleSource(t *testing.T) {
186 runner := func(str string) string {
187 var src testShuffleSource
188 return ShuffleSource(str, &src)
189 }
190
191 runTestCases(t, runner, _M{
192 "": "",
193 "facgbheidjk": "bkgfijached",
194 "尝试中文怎么样": "怎试么中样尝文",
195 "zh英文hun排": "zuhh文n英排",
196 })
197 }
198
199 func TestSuccessor(t *testing.T) {
200 runTestCases(t, Successor, _M{
201 "": "",
202 "abcd": "abce",
203 "THX1138": "THX1139",
204 "<<koala>>": "<<koalb>>",
205 "1999zzz": "2000aaa",
206 "ZZZ9999": "AAAA0000",
207 "***": "**+",
208
209 "来点中文试试": "来点中文试诖",
210 "中cZ英ZZ文zZ混9zZ9杂99进z位": "中dA英AA文aA混0aA0杂00进a位",
211 })
212 }
213
View as plain text