Debian Codegraph @

convert_test.go

raw
1 // Copyright 2015 Huan Du. All rights reserved.
2 // Licensed under the MIT license that can be found in the LICENSE file.
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 TestToPascalCase(t *testing.T) {
81 runTestCases(t, ToPascalCase, _M{
82 "http_server": "HttpServer",
83 "_camel_case": "_CamelCase",
84 "no_https": "NoHttps",
85 "_complex__case_": "_Complex_Case_",
86 " complex -case ": " Complex Case ",
87 "all": "All",
88 "GOLANG_IS_GREAT": "GolangIsGreat",
89 "GOLANG": "Golang",
90 "a": "A",
91 "好": "好",
92
93 "FROM CamelCase to snake/kebab-case": "FromCamelCaseToSnake/kebabCase",
94
95 "": "",
96 })
97 }
98
99 func TestSwapCase(t *testing.T) {
100 runTestCases(t, SwapCase, _M{
101 "swapCase": "SWAPcASE",
102 "Θ~λa云Ξπ": "θ~ΛA云ξΠ",
103 "a": "A",
104
105 "": "",
106 })
107 }
108
109 func TestFirstRuneToUpper(t *testing.T) {
110 runTestCases(t, FirstRuneToUpper, _M{
111 "hello, world!": "Hello, world!",
112 "Hello, world!": "Hello, world!",
113 "你好,世界!": "你好,世界!",
114 "a": "A",
115
116 "": "",
117 })
118 }
119
120 func TestFirstRuneToLower(t *testing.T) {
121 runTestCases(t, FirstRuneToLower, _M{
122 "hello, world!": "hello, world!",
123 "Hello, world!": "hello, world!",
124 "你好,世界!": "你好,世界!",
125 "a": "a",
126 "A": "a",
127
128 "": "",
129 })
130 }
131
132 func TestShuffle(t *testing.T) {
133 // It seems there is no reliable way to test shuffled string.
134 // Runner just make sure shuffled string has the same runes as origin string.
135 runner := func(str string) string {
136 s := Shuffle(str)
137 slice := sort.StringSlice(strings.Split(s, ""))
138 slice.Sort()
139 return strings.Join(slice, "")
140 }
141
142 runTestCases(t, runner, _M{
143 "": "",
144 "facgbheidjk": "abcdefghijk",
145 "尝试中文": "中尝文试",
146 "zh英文hun排": "hhnuz排文英",
147 })
148 }
149
150 type testShuffleSource int
151
152 // A generated random number sequance just for testing.
153 var testShuffleTable = []int64{
154 1874068156324778273,
155 3328451335138149956,
156 5263531936693774911,
157 7955079406183515637,
158 2703501726821866378,
159 2740103009342231109,
160 6941261091797652072,
161 1905388747193831650,
162 7981306761429961588,
163 6426100070888298971,
164 4831389563158288344,
165 261049867304784443,
166 1460320609597786623,
167 5600924393587988459,
168 8995016276575641803,
169 732830328053361739,
170 5486140987150761883,
171 545291762129038907,
172 6382800227808658932,
173 2781055864473387780,
174 1598098976185383115,
175 4990765271833742716,
176 5018949295715050020,
177 2568779411109623071,
178 3902890183311134652,
179 4893789450120281907,
180 2338498362660772719,
181 2601737961087659062,
182 7273596521315663110,
183 3337066551442961397,
184 8121576815539813105,
185 2740376916591569721,
186 8249030965139585917,
187 898860202204764712,
188 9010467728050264449,
189 685213522303989579,
190 2050257992909156333,
191 6281838661429879825,
192 2227583514184312746,
193 2873287401706343734,
194 }
195
196 func (src *testShuffleSource) Int63() int64 {
197 n := testShuffleTable[int(*src)%len(testShuffleTable)]
198 (*src)++
199 return n
200 }
201
202 func (*testShuffleSource) Seed(int64) {}
203
204 func TestShuffleSource(t *testing.T) {
205 runner := func(str string) string {
206 var src testShuffleSource
207 return ShuffleSource(str, &src)
208 }
209
210 runTestCases(t, runner, _M{
211 "": "",
212 "facgbheidjk": "bkgfijached",
213 "尝试中文怎么样": "怎试么中样尝文",
214 "zh英文hun排": "zuhh文n英排",
215 })
216 }
217
218 func TestSuccessor(t *testing.T) {
219 runTestCases(t, Successor, _M{
220 "": "",
221 "abcd": "abce",
222 "THX1138": "THX1139",
223 "<<koala>>": "<<koalb>>",
224 "1999zzz": "2000aaa",
225 "ZZZ9999": "AAAA0000",
226 "***": "**+",
227
228 "来点中文试试": "来点中文试诖",
229 "中cZ英ZZ文zZ混9zZ9杂99进z位": "中dA英AA文aA混0aA0杂00进a位",
230 })
231 }