-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_encode_test.go
More file actions
398 lines (345 loc) · 7.89 KB
/
string_encode_test.go
File metadata and controls
398 lines (345 loc) · 7.89 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package cast_test
import (
"bytes"
"encoding/xml"
"errors"
"testing"
"time"
"go.osspkg.com/cast"
)
// Вспомогательные структуры для тестирования интерфейсов
type testByter []byte
func (b testByter) Bytes() []byte {
return []byte(b)
}
type testStringer string
func (s testStringer) String() string {
return string(s)
}
type testGoStringer string
func (s testGoStringer) GoString() string {
return "Go" + string(s)
}
type testBinaryMarshaler string
func (b testBinaryMarshaler) MarshalBinary() ([]byte, error) {
return []byte(b), nil
}
type testTextMarshaler string
func (t testTextMarshaler) MarshalText() ([]byte, error) {
return []byte(t), nil
}
type testJSONMarshaler string
func (j testJSONMarshaler) MarshalJSON() ([]byte, error) {
return []byte(`"` + string(j) + `"`), nil
}
type testXMLStruct struct {
Value string `xml:"value"`
}
func (x *testXMLStruct) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(x.Value, start)
}
type testError string
func (e testError) Error() string {
return string(e)
}
type testUnsupported struct{}
// Тестовые структуры для проверки JSON и XML маршалинга
type testStruct struct {
Name string `json:"name"`
Age int `json:"age"`
}
type testMap map[string]int
// Вспомогательные структуры для тестирования ошибок
type errorReader struct {
err error
}
func (r *errorReader) Read(p []byte) (n int, err error) {
return 0, r.err
}
type errorBinaryMarshaler struct {
err error
}
func (b *errorBinaryMarshaler) MarshalBinary() ([]byte, error) {
return nil, b.err
}
type errorTextMarshaler struct {
err error
}
func (t *errorTextMarshaler) MarshalText() ([]byte, error) {
return nil, t.err
}
func TestUnit_StringEncode(t *testing.T) {
now := time.Now().UTC().Truncate(time.Second)
duration := 2 * time.Second
tests := []struct {
name string
input any
expected string
expectError bool
}{
// nil case
{
name: "nil",
input: nil,
expected: "",
},
// io.Reader case
{
name: "io.Reader",
input: bytes.NewBufferString("reader content"),
expected: "reader content",
},
// Byter interface
{
name: "Byter",
input: testByter("byter content"),
expected: "byter content",
},
// Stringer interface
{
name: "Stringer",
input: testStringer("stringer content"),
expected: "stringer content",
},
// GoStringer interface
{
name: "GoStringer",
input: testGoStringer("gostringer content"),
expected: "Gogostringer content",
},
// BinaryMarshaler interface
{
name: "BinaryMarshaler",
input: testBinaryMarshaler("binary marshaler content"),
expected: "binary marshaler content",
},
// TextMarshaler interface
{
name: "TextMarshaler",
input: testTextMarshaler("text marshaler content"),
expected: "text marshaler content",
},
// JSONMarshaler interface
{
name: "JSONMarshaler",
input: testJSONMarshaler("json marshaler content"),
expected: `"json marshaler content"`,
},
// XML case (using xml.Marshal for structs)
{
name: "XML struct",
input: &testXMLStruct{Value: "xml content"},
expected: `<testXMLStruct>xml content</testXMLStruct>`,
},
// Error interface
{
name: "error",
input: testError("error content"),
expected: "error content",
},
// Basic types
{
name: "string",
input: "basic string",
expected: "basic string",
},
{
name: "[]byte",
input: []byte("byte slice"),
expected: "byte slice",
},
{
name: "int",
input: 42,
expected: "42",
},
{
name: "int8",
input: int8(42),
expected: "42",
},
{
name: "int16",
input: int16(42),
expected: "42",
},
{
name: "int32",
input: int32(42),
expected: "42",
},
{
name: "int64",
input: int64(42),
expected: "42",
},
{
name: "uint",
input: uint(42),
expected: "42",
},
{
name: "uint8",
input: uint8(42),
expected: "42",
},
{
name: "uint16",
input: uint16(42),
expected: "42",
},
{
name: "uint32",
input: uint32(42),
expected: "42",
},
{
name: "uint64",
input: uint64(42),
expected: "42",
},
{
name: "float32",
input: float32(3.14),
expected: "3.14",
},
{
name: "float64",
input: 3.14,
expected: "3.14",
},
{
name: "bool true",
input: true,
expected: "true",
},
{
name: "bool false",
input: false,
expected: "false",
},
// Time types
{
name: "time.Duration",
input: duration,
expected: "2s",
},
{
name: "time.Time",
input: now,
expected: now.Format(time.RFC3339),
},
// Complex types
{
name: "struct",
input: testStruct{Name: "John", Age: 30},
expected: `{"name":"John","age":30}`,
},
{
name: "map",
input: testMap{"a": 1, "b": 2},
expected: `{"a":1,"b":2}`,
},
{
name: "slice",
input: []int{1, 2, 3},
expected: `[1,2,3]`,
},
{
name: "array",
input: [3]int{1, 2, 3},
expected: `[1,2,3]`,
},
// Pointers
{
name: "pointer to string",
input: func() *string { s := "pointer string"; return &s }(),
expected: "pointer string",
},
{
name: "pointer to struct",
input: &testStruct{Name: "Doe", Age: 25},
expected: `{"name":"Doe","age":25}`,
},
// Unsupported types
{
name: "unsupported func",
input: func() {},
expectError: true,
},
{
name: "unsupported chan",
input: make(chan int),
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := cast.StringEncode(tt.input)
if tt.expectError {
if err == nil {
t.Fatal("expected error but got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("result mismatch\nexpected: %q\nactual: %q", tt.expected, result)
}
})
}
// Тест для проверки обработки ошибок в io.Reader
t.Run("io.Reader error", func(t *testing.T) {
errReader := &errorReader{err: errors.New("read error")}
_, err := cast.StringEncode(errReader)
if err == nil {
t.Fatal("expected error but got nil")
}
if err.Error() != "read error" {
t.Errorf("unexpected error: %v", err)
}
})
// Тест для проверки обработки ошибок в BinaryMarshaler
t.Run("BinaryMarshaler error", func(t *testing.T) {
errMarshaler := &errorBinaryMarshaler{err: errors.New("binary marshal error")}
_, err := cast.StringEncode(errMarshaler)
if err == nil {
t.Fatal("expected error but got nil")
}
if err.Error() != "binary marshal error" {
t.Errorf("unexpected error: %v", err)
}
})
// Дополнительный тест для проверки, что nil-указатель обрабатывается как nil
t.Run("nil pointer", func(t *testing.T) {
var ptr *string
result, err := cast.StringEncode(ptr)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != "" {
t.Errorf("expected empty string for nil pointer, got %q", result)
}
})
// Тест для проверки, что структура без поддерживаемых интерфейсов маршалится в JSON
t.Run("struct without interfaces", func(t *testing.T) {
type simpleStruct struct {
Field1 string
Field2 int
}
s := simpleStruct{
Field1: "test",
Field2: 42,
}
expected := `{"Field1":"test","Field2":42}`
result, err := cast.StringEncode(s)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != expected {
t.Errorf("result mismatch\nexpected: %q\nactual: %q", expected, result)
}
})
}