-
Notifications
You must be signed in to change notification settings - Fork 5
/
strings_test.go
193 lines (183 loc) · 5.47 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package stringutil_test
import (
"reflect"
"testing"
"github.com/naoina/go-stringutil"
)
var commonTestCasesForToUpperCamelCase = []struct {
input, expect string
}{
{"", ""},
{"thequickbrownfoxoverthelazydog", "Thequickbrownfoxoverthelazydog"},
{"thequickbrownfoxoverthelazydoG", "ThequickbrownfoxoverthelazydoG"},
{"thequickbrownfoxoverthelazydo_g", "ThequickbrownfoxoverthelazydoG"},
{"TheQuickBrownFoxJumpsOverTheLazyDog", "TheQuickBrownFoxJumpsOverTheLazyDog"},
{"the_quick_brown_fox_jumps_over_the_lazy_dog", "TheQuickBrownFoxJumpsOverTheLazyDog"},
{"the_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog", "TheQuickBrownFoxJumpsOverTheLazyDog"},
{"api_server", "APIServer"},
{"a_t_api", "ATAPI"},
{"atapi", "Atapi"},
{"web_ui", "WebUI"},
{"api", "API"},
{"ascii", "ASCII"},
{"cpu", "CPU"},
{"csrf", "CSRF"},
{"css", "CSS"},
{"dns", "DNS"},
{"eof", "EOF"},
{"guid", "GUID"},
{"html", "HTML"},
{"http", "HTTP"},
{"https", "HTTPS"},
{"id", "ID"},
{"ip", "IP"},
{"json", "JSON"},
{"lhs", "LHS"},
{"qps", "QPS"},
{"ram", "RAM"},
{"rhs", "RHS"},
{"rpc", "RPC"},
{"sla", "SLA"},
{"smtp", "SMTP"},
{"sql", "SQL"},
{"ssh", "SSH"},
{"tcp", "TCP"},
{"tls", "TLS"},
{"ttl", "TTL"},
{"udp", "UDP"},
{"ui", "UI"},
{"uid", "UID"},
{"uuid", "UUID"},
{"uri", "URI"},
{"url", "URL"},
{"utf8", "UTF8"},
{"vm", "VM"},
{"xml", "XML"},
{"xsrf", "XSRF"},
{"xss", "XSS"},
}
func TestToUpperCamelCase(t *testing.T) {
for _, v := range append(commonTestCasesForToUpperCamelCase, []struct {
input, expect string
}{
{"the_quick_brown_fox_over_the_lazy_dog", "TheQuickBrownFoxOverTheLazyDog"},
}...) {
actual := stringutil.ToUpperCamelCase(v.input)
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`stringutil.ToUpperCamelCase(%#v) => %#v; want %#v`, v.input, actual, expect)
}
}
}
func TestToUpperCamelCaseASCII(t *testing.T) {
for _, v := range commonTestCasesForToUpperCamelCase {
actual := stringutil.ToUpperCamelCaseASCII(v.input)
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`stringutil.ToUpperCamelCaseASCII(%#v) => %#v; want %#v`, v.input, actual, expect)
}
}
}
var commonTestCasesForToSnakeCase = []struct {
input, expect string
}{
{"", ""},
{"thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"},
{"Thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"},
{"ThequickbrownfoxjumpsoverthelazydoG", "thequickbrownfoxjumpsoverthelazydo_g"},
{"TheQuickBrownFoxJumpsOverTheLazyDog", "the_quick_brown_fox_jumps_over_the_lazy_dog"},
{"the_quick_brown_fox_jumps_over_the_lazy_dog", "the_quick_brown_fox_jumps_over_the_lazy_dog"},
{"APIServer", "api_server"},
{"ATAPI", "a_t_api"},
{"Atapi", "atapi"},
{"WebUI", "web_ui"},
{"API", "api"},
{"ASCII", "ascii"},
{"CPU", "cpu"},
{"CSRF", "csrf"},
{"CSS", "css"},
{"DNS", "dns"},
{"EOF", "eof"},
{"GUID", "guid"},
{"HTML", "html"},
{"HTTP", "http"},
{"HTTPS", "https"},
{"ID", "id"},
{"ip", "ip"},
{"JSON", "json"},
{"LHS", "lhs"},
{"QPS", "qps"},
{"RAM", "ram"},
{"RHS", "rhs"},
{"RPC", "rpc"},
{"SLA", "sla"},
{"SMTP", "smtp"},
{"SQL", "sql"},
{"SSH", "ssh"},
{"TCP", "tcp"},
{"TLS", "tls"},
{"TTL", "ttl"},
{"UDP", "udp"},
{"UI", "ui"},
{"UID", "uid"},
{"UUID", "uuid"},
{"URI", "uri"},
{"URL", "url"},
{"UTF8", "utf8"},
{"VM", "vm"},
{"XML", "xml"},
{"XSRF", "xsrf"},
{"XSS", "xss"},
}
func TestToSnakeCase(t *testing.T) {
for _, v := range append(commonTestCasesForToSnakeCase, []struct {
input, expect string
}{
{"TheQuickBrownFoxOverTheLazyDog", "the_quick_brown_fox_over_the_lazy_dog"},
}...) {
actual := stringutil.ToSnakeCase(v.input)
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`stringutil.ToSnakeCase(%#v) => %#v; want %#v`, v.input, actual, expect)
}
}
}
func TestToSnakeCaseASCII(t *testing.T) {
for _, v := range commonTestCasesForToSnakeCase {
actual := stringutil.ToSnakeCaseASCII(v.input)
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`stringutil.ToSnakeCaseASCII(%#v) => %#v; want %#v`, v.input, actual, expect)
}
}
}
func TestAddCommonInitialismWithToUpperCamelCase(t *testing.T) {
input := "test_case"
actual := stringutil.ToUpperCamelCase(input)
expect := "TestCase"
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`ToUpperCamelCase(%#v) with AddCommonInitialism => %#v; want %#v`, input, actual, expect)
}
stringutil.AddCommonInitialism("TEST", "CASE")
defer stringutil.DelCommonInitialism("TEST", "CASE")
actual = stringutil.ToUpperCamelCase(input)
expect = "TESTCASE"
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`ToUpperCamelCase(%#v) with AddCommonInitialism => %#v; want %#v`, input, actual, expect)
}
}
func TestAddCommonInitialismWithToSnakeCase(t *testing.T) {
input := "TESTCase"
actual := stringutil.ToSnakeCase(input)
expect := "t_e_s_t_case"
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`ToSnakeCase(%#v) with AddCommonInitialism => %#v; want %#v`, input, actual, expect)
}
stringutil.AddCommonInitialism("TEST", "CASE")
defer stringutil.DelCommonInitialism("TEST", "CASE")
actual = stringutil.ToSnakeCase(input)
expect = "test_case"
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`ToSnakeCase(%#v) with AddCommonInitialism => %#v; want %#v`, input, actual, expect)
}
}