-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter_test.go
193 lines (168 loc) · 5.72 KB
/
converter_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 gocc_test
import (
"fmt"
"github.com/dtomasi/gocc"
"github.com/dtomasi/gocc/internal/testutils"
"github.com/dtomasi/gocc/internal/utils"
"github.com/dtomasi/gocc/pkg/casestyle"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func getAllTestStrings() []string {
return utils.MergeSlices(
testutils.TestStringsSnakeCase,
testutils.TestStringsPascalCase,
testutils.TestStringsCamelCase,
testutils.TestStringsKebabCase,
testutils.TestStringsDotNotation,
)
}
func TestConverter_ToNoMatch(t *testing.T) {
_, err := gocc.C("something:that_is|not.valid").Convert(-1, -1)
assert.Error(t, err)
}
func TestSConverter_ToSnakeCase(t *testing.T) {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToSnakeCase()
assert.NoError(t, err)
assert.True(t, casestyle.IsSnakeCase(result), testString)
}
}
func BenchmarkConverter_PascalCase_ToSnakeCase_with_style_detection(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsPascalCase[1]).ToSnakeCase()
}
}
func BenchmarkConverter_PascalCase_ToSnakeCase_with_known_style(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsPascalCase[1]).Convert(casestyle.StylePascalCase, casestyle.StyleSnakeCase)
}
}
func BenchmarkConverter_CamelCase_ToSnakeCase_with_style_detection(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsCamelCase[1]).ToSnakeCase()
}
}
func BenchmarkConverter_CamelCase_ToSnakeCase_with_known_style(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsCamelCase[1]).Convert(casestyle.StyleCamelCase, casestyle.StyleSnakeCase)
}
}
func BenchmarkConverter_KebabCase_ToSnakeCase_with_style_detection(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsKebabCase[1]).ToSnakeCase()
}
}
func BenchmarkConverter_KebabCase_ToSnakeCase_with_known_style(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsKebabCase[1]).Convert(casestyle.StyleKebabCase, casestyle.StyleSnakeCase)
}
}
func BenchmarkConverter_DotNotation_ToSnakeCase_with_style_detection(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsDotNotation[1]).ToSnakeCase()
}
}
func BenchmarkConverter_DotNotation_ToSnakeCase_with_known_style(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gocc.C(testutils.TestStringsDotNotation[1]).Convert(casestyle.StyleDotNotation, casestyle.StyleSnakeCase)
}
}
func TestSConverter_ToPascalCase(t *testing.T) {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToPascalCase()
assert.NoError(t, err)
assert.True(t, casestyle.IsPascalCase(result), testString)
}
}
func TestSConverter_ToCamelCase(t *testing.T) {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToCamelCase()
assert.NoError(t, err)
assert.True(t, casestyle.IsCamelCase(result), testString)
}
}
func TestSConverter_ToKebabCase(t *testing.T) {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToKebabCase()
assert.NoError(t, err)
assert.True(t, casestyle.IsKebabCase(result), testString)
}
}
func TestSConverter_ToDotNotation(t *testing.T) {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToDotNotation()
assert.NoError(t, err)
assert.True(t, casestyle.IsDotNotation(result), testString)
}
}
func TestConverter_ToCustomDelimiter(t *testing.T) {
for _, delimiter := range testutils.TestCustomDelimiters {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToCustomDelimiter(delimiter)
assert.NoError(t, err)
assert.True(t,
casestyle.IsCustomDelimiter(
result,
delimiter,
),
fmt.Sprintf("delimiter: %s | test string: %s | test result string: %s", delimiter, testString, result),
)
}
}
}
func TestConverter_ToUpperCustomDelimiter(t *testing.T) {
for _, delimiter := range testutils.TestCustomDelimiters {
for _, testString := range getAllTestStrings() {
result, err := gocc.C(testString).ToUpperCustomDelimiter(delimiter)
assert.NoError(t, err)
assert.True(t,
casestyle.IsUpperCustomDelimiter(
result,
delimiter,
),
fmt.Sprintf("delimiter: %s | test string: %s | test result string: %s", delimiter, testString, result),
)
}
}
}
func TestConverter_IsSnakeCase(t *testing.T) {
for _, testString := range testutils.TestStringsSnakeCase {
assert.True(t, gocc.C(testString).IsSnakeCase())
}
}
func TestConverter_IsCamelCase(t *testing.T) {
for _, testString := range testutils.TestStringsCamelCase {
assert.True(t, gocc.C(testString).IsCamelCase())
}
}
func TestConverter_IsDotNotation(t *testing.T) {
for _, testString := range testutils.TestStringsDotNotation {
assert.True(t, gocc.C(testString).IsDotNotation())
}
}
func TestConverter_IsPascalCase(t *testing.T) {
for _, testString := range testutils.TestStringsPascalCase {
assert.True(t, gocc.C(testString).IsPascalCase())
}
}
func TestConverter_IsKebabCase(t *testing.T) {
for _, testString := range testutils.TestStringsKebabCase {
assert.True(t, gocc.C(testString).IsKebabCase())
}
}
func TestConverter_IsCustomDelimiter(t *testing.T) {
stringTmpl := "test string template"
for _, testDelimiter := range testutils.TestCustomDelimiters {
testString := strings.ReplaceAll(stringTmpl, " ", testDelimiter)
assert.True(t, gocc.C(testString).IsCustomDelimiter(testDelimiter))
}
}
func TestConverter_IsUpperCustomDelimiter(t *testing.T) {
stringTmpl := "TEST STRING TEMPLATE"
for _, testDelimiter := range testutils.TestCustomDelimiters {
testString := strings.ReplaceAll(stringTmpl, " ", testDelimiter)
assert.True(t, gocc.C(testString).IsUpperCustomDelimiter(testDelimiter))
}
}