forked from viant/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.go
308 lines (288 loc) · 6.68 KB
/
text_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package toolbox
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"strings"
"testing"
)
func TestIsASCIIText(t *testing.T) {
var useCases = []struct {
Description string
Candidate string
Expected bool
}{
{
Description: "basic text",
Candidate: `abc`,
Expected: true,
},
{
Description: "JSON object like text",
Candidate: `{"k1"}`,
Expected: true,
},
{
Description: "JSON array like text",
Candidate: `["$k1"]`,
Expected: true,
},
{
Description: "bin data",
Candidate: "\u0000",
Expected: false,
},
{
Description: "JSON text",
Candidate: `{
"RepositoryDatastore":"db1",
"Db": [
{
"Name": "db1",
"Config": {
"PoolSize": 3,
"MaxPoolSize": 5,
"DriverName": "mysql",
"Descriptor": "[username]:[password]@tcp(127.0.0.1:3306)/db1?parseTime=true",
"Credentials": "$mysqlCredentials"
}
}
]
}
`,
Expected: true,
},
}
for _, useCase := range useCases {
assert.EqualValues(t, useCase.Expected, IsASCIIText(useCase.Candidate), useCase.Description)
}
}
func TestIsPrintText(t *testing.T) {
var useCases = []struct {
Description string
Candidate string
Expected bool
}{
{
Description: "basic text",
Candidate: `abc`,
Expected: true,
},
{
Description: "JSON object like text",
Candidate: `{"k1"}`,
Expected: true,
},
{
Description: "JSON array like text",
Candidate: `["$k1"]`,
Expected: true,
},
{
Description: "bin data",
Candidate: "\u0000",
Expected: false,
},
{
Description: "JSON text",
Candidate: `{
"RepositoryDatastore":"db1",
"Db": [
{
"Name": "db1",
"Config": {
"PoolSize": 3,
"MaxPoolSize": 5,
"DriverName": "mysql",
"Descriptor": "[username]:[password]@tcp(127.0.0.1:3306)/db1?parseTime=true",
"Credentials": "mysql"
}
}
]
}
`,
Expected: true,
},
}
for _, useCase := range useCases {
assert.EqualValues(t, useCase.Expected, IsPrintText(useCase.Candidate), useCase.Description)
}
}
func TestTerminatedSplitN(t *testing.T) {
var data = make([]byte, 0)
for i := 0; i < 9; i++ {
data = append(data, []byte(fmt.Sprintf("%v %v\n", strings.Repeat("x", 32), i))...)
}
text := string(data)
useCases := []struct {
description string
fragmentCount int
expectedFragmentSizes []int
}{
{
description: "one fragment case",
fragmentCount: 1,
expectedFragmentSizes: []int{len(data)},
},
{
description: "two fragments case",
fragmentCount: 2,
expectedFragmentSizes: []int{175, 140},
},
{
description: "3 fragments case",
fragmentCount: 3,
expectedFragmentSizes: []int{140, 140, 35},
},
{
description: "7 fragments case",
fragmentCount: 7,
expectedFragmentSizes: []int{70, 70, 70, 70, 35},
},
{
description: "10 fragments case", //no more fragments then lines, so only 9 fragments here
fragmentCount: 10,
expectedFragmentSizes: []int{35, 35, 35, 35, 35, 35, 35, 35, 35},
},
}
for _, useCase := range useCases {
fragments := TerminatedSplitN(text, useCase.fragmentCount, "\n")
var actualFragmentSizes = make([]int, len(fragments))
for i, fragment := range fragments {
actualFragmentSizes[i] = len(fragment)
}
assert.EqualValues(t, useCase.expectedFragmentSizes, actualFragmentSizes, useCase.description)
}
}
type testWriter struct {
*bytes.Buffer
data *[]string
}
func (t *testWriter) Close() error {
*t.data = append(*t.data, t.String())
return nil
}
func newTestWriter(data *[]string) io.WriteCloser {
return &testWriter{
data: data,
Buffer: new(bytes.Buffer),
}
}
func TestSplitTextStream(t *testing.T) {
var data = make([]byte, 0)
for i := 0; i < 9; i++ {
data = append(data, []byte(fmt.Sprintf("%v %v\n", strings.Repeat("x", 2), i))...)
}
text := string(data)
useCases := []struct {
description string
elements int
expect []string
}{
{
description: "no more then 4 lines case",
elements: 4,
expect: []string{
"xx 0\nxx 1\nxx 2\nxx 3", "xx 4\nxx 5\nxx 6\nxx 7", "xx 8",
},
},
{
description: "3 elements each",
elements: 3,
expect: []string{
"xx 0\nxx 1\nxx 2", "xx 3\nxx 4\nxx 5", "xx 6\nxx 7\nxx 8",
},
},
{
description: "9 elements",
elements: 1,
expect: []string{
"xx 0", "xx 1", "xx 2", "xx 3", "xx 4", "xx 5", "xx 6", "xx 7", "xx 8",
},
},
{
description: "9 elements",
elements: 0,
expect: []string{
"xx 0", "xx 1", "xx 2", "xx 3", "xx 4", "xx 5", "xx 6", "xx 7", "xx 8",
},
},
{
description: "1 elements",
elements: 10,
expect: []string{
"xx 0\nxx 1\nxx 2\nxx 3\nxx 4\nxx 5\nxx 6\nxx 7\nxx 8",
},
},
{
description: "1 elements",
elements: 9,
expect: []string{
"xx 0\nxx 1\nxx 2\nxx 3\nxx 4\nxx 5\nxx 6\nxx 7\nxx 8",
},
},
}
for _, useCase := range useCases {
var data = make([]string, 0)
err := SplitTextStream(strings.NewReader(text), func() io.WriteCloser { return newTestWriter(&data) }, useCase.elements)
assert.Nil(t, err)
assert.EqualValues(t, useCase.expect, data, useCase.description)
}
}
func Test_CaseFormat(t *testing.T) {
var useCases = []struct {
description string
caseFrom int
caseTo int
input string
expect string
}{
{
description: "camel to uppercase",
input: "thisIsMyTest",
caseFrom: CaseLowerCamel,
caseTo: CaseUpper,
expect: "THISISMYTEST",
},
{
description: "camel to lower underscore",
input: "thisIsMyTest",
caseFrom: CaseLowerCamel,
caseTo: CaseLowerUnderscore,
expect: "this_is_my_test",
},
{
description: "camel to upper underscore",
input: "thisIsMyTest",
caseFrom: CaseLowerCamel,
caseTo: CaseUpperUnderscore,
expect: "THIS_IS_MY_TEST",
},
{
description: "lower underscore to upper camel",
input: "this_is_my_test",
caseFrom: CaseLowerUnderscore,
caseTo: CaseUpperCamel,
expect: "ThisIsMyTest",
},
{
description: "upper underscore to lower camel",
input: "THIS_IS_MY_TEST",
caseFrom: CaseUpperUnderscore,
caseTo: CaseLowerCamel,
expect: "thisIsMyTest",
},
{
description: "upper camel to lower camel",
input: "ThisIsMyTest",
caseFrom: CaseUpperCamel,
caseTo: CaseLowerCamel,
expect: "thisIsMyTest",
},
}
for _, useCase := range useCases {
actual := ToCaseFormat(useCase.input, useCase.caseFrom, useCase.caseTo)
assert.Equal(t, useCase.expect, actual, useCase.description)
}
}